1 #ifndef CEPH_CRUSH_CRUSH_H
2 #define CEPH_CRUSH_CRUSH_H
4 #include <linux/types.h>
7 * CRUSH is a pseudo-random data distribution algorithm that
8 * efficiently distributes input values (typically, data objects)
9 * across a heterogeneous, structured storage cluster.
11 * The algorithm was originally described in detail in this paper
12 * (although the algorithm has evolved somewhat since then):
14 * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
20 #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
22 #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
25 #define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */
26 #define CRUSH_ITEM_NONE 0x7fffffff /* no result */
29 * CRUSH uses user-defined "rules" to describe how inputs should be
30 * mapped to devices. A rule consists of sequence of steps to perform
31 * to generate the set of output devices.
33 struct crush_rule_step {
42 CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
43 CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
45 CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
46 CRUSH_RULE_EMIT = 4, /* no args */
47 CRUSH_RULE_CHOOSELEAF_FIRSTN = 6,
48 CRUSH_RULE_CHOOSELEAF_INDEP = 7,
50 CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */
51 CRUSH_RULE_SET_CHOOSELEAF_TRIES = 9, /* override chooseleaf_descend_once */
52 CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES = 10,
53 CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES = 11,
54 CRUSH_RULE_SET_CHOOSELEAF_VARY_R = 12
58 * for specifying choose num (arg1) relative to the max parameter
61 #define CRUSH_CHOOSE_N 0
62 #define CRUSH_CHOOSE_N_MINUS(x) (-(x))
65 * The rule mask is used to describe what the rule is intended for.
66 * Given a ruleset and size of output set, we search through the
67 * rule list for a matching rule_mask.
69 struct crush_rule_mask {
78 struct crush_rule_mask mask;
79 struct crush_rule_step steps[0];
82 #define crush_rule_size(len) (sizeof(struct crush_rule) + \
83 (len)*sizeof(struct crush_rule_step))
88 * A bucket is a named container of other items (either devices or
89 * other buckets). Items within a bucket are chosen using one of a
90 * few different algorithms. The table summarizes how the speed of
91 * each option measures up against mapping stability when items are
94 * Bucket Alg Speed Additions Removals
95 * ------------------------------------------------
96 * uniform O(1) poor poor
97 * list O(n) optimal poor
98 * tree O(log n) good good
99 * straw O(n) better better
100 * straw2 O(n) optimal optimal
103 CRUSH_BUCKET_UNIFORM = 1,
104 CRUSH_BUCKET_LIST = 2,
105 CRUSH_BUCKET_TREE = 3,
106 CRUSH_BUCKET_STRAW = 4,
107 CRUSH_BUCKET_STRAW2 = 5,
109 extern const char *crush_bucket_alg_name(int alg);
111 struct crush_bucket {
112 __s32 id; /* this'll be negative */
113 __u16 type; /* non-zero; type=0 is reserved for devices */
114 __u8 alg; /* one of CRUSH_BUCKET_* */
115 __u8 hash; /* which hash function to use, CRUSH_HASH_* */
116 __u32 weight; /* 16-bit fixed point */
117 __u32 size; /* num items */
121 * cached random permutation: used for uniform bucket and for
122 * the linear search fallback for the other bucket types.
124 __u32 perm_x; /* @x for which *perm is defined */
125 __u32 perm_n; /* num elements of *perm that are permuted/defined */
129 struct crush_bucket_uniform {
130 struct crush_bucket h;
131 __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
134 struct crush_bucket_list {
135 struct crush_bucket h;
136 __u32 *item_weights; /* 16-bit fixed point */
137 __u32 *sum_weights; /* 16-bit fixed point. element i is sum
138 of weights 0..i, inclusive */
141 struct crush_bucket_tree {
142 struct crush_bucket h; /* note: h.size is _tree_ size, not number of
148 struct crush_bucket_straw {
149 struct crush_bucket h;
150 __u32 *item_weights; /* 16-bit fixed point */
151 __u32 *straws; /* 16-bit fixed point */
154 struct crush_bucket_straw2 {
155 struct crush_bucket h;
156 __u32 *item_weights; /* 16-bit fixed point */
162 * CRUSH map includes all buckets, rules, etc.
165 struct crush_bucket **buckets;
166 struct crush_rule **rules;
172 /* choose local retries before re-descent */
173 __u32 choose_local_tries;
174 /* choose local attempts using a fallback permutation before
176 __u32 choose_local_fallback_tries;
177 /* choose attempts before giving up */
178 __u32 choose_total_tries;
179 /* attempt chooseleaf inner descent once for firstn mode; on
180 * reject retry outer descent. Note that this does *not*
181 * apply to a collision: in that case we will retry as we used
183 __u32 chooseleaf_descend_once;
185 /* if non-zero, feed r into chooseleaf, bit-shifted right by (r-1)
186 * bits. a value of 1 is best for new clusters. for legacy clusters
187 * that want to limit reshuffling, a value of 3 or 4 will make the
188 * mappings line up a bit better with previous mappings. */
189 __u8 chooseleaf_vary_r;
194 extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
195 extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
196 extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
197 extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
198 extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
199 extern void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b);
200 extern void crush_destroy_bucket(struct crush_bucket *b);
201 extern void crush_destroy_rule(struct crush_rule *r);
202 extern void crush_destroy(struct crush_map *map);
204 static inline int crush_calc_tree_node(int i)
206 return ((i+1) << 1)-1;