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> |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame^] | 23 | #include "crush_ln_table.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 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame^] | 241 | // compute 2^44*log2(input+1) |
| 242 | uint64_t crush_ln(unsigned xin) |
| 243 | { |
| 244 | unsigned x=xin, x1; |
| 245 | int iexpon, index1, index2; |
| 246 | uint64_t RH, LH, LL, xl64, result; |
| 247 | |
| 248 | x++; |
| 249 | |
| 250 | // normalize input |
| 251 | iexpon = 15; |
| 252 | while(!(x&0x18000)) { x<<=1; iexpon--; } |
| 253 | |
| 254 | index1 = (x>>8)<<1; |
| 255 | // RH ~ 2^56/index1 |
| 256 | RH = __RH_LH_tbl[index1 - 256]; |
| 257 | // LH ~ 2^48 * log2(index1/256) |
| 258 | LH = __RH_LH_tbl[index1 + 1 - 256]; |
| 259 | |
| 260 | // RH*x ~ 2^48 * (2^15 + xf), xf<2^8 |
| 261 | xl64 = (int64_t)x * RH; |
| 262 | xl64 >>= 48; |
| 263 | x1 = xl64; |
| 264 | |
| 265 | result = iexpon; |
| 266 | result <<= (12 + 32); |
| 267 | |
| 268 | index2 = x1 & 0xff; |
| 269 | // LL ~ 2^48*log2(1.0+index2/2^15) |
| 270 | LL = __LL_tbl[index2]; |
| 271 | |
| 272 | LH = LH + LL; |
| 273 | |
| 274 | LH >>= (48-12 - 32); |
| 275 | result += LH; |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* |
| 282 | * straw2 |
| 283 | * |
| 284 | * for reference, see: |
| 285 | * |
| 286 | * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables |
| 287 | * |
| 288 | */ |
| 289 | |
| 290 | static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket, |
| 291 | int x, int r) |
| 292 | { |
| 293 | unsigned i, high = 0; |
| 294 | unsigned u; |
| 295 | unsigned w; |
| 296 | __s64 ln, draw, high_draw = 0; |
| 297 | |
| 298 | for (i = 0; i < bucket->h.size; i++) { |
| 299 | w = bucket->item_weights[i]; |
| 300 | if (w) { |
| 301 | u = crush_hash32_3(bucket->h.hash, x, |
| 302 | bucket->h.items[i], r); |
| 303 | u &= 0xffff; |
| 304 | |
| 305 | /* |
| 306 | * for some reason slightly less than 0x10000 produces |
| 307 | * a slightly more accurate distribution... probably a |
| 308 | * rounding effect. |
| 309 | * |
| 310 | * the natural log lookup table maps [0,0xffff] |
| 311 | * (corresponding to real numbers [1/0x10000, 1] to |
| 312 | * [0, 0xffffffffffff] (corresponding to real numbers |
| 313 | * [-11.090355,0]). |
| 314 | */ |
| 315 | ln = crush_ln(u) - 0x1000000000000ll; |
| 316 | |
| 317 | /* |
| 318 | * divide by 16.16 fixed-point weight. note |
| 319 | * that the ln value is negative, so a larger |
| 320 | * weight means a larger (less negative) value |
| 321 | * for draw. |
| 322 | */ |
| 323 | draw = div64_s64(ln, w); |
| 324 | } else { |
| 325 | draw = S64_MIN; |
| 326 | } |
| 327 | |
| 328 | if (i == 0 || draw > high_draw) { |
| 329 | high = i; |
| 330 | high_draw = draw; |
| 331 | } |
| 332 | } |
| 333 | return bucket->h.items[high]; |
| 334 | } |
| 335 | |
| 336 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 337 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) |
| 338 | { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 339 | 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] | 340 | BUG_ON(in->size == 0); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 341 | switch (in->alg) { |
| 342 | case CRUSH_BUCKET_UNIFORM: |
| 343 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, |
| 344 | x, r); |
| 345 | case CRUSH_BUCKET_LIST: |
| 346 | return bucket_list_choose((struct crush_bucket_list *)in, |
| 347 | x, r); |
| 348 | case CRUSH_BUCKET_TREE: |
| 349 | return bucket_tree_choose((struct crush_bucket_tree *)in, |
| 350 | x, r); |
| 351 | case CRUSH_BUCKET_STRAW: |
| 352 | return bucket_straw_choose((struct crush_bucket_straw *)in, |
| 353 | x, r); |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame^] | 354 | case CRUSH_BUCKET_STRAW2: |
| 355 | return bucket_straw2_choose((struct crush_bucket_straw2 *)in, |
| 356 | x, r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 357 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 358 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 359 | return in->items[0]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame^] | 363 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 364 | /* |
| 365 | * true if device is marked "out" (failed, fully offloaded) |
| 366 | * of the cluster |
| 367 | */ |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 368 | static int is_out(const struct crush_map *map, |
| 369 | const __u32 *weight, int weight_max, |
| 370 | int item, int x) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 371 | { |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 372 | if (item >= weight_max) |
| 373 | return 1; |
Sage Weil | 153a109 | 2010-07-05 09:44:17 -0700 | [diff] [blame] | 374 | if (weight[item] >= 0x10000) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 375 | return 0; |
| 376 | if (weight[item] == 0) |
| 377 | return 1; |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 378 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) |
| 379 | < weight[item]) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 385 | * crush_choose_firstn - choose numrep distinct items of given type |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 386 | * @map: the crush_map |
| 387 | * @bucket: the bucket we are choose an item from |
| 388 | * @x: crush input value |
| 389 | * @numrep: the number of items to choose |
| 390 | * @type: the type of item to choose |
| 391 | * @out: pointer to output vector |
| 392 | * @outpos: our position in that vector |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 393 | * @out_size: size of the out vector |
Ilya Dryomov | 0e32d71 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 394 | * @tries: number of attempts to make |
| 395 | * @recurse_tries: number of attempts to have recursive chooseleaf make |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 396 | * @local_retries: localized retries |
| 397 | * @local_fallback_retries: localized fallback retries |
Ilya Dryomov | 0e32d71 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 398 | * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 399 | * @vary_r: pass r to recursive calls |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 400 | * @out2: second output vector for leaf items (if @recurse_to_leaf) |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 401 | * @parent_r: r value passed from the parent |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 402 | */ |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 403 | static int crush_choose_firstn(const struct crush_map *map, |
| 404 | struct crush_bucket *bucket, |
| 405 | const __u32 *weight, int weight_max, |
| 406 | int x, int numrep, int type, |
| 407 | int *out, int outpos, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 408 | int out_size, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 409 | unsigned int tries, |
| 410 | unsigned int recurse_tries, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 411 | unsigned int local_retries, |
| 412 | unsigned int local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 413 | int recurse_to_leaf, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 414 | unsigned int vary_r, |
| 415 | int *out2, |
| 416 | int parent_r) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 417 | { |
| 418 | int rep; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 419 | unsigned int ftotal, flocal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 420 | int retry_descent, retry_bucket, skip_rep; |
| 421 | struct crush_bucket *in = bucket; |
| 422 | int r; |
| 423 | int i; |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 424 | int item = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 425 | int itemtype; |
| 426 | int collide, reject; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 427 | int count = out_size; |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 428 | |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 429 | dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d recurse_tries %d local_retries %d local_fallback_retries %d parent_r %d\n", |
| 430 | recurse_to_leaf ? "_LEAF" : "", |
| 431 | bucket->id, x, outpos, numrep, |
| 432 | tries, recurse_tries, local_retries, local_fallback_retries, |
| 433 | parent_r); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 434 | |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 435 | for (rep = outpos; rep < numrep && count > 0 ; rep++) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 436 | /* keep trying until we get a non-out, non-colliding item */ |
| 437 | ftotal = 0; |
| 438 | skip_rep = 0; |
| 439 | do { |
| 440 | retry_descent = 0; |
| 441 | in = bucket; /* initial bucket */ |
| 442 | |
| 443 | /* choose through intervening buckets */ |
| 444 | flocal = 0; |
| 445 | do { |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 446 | collide = 0; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 447 | retry_bucket = 0; |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 448 | r = rep + parent_r; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 449 | /* r' = r + f_total */ |
| 450 | r += ftotal; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 451 | |
| 452 | /* bucket choose */ |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 453 | if (in->size == 0) { |
| 454 | reject = 1; |
| 455 | goto reject; |
| 456 | } |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 457 | if (local_fallback_retries > 0 && |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 458 | flocal >= (in->size>>1) && |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 459 | flocal > local_fallback_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 460 | item = bucket_perm_choose(in, x, r); |
| 461 | else |
| 462 | item = crush_bucket_choose(in, x, r); |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 463 | if (item >= map->max_devices) { |
| 464 | dprintk(" bad item %d\n", item); |
| 465 | skip_rep = 1; |
| 466 | break; |
| 467 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 468 | |
| 469 | /* desired type? */ |
| 470 | if (item < 0) |
| 471 | itemtype = map->buckets[-1-item]->type; |
| 472 | else |
| 473 | itemtype = 0; |
| 474 | dprintk(" item %d type %d\n", item, itemtype); |
| 475 | |
| 476 | /* keep going? */ |
| 477 | if (itemtype != type) { |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 478 | if (item >= 0 || |
| 479 | (-1-item) >= map->max_buckets) { |
| 480 | dprintk(" bad item type %d\n", type); |
| 481 | skip_rep = 1; |
| 482 | break; |
| 483 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 484 | in = map->buckets[-1-item]; |
Sage Weil | 55bda7a | 2010-06-24 12:55:48 -0700 | [diff] [blame] | 485 | retry_bucket = 1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 486 | continue; |
| 487 | } |
| 488 | |
| 489 | /* collision? */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 490 | for (i = 0; i < outpos; i++) { |
| 491 | if (out[i] == item) { |
| 492 | collide = 1; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 497 | reject = 0; |
Sage Weil | 7d7c1f6 | 2013-01-15 18:49:09 -0800 | [diff] [blame] | 498 | if (!collide && recurse_to_leaf) { |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 499 | if (item < 0) { |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 500 | int sub_r; |
| 501 | if (vary_r) |
| 502 | sub_r = r >> (vary_r-1); |
| 503 | else |
| 504 | sub_r = 0; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 505 | if (crush_choose_firstn(map, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 506 | map->buckets[-1-item], |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 507 | weight, weight_max, |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 508 | x, outpos+1, 0, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 509 | out2, outpos, count, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 510 | recurse_tries, 0, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 511 | local_retries, |
| 512 | local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 513 | 0, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 514 | vary_r, |
| 515 | NULL, |
| 516 | sub_r) <= outpos) |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 517 | /* didn't get leaf */ |
| 518 | reject = 1; |
| 519 | } else { |
| 520 | /* we already have a leaf! */ |
| 521 | out2[outpos] = item; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (!reject) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 526 | /* out? */ |
| 527 | if (itemtype == 0) |
| 528 | reject = is_out(map, weight, |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 529 | weight_max, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 530 | item, x); |
| 531 | else |
| 532 | reject = 0; |
| 533 | } |
| 534 | |
Sage Weil | b28813a | 2009-10-07 10:59:34 -0700 | [diff] [blame] | 535 | reject: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 536 | if (reject || collide) { |
| 537 | ftotal++; |
| 538 | flocal++; |
| 539 | |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 540 | if (collide && flocal <= local_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 541 | /* retry locally a few times */ |
| 542 | retry_bucket = 1; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 543 | else if (local_fallback_retries > 0 && |
| 544 | flocal <= in->size + local_fallback_retries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 545 | /* exhaustive bucket search */ |
| 546 | retry_bucket = 1; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 547 | else if (ftotal < tries) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 548 | /* then retry descent */ |
| 549 | retry_descent = 1; |
| 550 | else |
| 551 | /* else give up */ |
| 552 | skip_rep = 1; |
| 553 | dprintk(" reject %d collide %d " |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 554 | "ftotal %u flocal %u\n", |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 555 | reject, collide, ftotal, |
| 556 | flocal); |
| 557 | } |
| 558 | } while (retry_bucket); |
| 559 | } while (retry_descent); |
| 560 | |
| 561 | if (skip_rep) { |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 562 | dprintk("skip rep\n"); |
| 563 | continue; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 566 | dprintk("CHOOSE got %d\n", item); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 567 | out[outpos] = item; |
| 568 | outpos++; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 569 | count--; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Sage Weil | a1a31e7 | 2010-06-24 12:58:14 -0700 | [diff] [blame] | 572 | dprintk("CHOOSE returns %d\n", outpos); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 573 | return outpos; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /** |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 578 | * crush_choose_indep: alternative breadth-first positionally stable mapping |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 579 | * |
| 580 | */ |
| 581 | static void crush_choose_indep(const struct crush_map *map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 582 | struct crush_bucket *bucket, |
| 583 | const __u32 *weight, int weight_max, |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 584 | int x, int left, int numrep, int type, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 585 | int *out, int outpos, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 586 | unsigned int tries, |
| 587 | unsigned int recurse_tries, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 588 | int recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 589 | int *out2, |
| 590 | int parent_r) |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 591 | { |
| 592 | struct crush_bucket *in = bucket; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 593 | int endpos = outpos + left; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 594 | int rep; |
| 595 | unsigned int ftotal; |
| 596 | int r; |
| 597 | int i; |
| 598 | int item = 0; |
| 599 | int itemtype; |
| 600 | int collide; |
| 601 | |
| 602 | dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", |
| 603 | bucket->id, x, outpos, numrep); |
| 604 | |
| 605 | /* initially my result is undefined */ |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 606 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 607 | out[rep] = CRUSH_ITEM_UNDEF; |
| 608 | if (out2) |
| 609 | out2[rep] = CRUSH_ITEM_UNDEF; |
| 610 | } |
| 611 | |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 612 | for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 613 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 614 | if (out[rep] != CRUSH_ITEM_UNDEF) |
| 615 | continue; |
| 616 | |
| 617 | in = bucket; /* initial bucket */ |
| 618 | |
| 619 | /* choose through intervening buckets */ |
| 620 | for (;;) { |
Ilya Dryomov | 3102b0a | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 621 | /* note: we base the choice on the position |
| 622 | * even in the nested call. that means that |
| 623 | * if the first layer chooses the same bucket |
| 624 | * in a different position, we will tend to |
| 625 | * choose a different item in that bucket. |
| 626 | * this will involve more devices in data |
| 627 | * movement and tend to distribute the load. |
| 628 | */ |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 629 | r = rep + parent_r; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 630 | |
| 631 | /* be careful */ |
| 632 | if (in->alg == CRUSH_BUCKET_UNIFORM && |
| 633 | in->size % numrep == 0) |
| 634 | /* r'=r+(n+1)*f_total */ |
| 635 | r += (numrep+1) * ftotal; |
| 636 | else |
| 637 | /* r' = r + n*f_total */ |
| 638 | r += numrep * ftotal; |
| 639 | |
| 640 | /* bucket choose */ |
| 641 | if (in->size == 0) { |
| 642 | dprintk(" empty bucket\n"); |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | item = crush_bucket_choose(in, x, r); |
| 647 | if (item >= map->max_devices) { |
| 648 | dprintk(" bad item %d\n", item); |
| 649 | out[rep] = CRUSH_ITEM_NONE; |
| 650 | if (out2) |
| 651 | out2[rep] = CRUSH_ITEM_NONE; |
| 652 | left--; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | /* desired type? */ |
| 657 | if (item < 0) |
| 658 | itemtype = map->buckets[-1-item]->type; |
| 659 | else |
| 660 | itemtype = 0; |
| 661 | dprintk(" item %d type %d\n", item, itemtype); |
| 662 | |
| 663 | /* keep going? */ |
| 664 | if (itemtype != type) { |
| 665 | if (item >= 0 || |
| 666 | (-1-item) >= map->max_buckets) { |
| 667 | dprintk(" bad item type %d\n", type); |
| 668 | out[rep] = CRUSH_ITEM_NONE; |
| 669 | if (out2) |
| 670 | out2[rep] = |
| 671 | CRUSH_ITEM_NONE; |
| 672 | left--; |
| 673 | break; |
| 674 | } |
| 675 | in = map->buckets[-1-item]; |
| 676 | continue; |
| 677 | } |
| 678 | |
| 679 | /* collision? */ |
| 680 | collide = 0; |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 681 | for (i = outpos; i < endpos; i++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 682 | if (out[i] == item) { |
| 683 | collide = 1; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | if (collide) |
| 688 | break; |
| 689 | |
| 690 | if (recurse_to_leaf) { |
| 691 | if (item < 0) { |
| 692 | crush_choose_indep(map, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 693 | map->buckets[-1-item], |
| 694 | weight, weight_max, |
| 695 | x, 1, numrep, 0, |
| 696 | out2, rep, |
Ilya Dryomov | 2d8be0b | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 697 | recurse_tries, 0, |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 698 | 0, NULL, r); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 699 | if (out2[rep] == CRUSH_ITEM_NONE) { |
| 700 | /* placed nothing; no leaf */ |
| 701 | break; |
| 702 | } |
| 703 | } else { |
| 704 | /* we already have a leaf! */ |
| 705 | out2[rep] = item; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* out? */ |
| 710 | if (itemtype == 0 && |
| 711 | is_out(map, weight, weight_max, item, x)) |
| 712 | break; |
| 713 | |
| 714 | /* yay! */ |
| 715 | out[rep] = item; |
| 716 | left--; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | } |
Ilya Dryomov | ab4ce2b5 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 721 | for (rep = outpos; rep < endpos; rep++) { |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 722 | if (out[rep] == CRUSH_ITEM_UNDEF) { |
| 723 | out[rep] = CRUSH_ITEM_NONE; |
| 724 | } |
| 725 | if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { |
| 726 | out2[rep] = CRUSH_ITEM_NONE; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /** |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 732 | * crush_do_rule - calculate a mapping with the given input and rule |
| 733 | * @map: the crush_map |
| 734 | * @ruleno: the rule id |
| 735 | * @x: hash input |
| 736 | * @result: pointer to result vector |
| 737 | * @result_max: maximum result size |
Ilya Dryomov | b3b33b0 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 738 | * @weight: weight vector (for map leaves) |
| 739 | * @weight_max: size of weight vector |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 740 | * @scratch: scratch vector for private use; must be >= 3 * result_max |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 741 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 742 | int crush_do_rule(const struct crush_map *map, |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 743 | int ruleno, int x, int *result, int result_max, |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 744 | const __u32 *weight, int weight_max, |
| 745 | int *scratch) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 746 | { |
| 747 | int result_len; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 748 | int *a = scratch; |
| 749 | int *b = scratch + result_max; |
| 750 | int *c = scratch + result_max*2; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 751 | int recurse_to_leaf; |
| 752 | int *w; |
| 753 | int wsize = 0; |
| 754 | int *o; |
| 755 | int osize; |
| 756 | int *tmp; |
| 757 | struct crush_rule *rule; |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 758 | __u32 step; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 759 | int i, j; |
| 760 | int numrep; |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 761 | int out_size; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 762 | /* |
| 763 | * the original choose_total_tries value was off by one (it |
| 764 | * counted "retries" and not "tries"). add one. |
| 765 | */ |
| 766 | int choose_tries = map->choose_total_tries + 1; |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 767 | int choose_leaf_tries = 0; |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 768 | /* |
| 769 | * the local tries values were counted as "retries", though, |
| 770 | * and need no adjustment |
| 771 | */ |
| 772 | int choose_local_retries = map->choose_local_tries; |
| 773 | int choose_local_fallback_retries = map->choose_local_fallback_tries; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 774 | |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 775 | int vary_r = map->chooseleaf_vary_r; |
| 776 | |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 777 | if ((__u32)ruleno >= map->max_rules) { |
| 778 | dprintk(" bad ruleno %d\n", ruleno); |
| 779 | return 0; |
| 780 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 781 | |
| 782 | rule = map->rules[ruleno]; |
| 783 | result_len = 0; |
| 784 | w = a; |
| 785 | o = b; |
| 786 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 787 | for (step = 0; step < rule->len; step++) { |
Ilya Dryomov | 8f99c85 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 788 | int firstn = 0; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 789 | struct crush_rule_step *curstep = &rule->steps[step]; |
| 790 | |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 791 | switch (curstep->op) { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 792 | case CRUSH_RULE_TAKE: |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 793 | w[0] = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 794 | wsize = 1; |
| 795 | break; |
| 796 | |
Ilya Dryomov | cc10df4 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 797 | case CRUSH_RULE_SET_CHOOSE_TRIES: |
| 798 | if (curstep->arg1 > 0) |
| 799 | choose_tries = curstep->arg1; |
| 800 | break; |
| 801 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 802 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: |
Ilya Dryomov | be3226a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 803 | if (curstep->arg1 > 0) |
| 804 | choose_leaf_tries = curstep->arg1; |
| 805 | break; |
| 806 | |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 807 | case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: |
Ilya Dryomov | 6ed1002 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 808 | if (curstep->arg1 >= 0) |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 809 | choose_local_retries = curstep->arg1; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: |
Ilya Dryomov | 6ed1002 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 813 | if (curstep->arg1 >= 0) |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 814 | choose_local_fallback_retries = curstep->arg1; |
Ilya Dryomov | f046bf9 | 2013-12-24 21:19:27 +0200 | [diff] [blame] | 815 | break; |
| 816 | |
Ilya Dryomov | d83ed85 | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 817 | case CRUSH_RULE_SET_CHOOSELEAF_VARY_R: |
| 818 | if (curstep->arg1 >= 0) |
| 819 | vary_r = curstep->arg1; |
| 820 | break; |
| 821 | |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 822 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 823 | case CRUSH_RULE_CHOOSE_FIRSTN: |
| 824 | firstn = 1; |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 825 | /* fall through */ |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 826 | case CRUSH_RULE_CHOOSELEAF_INDEP: |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 827 | case CRUSH_RULE_CHOOSE_INDEP: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 828 | if (wsize == 0) |
| 829 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 830 | |
| 831 | recurse_to_leaf = |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 832 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 833 | CRUSH_RULE_CHOOSELEAF_FIRSTN || |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 834 | curstep->op == |
Ilya Dryomov | 917edad | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 835 | CRUSH_RULE_CHOOSELEAF_INDEP; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 836 | |
| 837 | /* reset output */ |
| 838 | osize = 0; |
| 839 | |
| 840 | for (i = 0; i < wsize; i++) { |
| 841 | /* |
| 842 | * see CRUSH_N, CRUSH_N_MINUS macros. |
| 843 | * basically, numrep <= 0 means relative to |
| 844 | * the provided result_max |
| 845 | */ |
Sage Weil | 0668216 | 2012-05-07 15:35:48 -0700 | [diff] [blame] | 846 | numrep = curstep->arg1; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 847 | if (numrep <= 0) { |
| 848 | numrep += result_max; |
| 849 | if (numrep <= 0) |
| 850 | continue; |
| 851 | } |
| 852 | j = 0; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 853 | if (firstn) { |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 854 | int recurse_tries; |
| 855 | if (choose_leaf_tries) |
| 856 | recurse_tries = |
| 857 | choose_leaf_tries; |
| 858 | else if (map->chooseleaf_descend_once) |
| 859 | recurse_tries = 1; |
| 860 | else |
| 861 | recurse_tries = choose_tries; |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 862 | osize += crush_choose_firstn( |
| 863 | map, |
| 864 | map->buckets[-1-w[i]], |
| 865 | weight, weight_max, |
| 866 | x, numrep, |
| 867 | curstep->arg2, |
| 868 | o+osize, j, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 869 | result_max-osize, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 870 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 871 | recurse_tries, |
Ilya Dryomov | 48a163d | 2014-03-19 16:58:36 +0200 | [diff] [blame] | 872 | choose_local_retries, |
| 873 | choose_local_fallback_retries, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 874 | recurse_to_leaf, |
Ilya Dryomov | e2b149c | 2014-03-19 16:58:37 +0200 | [diff] [blame] | 875 | vary_r, |
| 876 | c+osize, |
| 877 | 0); |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 878 | } else { |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 879 | out_size = ((numrep < (result_max-osize)) ? |
| 880 | numrep : (result_max-osize)); |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 881 | crush_choose_indep( |
| 882 | map, |
| 883 | map->buckets[-1-w[i]], |
| 884 | weight, weight_max, |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 885 | x, out_size, numrep, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 886 | curstep->arg2, |
| 887 | o+osize, j, |
Ilya Dryomov | f18650a | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 888 | choose_tries, |
Ilya Dryomov | d390bb2 | 2013-12-24 21:19:26 +0200 | [diff] [blame] | 889 | choose_leaf_tries ? |
| 890 | choose_leaf_tries : 1, |
Ilya Dryomov | 9fe0718 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 891 | recurse_to_leaf, |
Ilya Dryomov | 4158608 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 892 | c+osize, |
| 893 | 0); |
Ilya Dryomov | 4500226 | 2015-04-14 16:04:23 +0300 | [diff] [blame] | 894 | osize += out_size; |
Ilya Dryomov | 9a3b490 | 2013-12-24 21:19:25 +0200 | [diff] [blame] | 895 | } |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | if (recurse_to_leaf) |
| 899 | /* copy final _leaf_ values to output set */ |
| 900 | memcpy(o, c, osize*sizeof(*o)); |
| 901 | |
Ilya Dryomov | 2a4ba74 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 902 | /* swap o and w arrays */ |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 903 | tmp = o; |
| 904 | o = w; |
| 905 | w = tmp; |
| 906 | wsize = osize; |
| 907 | break; |
| 908 | |
| 909 | |
| 910 | case CRUSH_RULE_EMIT: |
| 911 | for (i = 0; i < wsize && result_len < result_max; i++) { |
| 912 | result[result_len] = w[i]; |
| 913 | result_len++; |
| 914 | } |
| 915 | wsize = 0; |
| 916 | break; |
| 917 | |
| 918 | default: |
Sage Weil | a1f4895 | 2012-05-07 15:35:24 -0700 | [diff] [blame] | 919 | dprintk(" unknown op %d at step %d\n", |
| 920 | curstep->op, step); |
| 921 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
Sage Weil | f1932fc | 2011-12-07 09:10:26 -0800 | [diff] [blame] | 924 | return result_len; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | |