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