Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1 | |
| 2 | #ifdef __KERNEL__ |
| 3 | # include <linux/string.h> |
| 4 | # include <linux/slab.h> |
| 5 | # include <linux/bug.h> |
| 6 | # include <linux/kernel.h> |
| 7 | # ifndef dprintk |
| 8 | # define dprintk(args...) |
| 9 | # endif |
| 10 | #else |
| 11 | # include <string.h> |
| 12 | # include <stdio.h> |
| 13 | # include <stdlib.h> |
| 14 | # include <assert.h> |
| 15 | # define BUG_ON(x) assert(!(x)) |
| 16 | # define dprintk(args...) /* printf(args) */ |
| 17 | # define kmalloc(x, f) malloc(x) |
| 18 | # define kfree(x) free(x) |
| 19 | #endif |
| 20 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 21 | #include <linux/crush/crush.h> |
| 22 | #include <linux/crush/hash.h> |
hartleys | feb50ac | 2012-04-24 14:38:37 +0000 | [diff] [blame] | 23 | #include <linux/crush/mapper.h> |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Implement the core CRUSH mapping algorithm. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. |
| 31 | * @map: the crush_map |
| 32 | * @ruleset: the storage ruleset id (user defined) |
| 33 | * @type: storage ruleset type (user defined) |
| 34 | * @size: output set size |
| 35 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 36 | int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 37 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 38 | __u32 i; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 39 | |
| 40 | for (i = 0; i < map->max_rules; i++) { |
| 41 | if (map->rules[i] && |
| 42 | map->rules[i]->mask.ruleset == ruleset && |
| 43 | map->rules[i]->mask.type == type && |
| 44 | map->rules[i]->mask.min_size <= size && |
| 45 | map->rules[i]->mask.max_size >= size) |
| 46 | return i; |
| 47 | } |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * bucket choose methods |
| 54 | * |
| 55 | * For each bucket algorithm, we have a "choose" method that, given a |
| 56 | * crush input @x and replica position (usually, position in output set) @r, |
| 57 | * will produce an item in the bucket. |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * Choose based on a random permutation of the bucket. |
| 62 | * |
| 63 | * We used to use some prime number arithmetic to do this, but it |
| 64 | * wasn't very random, and had some other bad behaviors. Instead, we |
| 65 | * calculate an actual random permutation of the bucket members. |
| 66 | * Since this is expensive, we optimize for the r=0 case, which |
| 67 | * captures the vast majority of calls. |
| 68 | */ |
| 69 | static int bucket_perm_choose(struct crush_bucket *bucket, |
| 70 | int x, int r) |
| 71 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 72 | unsigned int pr = r % bucket->size; |
| 73 | unsigned int i, s; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 74 | |
| 75 | /* start a new permutation if @x has changed */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 76 | if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 77 | dprintk("bucket %d new x=%d\n", bucket->id, x); |
| 78 | bucket->perm_x = x; |
| 79 | |
| 80 | /* optimize common r=0 case */ |
| 81 | if (pr == 0) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 82 | s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 83 | bucket->size; |
| 84 | bucket->perm[0] = s; |
| 85 | bucket->perm_n = 0xffff; /* magic value, see below */ |
| 86 | goto out; |
| 87 | } |
| 88 | |
| 89 | for (i = 0; i < bucket->size; i++) |
| 90 | bucket->perm[i] = i; |
| 91 | bucket->perm_n = 0; |
| 92 | } else if (bucket->perm_n == 0xffff) { |
| 93 | /* clean up after the r=0 case above */ |
| 94 | for (i = 1; i < bucket->size; i++) |
| 95 | bucket->perm[i] = i; |
| 96 | bucket->perm[bucket->perm[0]] = 0; |
| 97 | bucket->perm_n = 1; |
| 98 | } |
| 99 | |
| 100 | /* calculate permutation up to pr */ |
| 101 | for (i = 0; i < bucket->perm_n; i++) |
| 102 | dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]); |
| 103 | while (bucket->perm_n <= pr) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 104 | unsigned int p = bucket->perm_n; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 105 | /* no point in swapping the final entry */ |
| 106 | if (p < bucket->size - 1) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 107 | i = crush_hash32_3(bucket->hash, x, bucket->id, p) % |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 108 | (bucket->size - p); |
| 109 | if (i) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 110 | unsigned int t = bucket->perm[p + i]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 111 | bucket->perm[p + i] = bucket->perm[p]; |
| 112 | bucket->perm[p] = t; |
| 113 | } |
| 114 | dprintk(" perm_choose swap %d with %d\n", p, p+i); |
| 115 | } |
| 116 | bucket->perm_n++; |
| 117 | } |
| 118 | for (i = 0; i < bucket->size; i++) |
| 119 | dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]); |
| 120 | |
| 121 | s = bucket->perm[pr]; |
| 122 | out: |
| 123 | dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id, |
| 124 | bucket->size, x, r, pr, s); |
| 125 | return bucket->items[s]; |
| 126 | } |
| 127 | |
| 128 | /* uniform */ |
| 129 | static int bucket_uniform_choose(struct crush_bucket_uniform *bucket, |
| 130 | int x, int r) |
| 131 | { |
| 132 | return bucket_perm_choose(&bucket->h, x, r); |
| 133 | } |
| 134 | |
| 135 | /* list */ |
| 136 | static int bucket_list_choose(struct crush_bucket_list *bucket, |
| 137 | int x, int r) |
| 138 | { |
| 139 | int i; |
| 140 | |
| 141 | for (i = bucket->h.size-1; i >= 0; i--) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 142 | __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i], |
| 143 | r, bucket->h.id); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 144 | w &= 0xffff; |
| 145 | dprintk("list_choose i=%d x=%d r=%d item %d weight %x " |
| 146 | "sw %x rand %llx", |
| 147 | i, x, r, bucket->h.items[i], bucket->item_weights[i], |
| 148 | bucket->sum_weights[i], w); |
| 149 | w *= bucket->sum_weights[i]; |
| 150 | w = w >> 16; |
| 151 | /*dprintk(" scaled %llx\n", w);*/ |
| 152 | if (w < bucket->item_weights[i]) |
| 153 | return bucket->h.items[i]; |
| 154 | } |
| 155 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 156 | dprintk("bad list sums for bucket %d\n", bucket->h.id); |
| 157 | return bucket->h.items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | /* (binary) tree */ |
| 162 | static int height(int n) |
| 163 | { |
| 164 | int h = 0; |
| 165 | while ((n & 1) == 0) { |
| 166 | h++; |
| 167 | n = n >> 1; |
| 168 | } |
| 169 | return h; |
| 170 | } |
| 171 | |
| 172 | static int left(int x) |
| 173 | { |
| 174 | int h = height(x); |
| 175 | return x - (1 << (h-1)); |
| 176 | } |
| 177 | |
| 178 | static int right(int x) |
| 179 | { |
| 180 | int h = height(x); |
| 181 | return x + (1 << (h-1)); |
| 182 | } |
| 183 | |
| 184 | static int terminal(int x) |
| 185 | { |
| 186 | return x & 1; |
| 187 | } |
| 188 | |
| 189 | static int bucket_tree_choose(struct crush_bucket_tree *bucket, |
| 190 | int x, int r) |
| 191 | { |
| 192 | int n, l; |
| 193 | __u32 w; |
| 194 | __u64 t; |
| 195 | |
| 196 | /* start at root */ |
| 197 | n = bucket->num_nodes >> 1; |
| 198 | |
| 199 | while (!terminal(n)) { |
| 200 | /* pick point in [0, w) */ |
| 201 | w = bucket->node_weights[n]; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 202 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, |
| 203 | bucket->h.id) * (__u64)w; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 204 | t = t >> 32; |
| 205 | |
| 206 | /* descend to the left or right? */ |
| 207 | l = left(n); |
| 208 | if (t < bucket->node_weights[l]) |
| 209 | n = l; |
| 210 | else |
| 211 | n = right(n); |
| 212 | } |
| 213 | |
| 214 | return bucket->h.items[n >> 1]; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /* straw */ |
| 219 | |
| 220 | static int bucket_straw_choose(struct crush_bucket_straw *bucket, |
| 221 | int x, int r) |
| 222 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 223 | __u32 i; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 224 | int high = 0; |
| 225 | __u64 high_draw = 0; |
| 226 | __u64 draw; |
| 227 | |
| 228 | for (i = 0; i < bucket->h.size; i++) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 229 | draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 230 | draw &= 0xffff; |
| 231 | draw *= bucket->straws[i]; |
| 232 | if (i == 0 || draw > high_draw) { |
| 233 | high = i; |
| 234 | high_draw = draw; |
| 235 | } |
| 236 | } |
| 237 | return bucket->h.items[high]; |
| 238 | } |
| 239 | |
| 240 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) |
| 241 | { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 242 | dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 243 | BUG_ON(in->size == 0); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 244 | switch (in->alg) { |
| 245 | case CRUSH_BUCKET_UNIFORM: |
| 246 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, |
| 247 | x, r); |
| 248 | case CRUSH_BUCKET_LIST: |
| 249 | return bucket_list_choose((struct crush_bucket_list *)in, |
| 250 | x, r); |
| 251 | case CRUSH_BUCKET_TREE: |
| 252 | return bucket_tree_choose((struct crush_bucket_tree *)in, |
| 253 | x, r); |
| 254 | case CRUSH_BUCKET_STRAW: |
| 255 | return bucket_straw_choose((struct crush_bucket_straw *)in, |
| 256 | x, r); |
| 257 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 258 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 259 | return in->items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * true if device is marked "out" (failed, fully offloaded) |
| 265 | * of the cluster |
| 266 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 267 | static int is_out(const struct crush_map *map, const __u32 *weight, int item, int x) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 268 | { |
Sage Weil | 153a109 | 2010-07-05 09:44:17 -0700 | [diff] [blame] | 269 | if (weight[item] >= 0x10000) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 270 | return 0; |
| 271 | if (weight[item] == 0) |
| 272 | return 1; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 273 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) |
| 274 | < weight[item]) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * crush_choose - choose numrep distinct items of given type |
| 281 | * @map: the crush_map |
| 282 | * @bucket: the bucket we are choose an item from |
| 283 | * @x: crush input value |
| 284 | * @numrep: the number of items to choose |
| 285 | * @type: the type of item to choose |
| 286 | * @out: pointer to output vector |
| 287 | * @outpos: our position in that vector |
| 288 | * @firstn: true if choosing "first n" items, false if choosing "indep" |
| 289 | * @recurse_to_leaf: true if we want one device under each item of given type |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 290 | * @descend_once: true if we should only try one descent before giving up |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 291 | * @out2: second output vector for leaf items (if @recurse_to_leaf) |
| 292 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 293 | static int crush_choose(const struct crush_map *map, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 294 | struct crush_bucket *bucket, |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 295 | const __u32 *weight, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 296 | int x, int numrep, int type, |
| 297 | int *out, int outpos, |
| 298 | int firstn, int recurse_to_leaf, |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 299 | int descend_once, int *out2) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 300 | { |
| 301 | int rep; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 302 | unsigned int ftotal, flocal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 303 | int retry_descent, retry_bucket, skip_rep; |
| 304 | struct crush_bucket *in = bucket; |
| 305 | int r; |
| 306 | int i; |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 307 | int item = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 308 | int itemtype; |
| 309 | int collide, reject; |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 310 | |
| 311 | dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", |
| 312 | bucket->id, x, outpos, numrep); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 313 | |
| 314 | for (rep = outpos; rep < numrep; rep++) { |
| 315 | /* keep trying until we get a non-out, non-colliding item */ |
| 316 | ftotal = 0; |
| 317 | skip_rep = 0; |
| 318 | do { |
| 319 | retry_descent = 0; |
| 320 | in = bucket; /* initial bucket */ |
| 321 | |
| 322 | /* choose through intervening buckets */ |
| 323 | flocal = 0; |
| 324 | do { |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 325 | collide = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 326 | retry_bucket = 0; |
| 327 | r = rep; |
| 328 | if (in->alg == CRUSH_BUCKET_UNIFORM) { |
| 329 | /* be careful */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 330 | if (firstn || (__u32)numrep >= in->size) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 331 | /* r' = r + f_total */ |
| 332 | r += ftotal; |
| 333 | else if (in->size % numrep == 0) |
| 334 | /* r'=r+(n+1)*f_local */ |
| 335 | r += (numrep+1) * |
| 336 | (flocal+ftotal); |
| 337 | else |
| 338 | /* r' = r + n*f_local */ |
| 339 | r += numrep * (flocal+ftotal); |
| 340 | } else { |
| 341 | if (firstn) |
| 342 | /* r' = r + f_total */ |
| 343 | r += ftotal; |
| 344 | else |
| 345 | /* r' = r + n*f_local */ |
| 346 | r += numrep * (flocal+ftotal); |
| 347 | } |
| 348 | |
| 349 | /* bucket choose */ |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 350 | if (in->size == 0) { |
| 351 | reject = 1; |
| 352 | goto reject; |
| 353 | } |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 354 | if (map->choose_local_fallback_tries > 0 && |
| 355 | flocal >= (in->size>>1) && |
| 356 | flocal > map->choose_local_fallback_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 357 | item = bucket_perm_choose(in, x, r); |
| 358 | else |
| 359 | item = crush_bucket_choose(in, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 360 | if (item >= map->max_devices) { |
| 361 | dprintk(" bad item %d\n", item); |
| 362 | skip_rep = 1; |
| 363 | break; |
| 364 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 365 | |
| 366 | /* desired type? */ |
| 367 | if (item < 0) |
| 368 | itemtype = map->buckets[-1-item]->type; |
| 369 | else |
| 370 | itemtype = 0; |
| 371 | dprintk(" item %d type %d\n", item, itemtype); |
| 372 | |
| 373 | /* keep going? */ |
| 374 | if (itemtype != type) { |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 375 | if (item >= 0 || |
| 376 | (-1-item) >= map->max_buckets) { |
| 377 | dprintk(" bad item type %d\n", type); |
| 378 | skip_rep = 1; |
| 379 | break; |
| 380 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 381 | in = map->buckets[-1-item]; |
Sage Weil | 55bda7a | 2010-06-24 12:55:48 -0700 | [diff] [blame] | 382 | retry_bucket = 1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 383 | continue; |
| 384 | } |
| 385 | |
| 386 | /* collision? */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 387 | for (i = 0; i < outpos; i++) { |
| 388 | if (out[i] == item) { |
| 389 | collide = 1; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 394 | reject = 0; |
Sage Weil | 7d7c1f6 | 2013-01-15 18:49:09 -0800 | [diff] [blame] | 395 | if (!collide && recurse_to_leaf) { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 396 | if (item < 0) { |
| 397 | if (crush_choose(map, |
| 398 | map->buckets[-1-item], |
| 399 | weight, |
| 400 | x, outpos+1, 0, |
| 401 | out2, outpos, |
| 402 | firstn, 0, |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 403 | map->chooseleaf_descend_once, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 404 | NULL) <= outpos) |
| 405 | /* didn't get leaf */ |
| 406 | reject = 1; |
| 407 | } else { |
| 408 | /* we already have a leaf! */ |
| 409 | out2[outpos] = item; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (!reject) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 414 | /* out? */ |
| 415 | if (itemtype == 0) |
| 416 | reject = is_out(map, weight, |
| 417 | item, x); |
| 418 | else |
| 419 | reject = 0; |
| 420 | } |
| 421 | |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 422 | reject: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 423 | if (reject || collide) { |
| 424 | ftotal++; |
| 425 | flocal++; |
| 426 | |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 427 | if (reject && descend_once) |
| 428 | /* let outer call try again */ |
| 429 | skip_rep = 1; |
| 430 | else if (collide && flocal <= map->choose_local_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 431 | /* retry locally a few times */ |
| 432 | retry_bucket = 1; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 433 | else if (map->choose_local_fallback_tries > 0 && |
| 434 | flocal <= in->size + map->choose_local_fallback_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 435 | /* exhaustive bucket search */ |
| 436 | retry_bucket = 1; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 437 | else if (ftotal <= map->choose_total_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 438 | /* then retry descent */ |
| 439 | retry_descent = 1; |
| 440 | else |
| 441 | /* else give up */ |
| 442 | skip_rep = 1; |
| 443 | dprintk(" reject %d collide %d " |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 444 | "ftotal %u flocal %u\n", |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 445 | reject, collide, ftotal, |
| 446 | flocal); |
| 447 | } |
| 448 | } while (retry_bucket); |
| 449 | } while (retry_descent); |
| 450 | |
| 451 | if (skip_rep) { |
| 452 | dprintk("skip rep\n"); |
| 453 | continue; |
| 454 | } |
| 455 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 456 | dprintk("CHOOSE got %d\n", item); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 457 | out[outpos] = item; |
| 458 | outpos++; |
| 459 | } |
| 460 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 461 | dprintk("CHOOSE returns %d\n", outpos); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 462 | return outpos; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /** |
| 467 | * crush_do_rule - calculate a mapping with the given input and rule |
| 468 | * @map: the crush_map |
| 469 | * @ruleno: the rule id |
| 470 | * @x: hash input |
| 471 | * @result: pointer to result vector |
| 472 | * @result_max: maximum result size |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 473 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 474 | int crush_do_rule(const struct crush_map *map, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 475 | int ruleno, int x, int *result, int result_max, |
Sage Weil | 41ebcc0 | 2012-05-07 15:39:29 -0700 | [diff] [blame] | 476 | const __u32 *weight) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 477 | { |
| 478 | int result_len; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 479 | int a[CRUSH_MAX_SET]; |
| 480 | int b[CRUSH_MAX_SET]; |
| 481 | int c[CRUSH_MAX_SET]; |
| 482 | int recurse_to_leaf; |
| 483 | int *w; |
| 484 | int wsize = 0; |
| 485 | int *o; |
| 486 | int osize; |
| 487 | int *tmp; |
| 488 | struct crush_rule *rule; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 489 | __u32 step; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 490 | int i, j; |
| 491 | int numrep; |
| 492 | int firstn; |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 493 | const int descend_once = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 494 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 495 | if ((__u32)ruleno >= map->max_rules) { |
| 496 | dprintk(" bad ruleno %d\n", ruleno); |
| 497 | return 0; |
| 498 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 499 | |
| 500 | rule = map->rules[ruleno]; |
| 501 | result_len = 0; |
| 502 | w = a; |
| 503 | o = b; |
| 504 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 505 | for (step = 0; step < rule->len; step++) { |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 506 | struct crush_rule_step *curstep = &rule->steps[step]; |
| 507 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 508 | firstn = 0; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 509 | switch (curstep->op) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 510 | case CRUSH_RULE_TAKE: |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 511 | w[0] = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 512 | wsize = 1; |
| 513 | break; |
| 514 | |
| 515 | case CRUSH_RULE_CHOOSE_LEAF_FIRSTN: |
| 516 | case CRUSH_RULE_CHOOSE_FIRSTN: |
| 517 | firstn = 1; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 518 | /* fall through */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 519 | case CRUSH_RULE_CHOOSE_LEAF_INDEP: |
| 520 | case CRUSH_RULE_CHOOSE_INDEP: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 521 | if (wsize == 0) |
| 522 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 523 | |
| 524 | recurse_to_leaf = |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 525 | curstep->op == |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 526 | CRUSH_RULE_CHOOSE_LEAF_FIRSTN || |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 527 | curstep->op == |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 528 | CRUSH_RULE_CHOOSE_LEAF_INDEP; |
| 529 | |
| 530 | /* reset output */ |
| 531 | osize = 0; |
| 532 | |
| 533 | for (i = 0; i < wsize; i++) { |
| 534 | /* |
| 535 | * see CRUSH_N, CRUSH_N_MINUS macros. |
| 536 | * basically, numrep <= 0 means relative to |
| 537 | * the provided result_max |
| 538 | */ |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 539 | numrep = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 540 | if (numrep <= 0) { |
| 541 | numrep += result_max; |
| 542 | if (numrep <= 0) |
| 543 | continue; |
| 544 | } |
| 545 | j = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 546 | osize += crush_choose(map, |
| 547 | map->buckets[-1-w[i]], |
| 548 | weight, |
| 549 | x, numrep, |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 550 | curstep->arg2, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 551 | o+osize, j, |
| 552 | firstn, |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 553 | recurse_to_leaf, |
| 554 | descend_once, c+osize); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (recurse_to_leaf) |
| 558 | /* copy final _leaf_ values to output set */ |
| 559 | memcpy(o, c, osize*sizeof(*o)); |
| 560 | |
| 561 | /* swap t and w arrays */ |
| 562 | tmp = o; |
| 563 | o = w; |
| 564 | w = tmp; |
| 565 | wsize = osize; |
| 566 | break; |
| 567 | |
| 568 | |
| 569 | case CRUSH_RULE_EMIT: |
| 570 | for (i = 0; i < wsize && result_len < result_max; i++) { |
| 571 | result[result_len] = w[i]; |
| 572 | result_len++; |
| 573 | } |
| 574 | wsize = 0; |
| 575 | break; |
| 576 | |
| 577 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 578 | dprintk(" unknown op %d at step %d\n", |
| 579 | curstep->op, step); |
| 580 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
Sage Weil | f1932fc | 2011-12-07 09:10:26 -0800 | [diff] [blame] | 583 | return result_len; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |