]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ceph/crush/mapper.c
crush: ensure bucket id is valid before indexing buckets array
[mirror_ubuntu-jammy-kernel.git] / net / ceph / crush / mapper.c
CommitLineData
b459be73
ID
1/*
2 * Ceph - scalable distributed file system
3 *
4 * Copyright (C) 2015 Intel Corporation All Rights Reserved
5 *
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
10 *
11 */
5ecc0a0f
SW
12
13#ifdef __KERNEL__
14# include <linux/string.h>
15# include <linux/slab.h>
16# include <linux/bug.h>
17# include <linux/kernel.h>
b459be73
ID
18# include <linux/crush/crush.h>
19# include <linux/crush/hash.h>
5ecc0a0f 20#else
b459be73
ID
21# include "crush_compat.h"
22# include "crush.h"
23# include "hash.h"
5ecc0a0f 24#endif
958a2765 25#include "crush_ln_table.h"
5ecc0a0f 26
b459be73
ID
27#define dprintk(args...) /* printf(args) */
28
5ecc0a0f
SW
29/*
30 * Implement the core CRUSH mapping algorithm.
31 */
32
33/**
34 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
35 * @map: the crush_map
36 * @ruleset: the storage ruleset id (user defined)
37 * @type: storage ruleset type (user defined)
38 * @size: output set size
39 */
8b12d47b 40int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
5ecc0a0f 41{
8b12d47b 42 __u32 i;
5ecc0a0f
SW
43
44 for (i = 0; i < map->max_rules; i++) {
45 if (map->rules[i] &&
46 map->rules[i]->mask.ruleset == ruleset &&
47 map->rules[i]->mask.type == type &&
48 map->rules[i]->mask.min_size <= size &&
49 map->rules[i]->mask.max_size >= size)
50 return i;
51 }
52 return -1;
53}
54
55
56/*
57 * bucket choose methods
58 *
59 * For each bucket algorithm, we have a "choose" method that, given a
60 * crush input @x and replica position (usually, position in output set) @r,
61 * will produce an item in the bucket.
62 */
63
64/*
65 * Choose based on a random permutation of the bucket.
66 *
67 * We used to use some prime number arithmetic to do this, but it
68 * wasn't very random, and had some other bad behaviors. Instead, we
69 * calculate an actual random permutation of the bucket members.
70 * Since this is expensive, we optimize for the r=0 case, which
71 * captures the vast majority of calls.
72 */
73static int bucket_perm_choose(struct crush_bucket *bucket,
74 int x, int r)
75{
95c96174
ED
76 unsigned int pr = r % bucket->size;
77 unsigned int i, s;
5ecc0a0f
SW
78
79 /* start a new permutation if @x has changed */
8b12d47b 80 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
5ecc0a0f
SW
81 dprintk("bucket %d new x=%d\n", bucket->id, x);
82 bucket->perm_x = x;
83
84 /* optimize common r=0 case */
85 if (pr == 0) {
fb690390 86 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
5ecc0a0f
SW
87 bucket->size;
88 bucket->perm[0] = s;
89 bucket->perm_n = 0xffff; /* magic value, see below */
90 goto out;
91 }
92
93 for (i = 0; i < bucket->size; i++)
94 bucket->perm[i] = i;
95 bucket->perm_n = 0;
96 } else if (bucket->perm_n == 0xffff) {
97 /* clean up after the r=0 case above */
98 for (i = 1; i < bucket->size; i++)
99 bucket->perm[i] = i;
100 bucket->perm[bucket->perm[0]] = 0;
101 bucket->perm_n = 1;
102 }
103
104 /* calculate permutation up to pr */
105 for (i = 0; i < bucket->perm_n; i++)
106 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
107 while (bucket->perm_n <= pr) {
95c96174 108 unsigned int p = bucket->perm_n;
5ecc0a0f
SW
109 /* no point in swapping the final entry */
110 if (p < bucket->size - 1) {
fb690390 111 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
5ecc0a0f
SW
112 (bucket->size - p);
113 if (i) {
95c96174 114 unsigned int t = bucket->perm[p + i];
5ecc0a0f
SW
115 bucket->perm[p + i] = bucket->perm[p];
116 bucket->perm[p] = t;
117 }
118 dprintk(" perm_choose swap %d with %d\n", p, p+i);
119 }
120 bucket->perm_n++;
121 }
122 for (i = 0; i < bucket->size; i++)
123 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
124
125 s = bucket->perm[pr];
126out:
127 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
128 bucket->size, x, r, pr, s);
129 return bucket->items[s];
130}
131
132/* uniform */
133static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
134 int x, int r)
135{
136 return bucket_perm_choose(&bucket->h, x, r);
137}
138
139/* list */
140static int bucket_list_choose(struct crush_bucket_list *bucket,
141 int x, int r)
142{
143 int i;
144
145 for (i = bucket->h.size-1; i >= 0; i--) {
b459be73 146 __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
fb690390 147 r, bucket->h.id);
5ecc0a0f
SW
148 w &= 0xffff;
149 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
150 "sw %x rand %llx",
151 i, x, r, bucket->h.items[i], bucket->item_weights[i],
152 bucket->sum_weights[i], w);
153 w *= bucket->sum_weights[i];
154 w = w >> 16;
155 /*dprintk(" scaled %llx\n", w);*/
156 if (w < bucket->item_weights[i])
157 return bucket->h.items[i];
158 }
159
a1f4895b
SW
160 dprintk("bad list sums for bucket %d\n", bucket->h.id);
161 return bucket->h.items[0];
5ecc0a0f
SW
162}
163
164
165/* (binary) tree */
166static int height(int n)
167{
168 int h = 0;
169 while ((n & 1) == 0) {
170 h++;
171 n = n >> 1;
172 }
173 return h;
174}
175
176static int left(int x)
177{
178 int h = height(x);
179 return x - (1 << (h-1));
180}
181
182static int right(int x)
183{
184 int h = height(x);
185 return x + (1 << (h-1));
186}
187
188static int terminal(int x)
189{
190 return x & 1;
191}
192
193static int bucket_tree_choose(struct crush_bucket_tree *bucket,
194 int x, int r)
195{
8f99c85b 196 int n;
5ecc0a0f
SW
197 __u32 w;
198 __u64 t;
199
200 /* start at root */
201 n = bucket->num_nodes >> 1;
202
203 while (!terminal(n)) {
8f99c85b 204 int l;
5ecc0a0f
SW
205 /* pick point in [0, w) */
206 w = bucket->node_weights[n];
fb690390
SW
207 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
208 bucket->h.id) * (__u64)w;
5ecc0a0f
SW
209 t = t >> 32;
210
211 /* descend to the left or right? */
212 l = left(n);
213 if (t < bucket->node_weights[l])
214 n = l;
215 else
216 n = right(n);
217 }
218
219 return bucket->h.items[n >> 1];
220}
221
222
223/* straw */
224
225static int bucket_straw_choose(struct crush_bucket_straw *bucket,
226 int x, int r)
227{
8b12d47b 228 __u32 i;
5ecc0a0f
SW
229 int high = 0;
230 __u64 high_draw = 0;
231 __u64 draw;
232
233 for (i = 0; i < bucket->h.size; i++) {
fb690390 234 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
5ecc0a0f
SW
235 draw &= 0xffff;
236 draw *= bucket->straws[i];
237 if (i == 0 || draw > high_draw) {
238 high = i;
239 high_draw = draw;
240 }
241 }
242 return bucket->h.items[high];
243}
244
b459be73
ID
245/* compute 2^44*log2(input+1) */
246static __u64 crush_ln(unsigned int xin)
958a2765 247{
b459be73
ID
248 unsigned int x = xin, x1;
249 int iexpon, index1, index2;
250 __u64 RH, LH, LL, xl64, result;
958a2765 251
b459be73 252 x++;
958a2765 253
b459be73
ID
254 /* normalize input */
255 iexpon = 15;
256 while (!(x & 0x18000)) {
257 x <<= 1;
258 iexpon--;
259 }
958a2765 260
b459be73
ID
261 index1 = (x >> 8) << 1;
262 /* RH ~ 2^56/index1 */
263 RH = __RH_LH_tbl[index1 - 256];
264 /* LH ~ 2^48 * log2(index1/256) */
265 LH = __RH_LH_tbl[index1 + 1 - 256];
958a2765 266
b459be73
ID
267 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
268 xl64 = (__s64)x * RH;
269 xl64 >>= 48;
270 x1 = xl64;
958a2765 271
b459be73
ID
272 result = iexpon;
273 result <<= (12 + 32);
958a2765 274
b459be73
ID
275 index2 = x1 & 0xff;
276 /* LL ~ 2^48*log2(1.0+index2/2^15) */
277 LL = __LL_tbl[index2];
958a2765 278
b459be73 279 LH = LH + LL;
958a2765 280
b459be73
ID
281 LH >>= (48 - 12 - 32);
282 result += LH;
958a2765 283
b459be73 284 return result;
958a2765
ID
285}
286
287
288/*
289 * straw2
290 *
291 * for reference, see:
292 *
293 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
294 *
295 */
296
297static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket,
298 int x, int r)
299{
b459be73
ID
300 unsigned int i, high = 0;
301 unsigned int u;
302 unsigned int w;
958a2765
ID
303 __s64 ln, draw, high_draw = 0;
304
305 for (i = 0; i < bucket->h.size; i++) {
306 w = bucket->item_weights[i];
307 if (w) {
308 u = crush_hash32_3(bucket->h.hash, x,
309 bucket->h.items[i], r);
310 u &= 0xffff;
311
312 /*
313 * for some reason slightly less than 0x10000 produces
314 * a slightly more accurate distribution... probably a
315 * rounding effect.
316 *
317 * the natural log lookup table maps [0,0xffff]
318 * (corresponding to real numbers [1/0x10000, 1] to
319 * [0, 0xffffffffffff] (corresponding to real numbers
320 * [-11.090355,0]).
321 */
322 ln = crush_ln(u) - 0x1000000000000ll;
323
324 /*
325 * divide by 16.16 fixed-point weight. note
326 * that the ln value is negative, so a larger
327 * weight means a larger (less negative) value
328 * for draw.
329 */
330 draw = div64_s64(ln, w);
331 } else {
332 draw = S64_MIN;
333 }
334
335 if (i == 0 || draw > high_draw) {
336 high = i;
337 high_draw = draw;
338 }
339 }
340 return bucket->h.items[high];
341}
342
343
5ecc0a0f
SW
344static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
345{
a1a31e73 346 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
a1f4895b 347 BUG_ON(in->size == 0);
5ecc0a0f
SW
348 switch (in->alg) {
349 case CRUSH_BUCKET_UNIFORM:
350 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
351 x, r);
352 case CRUSH_BUCKET_LIST:
353 return bucket_list_choose((struct crush_bucket_list *)in,
354 x, r);
355 case CRUSH_BUCKET_TREE:
356 return bucket_tree_choose((struct crush_bucket_tree *)in,
357 x, r);
358 case CRUSH_BUCKET_STRAW:
359 return bucket_straw_choose((struct crush_bucket_straw *)in,
360 x, r);
958a2765
ID
361 case CRUSH_BUCKET_STRAW2:
362 return bucket_straw2_choose((struct crush_bucket_straw2 *)in,
363 x, r);
5ecc0a0f 364 default:
a1f4895b 365 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
50b885b9 366 return in->items[0];
5ecc0a0f
SW
367 }
368}
369
958a2765 370
5ecc0a0f
SW
371/*
372 * true if device is marked "out" (failed, fully offloaded)
373 * of the cluster
374 */
b3b33b0e
ID
375static int is_out(const struct crush_map *map,
376 const __u32 *weight, int weight_max,
377 int item, int x)
5ecc0a0f 378{
b3b33b0e
ID
379 if (item >= weight_max)
380 return 1;
153a1093 381 if (weight[item] >= 0x10000)
5ecc0a0f
SW
382 return 0;
383 if (weight[item] == 0)
384 return 1;
fb690390
SW
385 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
386 < weight[item])
5ecc0a0f
SW
387 return 0;
388 return 1;
389}
390
391/**
9fe07182 392 * crush_choose_firstn - choose numrep distinct items of given type
5ecc0a0f
SW
393 * @map: the crush_map
394 * @bucket: the bucket we are choose an item from
395 * @x: crush input value
396 * @numrep: the number of items to choose
397 * @type: the type of item to choose
398 * @out: pointer to output vector
399 * @outpos: our position in that vector
45002267 400 * @out_size: size of the out vector
0e32d712
ID
401 * @tries: number of attempts to make
402 * @recurse_tries: number of attempts to have recursive chooseleaf make
48a163db
ID
403 * @local_retries: localized retries
404 * @local_fallback_retries: localized fallback retries
0e32d712 405 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
e2b149cc 406 * @vary_r: pass r to recursive calls
5ecc0a0f 407 * @out2: second output vector for leaf items (if @recurse_to_leaf)
e2b149cc 408 * @parent_r: r value passed from the parent
5ecc0a0f 409 */
9fe07182
ID
410static int crush_choose_firstn(const struct crush_map *map,
411 struct crush_bucket *bucket,
412 const __u32 *weight, int weight_max,
413 int x, int numrep, int type,
414 int *out, int outpos,
45002267 415 int out_size,
2d8be0bc
ID
416 unsigned int tries,
417 unsigned int recurse_tries,
48a163db
ID
418 unsigned int local_retries,
419 unsigned int local_fallback_retries,
9fe07182 420 int recurse_to_leaf,
e2b149cc
ID
421 unsigned int vary_r,
422 int *out2,
423 int parent_r)
5ecc0a0f
SW
424{
425 int rep;
8b12d47b 426 unsigned int ftotal, flocal;
5ecc0a0f
SW
427 int retry_descent, retry_bucket, skip_rep;
428 struct crush_bucket *in = bucket;
429 int r;
430 int i;
b28813a6 431 int item = 0;
5ecc0a0f
SW
432 int itemtype;
433 int collide, reject;
45002267 434 int count = out_size;
a1a31e73 435
e2b149cc
ID
436 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",
437 recurse_to_leaf ? "_LEAF" : "",
438 bucket->id, x, outpos, numrep,
439 tries, recurse_tries, local_retries, local_fallback_retries,
440 parent_r);
5ecc0a0f 441
45002267 442 for (rep = outpos; rep < numrep && count > 0 ; rep++) {
5ecc0a0f
SW
443 /* keep trying until we get a non-out, non-colliding item */
444 ftotal = 0;
445 skip_rep = 0;
446 do {
447 retry_descent = 0;
448 in = bucket; /* initial bucket */
449
450 /* choose through intervening buckets */
451 flocal = 0;
452 do {
b28813a6 453 collide = 0;
5ecc0a0f 454 retry_bucket = 0;
e2b149cc 455 r = rep + parent_r;
9fe07182
ID
456 /* r' = r + f_total */
457 r += ftotal;
5ecc0a0f
SW
458
459 /* bucket choose */
b28813a6
SW
460 if (in->size == 0) {
461 reject = 1;
462 goto reject;
463 }
48a163db 464 if (local_fallback_retries > 0 &&
546f04ef 465 flocal >= (in->size>>1) &&
48a163db 466 flocal > local_fallback_retries)
5ecc0a0f
SW
467 item = bucket_perm_choose(in, x, r);
468 else
469 item = crush_bucket_choose(in, x, r);
a1f4895b
SW
470 if (item >= map->max_devices) {
471 dprintk(" bad item %d\n", item);
472 skip_rep = 1;
473 break;
474 }
5ecc0a0f
SW
475
476 /* desired type? */
477 if (item < 0)
478 itemtype = map->buckets[-1-item]->type;
479 else
480 itemtype = 0;
481 dprintk(" item %d type %d\n", item, itemtype);
482
483 /* keep going? */
484 if (itemtype != type) {
a1f4895b
SW
485 if (item >= 0 ||
486 (-1-item) >= map->max_buckets) {
487 dprintk(" bad item type %d\n", type);
488 skip_rep = 1;
489 break;
490 }
5ecc0a0f 491 in = map->buckets[-1-item];
55bda7aa 492 retry_bucket = 1;
5ecc0a0f
SW
493 continue;
494 }
495
496 /* collision? */
5ecc0a0f
SW
497 for (i = 0; i < outpos; i++) {
498 if (out[i] == item) {
499 collide = 1;
500 break;
501 }
502 }
503
a1a31e73 504 reject = 0;
7d7c1f61 505 if (!collide && recurse_to_leaf) {
a1a31e73 506 if (item < 0) {
e2b149cc
ID
507 int sub_r;
508 if (vary_r)
509 sub_r = r >> (vary_r-1);
510 else
511 sub_r = 0;
9fe07182 512 if (crush_choose_firstn(map,
a1a31e73 513 map->buckets[-1-item],
b3b33b0e 514 weight, weight_max,
a1a31e73 515 x, outpos+1, 0,
45002267 516 out2, outpos, count,
2d8be0bc 517 recurse_tries, 0,
48a163db
ID
518 local_retries,
519 local_fallback_retries,
9fe07182 520 0,
e2b149cc
ID
521 vary_r,
522 NULL,
523 sub_r) <= outpos)
a1a31e73
SW
524 /* didn't get leaf */
525 reject = 1;
526 } else {
527 /* we already have a leaf! */
528 out2[outpos] = item;
529 }
530 }
531
532 if (!reject) {
5ecc0a0f
SW
533 /* out? */
534 if (itemtype == 0)
535 reject = is_out(map, weight,
b3b33b0e 536 weight_max,
5ecc0a0f
SW
537 item, x);
538 else
539 reject = 0;
540 }
541
b28813a6 542reject:
5ecc0a0f
SW
543 if (reject || collide) {
544 ftotal++;
545 flocal++;
546
48a163db 547 if (collide && flocal <= local_retries)
5ecc0a0f
SW
548 /* retry locally a few times */
549 retry_bucket = 1;
48a163db
ID
550 else if (local_fallback_retries > 0 &&
551 flocal <= in->size + local_fallback_retries)
5ecc0a0f
SW
552 /* exhaustive bucket search */
553 retry_bucket = 1;
48a163db 554 else if (ftotal < tries)
5ecc0a0f
SW
555 /* then retry descent */
556 retry_descent = 1;
557 else
558 /* else give up */
559 skip_rep = 1;
560 dprintk(" reject %d collide %d "
8b12d47b 561 "ftotal %u flocal %u\n",
5ecc0a0f
SW
562 reject, collide, ftotal,
563 flocal);
564 }
565 } while (retry_bucket);
566 } while (retry_descent);
567
568 if (skip_rep) {
9fe07182
ID
569 dprintk("skip rep\n");
570 continue;
5ecc0a0f
SW
571 }
572
a1a31e73 573 dprintk("CHOOSE got %d\n", item);
5ecc0a0f
SW
574 out[outpos] = item;
575 outpos++;
45002267 576 count--;
b459be73
ID
577#ifndef __KERNEL__
578 if (map->choose_tries && ftotal <= map->choose_total_tries)
579 map->choose_tries[ftotal]++;
580#endif
5ecc0a0f
SW
581 }
582
a1a31e73 583 dprintk("CHOOSE returns %d\n", outpos);
5ecc0a0f
SW
584 return outpos;
585}
586
587
9a3b490a 588/**
9fe07182 589 * crush_choose_indep: alternative breadth-first positionally stable mapping
9a3b490a
ID
590 *
591 */
592static void crush_choose_indep(const struct crush_map *map,
be3226ac
ID
593 struct crush_bucket *bucket,
594 const __u32 *weight, int weight_max,
ab4ce2b5 595 int x, int left, int numrep, int type,
be3226ac 596 int *out, int outpos,
2d8be0bc
ID
597 unsigned int tries,
598 unsigned int recurse_tries,
be3226ac 599 int recurse_to_leaf,
41586081
ID
600 int *out2,
601 int parent_r)
9a3b490a
ID
602{
603 struct crush_bucket *in = bucket;
ab4ce2b5 604 int endpos = outpos + left;
9a3b490a
ID
605 int rep;
606 unsigned int ftotal;
607 int r;
608 int i;
609 int item = 0;
610 int itemtype;
611 int collide;
612
613 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
614 bucket->id, x, outpos, numrep);
615
616 /* initially my result is undefined */
ab4ce2b5 617 for (rep = outpos; rep < endpos; rep++) {
9a3b490a
ID
618 out[rep] = CRUSH_ITEM_UNDEF;
619 if (out2)
620 out2[rep] = CRUSH_ITEM_UNDEF;
621 }
622
2d8be0bc 623 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
b459be73
ID
624#ifdef DEBUG_INDEP
625 if (out2 && ftotal) {
626 dprintk("%u %d a: ", ftotal, left);
627 for (rep = outpos; rep < endpos; rep++) {
628 dprintk(" %d", out[rep]);
629 }
630 dprintk("\n");
631 dprintk("%u %d b: ", ftotal, left);
632 for (rep = outpos; rep < endpos; rep++) {
633 dprintk(" %d", out2[rep]);
634 }
635 dprintk("\n");
636 }
637#endif
ab4ce2b5 638 for (rep = outpos; rep < endpos; rep++) {
9a3b490a
ID
639 if (out[rep] != CRUSH_ITEM_UNDEF)
640 continue;
641
642 in = bucket; /* initial bucket */
643
644 /* choose through intervening buckets */
645 for (;;) {
3102b0a5
ID
646 /* note: we base the choice on the position
647 * even in the nested call. that means that
648 * if the first layer chooses the same bucket
649 * in a different position, we will tend to
650 * choose a different item in that bucket.
651 * this will involve more devices in data
652 * movement and tend to distribute the load.
653 */
41586081 654 r = rep + parent_r;
9a3b490a
ID
655
656 /* be careful */
657 if (in->alg == CRUSH_BUCKET_UNIFORM &&
658 in->size % numrep == 0)
659 /* r'=r+(n+1)*f_total */
660 r += (numrep+1) * ftotal;
661 else
662 /* r' = r + n*f_total */
663 r += numrep * ftotal;
664
665 /* bucket choose */
666 if (in->size == 0) {
667 dprintk(" empty bucket\n");
668 break;
669 }
670
671 item = crush_bucket_choose(in, x, r);
672 if (item >= map->max_devices) {
673 dprintk(" bad item %d\n", item);
674 out[rep] = CRUSH_ITEM_NONE;
675 if (out2)
676 out2[rep] = CRUSH_ITEM_NONE;
677 left--;
678 break;
679 }
680
681 /* desired type? */
682 if (item < 0)
683 itemtype = map->buckets[-1-item]->type;
684 else
685 itemtype = 0;
686 dprintk(" item %d type %d\n", item, itemtype);
687
688 /* keep going? */
689 if (itemtype != type) {
690 if (item >= 0 ||
691 (-1-item) >= map->max_buckets) {
692 dprintk(" bad item type %d\n", type);
693 out[rep] = CRUSH_ITEM_NONE;
694 if (out2)
695 out2[rep] =
696 CRUSH_ITEM_NONE;
697 left--;
698 break;
699 }
700 in = map->buckets[-1-item];
701 continue;
702 }
703
704 /* collision? */
705 collide = 0;
ab4ce2b5 706 for (i = outpos; i < endpos; i++) {
9a3b490a
ID
707 if (out[i] == item) {
708 collide = 1;
709 break;
710 }
711 }
712 if (collide)
713 break;
714
715 if (recurse_to_leaf) {
716 if (item < 0) {
717 crush_choose_indep(map,
be3226ac
ID
718 map->buckets[-1-item],
719 weight, weight_max,
720 x, 1, numrep, 0,
721 out2, rep,
2d8be0bc 722 recurse_tries, 0,
be3226ac 723 0, NULL, r);
9a3b490a
ID
724 if (out2[rep] == CRUSH_ITEM_NONE) {
725 /* placed nothing; no leaf */
726 break;
727 }
728 } else {
729 /* we already have a leaf! */
730 out2[rep] = item;
731 }
732 }
733
734 /* out? */
735 if (itemtype == 0 &&
736 is_out(map, weight, weight_max, item, x))
737 break;
738
739 /* yay! */
740 out[rep] = item;
741 left--;
742 break;
743 }
744 }
745 }
ab4ce2b5 746 for (rep = outpos; rep < endpos; rep++) {
9a3b490a
ID
747 if (out[rep] == CRUSH_ITEM_UNDEF) {
748 out[rep] = CRUSH_ITEM_NONE;
749 }
750 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
751 out2[rep] = CRUSH_ITEM_NONE;
752 }
753 }
b459be73
ID
754#ifndef __KERNEL__
755 if (map->choose_tries && ftotal <= map->choose_total_tries)
756 map->choose_tries[ftotal]++;
757#endif
758#ifdef DEBUG_INDEP
759 if (out2) {
760 dprintk("%u %d a: ", ftotal, left);
761 for (rep = outpos; rep < endpos; rep++) {
762 dprintk(" %d", out[rep]);
763 }
764 dprintk("\n");
765 dprintk("%u %d b: ", ftotal, left);
766 for (rep = outpos; rep < endpos; rep++) {
767 dprintk(" %d", out2[rep]);
768 }
769 dprintk("\n");
770 }
771#endif
9a3b490a
ID
772}
773
5ecc0a0f
SW
774/**
775 * crush_do_rule - calculate a mapping with the given input and rule
776 * @map: the crush_map
777 * @ruleno: the rule id
778 * @x: hash input
779 * @result: pointer to result vector
780 * @result_max: maximum result size
b3b33b0e
ID
781 * @weight: weight vector (for map leaves)
782 * @weight_max: size of weight vector
e8ef19c4 783 * @scratch: scratch vector for private use; must be >= 3 * result_max
5ecc0a0f 784 */
8b12d47b 785int crush_do_rule(const struct crush_map *map,
5ecc0a0f 786 int ruleno, int x, int *result, int result_max,
e8ef19c4
ID
787 const __u32 *weight, int weight_max,
788 int *scratch)
5ecc0a0f
SW
789{
790 int result_len;
e8ef19c4
ID
791 int *a = scratch;
792 int *b = scratch + result_max;
793 int *c = scratch + result_max*2;
5ecc0a0f
SW
794 int recurse_to_leaf;
795 int *w;
796 int wsize = 0;
797 int *o;
798 int osize;
799 int *tmp;
800 struct crush_rule *rule;
8b12d47b 801 __u32 step;
5ecc0a0f
SW
802 int i, j;
803 int numrep;
45002267 804 int out_size;
48a163db
ID
805 /*
806 * the original choose_total_tries value was off by one (it
807 * counted "retries" and not "tries"). add one.
808 */
809 int choose_tries = map->choose_total_tries + 1;
f18650ac 810 int choose_leaf_tries = 0;
48a163db
ID
811 /*
812 * the local tries values were counted as "retries", though,
813 * and need no adjustment
814 */
815 int choose_local_retries = map->choose_local_tries;
816 int choose_local_fallback_retries = map->choose_local_fallback_tries;
5ecc0a0f 817
e2b149cc
ID
818 int vary_r = map->chooseleaf_vary_r;
819
a1f4895b
SW
820 if ((__u32)ruleno >= map->max_rules) {
821 dprintk(" bad ruleno %d\n", ruleno);
822 return 0;
823 }
5ecc0a0f
SW
824
825 rule = map->rules[ruleno];
826 result_len = 0;
827 w = a;
828 o = b;
829
5ecc0a0f 830 for (step = 0; step < rule->len; step++) {
8f99c85b 831 int firstn = 0;
0668216e
SW
832 struct crush_rule_step *curstep = &rule->steps[step];
833
0668216e 834 switch (curstep->op) {
5ecc0a0f 835 case CRUSH_RULE_TAKE:
8f529795
ID
836 if ((curstep->arg1 >= 0 &&
837 curstep->arg1 < map->max_devices) ||
838 (-1-curstep->arg1 < map->max_buckets &&
839 map->buckets[-1-curstep->arg1])) {
840 w[0] = curstep->arg1;
841 wsize = 1;
842 } else {
843 dprintk(" bad take value %d\n", curstep->arg1);
844 }
5ecc0a0f
SW
845 break;
846
cc10df4a
ID
847 case CRUSH_RULE_SET_CHOOSE_TRIES:
848 if (curstep->arg1 > 0)
849 choose_tries = curstep->arg1;
850 break;
851
917edad5 852 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
be3226ac
ID
853 if (curstep->arg1 > 0)
854 choose_leaf_tries = curstep->arg1;
855 break;
856
f046bf92 857 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
6ed1002f 858 if (curstep->arg1 >= 0)
48a163db 859 choose_local_retries = curstep->arg1;
f046bf92
ID
860 break;
861
862 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
6ed1002f 863 if (curstep->arg1 >= 0)
48a163db 864 choose_local_fallback_retries = curstep->arg1;
f046bf92 865 break;
d83ed858
ID
866
867 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
868 if (curstep->arg1 >= 0)
869 vary_r = curstep->arg1;
870 break;
f046bf92 871
917edad5 872 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
5ecc0a0f
SW
873 case CRUSH_RULE_CHOOSE_FIRSTN:
874 firstn = 1;
0668216e 875 /* fall through */
917edad5 876 case CRUSH_RULE_CHOOSELEAF_INDEP:
5ecc0a0f 877 case CRUSH_RULE_CHOOSE_INDEP:
a1f4895b
SW
878 if (wsize == 0)
879 break;
5ecc0a0f
SW
880
881 recurse_to_leaf =
0668216e 882 curstep->op ==
917edad5 883 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
0668216e 884 curstep->op ==
917edad5 885 CRUSH_RULE_CHOOSELEAF_INDEP;
5ecc0a0f
SW
886
887 /* reset output */
888 osize = 0;
889
890 for (i = 0; i < wsize; i++) {
f224a691 891 int bno;
5ecc0a0f
SW
892 /*
893 * see CRUSH_N, CRUSH_N_MINUS macros.
894 * basically, numrep <= 0 means relative to
895 * the provided result_max
896 */
0668216e 897 numrep = curstep->arg1;
5ecc0a0f
SW
898 if (numrep <= 0) {
899 numrep += result_max;
900 if (numrep <= 0)
901 continue;
902 }
903 j = 0;
f224a691
ID
904 /* make sure bucket id is valid */
905 bno = -1 - w[i];
906 if (bno < 0 || bno >= map->max_buckets) {
907 /* w[i] is probably CRUSH_ITEM_NONE */
908 dprintk(" bad w[i] %d\n", w[i]);
909 continue;
910 }
9a3b490a 911 if (firstn) {
d390bb2a
ID
912 int recurse_tries;
913 if (choose_leaf_tries)
914 recurse_tries =
915 choose_leaf_tries;
916 else if (map->chooseleaf_descend_once)
917 recurse_tries = 1;
918 else
919 recurse_tries = choose_tries;
9fe07182
ID
920 osize += crush_choose_firstn(
921 map,
f224a691 922 map->buckets[bno],
9fe07182
ID
923 weight, weight_max,
924 x, numrep,
925 curstep->arg2,
926 o+osize, j,
45002267 927 result_max-osize,
f18650ac 928 choose_tries,
d390bb2a 929 recurse_tries,
48a163db
ID
930 choose_local_retries,
931 choose_local_fallback_retries,
9fe07182 932 recurse_to_leaf,
e2b149cc
ID
933 vary_r,
934 c+osize,
935 0);
9a3b490a 936 } else {
45002267 937 out_size = ((numrep < (result_max-osize)) ?
b459be73 938 numrep : (result_max-osize));
9fe07182
ID
939 crush_choose_indep(
940 map,
f224a691 941 map->buckets[bno],
9fe07182 942 weight, weight_max,
45002267 943 x, out_size, numrep,
9fe07182
ID
944 curstep->arg2,
945 o+osize, j,
f18650ac 946 choose_tries,
d390bb2a
ID
947 choose_leaf_tries ?
948 choose_leaf_tries : 1,
9fe07182 949 recurse_to_leaf,
41586081
ID
950 c+osize,
951 0);
45002267 952 osize += out_size;
9a3b490a 953 }
5ecc0a0f
SW
954 }
955
956 if (recurse_to_leaf)
957 /* copy final _leaf_ values to output set */
958 memcpy(o, c, osize*sizeof(*o));
959
2a4ba74e 960 /* swap o and w arrays */
5ecc0a0f
SW
961 tmp = o;
962 o = w;
963 w = tmp;
964 wsize = osize;
965 break;
966
967
968 case CRUSH_RULE_EMIT:
969 for (i = 0; i < wsize && result_len < result_max; i++) {
970 result[result_len] = w[i];
971 result_len++;
972 }
973 wsize = 0;
974 break;
975
976 default:
a1f4895b
SW
977 dprintk(" unknown op %d at step %d\n",
978 curstep->op, step);
979 break;
5ecc0a0f
SW
980 }
981 }
f1932fc1 982 return result_len;
5ecc0a0f 983}