]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/crush/crush.h
crush: add SET_CHOOSE_TRIES rule step
[karo-tx-linux.git] / include / linux / crush / crush.h
1 #ifndef CEPH_CRUSH_CRUSH_H
2 #define CEPH_CRUSH_CRUSH_H
3
4 #include <linux/types.h>
5
6 /*
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.
10  *
11  * The algorithm was originally described in detail in this paper
12  * (although the algorithm has evolved somewhat since then):
13  *
14  *     http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
15  *
16  * LGPL2
17  */
18
19
20 #define CRUSH_MAGIC 0x00010000ul   /* for detecting algorithm revisions */
21
22 #define CRUSH_MAX_DEPTH 10  /* max crush hierarchy depth */
23
24
25 #define CRUSH_ITEM_UNDEF  0x7ffffffe  /* undefined result (internal use only) */
26 #define CRUSH_ITEM_NONE   0x7fffffff  /* no result */
27
28 /*
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.
32  */
33 struct crush_rule_step {
34         __u32 op;
35         __s32 arg1;
36         __s32 arg2;
37 };
38
39 /* step op codes */
40 enum {
41         CRUSH_RULE_NOOP = 0,
42         CRUSH_RULE_TAKE = 1,          /* arg1 = value to start with */
43         CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
44                                       /* arg2 = type */
45         CRUSH_RULE_CHOOSE_INDEP = 3,  /* same */
46         CRUSH_RULE_EMIT = 4,          /* no args */
47         CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6,
48         CRUSH_RULE_CHOOSE_LEAF_INDEP = 7,
49
50         CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */
51         CRUSH_RULE_SET_CHOOSE_LEAF_TRIES = 9, /* override chooseleaf_descend_once */
52 };
53
54 /*
55  * for specifying choose num (arg1) relative to the max parameter
56  * passed to do_rule
57  */
58 #define CRUSH_CHOOSE_N            0
59 #define CRUSH_CHOOSE_N_MINUS(x)   (-(x))
60
61 /*
62  * The rule mask is used to describe what the rule is intended for.
63  * Given a ruleset and size of output set, we search through the
64  * rule list for a matching rule_mask.
65  */
66 struct crush_rule_mask {
67         __u8 ruleset;
68         __u8 type;
69         __u8 min_size;
70         __u8 max_size;
71 };
72
73 struct crush_rule {
74         __u32 len;
75         struct crush_rule_mask mask;
76         struct crush_rule_step steps[0];
77 };
78
79 #define crush_rule_size(len) (sizeof(struct crush_rule) + \
80                               (len)*sizeof(struct crush_rule_step))
81
82
83
84 /*
85  * A bucket is a named container of other items (either devices or
86  * other buckets).  Items within a bucket are chosen using one of a
87  * few different algorithms.  The table summarizes how the speed of
88  * each option measures up against mapping stability when items are
89  * added or removed.
90  *
91  *  Bucket Alg     Speed       Additions    Removals
92  *  ------------------------------------------------
93  *  uniform         O(1)       poor         poor
94  *  list            O(n)       optimal      poor
95  *  tree            O(log n)   good         good
96  *  straw           O(n)       optimal      optimal
97  */
98 enum {
99         CRUSH_BUCKET_UNIFORM = 1,
100         CRUSH_BUCKET_LIST = 2,
101         CRUSH_BUCKET_TREE = 3,
102         CRUSH_BUCKET_STRAW = 4
103 };
104 extern const char *crush_bucket_alg_name(int alg);
105
106 struct crush_bucket {
107         __s32 id;        /* this'll be negative */
108         __u16 type;      /* non-zero; type=0 is reserved for devices */
109         __u8 alg;        /* one of CRUSH_BUCKET_* */
110         __u8 hash;       /* which hash function to use, CRUSH_HASH_* */
111         __u32 weight;    /* 16-bit fixed point */
112         __u32 size;      /* num items */
113         __s32 *items;
114
115         /*
116          * cached random permutation: used for uniform bucket and for
117          * the linear search fallback for the other bucket types.
118          */
119         __u32 perm_x;  /* @x for which *perm is defined */
120         __u32 perm_n;  /* num elements of *perm that are permuted/defined */
121         __u32 *perm;
122 };
123
124 struct crush_bucket_uniform {
125         struct crush_bucket h;
126         __u32 item_weight;  /* 16-bit fixed point; all items equally weighted */
127 };
128
129 struct crush_bucket_list {
130         struct crush_bucket h;
131         __u32 *item_weights;  /* 16-bit fixed point */
132         __u32 *sum_weights;   /* 16-bit fixed point.  element i is sum
133                                  of weights 0..i, inclusive */
134 };
135
136 struct crush_bucket_tree {
137         struct crush_bucket h;  /* note: h.size is _tree_ size, not number of
138                                    actual items */
139         __u8 num_nodes;
140         __u32 *node_weights;
141 };
142
143 struct crush_bucket_straw {
144         struct crush_bucket h;
145         __u32 *item_weights;   /* 16-bit fixed point */
146         __u32 *straws;         /* 16-bit fixed point */
147 };
148
149
150
151 /*
152  * CRUSH map includes all buckets, rules, etc.
153  */
154 struct crush_map {
155         struct crush_bucket **buckets;
156         struct crush_rule **rules;
157
158         __s32 max_buckets;
159         __u32 max_rules;
160         __s32 max_devices;
161
162         /* choose local retries before re-descent */
163         __u32 choose_local_tries;
164         /* choose local attempts using a fallback permutation before
165          * re-descent */
166         __u32 choose_local_fallback_tries;
167         /* choose attempts before giving up */ 
168         __u32 choose_total_tries;
169         /* attempt chooseleaf inner descent once for firstn mode; on
170          * reject retry outer descent.  Note that this does *not*
171          * apply to a collision: in that case we will retry as we used
172          * to. */
173         __u32 chooseleaf_descend_once;
174 };
175
176
177 /* crush.c */
178 extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
179 extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
180 extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
181 extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
182 extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
183 extern void crush_destroy_bucket(struct crush_bucket *b);
184 extern void crush_destroy_rule(struct crush_rule *r);
185 extern void crush_destroy(struct crush_map *map);
186
187 static inline int crush_calc_tree_node(int i)
188 {
189         return ((i+1) << 1)-1;
190 }
191
192 #endif