1 #ifndef CEPH_CRUSH_CRUSH_H
2 #define CEPH_CRUSH_CRUSH_H
5 # include <linux/rbtree.h>
6 # include <linux/types.h>
8 # include "crush_compat.h"
12 * CRUSH is a pseudo-random data distribution algorithm that
13 * efficiently distributes input values (typically, data objects)
14 * across a heterogeneous, structured storage cluster.
16 * The algorithm was originally described in detail in this paper
17 * (although the algorithm has evolved somewhat since then):
19 * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
25 #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
27 #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
28 #define CRUSH_MAX_RULESET (1<<8) /* max crush ruleset number */
29 #define CRUSH_MAX_RULES CRUSH_MAX_RULESET /* should be the same as max rulesets */
31 #define CRUSH_MAX_DEVICE_WEIGHT (100u * 0x10000u)
32 #define CRUSH_MAX_BUCKET_WEIGHT (65535u * 0x10000u)
34 #define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */
35 #define CRUSH_ITEM_NONE 0x7fffffff /* no result */
38 * CRUSH uses user-defined "rules" to describe how inputs should be
39 * mapped to devices. A rule consists of sequence of steps to perform
40 * to generate the set of output devices.
42 struct crush_rule_step {
51 CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
52 CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
54 CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
55 CRUSH_RULE_EMIT = 4, /* no args */
56 CRUSH_RULE_CHOOSELEAF_FIRSTN = 6,
57 CRUSH_RULE_CHOOSELEAF_INDEP = 7,
59 CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */
60 CRUSH_RULE_SET_CHOOSELEAF_TRIES = 9, /* override chooseleaf_descend_once */
61 CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES = 10,
62 CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES = 11,
63 CRUSH_RULE_SET_CHOOSELEAF_VARY_R = 12,
64 CRUSH_RULE_SET_CHOOSELEAF_STABLE = 13
68 * for specifying choose num (arg1) relative to the max parameter
71 #define CRUSH_CHOOSE_N 0
72 #define CRUSH_CHOOSE_N_MINUS(x) (-(x))
75 * The rule mask is used to describe what the rule is intended for.
76 * Given a ruleset and size of output set, we search through the
77 * rule list for a matching rule_mask.
79 struct crush_rule_mask {
88 struct crush_rule_mask mask;
89 struct crush_rule_step steps[0];
92 #define crush_rule_size(len) (sizeof(struct crush_rule) + \
93 (len)*sizeof(struct crush_rule_step))
98 * A bucket is a named container of other items (either devices or
99 * other buckets). Items within a bucket are chosen using one of a
100 * few different algorithms. The table summarizes how the speed of
101 * each option measures up against mapping stability when items are
104 * Bucket Alg Speed Additions Removals
105 * ------------------------------------------------
106 * uniform O(1) poor poor
107 * list O(n) optimal poor
108 * tree O(log n) good good
109 * straw O(n) better better
110 * straw2 O(n) optimal optimal
113 CRUSH_BUCKET_UNIFORM = 1,
114 CRUSH_BUCKET_LIST = 2,
115 CRUSH_BUCKET_TREE = 3,
116 CRUSH_BUCKET_STRAW = 4,
117 CRUSH_BUCKET_STRAW2 = 5,
119 extern const char *crush_bucket_alg_name(int alg);
122 * although tree was a legacy algorithm, it has been buggy, so
125 #define CRUSH_LEGACY_ALLOWED_BUCKET_ALGS ( \
126 (1 << CRUSH_BUCKET_UNIFORM) | \
127 (1 << CRUSH_BUCKET_LIST) | \
128 (1 << CRUSH_BUCKET_STRAW))
130 struct crush_bucket {
131 __s32 id; /* this'll be negative */
132 __u16 type; /* non-zero; type=0 is reserved for devices */
133 __u8 alg; /* one of CRUSH_BUCKET_* */
134 __u8 hash; /* which hash function to use, CRUSH_HASH_* */
135 __u32 weight; /* 16-bit fixed point */
136 __u32 size; /* num items */
143 * Replacement weights for each item in a bucket. The size of the
144 * array must be exactly the size of the straw2 bucket, just as the
145 * item_weights array.
148 struct crush_weight_set {
149 __u32 *weights; /*!< 16.16 fixed point weights
150 in the same order as items */
151 __u32 size; /*!< size of the __weights__ array */
156 * Replacement weights and ids for a given straw2 bucket, for
157 * placement purposes.
159 * When crush_do_rule() chooses the Nth item from a straw2 bucket, the
160 * replacement weights found at __weight_set[N]__ are used instead of
161 * the weights from __item_weights__. If __N__ is greater than
162 * __weight_set_size__, the weights found at __weight_set_size-1__ are
163 * used instead. For instance if __weight_set__ is:
165 * [ [ 0x10000, 0x20000 ], // position 0
166 * [ 0x20000, 0x40000 ] ] // position 1
168 * choosing the 0th item will use position 0 weights [ 0x10000, 0x20000 ]
169 * choosing the 1th item will use position 1 weights [ 0x20000, 0x40000 ]
170 * choosing the 2th item will use position 1 weights [ 0x20000, 0x40000 ]
174 struct crush_choose_arg {
175 __s32 *ids; /*!< values to use instead of items */
176 __u32 ids_size; /*!< size of the __ids__ array */
177 struct crush_weight_set *weight_set; /*!< weight replacements for
179 __u32 weight_set_size; /*!< size of the __weight_set__ array */
184 * Replacement weights and ids for each bucket in the crushmap. The
185 * __size__ of the __args__ array must be exactly the same as the
186 * __map->max_buckets__.
188 * The __crush_choose_arg__ at index N will be used when choosing
189 * an item from the bucket __map->buckets[N]__ bucket, provided it
190 * is a straw2 bucket.
193 struct crush_choose_arg_map {
196 u64 choose_args_index;
198 struct crush_choose_arg *args; /*!< replacement for each bucket
200 __u32 size; /*!< size of the __args__ array */
203 struct crush_bucket_uniform {
204 struct crush_bucket h;
205 __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
208 struct crush_bucket_list {
209 struct crush_bucket h;
210 __u32 *item_weights; /* 16-bit fixed point */
211 __u32 *sum_weights; /* 16-bit fixed point. element i is sum
212 of weights 0..i, inclusive */
215 struct crush_bucket_tree {
216 struct crush_bucket h; /* note: h.size is _tree_ size, not number of
222 struct crush_bucket_straw {
223 struct crush_bucket h;
224 __u32 *item_weights; /* 16-bit fixed point */
225 __u32 *straws; /* 16-bit fixed point */
228 struct crush_bucket_straw2 {
229 struct crush_bucket h;
230 __u32 *item_weights; /* 16-bit fixed point */
236 * CRUSH map includes all buckets, rules, etc.
239 struct crush_bucket **buckets;
240 struct crush_rule **rules;
246 /* choose local retries before re-descent */
247 __u32 choose_local_tries;
248 /* choose local attempts using a fallback permutation before
250 __u32 choose_local_fallback_tries;
251 /* choose attempts before giving up */
252 __u32 choose_total_tries;
253 /* attempt chooseleaf inner descent once for firstn mode; on
254 * reject retry outer descent. Note that this does *not*
255 * apply to a collision: in that case we will retry as we used
257 __u32 chooseleaf_descend_once;
259 /* if non-zero, feed r into chooseleaf, bit-shifted right by (r-1)
260 * bits. a value of 1 is best for new clusters. for legacy clusters
261 * that want to limit reshuffling, a value of 3 or 4 will make the
262 * mappings line up a bit better with previous mappings. */
263 __u8 chooseleaf_vary_r;
265 /* if true, it makes chooseleaf firstn to return stable results (if
266 * no local retry) so that data migrations would be optimal when some
268 __u8 chooseleaf_stable;
271 * This value is calculated after decode or construction by
272 * the builder. It is exposed here (rather than having a
273 * 'build CRUSH working space' function) so that callers can
274 * reserve a static buffer, allocate space on the stack, or
275 * otherwise avoid calling into the heap allocator if they
276 * want to. The size of the working space depends on the map,
277 * while the size of the scratch vector passed to the mapper
278 * depends on the size of the desired result set.
280 * Nothing stops the caller from allocating both in one swell
281 * foop and passing in two points, though.
287 * version 0 (original) of straw_calc has various flaws. version 1
288 * fixes a few of them.
290 __u8 straw_calc_version;
293 * allowed bucket algs is a bitmask, here the bit positions
294 * are CRUSH_BUCKET_*. note that these are *bits* and
295 * CRUSH_BUCKET_* values are not, so we need to or together (1
296 * << CRUSH_BUCKET_WHATEVER). The 0th bit is not used to
297 * minimize confusion (bucket type values start at 1).
299 __u32 allowed_bucket_algs;
303 /* CrushWrapper::choose_args */
304 struct rb_root choose_args;
310 extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
311 extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
312 extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
313 extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
314 extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
315 extern void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b);
316 extern void crush_destroy_bucket(struct crush_bucket *b);
317 extern void crush_destroy_rule(struct crush_rule *r);
318 extern void crush_destroy(struct crush_map *map);
320 static inline int crush_calc_tree_node(int i)
322 return ((i+1) << 1)-1;
326 * These data structures are private to the CRUSH implementation. They
327 * are exposed in this header file because builder needs their
328 * definitions to calculate the total working size.
330 * Moving this out of the crush map allow us to treat the CRUSH map as
331 * immutable within the mapper and removes the requirement for a CRUSH
334 struct crush_work_bucket {
335 __u32 perm_x; /* @x for which *perm is defined */
336 __u32 perm_n; /* num elements of *perm that are permuted/defined */
337 __u32 *perm; /* Permutation of the bucket's items */
341 struct crush_work_bucket **work; /* Per-bucket working store */