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 | { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 192 | int n; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 193 | __u32 w; |
| 194 | __u64 t; |
| 195 | |
| 196 | /* start at root */ |
| 197 | n = bucket->num_nodes >> 1; |
| 198 | |
| 199 | while (!terminal(n)) { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 200 | int l; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 201 | /* pick point in [0, w) */ |
| 202 | w = bucket->node_weights[n]; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 203 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, |
| 204 | bucket->h.id) * (__u64)w; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 205 | t = t >> 32; |
| 206 | |
| 207 | /* descend to the left or right? */ |
| 208 | l = left(n); |
| 209 | if (t < bucket->node_weights[l]) |
| 210 | n = l; |
| 211 | else |
| 212 | n = right(n); |
| 213 | } |
| 214 | |
| 215 | return bucket->h.items[n >> 1]; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* straw */ |
| 220 | |
| 221 | static int bucket_straw_choose(struct crush_bucket_straw *bucket, |
| 222 | int x, int r) |
| 223 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 224 | __u32 i; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 225 | int high = 0; |
| 226 | __u64 high_draw = 0; |
| 227 | __u64 draw; |
| 228 | |
| 229 | for (i = 0; i < bucket->h.size; i++) { |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 230 | 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] | 231 | draw &= 0xffff; |
| 232 | draw *= bucket->straws[i]; |
| 233 | if (i == 0 || draw > high_draw) { |
| 234 | high = i; |
| 235 | high_draw = draw; |
| 236 | } |
| 237 | } |
| 238 | return bucket->h.items[high]; |
| 239 | } |
| 240 | |
| 241 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) |
| 242 | { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 243 | 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] | 244 | BUG_ON(in->size == 0); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 245 | switch (in->alg) { |
| 246 | case CRUSH_BUCKET_UNIFORM: |
| 247 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, |
| 248 | x, r); |
| 249 | case CRUSH_BUCKET_LIST: |
| 250 | return bucket_list_choose((struct crush_bucket_list *)in, |
| 251 | x, r); |
| 252 | case CRUSH_BUCKET_TREE: |
| 253 | return bucket_tree_choose((struct crush_bucket_tree *)in, |
| 254 | x, r); |
| 255 | case CRUSH_BUCKET_STRAW: |
| 256 | return bucket_straw_choose((struct crush_bucket_straw *)in, |
| 257 | x, r); |
| 258 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 259 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 260 | return in->items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * true if device is marked "out" (failed, fully offloaded) |
| 266 | * of the cluster |
| 267 | */ |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 268 | static int is_out(const struct crush_map *map, |
| 269 | const __u32 *weight, int weight_max, |
| 270 | int item, int x) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 271 | { |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 272 | if (item >= weight_max) |
| 273 | return 1; |
Sage Weil | 153a109 | 2010-07-05 09:44:17 -0700 | [diff] [blame] | 274 | if (weight[item] >= 0x10000) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | if (weight[item] == 0) |
| 277 | return 1; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 278 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) |
| 279 | < weight[item]) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 285 | * crush_choose_firstn - choose numrep distinct items of given type |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 286 | * @map: the crush_map |
| 287 | * @bucket: the bucket we are choose an item from |
| 288 | * @x: crush input value |
| 289 | * @numrep: the number of items to choose |
| 290 | * @type: the type of item to choose |
| 291 | * @out: pointer to output vector |
| 292 | * @outpos: our position in that vector |
Ilya Dryomov | 0e32d71 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 293 | * @tries: number of attempts to make |
| 294 | * @recurse_tries: number of attempts to have recursive chooseleaf make |
| 295 | * @local_tries: localized retries |
| 296 | * @local_fallback_tries: localized fallback retries |
| 297 | * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 298 | * @out2: second output vector for leaf items (if @recurse_to_leaf) |
| 299 | */ |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 300 | static int crush_choose_firstn(const struct crush_map *map, |
| 301 | struct crush_bucket *bucket, |
| 302 | const __u32 *weight, int weight_max, |
| 303 | int x, int numrep, int type, |
| 304 | int *out, int outpos, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 305 | unsigned int tries, |
| 306 | unsigned int recurse_tries, |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 307 | unsigned int local_tries, |
| 308 | unsigned int local_fallback_tries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 309 | int recurse_to_leaf, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 310 | int *out2) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 311 | { |
| 312 | int rep; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 313 | unsigned int ftotal, flocal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 314 | int retry_descent, retry_bucket, skip_rep; |
| 315 | struct crush_bucket *in = bucket; |
| 316 | int r; |
| 317 | int i; |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 318 | int item = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 319 | int itemtype; |
| 320 | int collide, reject; |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 321 | |
| 322 | dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", |
| 323 | bucket->id, x, outpos, numrep); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 324 | |
| 325 | for (rep = outpos; rep < numrep; rep++) { |
| 326 | /* keep trying until we get a non-out, non-colliding item */ |
| 327 | ftotal = 0; |
| 328 | skip_rep = 0; |
| 329 | do { |
| 330 | retry_descent = 0; |
| 331 | in = bucket; /* initial bucket */ |
| 332 | |
| 333 | /* choose through intervening buckets */ |
| 334 | flocal = 0; |
| 335 | do { |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 336 | collide = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 337 | retry_bucket = 0; |
| 338 | r = rep; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 339 | /* r' = r + f_total */ |
| 340 | r += ftotal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 341 | |
| 342 | /* bucket choose */ |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 343 | if (in->size == 0) { |
| 344 | reject = 1; |
| 345 | goto reject; |
| 346 | } |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 347 | if (local_fallback_tries > 0 && |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 348 | flocal >= (in->size>>1) && |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 349 | flocal > local_fallback_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 350 | item = bucket_perm_choose(in, x, r); |
| 351 | else |
| 352 | item = crush_bucket_choose(in, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 353 | if (item >= map->max_devices) { |
| 354 | dprintk(" bad item %d\n", item); |
| 355 | skip_rep = 1; |
| 356 | break; |
| 357 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 358 | |
| 359 | /* desired type? */ |
| 360 | if (item < 0) |
| 361 | itemtype = map->buckets[-1-item]->type; |
| 362 | else |
| 363 | itemtype = 0; |
| 364 | dprintk(" item %d type %d\n", item, itemtype); |
| 365 | |
| 366 | /* keep going? */ |
| 367 | if (itemtype != type) { |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 368 | if (item >= 0 || |
| 369 | (-1-item) >= map->max_buckets) { |
| 370 | dprintk(" bad item type %d\n", type); |
| 371 | skip_rep = 1; |
| 372 | break; |
| 373 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 374 | in = map->buckets[-1-item]; |
Sage Weil | 55bda7a | 2010-06-24 12:55:48 -0700 | [diff] [blame] | 375 | retry_bucket = 1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 376 | continue; |
| 377 | } |
| 378 | |
| 379 | /* collision? */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 380 | for (i = 0; i < outpos; i++) { |
| 381 | if (out[i] == item) { |
| 382 | collide = 1; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 387 | reject = 0; |
Sage Weil | 7d7c1f6 | 2013-01-15 18:49:09 -0800 | [diff] [blame] | 388 | if (!collide && recurse_to_leaf) { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 389 | if (item < 0) { |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 390 | if (crush_choose_firstn(map, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 391 | map->buckets[-1-item], |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 392 | weight, weight_max, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 393 | x, outpos+1, 0, |
| 394 | out2, outpos, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 395 | recurse_tries, 0, |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 396 | local_tries, |
| 397 | local_fallback_tries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 398 | 0, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 399 | NULL) <= outpos) |
| 400 | /* didn't get leaf */ |
| 401 | reject = 1; |
| 402 | } else { |
| 403 | /* we already have a leaf! */ |
| 404 | out2[outpos] = item; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (!reject) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 409 | /* out? */ |
| 410 | if (itemtype == 0) |
| 411 | reject = is_out(map, weight, |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 412 | weight_max, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 413 | item, x); |
| 414 | else |
| 415 | reject = 0; |
| 416 | } |
| 417 | |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 418 | reject: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 419 | if (reject || collide) { |
| 420 | ftotal++; |
| 421 | flocal++; |
| 422 | |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 423 | if (collide && flocal <= local_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 424 | /* retry locally a few times */ |
| 425 | retry_bucket = 1; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 426 | else if (local_fallback_tries > 0 && |
| 427 | flocal <= in->size + local_fallback_tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 428 | /* exhaustive bucket search */ |
| 429 | retry_bucket = 1; |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 430 | else if (ftotal <= tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 431 | /* then retry descent */ |
| 432 | retry_descent = 1; |
| 433 | else |
| 434 | /* else give up */ |
| 435 | skip_rep = 1; |
| 436 | dprintk(" reject %d collide %d " |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 437 | "ftotal %u flocal %u\n", |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 438 | reject, collide, ftotal, |
| 439 | flocal); |
| 440 | } |
| 441 | } while (retry_bucket); |
| 442 | } while (retry_descent); |
| 443 | |
| 444 | if (skip_rep) { |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 445 | dprintk("skip rep\n"); |
| 446 | continue; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 449 | dprintk("CHOOSE got %d\n", item); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 450 | out[outpos] = item; |
| 451 | outpos++; |
| 452 | } |
| 453 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 454 | dprintk("CHOOSE returns %d\n", outpos); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 455 | return outpos; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 460 | * crush_choose_indep: alternative breadth-first positionally stable mapping |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 461 | * |
| 462 | */ |
| 463 | static void crush_choose_indep(const struct crush_map *map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 464 | struct crush_bucket *bucket, |
| 465 | const __u32 *weight, int weight_max, |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 466 | int x, int left, int numrep, int type, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 467 | int *out, int outpos, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 468 | unsigned int tries, |
| 469 | unsigned int recurse_tries, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 470 | int recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 471 | int *out2, |
| 472 | int parent_r) |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 473 | { |
| 474 | struct crush_bucket *in = bucket; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 475 | int endpos = outpos + left; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 476 | int rep; |
| 477 | unsigned int ftotal; |
| 478 | int r; |
| 479 | int i; |
| 480 | int item = 0; |
| 481 | int itemtype; |
| 482 | int collide; |
| 483 | |
| 484 | dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", |
| 485 | bucket->id, x, outpos, numrep); |
| 486 | |
| 487 | /* initially my result is undefined */ |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 488 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 489 | out[rep] = CRUSH_ITEM_UNDEF; |
| 490 | if (out2) |
| 491 | out2[rep] = CRUSH_ITEM_UNDEF; |
| 492 | } |
| 493 | |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 494 | for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 495 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 496 | if (out[rep] != CRUSH_ITEM_UNDEF) |
| 497 | continue; |
| 498 | |
| 499 | in = bucket; /* initial bucket */ |
| 500 | |
| 501 | /* choose through intervening buckets */ |
| 502 | for (;;) { |
Ilya Dryomov | 3102b0a | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 503 | /* note: we base the choice on the position |
| 504 | * even in the nested call. that means that |
| 505 | * if the first layer chooses the same bucket |
| 506 | * in a different position, we will tend to |
| 507 | * choose a different item in that bucket. |
| 508 | * this will involve more devices in data |
| 509 | * movement and tend to distribute the load. |
| 510 | */ |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 511 | r = rep + parent_r; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 512 | |
| 513 | /* be careful */ |
| 514 | if (in->alg == CRUSH_BUCKET_UNIFORM && |
| 515 | in->size % numrep == 0) |
| 516 | /* r'=r+(n+1)*f_total */ |
| 517 | r += (numrep+1) * ftotal; |
| 518 | else |
| 519 | /* r' = r + n*f_total */ |
| 520 | r += numrep * ftotal; |
| 521 | |
| 522 | /* bucket choose */ |
| 523 | if (in->size == 0) { |
| 524 | dprintk(" empty bucket\n"); |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | item = crush_bucket_choose(in, x, r); |
| 529 | if (item >= map->max_devices) { |
| 530 | dprintk(" bad item %d\n", item); |
| 531 | out[rep] = CRUSH_ITEM_NONE; |
| 532 | if (out2) |
| 533 | out2[rep] = CRUSH_ITEM_NONE; |
| 534 | left--; |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | /* desired type? */ |
| 539 | if (item < 0) |
| 540 | itemtype = map->buckets[-1-item]->type; |
| 541 | else |
| 542 | itemtype = 0; |
| 543 | dprintk(" item %d type %d\n", item, itemtype); |
| 544 | |
| 545 | /* keep going? */ |
| 546 | if (itemtype != type) { |
| 547 | if (item >= 0 || |
| 548 | (-1-item) >= map->max_buckets) { |
| 549 | dprintk(" bad item type %d\n", type); |
| 550 | out[rep] = CRUSH_ITEM_NONE; |
| 551 | if (out2) |
| 552 | out2[rep] = |
| 553 | CRUSH_ITEM_NONE; |
| 554 | left--; |
| 555 | break; |
| 556 | } |
| 557 | in = map->buckets[-1-item]; |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | /* collision? */ |
| 562 | collide = 0; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 563 | for (i = outpos; i < endpos; i++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 564 | if (out[i] == item) { |
| 565 | collide = 1; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | if (collide) |
| 570 | break; |
| 571 | |
| 572 | if (recurse_to_leaf) { |
| 573 | if (item < 0) { |
| 574 | crush_choose_indep(map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 575 | map->buckets[-1-item], |
| 576 | weight, weight_max, |
| 577 | x, 1, numrep, 0, |
| 578 | out2, rep, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 579 | recurse_tries, 0, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 580 | 0, NULL, r); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 581 | if (out2[rep] == CRUSH_ITEM_NONE) { |
| 582 | /* placed nothing; no leaf */ |
| 583 | break; |
| 584 | } |
| 585 | } else { |
| 586 | /* we already have a leaf! */ |
| 587 | out2[rep] = item; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* out? */ |
| 592 | if (itemtype == 0 && |
| 593 | is_out(map, weight, weight_max, item, x)) |
| 594 | break; |
| 595 | |
| 596 | /* yay! */ |
| 597 | out[rep] = item; |
| 598 | left--; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 603 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 604 | if (out[rep] == CRUSH_ITEM_UNDEF) { |
| 605 | out[rep] = CRUSH_ITEM_NONE; |
| 606 | } |
| 607 | if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { |
| 608 | out2[rep] = CRUSH_ITEM_NONE; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 614 | * crush_do_rule - calculate a mapping with the given input and rule |
| 615 | * @map: the crush_map |
| 616 | * @ruleno: the rule id |
| 617 | * @x: hash input |
| 618 | * @result: pointer to result vector |
| 619 | * @result_max: maximum result size |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 620 | * @weight: weight vector (for map leaves) |
| 621 | * @weight_max: size of weight vector |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 622 | * @scratch: scratch vector for private use; must be >= 3 * result_max |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 623 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 624 | int crush_do_rule(const struct crush_map *map, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 625 | int ruleno, int x, int *result, int result_max, |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 626 | const __u32 *weight, int weight_max, |
| 627 | int *scratch) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 628 | { |
| 629 | int result_len; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 630 | int *a = scratch; |
| 631 | int *b = scratch + result_max; |
| 632 | int *c = scratch + result_max*2; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 633 | int recurse_to_leaf; |
| 634 | int *w; |
| 635 | int wsize = 0; |
| 636 | int *o; |
| 637 | int osize; |
| 638 | int *tmp; |
| 639 | struct crush_rule *rule; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 640 | __u32 step; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 641 | int i, j; |
| 642 | int numrep; |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 643 | int choose_tries = map->choose_total_tries; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 644 | int choose_local_tries = map->choose_local_tries; |
| 645 | int choose_local_fallback_tries = map->choose_local_fallback_tries; |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 646 | int choose_leaf_tries = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 647 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 648 | if ((__u32)ruleno >= map->max_rules) { |
| 649 | dprintk(" bad ruleno %d\n", ruleno); |
| 650 | return 0; |
| 651 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 652 | |
| 653 | rule = map->rules[ruleno]; |
| 654 | result_len = 0; |
| 655 | w = a; |
| 656 | o = b; |
| 657 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 658 | for (step = 0; step < rule->len; step++) { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 659 | int firstn = 0; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 660 | struct crush_rule_step *curstep = &rule->steps[step]; |
| 661 | |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 662 | switch (curstep->op) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 663 | case CRUSH_RULE_TAKE: |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 664 | w[0] = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 665 | wsize = 1; |
| 666 | break; |
| 667 | |
Ilya Dryomov | cc10df4 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 668 | case CRUSH_RULE_SET_CHOOSE_TRIES: |
| 669 | if (curstep->arg1 > 0) |
| 670 | choose_tries = curstep->arg1; |
| 671 | break; |
| 672 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 673 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 674 | if (curstep->arg1 > 0) |
| 675 | choose_leaf_tries = curstep->arg1; |
| 676 | break; |
| 677 | |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 678 | case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: |
| 679 | if (curstep->arg1 > 0) |
| 680 | choose_local_tries = curstep->arg1; |
| 681 | break; |
| 682 | |
| 683 | case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: |
| 684 | if (curstep->arg1 > 0) |
| 685 | choose_local_fallback_tries = curstep->arg1; |
| 686 | break; |
| 687 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 688 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 689 | case CRUSH_RULE_CHOOSE_FIRSTN: |
| 690 | firstn = 1; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 691 | /* fall through */ |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 692 | case CRUSH_RULE_CHOOSELEAF_INDEP: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 693 | case CRUSH_RULE_CHOOSE_INDEP: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 694 | if (wsize == 0) |
| 695 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 696 | |
| 697 | recurse_to_leaf = |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 698 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 699 | CRUSH_RULE_CHOOSELEAF_FIRSTN || |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 700 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 701 | CRUSH_RULE_CHOOSELEAF_INDEP; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 702 | |
| 703 | /* reset output */ |
| 704 | osize = 0; |
| 705 | |
| 706 | for (i = 0; i < wsize; i++) { |
| 707 | /* |
| 708 | * see CRUSH_N, CRUSH_N_MINUS macros. |
| 709 | * basically, numrep <= 0 means relative to |
| 710 | * the provided result_max |
| 711 | */ |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 712 | numrep = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 713 | if (numrep <= 0) { |
| 714 | numrep += result_max; |
| 715 | if (numrep <= 0) |
| 716 | continue; |
| 717 | } |
| 718 | j = 0; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 719 | if (firstn) { |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 720 | int recurse_tries; |
| 721 | if (choose_leaf_tries) |
| 722 | recurse_tries = |
| 723 | choose_leaf_tries; |
| 724 | else if (map->chooseleaf_descend_once) |
| 725 | recurse_tries = 1; |
| 726 | else |
| 727 | recurse_tries = choose_tries; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 728 | osize += crush_choose_firstn( |
| 729 | map, |
| 730 | map->buckets[-1-w[i]], |
| 731 | weight, weight_max, |
| 732 | x, numrep, |
| 733 | curstep->arg2, |
| 734 | o+osize, j, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 735 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 736 | recurse_tries, |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 737 | choose_local_tries, |
| 738 | choose_local_fallback_tries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 739 | recurse_to_leaf, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 740 | c+osize); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 741 | } else { |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 742 | crush_choose_indep( |
| 743 | map, |
| 744 | map->buckets[-1-w[i]], |
| 745 | weight, weight_max, |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 746 | x, numrep, numrep, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 747 | curstep->arg2, |
| 748 | o+osize, j, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 749 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 750 | choose_leaf_tries ? |
| 751 | choose_leaf_tries : 1, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 752 | recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 753 | c+osize, |
| 754 | 0); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 755 | osize += numrep; |
| 756 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | if (recurse_to_leaf) |
| 760 | /* copy final _leaf_ values to output set */ |
| 761 | memcpy(o, c, osize*sizeof(*o)); |
| 762 | |
Ilya Dryomov | 2a4ba74 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 763 | /* swap o and w arrays */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 764 | tmp = o; |
| 765 | o = w; |
| 766 | w = tmp; |
| 767 | wsize = osize; |
| 768 | break; |
| 769 | |
| 770 | |
| 771 | case CRUSH_RULE_EMIT: |
| 772 | for (i = 0; i < wsize && result_len < result_max; i++) { |
| 773 | result[result_len] = w[i]; |
| 774 | result_len++; |
| 775 | } |
| 776 | wsize = 0; |
| 777 | break; |
| 778 | |
| 779 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 780 | dprintk(" unknown op %d at step %d\n", |
| 781 | curstep->op, step); |
| 782 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
Sage Weil | f1932fc | 2011-12-07 09:10:26 -0800 | [diff] [blame] | 785 | return result_len; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | |