1b459be73SIlya Dryomov /*
2b459be73SIlya Dryomov * Ceph - scalable distributed file system
3b459be73SIlya Dryomov *
4b459be73SIlya Dryomov * Copyright (C) 2015 Intel Corporation All Rights Reserved
5b459be73SIlya Dryomov *
6b459be73SIlya Dryomov * This is free software; you can redistribute it and/or
7b459be73SIlya Dryomov * modify it under the terms of the GNU Lesser General Public
8b459be73SIlya Dryomov * License version 2.1, as published by the Free Software
9b459be73SIlya Dryomov * Foundation. See file COPYING.
10b459be73SIlya Dryomov *
11b459be73SIlya Dryomov */
123d14c5d2SYehuda Sadeh
133d14c5d2SYehuda Sadeh #ifdef __KERNEL__
143d14c5d2SYehuda Sadeh # include <linux/string.h>
153d14c5d2SYehuda Sadeh # include <linux/slab.h>
163d14c5d2SYehuda Sadeh # include <linux/bug.h>
173d14c5d2SYehuda Sadeh # include <linux/kernel.h>
183d14c5d2SYehuda Sadeh # include <linux/crush/crush.h>
193d14c5d2SYehuda Sadeh # include <linux/crush/hash.h>
20f6c0d1a3STobias Klauser # include <linux/crush/mapper.h>
21b459be73SIlya Dryomov #else
22b459be73SIlya Dryomov # include "crush_compat.h"
23b459be73SIlya Dryomov # include "crush.h"
24b459be73SIlya Dryomov # include "hash.h"
25f6c0d1a3STobias Klauser # include "mapper.h"
26b459be73SIlya Dryomov #endif
27958a2765SIlya Dryomov #include "crush_ln_table.h"
283d14c5d2SYehuda Sadeh
29b459be73SIlya Dryomov #define dprintk(args...) /* printf(args) */
30b459be73SIlya Dryomov
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh * Implement the core CRUSH mapping algorithm.
333d14c5d2SYehuda Sadeh */
343d14c5d2SYehuda Sadeh
353d14c5d2SYehuda Sadeh /**
363d14c5d2SYehuda Sadeh * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
373d14c5d2SYehuda Sadeh * @map: the crush_map
383d14c5d2SYehuda Sadeh * @ruleset: the storage ruleset id (user defined)
393d14c5d2SYehuda Sadeh * @type: storage ruleset type (user defined)
403d14c5d2SYehuda Sadeh * @size: output set size
413d14c5d2SYehuda Sadeh */
crush_find_rule(const struct crush_map * map,int ruleset,int type,int size)428b12d47bSSage Weil int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
433d14c5d2SYehuda Sadeh {
448b12d47bSSage Weil __u32 i;
453d14c5d2SYehuda Sadeh
463d14c5d2SYehuda Sadeh for (i = 0; i < map->max_rules; i++) {
473d14c5d2SYehuda Sadeh if (map->rules[i] &&
483d14c5d2SYehuda Sadeh map->rules[i]->mask.ruleset == ruleset &&
493d14c5d2SYehuda Sadeh map->rules[i]->mask.type == type &&
503d14c5d2SYehuda Sadeh map->rules[i]->mask.min_size <= size &&
513d14c5d2SYehuda Sadeh map->rules[i]->mask.max_size >= size)
523d14c5d2SYehuda Sadeh return i;
533d14c5d2SYehuda Sadeh }
543d14c5d2SYehuda Sadeh return -1;
553d14c5d2SYehuda Sadeh }
563d14c5d2SYehuda Sadeh
573d14c5d2SYehuda Sadeh /*
583d14c5d2SYehuda Sadeh * bucket choose methods
593d14c5d2SYehuda Sadeh *
603d14c5d2SYehuda Sadeh * For each bucket algorithm, we have a "choose" method that, given a
613d14c5d2SYehuda Sadeh * crush input @x and replica position (usually, position in output set) @r,
623d14c5d2SYehuda Sadeh * will produce an item in the bucket.
633d14c5d2SYehuda Sadeh */
643d14c5d2SYehuda Sadeh
653d14c5d2SYehuda Sadeh /*
663d14c5d2SYehuda Sadeh * Choose based on a random permutation of the bucket.
673d14c5d2SYehuda Sadeh *
683d14c5d2SYehuda Sadeh * We used to use some prime number arithmetic to do this, but it
693d14c5d2SYehuda Sadeh * wasn't very random, and had some other bad behaviors. Instead, we
703d14c5d2SYehuda Sadeh * calculate an actual random permutation of the bucket members.
713d14c5d2SYehuda Sadeh * Since this is expensive, we optimize for the r=0 case, which
723d14c5d2SYehuda Sadeh * captures the vast majority of calls.
733d14c5d2SYehuda Sadeh */
bucket_perm_choose(const struct crush_bucket * bucket,struct crush_work_bucket * work,int x,int r)7466a0e2d5SIlya Dryomov static int bucket_perm_choose(const struct crush_bucket *bucket,
7566a0e2d5SIlya Dryomov struct crush_work_bucket *work,
763d14c5d2SYehuda Sadeh int x, int r)
773d14c5d2SYehuda Sadeh {
7895c96174SEric Dumazet unsigned int pr = r % bucket->size;
7995c96174SEric Dumazet unsigned int i, s;
803d14c5d2SYehuda Sadeh
813d14c5d2SYehuda Sadeh /* start a new permutation if @x has changed */
8266a0e2d5SIlya Dryomov if (work->perm_x != (__u32)x || work->perm_n == 0) {
833d14c5d2SYehuda Sadeh dprintk("bucket %d new x=%d\n", bucket->id, x);
8466a0e2d5SIlya Dryomov work->perm_x = x;
853d14c5d2SYehuda Sadeh
863d14c5d2SYehuda Sadeh /* optimize common r=0 case */
873d14c5d2SYehuda Sadeh if (pr == 0) {
883d14c5d2SYehuda Sadeh s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
893d14c5d2SYehuda Sadeh bucket->size;
9066a0e2d5SIlya Dryomov work->perm[0] = s;
9166a0e2d5SIlya Dryomov work->perm_n = 0xffff; /* magic value, see below */
923d14c5d2SYehuda Sadeh goto out;
933d14c5d2SYehuda Sadeh }
943d14c5d2SYehuda Sadeh
953d14c5d2SYehuda Sadeh for (i = 0; i < bucket->size; i++)
9666a0e2d5SIlya Dryomov work->perm[i] = i;
9766a0e2d5SIlya Dryomov work->perm_n = 0;
9866a0e2d5SIlya Dryomov } else if (work->perm_n == 0xffff) {
993d14c5d2SYehuda Sadeh /* clean up after the r=0 case above */
1003d14c5d2SYehuda Sadeh for (i = 1; i < bucket->size; i++)
10166a0e2d5SIlya Dryomov work->perm[i] = i;
10266a0e2d5SIlya Dryomov work->perm[work->perm[0]] = 0;
10366a0e2d5SIlya Dryomov work->perm_n = 1;
1043d14c5d2SYehuda Sadeh }
1053d14c5d2SYehuda Sadeh
1063d14c5d2SYehuda Sadeh /* calculate permutation up to pr */
10766a0e2d5SIlya Dryomov for (i = 0; i < work->perm_n; i++)
1087ba0487cSIlya Dryomov dprintk(" perm_choose have %d: %d\n", i, work->perm[i]);
10966a0e2d5SIlya Dryomov while (work->perm_n <= pr) {
11066a0e2d5SIlya Dryomov unsigned int p = work->perm_n;
1113d14c5d2SYehuda Sadeh /* no point in swapping the final entry */
1123d14c5d2SYehuda Sadeh if (p < bucket->size - 1) {
1133d14c5d2SYehuda Sadeh i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
1143d14c5d2SYehuda Sadeh (bucket->size - p);
1153d14c5d2SYehuda Sadeh if (i) {
11666a0e2d5SIlya Dryomov unsigned int t = work->perm[p + i];
11766a0e2d5SIlya Dryomov work->perm[p + i] = work->perm[p];
11866a0e2d5SIlya Dryomov work->perm[p] = t;
1193d14c5d2SYehuda Sadeh }
1203d14c5d2SYehuda Sadeh dprintk(" perm_choose swap %d with %d\n", p, p+i);
1213d14c5d2SYehuda Sadeh }
12266a0e2d5SIlya Dryomov work->perm_n++;
1233d14c5d2SYehuda Sadeh }
1243d14c5d2SYehuda Sadeh for (i = 0; i < bucket->size; i++)
1257ba0487cSIlya Dryomov dprintk(" perm_choose %d: %d\n", i, work->perm[i]);
1263d14c5d2SYehuda Sadeh
12766a0e2d5SIlya Dryomov s = work->perm[pr];
1283d14c5d2SYehuda Sadeh out:
1293d14c5d2SYehuda Sadeh dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
1303d14c5d2SYehuda Sadeh bucket->size, x, r, pr, s);
1313d14c5d2SYehuda Sadeh return bucket->items[s];
1323d14c5d2SYehuda Sadeh }
1333d14c5d2SYehuda Sadeh
1343d14c5d2SYehuda Sadeh /* uniform */
bucket_uniform_choose(const struct crush_bucket_uniform * bucket,struct crush_work_bucket * work,int x,int r)13566a0e2d5SIlya Dryomov static int bucket_uniform_choose(const struct crush_bucket_uniform *bucket,
13666a0e2d5SIlya Dryomov struct crush_work_bucket *work, int x, int r)
1373d14c5d2SYehuda Sadeh {
13866a0e2d5SIlya Dryomov return bucket_perm_choose(&bucket->h, work, x, r);
1393d14c5d2SYehuda Sadeh }
1403d14c5d2SYehuda Sadeh
1413d14c5d2SYehuda Sadeh /* list */
bucket_list_choose(const struct crush_bucket_list * bucket,int x,int r)14266a0e2d5SIlya Dryomov static int bucket_list_choose(const struct crush_bucket_list *bucket,
1433d14c5d2SYehuda Sadeh int x, int r)
1443d14c5d2SYehuda Sadeh {
1453d14c5d2SYehuda Sadeh int i;
1463d14c5d2SYehuda Sadeh
1473d14c5d2SYehuda Sadeh for (i = bucket->h.size-1; i >= 0; i--) {
1483d14c5d2SYehuda Sadeh __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
1493d14c5d2SYehuda Sadeh r, bucket->h.id);
1503d14c5d2SYehuda Sadeh w &= 0xffff;
1513d14c5d2SYehuda Sadeh dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
1523d14c5d2SYehuda Sadeh "sw %x rand %llx",
1533d14c5d2SYehuda Sadeh i, x, r, bucket->h.items[i], bucket->item_weights[i],
1543d14c5d2SYehuda Sadeh bucket->sum_weights[i], w);
1553d14c5d2SYehuda Sadeh w *= bucket->sum_weights[i];
1563d14c5d2SYehuda Sadeh w = w >> 16;
1573d14c5d2SYehuda Sadeh /*dprintk(" scaled %llx\n", w);*/
15866a0e2d5SIlya Dryomov if (w < bucket->item_weights[i]) {
1593d14c5d2SYehuda Sadeh return bucket->h.items[i];
1603d14c5d2SYehuda Sadeh }
16166a0e2d5SIlya Dryomov }
1623d14c5d2SYehuda Sadeh
163a1f4895bSSage Weil dprintk("bad list sums for bucket %d\n", bucket->h.id);
164a1f4895bSSage Weil return bucket->h.items[0];
1653d14c5d2SYehuda Sadeh }
1663d14c5d2SYehuda Sadeh
1673d14c5d2SYehuda Sadeh
1683d14c5d2SYehuda Sadeh /* (binary) tree */
height(int n)1693d14c5d2SYehuda Sadeh static int height(int n)
1703d14c5d2SYehuda Sadeh {
1713d14c5d2SYehuda Sadeh int h = 0;
1723d14c5d2SYehuda Sadeh while ((n & 1) == 0) {
1733d14c5d2SYehuda Sadeh h++;
1743d14c5d2SYehuda Sadeh n = n >> 1;
1753d14c5d2SYehuda Sadeh }
1763d14c5d2SYehuda Sadeh return h;
1773d14c5d2SYehuda Sadeh }
1783d14c5d2SYehuda Sadeh
left(int x)1793d14c5d2SYehuda Sadeh static int left(int x)
1803d14c5d2SYehuda Sadeh {
1813d14c5d2SYehuda Sadeh int h = height(x);
1823d14c5d2SYehuda Sadeh return x - (1 << (h-1));
1833d14c5d2SYehuda Sadeh }
1843d14c5d2SYehuda Sadeh
right(int x)1853d14c5d2SYehuda Sadeh static int right(int x)
1863d14c5d2SYehuda Sadeh {
1873d14c5d2SYehuda Sadeh int h = height(x);
1883d14c5d2SYehuda Sadeh return x + (1 << (h-1));
1893d14c5d2SYehuda Sadeh }
1903d14c5d2SYehuda Sadeh
terminal(int x)1913d14c5d2SYehuda Sadeh static int terminal(int x)
1923d14c5d2SYehuda Sadeh {
1933d14c5d2SYehuda Sadeh return x & 1;
1943d14c5d2SYehuda Sadeh }
1953d14c5d2SYehuda Sadeh
bucket_tree_choose(const struct crush_bucket_tree * bucket,int x,int r)19666a0e2d5SIlya Dryomov static int bucket_tree_choose(const struct crush_bucket_tree *bucket,
1973d14c5d2SYehuda Sadeh int x, int r)
1983d14c5d2SYehuda Sadeh {
1998f99c85bSIlya Dryomov int n;
2003d14c5d2SYehuda Sadeh __u32 w;
2013d14c5d2SYehuda Sadeh __u64 t;
2023d14c5d2SYehuda Sadeh
2033d14c5d2SYehuda Sadeh /* start at root */
2043d14c5d2SYehuda Sadeh n = bucket->num_nodes >> 1;
2053d14c5d2SYehuda Sadeh
2063d14c5d2SYehuda Sadeh while (!terminal(n)) {
2078f99c85bSIlya Dryomov int l;
2083d14c5d2SYehuda Sadeh /* pick point in [0, w) */
2093d14c5d2SYehuda Sadeh w = bucket->node_weights[n];
2103d14c5d2SYehuda Sadeh t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
2113d14c5d2SYehuda Sadeh bucket->h.id) * (__u64)w;
2123d14c5d2SYehuda Sadeh t = t >> 32;
2133d14c5d2SYehuda Sadeh
2143d14c5d2SYehuda Sadeh /* descend to the left or right? */
2153d14c5d2SYehuda Sadeh l = left(n);
2163d14c5d2SYehuda Sadeh if (t < bucket->node_weights[l])
2173d14c5d2SYehuda Sadeh n = l;
2183d14c5d2SYehuda Sadeh else
2193d14c5d2SYehuda Sadeh n = right(n);
2203d14c5d2SYehuda Sadeh }
2213d14c5d2SYehuda Sadeh
2223d14c5d2SYehuda Sadeh return bucket->h.items[n >> 1];
2233d14c5d2SYehuda Sadeh }
2243d14c5d2SYehuda Sadeh
2253d14c5d2SYehuda Sadeh
2263d14c5d2SYehuda Sadeh /* straw */
2273d14c5d2SYehuda Sadeh
bucket_straw_choose(const struct crush_bucket_straw * bucket,int x,int r)22866a0e2d5SIlya Dryomov static int bucket_straw_choose(const struct crush_bucket_straw *bucket,
2293d14c5d2SYehuda Sadeh int x, int r)
2303d14c5d2SYehuda Sadeh {
2318b12d47bSSage Weil __u32 i;
2323d14c5d2SYehuda Sadeh int high = 0;
2333d14c5d2SYehuda Sadeh __u64 high_draw = 0;
2343d14c5d2SYehuda Sadeh __u64 draw;
2353d14c5d2SYehuda Sadeh
2363d14c5d2SYehuda Sadeh for (i = 0; i < bucket->h.size; i++) {
2373d14c5d2SYehuda Sadeh draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
2383d14c5d2SYehuda Sadeh draw &= 0xffff;
2393d14c5d2SYehuda Sadeh draw *= bucket->straws[i];
2403d14c5d2SYehuda Sadeh if (i == 0 || draw > high_draw) {
2413d14c5d2SYehuda Sadeh high = i;
2423d14c5d2SYehuda Sadeh high_draw = draw;
2433d14c5d2SYehuda Sadeh }
2443d14c5d2SYehuda Sadeh }
2453d14c5d2SYehuda Sadeh return bucket->h.items[high];
2463d14c5d2SYehuda Sadeh }
2473d14c5d2SYehuda Sadeh
248b459be73SIlya Dryomov /* compute 2^44*log2(input+1) */
crush_ln(unsigned int xin)249b459be73SIlya Dryomov static __u64 crush_ln(unsigned int xin)
250958a2765SIlya Dryomov {
25164f77566SIlya Dryomov unsigned int x = xin;
252958a2765SIlya Dryomov int iexpon, index1, index2;
253b459be73SIlya Dryomov __u64 RH, LH, LL, xl64, result;
254958a2765SIlya Dryomov
255958a2765SIlya Dryomov x++;
256958a2765SIlya Dryomov
257b459be73SIlya Dryomov /* normalize input */
258958a2765SIlya Dryomov iexpon = 15;
25974a52938SIlya Dryomov
26074a52938SIlya Dryomov /*
26174a52938SIlya Dryomov * figure out number of bits we need to shift and
26274a52938SIlya Dryomov * do it in one step instead of iteratively
26374a52938SIlya Dryomov */
26474a52938SIlya Dryomov if (!(x & 0x18000)) {
26574a52938SIlya Dryomov int bits = __builtin_clz(x & 0x1FFFF) - 16;
26674a52938SIlya Dryomov x <<= bits;
26774a52938SIlya Dryomov iexpon = 15 - bits;
268b459be73SIlya Dryomov }
269958a2765SIlya Dryomov
270958a2765SIlya Dryomov index1 = (x >> 8) << 1;
271b459be73SIlya Dryomov /* RH ~ 2^56/index1 */
272958a2765SIlya Dryomov RH = __RH_LH_tbl[index1 - 256];
273b459be73SIlya Dryomov /* LH ~ 2^48 * log2(index1/256) */
274958a2765SIlya Dryomov LH = __RH_LH_tbl[index1 + 1 - 256];
275958a2765SIlya Dryomov
276b459be73SIlya Dryomov /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
277b459be73SIlya Dryomov xl64 = (__s64)x * RH;
278958a2765SIlya Dryomov xl64 >>= 48;
279958a2765SIlya Dryomov
280958a2765SIlya Dryomov result = iexpon;
281958a2765SIlya Dryomov result <<= (12 + 32);
282958a2765SIlya Dryomov
28364f77566SIlya Dryomov index2 = xl64 & 0xff;
284b459be73SIlya Dryomov /* LL ~ 2^48*log2(1.0+index2/2^15) */
285958a2765SIlya Dryomov LL = __LL_tbl[index2];
286958a2765SIlya Dryomov
287958a2765SIlya Dryomov LH = LH + LL;
288958a2765SIlya Dryomov
289958a2765SIlya Dryomov LH >>= (48 - 12 - 32);
290958a2765SIlya Dryomov result += LH;
291958a2765SIlya Dryomov
292958a2765SIlya Dryomov return result;
293958a2765SIlya Dryomov }
294958a2765SIlya Dryomov
295958a2765SIlya Dryomov
296958a2765SIlya Dryomov /*
297958a2765SIlya Dryomov * straw2
298958a2765SIlya Dryomov *
299958a2765SIlya Dryomov * for reference, see:
300958a2765SIlya Dryomov *
30194f17c00SAlexander A. Klimov * https://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
302958a2765SIlya Dryomov *
303958a2765SIlya Dryomov */
304958a2765SIlya Dryomov
get_choose_arg_weights(const struct crush_bucket_straw2 * bucket,const struct crush_choose_arg * arg,int position)305069f3222SIlya Dryomov static __u32 *get_choose_arg_weights(const struct crush_bucket_straw2 *bucket,
306069f3222SIlya Dryomov const struct crush_choose_arg *arg,
307069f3222SIlya Dryomov int position)
308069f3222SIlya Dryomov {
309c7ed1a4bSIlya Dryomov if (!arg || !arg->weight_set)
310069f3222SIlya Dryomov return bucket->item_weights;
311069f3222SIlya Dryomov
312069f3222SIlya Dryomov if (position >= arg->weight_set_size)
313069f3222SIlya Dryomov position = arg->weight_set_size - 1;
314069f3222SIlya Dryomov return arg->weight_set[position].weights;
315069f3222SIlya Dryomov }
316069f3222SIlya Dryomov
get_choose_arg_ids(const struct crush_bucket_straw2 * bucket,const struct crush_choose_arg * arg)317069f3222SIlya Dryomov static __s32 *get_choose_arg_ids(const struct crush_bucket_straw2 *bucket,
318069f3222SIlya Dryomov const struct crush_choose_arg *arg)
319069f3222SIlya Dryomov {
320069f3222SIlya Dryomov if (!arg || !arg->ids)
321069f3222SIlya Dryomov return bucket->h.items;
322069f3222SIlya Dryomov
323069f3222SIlya Dryomov return arg->ids;
324069f3222SIlya Dryomov }
325069f3222SIlya Dryomov
bucket_straw2_choose(const struct crush_bucket_straw2 * bucket,int x,int r,const struct crush_choose_arg * arg,int position)32666a0e2d5SIlya Dryomov static int bucket_straw2_choose(const struct crush_bucket_straw2 *bucket,
327069f3222SIlya Dryomov int x, int r,
328069f3222SIlya Dryomov const struct crush_choose_arg *arg,
329069f3222SIlya Dryomov int position)
330958a2765SIlya Dryomov {
331b459be73SIlya Dryomov unsigned int i, high = 0;
332b459be73SIlya Dryomov unsigned int u;
333958a2765SIlya Dryomov __s64 ln, draw, high_draw = 0;
334069f3222SIlya Dryomov __u32 *weights = get_choose_arg_weights(bucket, arg, position);
335069f3222SIlya Dryomov __s32 *ids = get_choose_arg_ids(bucket, arg);
336958a2765SIlya Dryomov
337958a2765SIlya Dryomov for (i = 0; i < bucket->h.size; i++) {
338069f3222SIlya Dryomov dprintk("weight 0x%x item %d\n", weights[i], ids[i]);
339069f3222SIlya Dryomov if (weights[i]) {
340069f3222SIlya Dryomov u = crush_hash32_3(bucket->h.hash, x, ids[i], r);
341958a2765SIlya Dryomov u &= 0xffff;
342958a2765SIlya Dryomov
343958a2765SIlya Dryomov /*
344958a2765SIlya Dryomov * for some reason slightly less than 0x10000 produces
345958a2765SIlya Dryomov * a slightly more accurate distribution... probably a
346958a2765SIlya Dryomov * rounding effect.
347958a2765SIlya Dryomov *
348958a2765SIlya Dryomov * the natural log lookup table maps [0,0xffff]
349958a2765SIlya Dryomov * (corresponding to real numbers [1/0x10000, 1] to
350958a2765SIlya Dryomov * [0, 0xffffffffffff] (corresponding to real numbers
351958a2765SIlya Dryomov * [-11.090355,0]).
352958a2765SIlya Dryomov */
353958a2765SIlya Dryomov ln = crush_ln(u) - 0x1000000000000ll;
354958a2765SIlya Dryomov
355958a2765SIlya Dryomov /*
356958a2765SIlya Dryomov * divide by 16.16 fixed-point weight. note
357958a2765SIlya Dryomov * that the ln value is negative, so a larger
358958a2765SIlya Dryomov * weight means a larger (less negative) value
359958a2765SIlya Dryomov * for draw.
360958a2765SIlya Dryomov */
361069f3222SIlya Dryomov draw = div64_s64(ln, weights[i]);
362958a2765SIlya Dryomov } else {
363958a2765SIlya Dryomov draw = S64_MIN;
364958a2765SIlya Dryomov }
365958a2765SIlya Dryomov
366958a2765SIlya Dryomov if (i == 0 || draw > high_draw) {
367958a2765SIlya Dryomov high = i;
368958a2765SIlya Dryomov high_draw = draw;
369958a2765SIlya Dryomov }
370958a2765SIlya Dryomov }
37166a0e2d5SIlya Dryomov
372958a2765SIlya Dryomov return bucket->h.items[high];
373958a2765SIlya Dryomov }
374958a2765SIlya Dryomov
375958a2765SIlya Dryomov
crush_bucket_choose(const struct crush_bucket * in,struct crush_work_bucket * work,int x,int r,const struct crush_choose_arg * arg,int position)37666a0e2d5SIlya Dryomov static int crush_bucket_choose(const struct crush_bucket *in,
37766a0e2d5SIlya Dryomov struct crush_work_bucket *work,
378069f3222SIlya Dryomov int x, int r,
379069f3222SIlya Dryomov const struct crush_choose_arg *arg,
380069f3222SIlya Dryomov int position)
3813d14c5d2SYehuda Sadeh {
3823d14c5d2SYehuda Sadeh dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
383a1f4895bSSage Weil BUG_ON(in->size == 0);
3843d14c5d2SYehuda Sadeh switch (in->alg) {
3853d14c5d2SYehuda Sadeh case CRUSH_BUCKET_UNIFORM:
38666a0e2d5SIlya Dryomov return bucket_uniform_choose(
38766a0e2d5SIlya Dryomov (const struct crush_bucket_uniform *)in,
38866a0e2d5SIlya Dryomov work, x, r);
3893d14c5d2SYehuda Sadeh case CRUSH_BUCKET_LIST:
39066a0e2d5SIlya Dryomov return bucket_list_choose((const struct crush_bucket_list *)in,
3913d14c5d2SYehuda Sadeh x, r);
3923d14c5d2SYehuda Sadeh case CRUSH_BUCKET_TREE:
39366a0e2d5SIlya Dryomov return bucket_tree_choose((const struct crush_bucket_tree *)in,
3943d14c5d2SYehuda Sadeh x, r);
3953d14c5d2SYehuda Sadeh case CRUSH_BUCKET_STRAW:
39666a0e2d5SIlya Dryomov return bucket_straw_choose(
39766a0e2d5SIlya Dryomov (const struct crush_bucket_straw *)in,
3983d14c5d2SYehuda Sadeh x, r);
399958a2765SIlya Dryomov case CRUSH_BUCKET_STRAW2:
40066a0e2d5SIlya Dryomov return bucket_straw2_choose(
40166a0e2d5SIlya Dryomov (const struct crush_bucket_straw2 *)in,
402069f3222SIlya Dryomov x, r, arg, position);
4033d14c5d2SYehuda Sadeh default:
404a1f4895bSSage Weil dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
4053d14c5d2SYehuda Sadeh return in->items[0];
4063d14c5d2SYehuda Sadeh }
4073d14c5d2SYehuda Sadeh }
4083d14c5d2SYehuda Sadeh
4093d14c5d2SYehuda Sadeh /*
4103d14c5d2SYehuda Sadeh * true if device is marked "out" (failed, fully offloaded)
4113d14c5d2SYehuda Sadeh * of the cluster
4123d14c5d2SYehuda Sadeh */
is_out(const struct crush_map * map,const __u32 * weight,int weight_max,int item,int x)413b3b33b0eSIlya Dryomov static int is_out(const struct crush_map *map,
414b3b33b0eSIlya Dryomov const __u32 *weight, int weight_max,
415b3b33b0eSIlya Dryomov int item, int x)
4163d14c5d2SYehuda Sadeh {
417b3b33b0eSIlya Dryomov if (item >= weight_max)
418b3b33b0eSIlya Dryomov return 1;
4193d14c5d2SYehuda Sadeh if (weight[item] >= 0x10000)
4203d14c5d2SYehuda Sadeh return 0;
4213d14c5d2SYehuda Sadeh if (weight[item] == 0)
4223d14c5d2SYehuda Sadeh return 1;
4233d14c5d2SYehuda Sadeh if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
4243d14c5d2SYehuda Sadeh < weight[item])
4253d14c5d2SYehuda Sadeh return 0;
4263d14c5d2SYehuda Sadeh return 1;
4273d14c5d2SYehuda Sadeh }
4283d14c5d2SYehuda Sadeh
4293d14c5d2SYehuda Sadeh /**
4309fe07182SIlya Dryomov * crush_choose_firstn - choose numrep distinct items of given type
4313d14c5d2SYehuda Sadeh * @map: the crush_map
4323d14c5d2SYehuda Sadeh * @bucket: the bucket we are choose an item from
4333d14c5d2SYehuda Sadeh * @x: crush input value
4343d14c5d2SYehuda Sadeh * @numrep: the number of items to choose
4353d14c5d2SYehuda Sadeh * @type: the type of item to choose
4363d14c5d2SYehuda Sadeh * @out: pointer to output vector
4373d14c5d2SYehuda Sadeh * @outpos: our position in that vector
43845002267SIlya Dryomov * @out_size: size of the out vector
4390e32d712SIlya Dryomov * @tries: number of attempts to make
4400e32d712SIlya Dryomov * @recurse_tries: number of attempts to have recursive chooseleaf make
44148a163dbSIlya Dryomov * @local_retries: localized retries
44248a163dbSIlya Dryomov * @local_fallback_retries: localized fallback retries
4430e32d712SIlya Dryomov * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
444dc6ae6d8SIlya Dryomov * @stable: stable mode starts rep=0 in the recursive call for all replicas
445e2b149ccSIlya Dryomov * @vary_r: pass r to recursive calls
4463d14c5d2SYehuda Sadeh * @out2: second output vector for leaf items (if @recurse_to_leaf)
447e2b149ccSIlya Dryomov * @parent_r: r value passed from the parent
4483d14c5d2SYehuda Sadeh */
crush_choose_firstn(const struct crush_map * map,struct crush_work * work,const struct crush_bucket * bucket,const __u32 * weight,int weight_max,int x,int numrep,int type,int * out,int outpos,int out_size,unsigned int tries,unsigned int recurse_tries,unsigned int local_retries,unsigned int local_fallback_retries,int recurse_to_leaf,unsigned int vary_r,unsigned int stable,int * out2,int parent_r,const struct crush_choose_arg * choose_args)4499fe07182SIlya Dryomov static int crush_choose_firstn(const struct crush_map *map,
45066a0e2d5SIlya Dryomov struct crush_work *work,
45166a0e2d5SIlya Dryomov const struct crush_bucket *bucket,
452b3b33b0eSIlya Dryomov const __u32 *weight, int weight_max,
4533d14c5d2SYehuda Sadeh int x, int numrep, int type,
4543d14c5d2SYehuda Sadeh int *out, int outpos,
45545002267SIlya Dryomov int out_size,
4562d8be0bcSIlya Dryomov unsigned int tries,
4572d8be0bcSIlya Dryomov unsigned int recurse_tries,
45848a163dbSIlya Dryomov unsigned int local_retries,
45948a163dbSIlya Dryomov unsigned int local_fallback_retries,
4609fe07182SIlya Dryomov int recurse_to_leaf,
461e2b149ccSIlya Dryomov unsigned int vary_r,
462dc6ae6d8SIlya Dryomov unsigned int stable,
463e2b149ccSIlya Dryomov int *out2,
464069f3222SIlya Dryomov int parent_r,
465069f3222SIlya Dryomov const struct crush_choose_arg *choose_args)
4663d14c5d2SYehuda Sadeh {
4673d14c5d2SYehuda Sadeh int rep;
4688b12d47bSSage Weil unsigned int ftotal, flocal;
4693d14c5d2SYehuda Sadeh int retry_descent, retry_bucket, skip_rep;
47066a0e2d5SIlya Dryomov const struct crush_bucket *in = bucket;
4713d14c5d2SYehuda Sadeh int r;
4723d14c5d2SYehuda Sadeh int i;
4733d14c5d2SYehuda Sadeh int item = 0;
4743d14c5d2SYehuda Sadeh int itemtype;
4753d14c5d2SYehuda Sadeh int collide, reject;
47645002267SIlya Dryomov int count = out_size;
4773d14c5d2SYehuda Sadeh
478dc6ae6d8SIlya Dryomov 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 stable %d\n",
479e2b149ccSIlya Dryomov recurse_to_leaf ? "_LEAF" : "",
480e2b149ccSIlya Dryomov bucket->id, x, outpos, numrep,
481e2b149ccSIlya Dryomov tries, recurse_tries, local_retries, local_fallback_retries,
482dc6ae6d8SIlya Dryomov parent_r, stable);
4833d14c5d2SYehuda Sadeh
484dc6ae6d8SIlya Dryomov for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) {
4853d14c5d2SYehuda Sadeh /* keep trying until we get a non-out, non-colliding item */
4863d14c5d2SYehuda Sadeh ftotal = 0;
4873d14c5d2SYehuda Sadeh skip_rep = 0;
4883d14c5d2SYehuda Sadeh do {
4893d14c5d2SYehuda Sadeh retry_descent = 0;
4903d14c5d2SYehuda Sadeh in = bucket; /* initial bucket */
4913d14c5d2SYehuda Sadeh
4923d14c5d2SYehuda Sadeh /* choose through intervening buckets */
4933d14c5d2SYehuda Sadeh flocal = 0;
4943d14c5d2SYehuda Sadeh do {
4953d14c5d2SYehuda Sadeh collide = 0;
4963d14c5d2SYehuda Sadeh retry_bucket = 0;
497e2b149ccSIlya Dryomov r = rep + parent_r;
4983d14c5d2SYehuda Sadeh /* r' = r + f_total */
4993d14c5d2SYehuda Sadeh r += ftotal;
5003d14c5d2SYehuda Sadeh
5013d14c5d2SYehuda Sadeh /* bucket choose */
5023d14c5d2SYehuda Sadeh if (in->size == 0) {
5033d14c5d2SYehuda Sadeh reject = 1;
5043d14c5d2SYehuda Sadeh goto reject;
5053d14c5d2SYehuda Sadeh }
50648a163dbSIlya Dryomov if (local_fallback_retries > 0 &&
507546f04efSSage Weil flocal >= (in->size>>1) &&
50848a163dbSIlya Dryomov flocal > local_fallback_retries)
50966a0e2d5SIlya Dryomov item = bucket_perm_choose(
51066a0e2d5SIlya Dryomov in, work->work[-1-in->id],
51166a0e2d5SIlya Dryomov x, r);
5123d14c5d2SYehuda Sadeh else
51366a0e2d5SIlya Dryomov item = crush_bucket_choose(
51466a0e2d5SIlya Dryomov in, work->work[-1-in->id],
515069f3222SIlya Dryomov x, r,
516069f3222SIlya Dryomov (choose_args ?
5174de17aeaSYueHaibing &choose_args[-1-in->id] : NULL),
518069f3222SIlya Dryomov outpos);
519a1f4895bSSage Weil if (item >= map->max_devices) {
520a1f4895bSSage Weil dprintk(" bad item %d\n", item);
521a1f4895bSSage Weil skip_rep = 1;
522a1f4895bSSage Weil break;
523a1f4895bSSage Weil }
5243d14c5d2SYehuda Sadeh
5253d14c5d2SYehuda Sadeh /* desired type? */
5263d14c5d2SYehuda Sadeh if (item < 0)
5273d14c5d2SYehuda Sadeh itemtype = map->buckets[-1-item]->type;
5283d14c5d2SYehuda Sadeh else
5293d14c5d2SYehuda Sadeh itemtype = 0;
5303d14c5d2SYehuda Sadeh dprintk(" item %d type %d\n", item, itemtype);
5313d14c5d2SYehuda Sadeh
5323d14c5d2SYehuda Sadeh /* keep going? */
5333d14c5d2SYehuda Sadeh if (itemtype != type) {
534a1f4895bSSage Weil if (item >= 0 ||
535a1f4895bSSage Weil (-1-item) >= map->max_buckets) {
536a1f4895bSSage Weil dprintk(" bad item type %d\n", type);
537a1f4895bSSage Weil skip_rep = 1;
538a1f4895bSSage Weil break;
539a1f4895bSSage Weil }
5403d14c5d2SYehuda Sadeh in = map->buckets[-1-item];
5413d14c5d2SYehuda Sadeh retry_bucket = 1;
5423d14c5d2SYehuda Sadeh continue;
5433d14c5d2SYehuda Sadeh }
5443d14c5d2SYehuda Sadeh
5453d14c5d2SYehuda Sadeh /* collision? */
5463d14c5d2SYehuda Sadeh for (i = 0; i < outpos; i++) {
5473d14c5d2SYehuda Sadeh if (out[i] == item) {
5483d14c5d2SYehuda Sadeh collide = 1;
5493d14c5d2SYehuda Sadeh break;
5503d14c5d2SYehuda Sadeh }
5513d14c5d2SYehuda Sadeh }
5523d14c5d2SYehuda Sadeh
5533d14c5d2SYehuda Sadeh reject = 0;
5547d7c1f61SSage Weil if (!collide && recurse_to_leaf) {
5553d14c5d2SYehuda Sadeh if (item < 0) {
556e2b149ccSIlya Dryomov int sub_r;
557e2b149ccSIlya Dryomov if (vary_r)
558e2b149ccSIlya Dryomov sub_r = r >> (vary_r-1);
559e2b149ccSIlya Dryomov else
560e2b149ccSIlya Dryomov sub_r = 0;
56166a0e2d5SIlya Dryomov if (crush_choose_firstn(
56266a0e2d5SIlya Dryomov map,
56366a0e2d5SIlya Dryomov work,
5643d14c5d2SYehuda Sadeh map->buckets[-1-item],
565b3b33b0eSIlya Dryomov weight, weight_max,
566dc6ae6d8SIlya Dryomov x, stable ? 1 : outpos+1, 0,
56745002267SIlya Dryomov out2, outpos, count,
5682d8be0bcSIlya Dryomov recurse_tries, 0,
56948a163dbSIlya Dryomov local_retries,
57048a163dbSIlya Dryomov local_fallback_retries,
5719fe07182SIlya Dryomov 0,
572e2b149ccSIlya Dryomov vary_r,
573dc6ae6d8SIlya Dryomov stable,
574e2b149ccSIlya Dryomov NULL,
575069f3222SIlya Dryomov sub_r,
576069f3222SIlya Dryomov choose_args) <= outpos)
5773d14c5d2SYehuda Sadeh /* didn't get leaf */
5783d14c5d2SYehuda Sadeh reject = 1;
5793d14c5d2SYehuda Sadeh } else {
5803d14c5d2SYehuda Sadeh /* we already have a leaf! */
5813d14c5d2SYehuda Sadeh out2[outpos] = item;
5823d14c5d2SYehuda Sadeh }
5833d14c5d2SYehuda Sadeh }
5843d14c5d2SYehuda Sadeh
58598ba6af7SIlya Dryomov if (!reject && !collide) {
5863d14c5d2SYehuda Sadeh /* out? */
5873d14c5d2SYehuda Sadeh if (itemtype == 0)
5883d14c5d2SYehuda Sadeh reject = is_out(map, weight,
589b3b33b0eSIlya Dryomov weight_max,
5903d14c5d2SYehuda Sadeh item, x);
5913d14c5d2SYehuda Sadeh }
5923d14c5d2SYehuda Sadeh
5933d14c5d2SYehuda Sadeh reject:
5943d14c5d2SYehuda Sadeh if (reject || collide) {
5953d14c5d2SYehuda Sadeh ftotal++;
5963d14c5d2SYehuda Sadeh flocal++;
5973d14c5d2SYehuda Sadeh
59848a163dbSIlya Dryomov if (collide && flocal <= local_retries)
5993d14c5d2SYehuda Sadeh /* retry locally a few times */
6003d14c5d2SYehuda Sadeh retry_bucket = 1;
60148a163dbSIlya Dryomov else if (local_fallback_retries > 0 &&
60248a163dbSIlya Dryomov flocal <= in->size + local_fallback_retries)
6033d14c5d2SYehuda Sadeh /* exhaustive bucket search */
6043d14c5d2SYehuda Sadeh retry_bucket = 1;
60548a163dbSIlya Dryomov else if (ftotal < tries)
6063d14c5d2SYehuda Sadeh /* then retry descent */
6073d14c5d2SYehuda Sadeh retry_descent = 1;
6083d14c5d2SYehuda Sadeh else
6093d14c5d2SYehuda Sadeh /* else give up */
6103d14c5d2SYehuda Sadeh skip_rep = 1;
6113d14c5d2SYehuda Sadeh dprintk(" reject %d collide %d "
6128b12d47bSSage Weil "ftotal %u flocal %u\n",
6133d14c5d2SYehuda Sadeh reject, collide, ftotal,
6143d14c5d2SYehuda Sadeh flocal);
6153d14c5d2SYehuda Sadeh }
6163d14c5d2SYehuda Sadeh } while (retry_bucket);
6173d14c5d2SYehuda Sadeh } while (retry_descent);
6183d14c5d2SYehuda Sadeh
6193d14c5d2SYehuda Sadeh if (skip_rep) {
6203d14c5d2SYehuda Sadeh dprintk("skip rep\n");
6213d14c5d2SYehuda Sadeh continue;
6223d14c5d2SYehuda Sadeh }
6233d14c5d2SYehuda Sadeh
6243d14c5d2SYehuda Sadeh dprintk("CHOOSE got %d\n", item);
6253d14c5d2SYehuda Sadeh out[outpos] = item;
6263d14c5d2SYehuda Sadeh outpos++;
62745002267SIlya Dryomov count--;
628b459be73SIlya Dryomov #ifndef __KERNEL__
629b459be73SIlya Dryomov if (map->choose_tries && ftotal <= map->choose_total_tries)
630b459be73SIlya Dryomov map->choose_tries[ftotal]++;
631b459be73SIlya Dryomov #endif
6323d14c5d2SYehuda Sadeh }
6333d14c5d2SYehuda Sadeh
6343d14c5d2SYehuda Sadeh dprintk("CHOOSE returns %d\n", outpos);
6353d14c5d2SYehuda Sadeh return outpos;
6363d14c5d2SYehuda Sadeh }
6373d14c5d2SYehuda Sadeh
6383d14c5d2SYehuda Sadeh
6393d14c5d2SYehuda Sadeh /**
6409fe07182SIlya Dryomov * crush_choose_indep: alternative breadth-first positionally stable mapping
6419a3b490aSIlya Dryomov *
6429a3b490aSIlya Dryomov */
crush_choose_indep(const struct crush_map * map,struct crush_work * work,const struct crush_bucket * bucket,const __u32 * weight,int weight_max,int x,int left,int numrep,int type,int * out,int outpos,unsigned int tries,unsigned int recurse_tries,int recurse_to_leaf,int * out2,int parent_r,const struct crush_choose_arg * choose_args)6439a3b490aSIlya Dryomov static void crush_choose_indep(const struct crush_map *map,
64466a0e2d5SIlya Dryomov struct crush_work *work,
64566a0e2d5SIlya Dryomov const struct crush_bucket *bucket,
6469a3b490aSIlya Dryomov const __u32 *weight, int weight_max,
647ab4ce2b5SIlya Dryomov int x, int left, int numrep, int type,
6489a3b490aSIlya Dryomov int *out, int outpos,
6492d8be0bcSIlya Dryomov unsigned int tries,
6502d8be0bcSIlya Dryomov unsigned int recurse_tries,
6519a3b490aSIlya Dryomov int recurse_to_leaf,
65241586081SIlya Dryomov int *out2,
653069f3222SIlya Dryomov int parent_r,
654069f3222SIlya Dryomov const struct crush_choose_arg *choose_args)
6559a3b490aSIlya Dryomov {
65666a0e2d5SIlya Dryomov const struct crush_bucket *in = bucket;
657ab4ce2b5SIlya Dryomov int endpos = outpos + left;
6589a3b490aSIlya Dryomov int rep;
6599a3b490aSIlya Dryomov unsigned int ftotal;
6609a3b490aSIlya Dryomov int r;
6619a3b490aSIlya Dryomov int i;
6629a3b490aSIlya Dryomov int item = 0;
6639a3b490aSIlya Dryomov int itemtype;
6649a3b490aSIlya Dryomov int collide;
6659a3b490aSIlya Dryomov
6669a3b490aSIlya Dryomov dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
6679a3b490aSIlya Dryomov bucket->id, x, outpos, numrep);
6689a3b490aSIlya Dryomov
6699a3b490aSIlya Dryomov /* initially my result is undefined */
670ab4ce2b5SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
6719a3b490aSIlya Dryomov out[rep] = CRUSH_ITEM_UNDEF;
6729a3b490aSIlya Dryomov if (out2)
6739a3b490aSIlya Dryomov out2[rep] = CRUSH_ITEM_UNDEF;
6749a3b490aSIlya Dryomov }
6759a3b490aSIlya Dryomov
6762d8be0bcSIlya Dryomov for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
677b459be73SIlya Dryomov #ifdef DEBUG_INDEP
678b459be73SIlya Dryomov if (out2 && ftotal) {
679b459be73SIlya Dryomov dprintk("%u %d a: ", ftotal, left);
680b459be73SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
681b459be73SIlya Dryomov dprintk(" %d", out[rep]);
682b459be73SIlya Dryomov }
683b459be73SIlya Dryomov dprintk("\n");
684b459be73SIlya Dryomov dprintk("%u %d b: ", ftotal, left);
685b459be73SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
686b459be73SIlya Dryomov dprintk(" %d", out2[rep]);
687b459be73SIlya Dryomov }
688b459be73SIlya Dryomov dprintk("\n");
689b459be73SIlya Dryomov }
690b459be73SIlya Dryomov #endif
691ab4ce2b5SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
6929a3b490aSIlya Dryomov if (out[rep] != CRUSH_ITEM_UNDEF)
6939a3b490aSIlya Dryomov continue;
6949a3b490aSIlya Dryomov
6959a3b490aSIlya Dryomov in = bucket; /* initial bucket */
6969a3b490aSIlya Dryomov
6979a3b490aSIlya Dryomov /* choose through intervening buckets */
6989a3b490aSIlya Dryomov for (;;) {
6993102b0a5SIlya Dryomov /* note: we base the choice on the position
7003102b0a5SIlya Dryomov * even in the nested call. that means that
7013102b0a5SIlya Dryomov * if the first layer chooses the same bucket
7023102b0a5SIlya Dryomov * in a different position, we will tend to
7033102b0a5SIlya Dryomov * choose a different item in that bucket.
7043102b0a5SIlya Dryomov * this will involve more devices in data
7053102b0a5SIlya Dryomov * movement and tend to distribute the load.
7063102b0a5SIlya Dryomov */
70741586081SIlya Dryomov r = rep + parent_r;
7089a3b490aSIlya Dryomov
7099a3b490aSIlya Dryomov /* be careful */
7109a3b490aSIlya Dryomov if (in->alg == CRUSH_BUCKET_UNIFORM &&
7119a3b490aSIlya Dryomov in->size % numrep == 0)
7129a3b490aSIlya Dryomov /* r'=r+(n+1)*f_total */
7139a3b490aSIlya Dryomov r += (numrep+1) * ftotal;
7149a3b490aSIlya Dryomov else
7159a3b490aSIlya Dryomov /* r' = r + n*f_total */
7169a3b490aSIlya Dryomov r += numrep * ftotal;
7179a3b490aSIlya Dryomov
7189a3b490aSIlya Dryomov /* bucket choose */
7199a3b490aSIlya Dryomov if (in->size == 0) {
7209a3b490aSIlya Dryomov dprintk(" empty bucket\n");
7219a3b490aSIlya Dryomov break;
7229a3b490aSIlya Dryomov }
7239a3b490aSIlya Dryomov
72466a0e2d5SIlya Dryomov item = crush_bucket_choose(
72566a0e2d5SIlya Dryomov in, work->work[-1-in->id],
726069f3222SIlya Dryomov x, r,
727069f3222SIlya Dryomov (choose_args ?
7284de17aeaSYueHaibing &choose_args[-1-in->id] : NULL),
729069f3222SIlya Dryomov outpos);
7309a3b490aSIlya Dryomov if (item >= map->max_devices) {
7319a3b490aSIlya Dryomov dprintk(" bad item %d\n", item);
7329a3b490aSIlya Dryomov out[rep] = CRUSH_ITEM_NONE;
7339a3b490aSIlya Dryomov if (out2)
7349a3b490aSIlya Dryomov out2[rep] = CRUSH_ITEM_NONE;
7359a3b490aSIlya Dryomov left--;
7369a3b490aSIlya Dryomov break;
7379a3b490aSIlya Dryomov }
7389a3b490aSIlya Dryomov
7399a3b490aSIlya Dryomov /* desired type? */
7409a3b490aSIlya Dryomov if (item < 0)
7419a3b490aSIlya Dryomov itemtype = map->buckets[-1-item]->type;
7429a3b490aSIlya Dryomov else
7439a3b490aSIlya Dryomov itemtype = 0;
7449a3b490aSIlya Dryomov dprintk(" item %d type %d\n", item, itemtype);
7459a3b490aSIlya Dryomov
7469a3b490aSIlya Dryomov /* keep going? */
7479a3b490aSIlya Dryomov if (itemtype != type) {
7489a3b490aSIlya Dryomov if (item >= 0 ||
7499a3b490aSIlya Dryomov (-1-item) >= map->max_buckets) {
7509a3b490aSIlya Dryomov dprintk(" bad item type %d\n", type);
7519a3b490aSIlya Dryomov out[rep] = CRUSH_ITEM_NONE;
7529a3b490aSIlya Dryomov if (out2)
7539a3b490aSIlya Dryomov out2[rep] =
7549a3b490aSIlya Dryomov CRUSH_ITEM_NONE;
7559a3b490aSIlya Dryomov left--;
7569a3b490aSIlya Dryomov break;
7579a3b490aSIlya Dryomov }
7589a3b490aSIlya Dryomov in = map->buckets[-1-item];
7599a3b490aSIlya Dryomov continue;
7609a3b490aSIlya Dryomov }
7619a3b490aSIlya Dryomov
7629a3b490aSIlya Dryomov /* collision? */
7639a3b490aSIlya Dryomov collide = 0;
764ab4ce2b5SIlya Dryomov for (i = outpos; i < endpos; i++) {
7659a3b490aSIlya Dryomov if (out[i] == item) {
7669a3b490aSIlya Dryomov collide = 1;
7679a3b490aSIlya Dryomov break;
7689a3b490aSIlya Dryomov }
7699a3b490aSIlya Dryomov }
7709a3b490aSIlya Dryomov if (collide)
7719a3b490aSIlya Dryomov break;
7729a3b490aSIlya Dryomov
7739a3b490aSIlya Dryomov if (recurse_to_leaf) {
7749a3b490aSIlya Dryomov if (item < 0) {
77566a0e2d5SIlya Dryomov crush_choose_indep(
77666a0e2d5SIlya Dryomov map,
77766a0e2d5SIlya Dryomov work,
7789a3b490aSIlya Dryomov map->buckets[-1-item],
7799a3b490aSIlya Dryomov weight, weight_max,
780ab4ce2b5SIlya Dryomov x, 1, numrep, 0,
7819a3b490aSIlya Dryomov out2, rep,
7822d8be0bcSIlya Dryomov recurse_tries, 0,
783069f3222SIlya Dryomov 0, NULL, r,
784069f3222SIlya Dryomov choose_args);
7859a3b490aSIlya Dryomov if (out2[rep] == CRUSH_ITEM_NONE) {
7869a3b490aSIlya Dryomov /* placed nothing; no leaf */
7879a3b490aSIlya Dryomov break;
7889a3b490aSIlya Dryomov }
7899a3b490aSIlya Dryomov } else {
7909a3b490aSIlya Dryomov /* we already have a leaf! */
7919a3b490aSIlya Dryomov out2[rep] = item;
7929a3b490aSIlya Dryomov }
7939a3b490aSIlya Dryomov }
7949a3b490aSIlya Dryomov
7959a3b490aSIlya Dryomov /* out? */
7969a3b490aSIlya Dryomov if (itemtype == 0 &&
7979a3b490aSIlya Dryomov is_out(map, weight, weight_max, item, x))
7989a3b490aSIlya Dryomov break;
7999a3b490aSIlya Dryomov
8009a3b490aSIlya Dryomov /* yay! */
8019a3b490aSIlya Dryomov out[rep] = item;
8029a3b490aSIlya Dryomov left--;
8039a3b490aSIlya Dryomov break;
8049a3b490aSIlya Dryomov }
8059a3b490aSIlya Dryomov }
8069a3b490aSIlya Dryomov }
807ab4ce2b5SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
8089a3b490aSIlya Dryomov if (out[rep] == CRUSH_ITEM_UNDEF) {
8099a3b490aSIlya Dryomov out[rep] = CRUSH_ITEM_NONE;
8109a3b490aSIlya Dryomov }
8119a3b490aSIlya Dryomov if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
8129a3b490aSIlya Dryomov out2[rep] = CRUSH_ITEM_NONE;
8139a3b490aSIlya Dryomov }
8149a3b490aSIlya Dryomov }
815b459be73SIlya Dryomov #ifndef __KERNEL__
816b459be73SIlya Dryomov if (map->choose_tries && ftotal <= map->choose_total_tries)
817b459be73SIlya Dryomov map->choose_tries[ftotal]++;
818b459be73SIlya Dryomov #endif
819b459be73SIlya Dryomov #ifdef DEBUG_INDEP
820b459be73SIlya Dryomov if (out2) {
821b459be73SIlya Dryomov dprintk("%u %d a: ", ftotal, left);
822b459be73SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
823b459be73SIlya Dryomov dprintk(" %d", out[rep]);
824b459be73SIlya Dryomov }
825b459be73SIlya Dryomov dprintk("\n");
826b459be73SIlya Dryomov dprintk("%u %d b: ", ftotal, left);
827b459be73SIlya Dryomov for (rep = outpos; rep < endpos; rep++) {
828b459be73SIlya Dryomov dprintk(" %d", out2[rep]);
829b459be73SIlya Dryomov }
830b459be73SIlya Dryomov dprintk("\n");
831b459be73SIlya Dryomov }
832b459be73SIlya Dryomov #endif
8339a3b490aSIlya Dryomov }
8349a3b490aSIlya Dryomov
83566a0e2d5SIlya Dryomov
83666a0e2d5SIlya Dryomov /*
83766a0e2d5SIlya Dryomov * This takes a chunk of memory and sets it up to be a shiny new
83866a0e2d5SIlya Dryomov * working area for a CRUSH placement computation. It must be called
83966a0e2d5SIlya Dryomov * on any newly allocated memory before passing it in to
84066a0e2d5SIlya Dryomov * crush_do_rule. It may be used repeatedly after that, so long as the
84166a0e2d5SIlya Dryomov * map has not changed. If the map /has/ changed, you must make sure
84266a0e2d5SIlya Dryomov * the working size is no smaller than what was allocated and re-run
84366a0e2d5SIlya Dryomov * crush_init_workspace.
84466a0e2d5SIlya Dryomov *
84566a0e2d5SIlya Dryomov * If you do retain the working space between calls to crush, make it
84666a0e2d5SIlya Dryomov * thread-local.
84766a0e2d5SIlya Dryomov */
crush_init_workspace(const struct crush_map * map,void * v)84866a0e2d5SIlya Dryomov void crush_init_workspace(const struct crush_map *map, void *v)
84966a0e2d5SIlya Dryomov {
85066a0e2d5SIlya Dryomov struct crush_work *w = v;
85166a0e2d5SIlya Dryomov __s32 b;
85266a0e2d5SIlya Dryomov
85366a0e2d5SIlya Dryomov /*
85466a0e2d5SIlya Dryomov * We work by moving through the available space and setting
85566a0e2d5SIlya Dryomov * values and pointers as we go.
85666a0e2d5SIlya Dryomov *
85766a0e2d5SIlya Dryomov * It's a bit like Forth's use of the 'allot' word since we
85866a0e2d5SIlya Dryomov * set the pointer first and then reserve the space for it to
85966a0e2d5SIlya Dryomov * point to by incrementing the point.
86066a0e2d5SIlya Dryomov */
861b88ed8d8SIlya Dryomov v += sizeof(struct crush_work);
86266a0e2d5SIlya Dryomov w->work = v;
86366a0e2d5SIlya Dryomov v += map->max_buckets * sizeof(struct crush_work_bucket *);
86466a0e2d5SIlya Dryomov for (b = 0; b < map->max_buckets; ++b) {
86566a0e2d5SIlya Dryomov if (!map->buckets[b])
86666a0e2d5SIlya Dryomov continue;
86766a0e2d5SIlya Dryomov
86866a0e2d5SIlya Dryomov w->work[b] = v;
86966a0e2d5SIlya Dryomov switch (map->buckets[b]->alg) {
87066a0e2d5SIlya Dryomov default:
87166a0e2d5SIlya Dryomov v += sizeof(struct crush_work_bucket);
87266a0e2d5SIlya Dryomov break;
87366a0e2d5SIlya Dryomov }
87466a0e2d5SIlya Dryomov w->work[b]->perm_x = 0;
87566a0e2d5SIlya Dryomov w->work[b]->perm_n = 0;
87666a0e2d5SIlya Dryomov w->work[b]->perm = v;
87766a0e2d5SIlya Dryomov v += map->buckets[b]->size * sizeof(__u32);
87866a0e2d5SIlya Dryomov }
87966a0e2d5SIlya Dryomov BUG_ON(v - (void *)w != map->working_size);
88066a0e2d5SIlya Dryomov }
88166a0e2d5SIlya Dryomov
8829a3b490aSIlya Dryomov /**
8833d14c5d2SYehuda Sadeh * crush_do_rule - calculate a mapping with the given input and rule
8843d14c5d2SYehuda Sadeh * @map: the crush_map
8853d14c5d2SYehuda Sadeh * @ruleno: the rule id
8863d14c5d2SYehuda Sadeh * @x: hash input
8873d14c5d2SYehuda Sadeh * @result: pointer to result vector
8883d14c5d2SYehuda Sadeh * @result_max: maximum result size
889b3b33b0eSIlya Dryomov * @weight: weight vector (for map leaves)
890b3b33b0eSIlya Dryomov * @weight_max: size of weight vector
891743efcffSIlya Dryomov * @cwin: pointer to at least crush_work_size() bytes of memory
892069f3222SIlya Dryomov * @choose_args: weights and ids for each known bucket
8933d14c5d2SYehuda Sadeh */
crush_do_rule(const struct crush_map * map,int ruleno,int x,int * result,int result_max,const __u32 * weight,int weight_max,void * cwin,const struct crush_choose_arg * choose_args)8948b12d47bSSage Weil int crush_do_rule(const struct crush_map *map,
8953d14c5d2SYehuda Sadeh int ruleno, int x, int *result, int result_max,
896e8ef19c4SIlya Dryomov const __u32 *weight, int weight_max,
897069f3222SIlya Dryomov void *cwin, const struct crush_choose_arg *choose_args)
8983d14c5d2SYehuda Sadeh {
8993d14c5d2SYehuda Sadeh int result_len;
90066a0e2d5SIlya Dryomov struct crush_work *cw = cwin;
901743efcffSIlya Dryomov int *a = cwin + map->working_size;
902743efcffSIlya Dryomov int *b = a + result_max;
903743efcffSIlya Dryomov int *c = b + result_max;
904743efcffSIlya Dryomov int *w = a;
905743efcffSIlya Dryomov int *o = b;
9063d14c5d2SYehuda Sadeh int recurse_to_leaf;
9073d14c5d2SYehuda Sadeh int wsize = 0;
9083d14c5d2SYehuda Sadeh int osize;
90966a0e2d5SIlya Dryomov const struct crush_rule *rule;
9108b12d47bSSage Weil __u32 step;
9113d14c5d2SYehuda Sadeh int i, j;
9123d14c5d2SYehuda Sadeh int numrep;
91345002267SIlya Dryomov int out_size;
91448a163dbSIlya Dryomov /*
91548a163dbSIlya Dryomov * the original choose_total_tries value was off by one (it
91648a163dbSIlya Dryomov * counted "retries" and not "tries"). add one.
91748a163dbSIlya Dryomov */
91848a163dbSIlya Dryomov int choose_tries = map->choose_total_tries + 1;
919f18650acSIlya Dryomov int choose_leaf_tries = 0;
92048a163dbSIlya Dryomov /*
92148a163dbSIlya Dryomov * the local tries values were counted as "retries", though,
92248a163dbSIlya Dryomov * and need no adjustment
92348a163dbSIlya Dryomov */
92448a163dbSIlya Dryomov int choose_local_retries = map->choose_local_tries;
92548a163dbSIlya Dryomov int choose_local_fallback_retries = map->choose_local_fallback_tries;
9263d14c5d2SYehuda Sadeh
927e2b149ccSIlya Dryomov int vary_r = map->chooseleaf_vary_r;
928dc6ae6d8SIlya Dryomov int stable = map->chooseleaf_stable;
929e2b149ccSIlya Dryomov
930a1f4895bSSage Weil if ((__u32)ruleno >= map->max_rules) {
931a1f4895bSSage Weil dprintk(" bad ruleno %d\n", ruleno);
932a1f4895bSSage Weil return 0;
933a1f4895bSSage Weil }
9343d14c5d2SYehuda Sadeh
9353d14c5d2SYehuda Sadeh rule = map->rules[ruleno];
9363d14c5d2SYehuda Sadeh result_len = 0;
9373d14c5d2SYehuda Sadeh
9383d14c5d2SYehuda Sadeh for (step = 0; step < rule->len; step++) {
9398f99c85bSIlya Dryomov int firstn = 0;
94066a0e2d5SIlya Dryomov const struct crush_rule_step *curstep = &rule->steps[step];
9410668216eSSage Weil
9420668216eSSage Weil switch (curstep->op) {
9433d14c5d2SYehuda Sadeh case CRUSH_RULE_TAKE:
9448f529795SIlya Dryomov if ((curstep->arg1 >= 0 &&
9458f529795SIlya Dryomov curstep->arg1 < map->max_devices) ||
94656a4f309SIlya Dryomov (-1-curstep->arg1 >= 0 &&
94756a4f309SIlya Dryomov -1-curstep->arg1 < map->max_buckets &&
9488f529795SIlya Dryomov map->buckets[-1-curstep->arg1])) {
9490668216eSSage Weil w[0] = curstep->arg1;
9503d14c5d2SYehuda Sadeh wsize = 1;
9518f529795SIlya Dryomov } else {
9528f529795SIlya Dryomov dprintk(" bad take value %d\n", curstep->arg1);
9538f529795SIlya Dryomov }
9543d14c5d2SYehuda Sadeh break;
9553d14c5d2SYehuda Sadeh
956cc10df4aSIlya Dryomov case CRUSH_RULE_SET_CHOOSE_TRIES:
957cc10df4aSIlya Dryomov if (curstep->arg1 > 0)
958cc10df4aSIlya Dryomov choose_tries = curstep->arg1;
959cc10df4aSIlya Dryomov break;
960cc10df4aSIlya Dryomov
961917edad5SIlya Dryomov case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
962be3226acSIlya Dryomov if (curstep->arg1 > 0)
963be3226acSIlya Dryomov choose_leaf_tries = curstep->arg1;
964be3226acSIlya Dryomov break;
965be3226acSIlya Dryomov
966f046bf92SIlya Dryomov case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
9676ed1002fSIlya Dryomov if (curstep->arg1 >= 0)
96848a163dbSIlya Dryomov choose_local_retries = curstep->arg1;
969f046bf92SIlya Dryomov break;
970f046bf92SIlya Dryomov
971f046bf92SIlya Dryomov case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
9726ed1002fSIlya Dryomov if (curstep->arg1 >= 0)
97348a163dbSIlya Dryomov choose_local_fallback_retries = curstep->arg1;
974f046bf92SIlya Dryomov break;
975f046bf92SIlya Dryomov
976d83ed858SIlya Dryomov case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
977d83ed858SIlya Dryomov if (curstep->arg1 >= 0)
978d83ed858SIlya Dryomov vary_r = curstep->arg1;
979d83ed858SIlya Dryomov break;
980d83ed858SIlya Dryomov
981dc6ae6d8SIlya Dryomov case CRUSH_RULE_SET_CHOOSELEAF_STABLE:
982dc6ae6d8SIlya Dryomov if (curstep->arg1 >= 0)
983dc6ae6d8SIlya Dryomov stable = curstep->arg1;
984dc6ae6d8SIlya Dryomov break;
985dc6ae6d8SIlya Dryomov
986917edad5SIlya Dryomov case CRUSH_RULE_CHOOSELEAF_FIRSTN:
9873d14c5d2SYehuda Sadeh case CRUSH_RULE_CHOOSE_FIRSTN:
9883d14c5d2SYehuda Sadeh firstn = 1;
989df561f66SGustavo A. R. Silva fallthrough;
990917edad5SIlya Dryomov case CRUSH_RULE_CHOOSELEAF_INDEP:
9913d14c5d2SYehuda Sadeh case CRUSH_RULE_CHOOSE_INDEP:
992a1f4895bSSage Weil if (wsize == 0)
993a1f4895bSSage Weil break;
9943d14c5d2SYehuda Sadeh
9953d14c5d2SYehuda Sadeh recurse_to_leaf =
9960668216eSSage Weil curstep->op ==
997917edad5SIlya Dryomov CRUSH_RULE_CHOOSELEAF_FIRSTN ||
9980668216eSSage Weil curstep->op ==
999917edad5SIlya Dryomov CRUSH_RULE_CHOOSELEAF_INDEP;
10003d14c5d2SYehuda Sadeh
10013d14c5d2SYehuda Sadeh /* reset output */
10023d14c5d2SYehuda Sadeh osize = 0;
10033d14c5d2SYehuda Sadeh
10043d14c5d2SYehuda Sadeh for (i = 0; i < wsize; i++) {
1005f224a691SIlya Dryomov int bno;
10060668216eSSage Weil numrep = curstep->arg1;
10073d14c5d2SYehuda Sadeh if (numrep <= 0) {
10083d14c5d2SYehuda Sadeh numrep += result_max;
10093d14c5d2SYehuda Sadeh if (numrep <= 0)
10103d14c5d2SYehuda Sadeh continue;
10113d14c5d2SYehuda Sadeh }
10123d14c5d2SYehuda Sadeh j = 0;
1013f224a691SIlya Dryomov /* make sure bucket id is valid */
1014f224a691SIlya Dryomov bno = -1 - w[i];
1015f224a691SIlya Dryomov if (bno < 0 || bno >= map->max_buckets) {
1016f224a691SIlya Dryomov /* w[i] is probably CRUSH_ITEM_NONE */
1017f224a691SIlya Dryomov dprintk(" bad w[i] %d\n", w[i]);
1018f224a691SIlya Dryomov continue;
1019f224a691SIlya Dryomov }
10209a3b490aSIlya Dryomov if (firstn) {
1021d390bb2aSIlya Dryomov int recurse_tries;
1022d390bb2aSIlya Dryomov if (choose_leaf_tries)
1023d390bb2aSIlya Dryomov recurse_tries =
1024d390bb2aSIlya Dryomov choose_leaf_tries;
1025d390bb2aSIlya Dryomov else if (map->chooseleaf_descend_once)
1026d390bb2aSIlya Dryomov recurse_tries = 1;
1027d390bb2aSIlya Dryomov else
1028d390bb2aSIlya Dryomov recurse_tries = choose_tries;
10299fe07182SIlya Dryomov osize += crush_choose_firstn(
10309fe07182SIlya Dryomov map,
103166a0e2d5SIlya Dryomov cw,
1032f224a691SIlya Dryomov map->buckets[bno],
1033b3b33b0eSIlya Dryomov weight, weight_max,
10343d14c5d2SYehuda Sadeh x, numrep,
10350668216eSSage Weil curstep->arg2,
10363d14c5d2SYehuda Sadeh o+osize, j,
103745002267SIlya Dryomov result_max-osize,
1038f18650acSIlya Dryomov choose_tries,
1039d390bb2aSIlya Dryomov recurse_tries,
104048a163dbSIlya Dryomov choose_local_retries,
104148a163dbSIlya Dryomov choose_local_fallback_retries,
10421604f488SJim Schutt recurse_to_leaf,
1043e2b149ccSIlya Dryomov vary_r,
1044dc6ae6d8SIlya Dryomov stable,
1045e2b149ccSIlya Dryomov c+osize,
1046069f3222SIlya Dryomov 0,
1047069f3222SIlya Dryomov choose_args);
10489a3b490aSIlya Dryomov } else {
104945002267SIlya Dryomov out_size = ((numrep < (result_max-osize)) ?
105045002267SIlya Dryomov numrep : (result_max-osize));
10519fe07182SIlya Dryomov crush_choose_indep(
10529fe07182SIlya Dryomov map,
105366a0e2d5SIlya Dryomov cw,
1054f224a691SIlya Dryomov map->buckets[bno],
10559a3b490aSIlya Dryomov weight, weight_max,
105645002267SIlya Dryomov x, out_size, numrep,
10579a3b490aSIlya Dryomov curstep->arg2,
10589a3b490aSIlya Dryomov o+osize, j,
1059f18650acSIlya Dryomov choose_tries,
1060d390bb2aSIlya Dryomov choose_leaf_tries ?
1061d390bb2aSIlya Dryomov choose_leaf_tries : 1,
10629a3b490aSIlya Dryomov recurse_to_leaf,
106341586081SIlya Dryomov c+osize,
1064069f3222SIlya Dryomov 0,
1065069f3222SIlya Dryomov choose_args);
106645002267SIlya Dryomov osize += out_size;
10679a3b490aSIlya Dryomov }
10683d14c5d2SYehuda Sadeh }
10693d14c5d2SYehuda Sadeh
10703d14c5d2SYehuda Sadeh if (recurse_to_leaf)
10713d14c5d2SYehuda Sadeh /* copy final _leaf_ values to output set */
10723d14c5d2SYehuda Sadeh memcpy(o, c, osize*sizeof(*o));
10733d14c5d2SYehuda Sadeh
10742a4ba74eSIlya Dryomov /* swap o and w arrays */
1075*d9d58f04SGuo Zhengkui swap(o, w);
10763d14c5d2SYehuda Sadeh wsize = osize;
10773d14c5d2SYehuda Sadeh break;
10783d14c5d2SYehuda Sadeh
10793d14c5d2SYehuda Sadeh
10803d14c5d2SYehuda Sadeh case CRUSH_RULE_EMIT:
10813d14c5d2SYehuda Sadeh for (i = 0; i < wsize && result_len < result_max; i++) {
10823d14c5d2SYehuda Sadeh result[result_len] = w[i];
10833d14c5d2SYehuda Sadeh result_len++;
10843d14c5d2SYehuda Sadeh }
10853d14c5d2SYehuda Sadeh wsize = 0;
10863d14c5d2SYehuda Sadeh break;
10873d14c5d2SYehuda Sadeh
10883d14c5d2SYehuda Sadeh default:
1089a1f4895bSSage Weil dprintk(" unknown op %d at step %d\n",
1090a1f4895bSSage Weil curstep->op, step);
1091a1f4895bSSage Weil break;
10923d14c5d2SYehuda Sadeh }
10933d14c5d2SYehuda Sadeh }
109466a0e2d5SIlya Dryomov
1095f1932fc1SSage Weil return result_len;
10963d14c5d2SYehuda Sadeh }
1097