mapper.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #ifdef __KERNEL__
  2. # include <linux/string.h>
  3. # include <linux/slab.h>
  4. # include <linux/bug.h>
  5. # include <linux/kernel.h>
  6. # ifndef dprintk
  7. # define dprintk(args...)
  8. # endif
  9. #else
  10. # include <string.h>
  11. # include <stdio.h>
  12. # include <stdlib.h>
  13. # include <assert.h>
  14. # define BUG_ON(x) assert(!(x))
  15. # define dprintk(args...) /* printf(args) */
  16. # define kmalloc(x, f) malloc(x)
  17. # define kfree(x) free(x)
  18. #endif
  19. #include <linux/crush/crush.h>
  20. #include <linux/crush/hash.h>
  21. #include <linux/crush/mapper.h>
  22. /*
  23. * Implement the core CRUSH mapping algorithm.
  24. */
  25. /**
  26. * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
  27. * @map: the crush_map
  28. * @ruleset: the storage ruleset id (user defined)
  29. * @type: storage ruleset type (user defined)
  30. * @size: output set size
  31. */
  32. int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
  33. {
  34. __u32 i;
  35. for (i = 0; i < map->max_rules; i++) {
  36. if (map->rules[i] &&
  37. map->rules[i]->mask.ruleset == ruleset &&
  38. map->rules[i]->mask.type == type &&
  39. map->rules[i]->mask.min_size <= size &&
  40. map->rules[i]->mask.max_size >= size)
  41. return i;
  42. }
  43. return -1;
  44. }
  45. /*
  46. * bucket choose methods
  47. *
  48. * For each bucket algorithm, we have a "choose" method that, given a
  49. * crush input @x and replica position (usually, position in output set) @r,
  50. * will produce an item in the bucket.
  51. */
  52. /*
  53. * Choose based on a random permutation of the bucket.
  54. *
  55. * We used to use some prime number arithmetic to do this, but it
  56. * wasn't very random, and had some other bad behaviors. Instead, we
  57. * calculate an actual random permutation of the bucket members.
  58. * Since this is expensive, we optimize for the r=0 case, which
  59. * captures the vast majority of calls.
  60. */
  61. static int bucket_perm_choose(struct crush_bucket *bucket,
  62. int x, int r)
  63. {
  64. unsigned int pr = r % bucket->size;
  65. unsigned int i, s;
  66. /* start a new permutation if @x has changed */
  67. if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
  68. dprintk("bucket %d new x=%d\n", bucket->id, x);
  69. bucket->perm_x = x;
  70. /* optimize common r=0 case */
  71. if (pr == 0) {
  72. s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
  73. bucket->size;
  74. bucket->perm[0] = s;
  75. bucket->perm_n = 0xffff; /* magic value, see below */
  76. goto out;
  77. }
  78. for (i = 0; i < bucket->size; i++)
  79. bucket->perm[i] = i;
  80. bucket->perm_n = 0;
  81. } else if (bucket->perm_n == 0xffff) {
  82. /* clean up after the r=0 case above */
  83. for (i = 1; i < bucket->size; i++)
  84. bucket->perm[i] = i;
  85. bucket->perm[bucket->perm[0]] = 0;
  86. bucket->perm_n = 1;
  87. }
  88. /* calculate permutation up to pr */
  89. for (i = 0; i < bucket->perm_n; i++)
  90. dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
  91. while (bucket->perm_n <= pr) {
  92. unsigned int p = bucket->perm_n;
  93. /* no point in swapping the final entry */
  94. if (p < bucket->size - 1) {
  95. i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
  96. (bucket->size - p);
  97. if (i) {
  98. unsigned int t = bucket->perm[p + i];
  99. bucket->perm[p + i] = bucket->perm[p];
  100. bucket->perm[p] = t;
  101. }
  102. dprintk(" perm_choose swap %d with %d\n", p, p+i);
  103. }
  104. bucket->perm_n++;
  105. }
  106. for (i = 0; i < bucket->size; i++)
  107. dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
  108. s = bucket->perm[pr];
  109. out:
  110. dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
  111. bucket->size, x, r, pr, s);
  112. return bucket->items[s];
  113. }
  114. /* uniform */
  115. static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
  116. int x, int r)
  117. {
  118. return bucket_perm_choose(&bucket->h, x, r);
  119. }
  120. /* list */
  121. static int bucket_list_choose(struct crush_bucket_list *bucket,
  122. int x, int r)
  123. {
  124. int i;
  125. for (i = bucket->h.size-1; i >= 0; i--) {
  126. __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
  127. r, bucket->h.id);
  128. w &= 0xffff;
  129. dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
  130. "sw %x rand %llx",
  131. i, x, r, bucket->h.items[i], bucket->item_weights[i],
  132. bucket->sum_weights[i], w);
  133. w *= bucket->sum_weights[i];
  134. w = w >> 16;
  135. /*dprintk(" scaled %llx\n", w);*/
  136. if (w < bucket->item_weights[i])
  137. return bucket->h.items[i];
  138. }
  139. dprintk("bad list sums for bucket %d\n", bucket->h.id);
  140. return bucket->h.items[0];
  141. }
  142. /* (binary) tree */
  143. static int height(int n)
  144. {
  145. int h = 0;
  146. while ((n & 1) == 0) {
  147. h++;
  148. n = n >> 1;
  149. }
  150. return h;
  151. }
  152. static int left(int x)
  153. {
  154. int h = height(x);
  155. return x - (1 << (h-1));
  156. }
  157. static int right(int x)
  158. {
  159. int h = height(x);
  160. return x + (1 << (h-1));
  161. }
  162. static int terminal(int x)
  163. {
  164. return x & 1;
  165. }
  166. static int bucket_tree_choose(struct crush_bucket_tree *bucket,
  167. int x, int r)
  168. {
  169. int n;
  170. __u32 w;
  171. __u64 t;
  172. /* start at root */
  173. n = bucket->num_nodes >> 1;
  174. while (!terminal(n)) {
  175. int l;
  176. /* pick point in [0, w) */
  177. w = bucket->node_weights[n];
  178. t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
  179. bucket->h.id) * (__u64)w;
  180. t = t >> 32;
  181. /* descend to the left or right? */
  182. l = left(n);
  183. if (t < bucket->node_weights[l])
  184. n = l;
  185. else
  186. n = right(n);
  187. }
  188. return bucket->h.items[n >> 1];
  189. }
  190. /* straw */
  191. static int bucket_straw_choose(struct crush_bucket_straw *bucket,
  192. int x, int r)
  193. {
  194. __u32 i;
  195. int high = 0;
  196. __u64 high_draw = 0;
  197. __u64 draw;
  198. for (i = 0; i < bucket->h.size; i++) {
  199. draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
  200. draw &= 0xffff;
  201. draw *= bucket->straws[i];
  202. if (i == 0 || draw > high_draw) {
  203. high = i;
  204. high_draw = draw;
  205. }
  206. }
  207. return bucket->h.items[high];
  208. }
  209. static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
  210. {
  211. dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
  212. BUG_ON(in->size == 0);
  213. switch (in->alg) {
  214. case CRUSH_BUCKET_UNIFORM:
  215. return bucket_uniform_choose((struct crush_bucket_uniform *)in,
  216. x, r);
  217. case CRUSH_BUCKET_LIST:
  218. return bucket_list_choose((struct crush_bucket_list *)in,
  219. x, r);
  220. case CRUSH_BUCKET_TREE:
  221. return bucket_tree_choose((struct crush_bucket_tree *)in,
  222. x, r);
  223. case CRUSH_BUCKET_STRAW:
  224. return bucket_straw_choose((struct crush_bucket_straw *)in,
  225. x, r);
  226. default:
  227. dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
  228. return in->items[0];
  229. }
  230. }
  231. /*
  232. * true if device is marked "out" (failed, fully offloaded)
  233. * of the cluster
  234. */
  235. static int is_out(const struct crush_map *map,
  236. const __u32 *weight, int weight_max,
  237. int item, int x)
  238. {
  239. if (item >= weight_max)
  240. return 1;
  241. if (weight[item] >= 0x10000)
  242. return 0;
  243. if (weight[item] == 0)
  244. return 1;
  245. if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
  246. < weight[item])
  247. return 0;
  248. return 1;
  249. }
  250. /**
  251. * crush_choose_firstn - choose numrep distinct items of given type
  252. * @map: the crush_map
  253. * @bucket: the bucket we are choose an item from
  254. * @x: crush input value
  255. * @numrep: the number of items to choose
  256. * @type: the type of item to choose
  257. * @out: pointer to output vector
  258. * @outpos: our position in that vector
  259. * @tries: number of attempts to make
  260. * @recurse_tries: number of attempts to have recursive chooseleaf make
  261. * @local_tries: localized retries
  262. * @local_fallback_tries: localized fallback retries
  263. * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
  264. * @out2: second output vector for leaf items (if @recurse_to_leaf)
  265. */
  266. static int crush_choose_firstn(const struct crush_map *map,
  267. struct crush_bucket *bucket,
  268. const __u32 *weight, int weight_max,
  269. int x, int numrep, int type,
  270. int *out, int outpos,
  271. unsigned int tries,
  272. unsigned int recurse_tries,
  273. unsigned int local_tries,
  274. unsigned int local_fallback_tries,
  275. int recurse_to_leaf,
  276. int *out2)
  277. {
  278. int rep;
  279. unsigned int ftotal, flocal;
  280. int retry_descent, retry_bucket, skip_rep;
  281. struct crush_bucket *in = bucket;
  282. int r;
  283. int i;
  284. int item = 0;
  285. int itemtype;
  286. int collide, reject;
  287. dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
  288. bucket->id, x, outpos, numrep);
  289. for (rep = outpos; rep < numrep; rep++) {
  290. /* keep trying until we get a non-out, non-colliding item */
  291. ftotal = 0;
  292. skip_rep = 0;
  293. do {
  294. retry_descent = 0;
  295. in = bucket; /* initial bucket */
  296. /* choose through intervening buckets */
  297. flocal = 0;
  298. do {
  299. collide = 0;
  300. retry_bucket = 0;
  301. r = rep;
  302. /* r' = r + f_total */
  303. r += ftotal;
  304. /* bucket choose */
  305. if (in->size == 0) {
  306. reject = 1;
  307. goto reject;
  308. }
  309. if (local_fallback_tries > 0 &&
  310. flocal >= (in->size>>1) &&
  311. flocal > local_fallback_tries)
  312. item = bucket_perm_choose(in, x, r);
  313. else
  314. item = crush_bucket_choose(in, x, r);
  315. if (item >= map->max_devices) {
  316. dprintk(" bad item %d\n", item);
  317. skip_rep = 1;
  318. break;
  319. }
  320. /* desired type? */
  321. if (item < 0)
  322. itemtype = map->buckets[-1-item]->type;
  323. else
  324. itemtype = 0;
  325. dprintk(" item %d type %d\n", item, itemtype);
  326. /* keep going? */
  327. if (itemtype != type) {
  328. if (item >= 0 ||
  329. (-1-item) >= map->max_buckets) {
  330. dprintk(" bad item type %d\n", type);
  331. skip_rep = 1;
  332. break;
  333. }
  334. in = map->buckets[-1-item];
  335. retry_bucket = 1;
  336. continue;
  337. }
  338. /* collision? */
  339. for (i = 0; i < outpos; i++) {
  340. if (out[i] == item) {
  341. collide = 1;
  342. break;
  343. }
  344. }
  345. reject = 0;
  346. if (!collide && recurse_to_leaf) {
  347. if (item < 0) {
  348. if (crush_choose_firstn(map,
  349. map->buckets[-1-item],
  350. weight, weight_max,
  351. x, outpos+1, 0,
  352. out2, outpos,
  353. recurse_tries, 0,
  354. local_tries,
  355. local_fallback_tries,
  356. 0,
  357. NULL) <= outpos)
  358. /* didn't get leaf */
  359. reject = 1;
  360. } else {
  361. /* we already have a leaf! */
  362. out2[outpos] = item;
  363. }
  364. }
  365. if (!reject) {
  366. /* out? */
  367. if (itemtype == 0)
  368. reject = is_out(map, weight,
  369. weight_max,
  370. item, x);
  371. else
  372. reject = 0;
  373. }
  374. reject:
  375. if (reject || collide) {
  376. ftotal++;
  377. flocal++;
  378. if (collide && flocal <= local_tries)
  379. /* retry locally a few times */
  380. retry_bucket = 1;
  381. else if (local_fallback_tries > 0 &&
  382. flocal <= in->size + local_fallback_tries)
  383. /* exhaustive bucket search */
  384. retry_bucket = 1;
  385. else if (ftotal <= tries)
  386. /* then retry descent */
  387. retry_descent = 1;
  388. else
  389. /* else give up */
  390. skip_rep = 1;
  391. dprintk(" reject %d collide %d "
  392. "ftotal %u flocal %u\n",
  393. reject, collide, ftotal,
  394. flocal);
  395. }
  396. } while (retry_bucket);
  397. } while (retry_descent);
  398. if (skip_rep) {
  399. dprintk("skip rep\n");
  400. continue;
  401. }
  402. dprintk("CHOOSE got %d\n", item);
  403. out[outpos] = item;
  404. outpos++;
  405. }
  406. dprintk("CHOOSE returns %d\n", outpos);
  407. return outpos;
  408. }
  409. /**
  410. * crush_choose_indep: alternative breadth-first positionally stable mapping
  411. *
  412. */
  413. static void crush_choose_indep(const struct crush_map *map,
  414. struct crush_bucket *bucket,
  415. const __u32 *weight, int weight_max,
  416. int x, int left, int numrep, int type,
  417. int *out, int outpos,
  418. unsigned int tries,
  419. unsigned int recurse_tries,
  420. int recurse_to_leaf,
  421. int *out2,
  422. int parent_r)
  423. {
  424. struct crush_bucket *in = bucket;
  425. int endpos = outpos + left;
  426. int rep;
  427. unsigned int ftotal;
  428. int r;
  429. int i;
  430. int item = 0;
  431. int itemtype;
  432. int collide;
  433. dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
  434. bucket->id, x, outpos, numrep);
  435. /* initially my result is undefined */
  436. for (rep = outpos; rep < endpos; rep++) {
  437. out[rep] = CRUSH_ITEM_UNDEF;
  438. if (out2)
  439. out2[rep] = CRUSH_ITEM_UNDEF;
  440. }
  441. for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
  442. for (rep = outpos; rep < endpos; rep++) {
  443. if (out[rep] != CRUSH_ITEM_UNDEF)
  444. continue;
  445. in = bucket; /* initial bucket */
  446. /* choose through intervening buckets */
  447. for (;;) {
  448. /* note: we base the choice on the position
  449. * even in the nested call. that means that
  450. * if the first layer chooses the same bucket
  451. * in a different position, we will tend to
  452. * choose a different item in that bucket.
  453. * this will involve more devices in data
  454. * movement and tend to distribute the load.
  455. */
  456. r = rep + parent_r;
  457. /* be careful */
  458. if (in->alg == CRUSH_BUCKET_UNIFORM &&
  459. in->size % numrep == 0)
  460. /* r'=r+(n+1)*f_total */
  461. r += (numrep+1) * ftotal;
  462. else
  463. /* r' = r + n*f_total */
  464. r += numrep * ftotal;
  465. /* bucket choose */
  466. if (in->size == 0) {
  467. dprintk(" empty bucket\n");
  468. break;
  469. }
  470. item = crush_bucket_choose(in, x, r);
  471. if (item >= map->max_devices) {
  472. dprintk(" bad item %d\n", item);
  473. out[rep] = CRUSH_ITEM_NONE;
  474. if (out2)
  475. out2[rep] = CRUSH_ITEM_NONE;
  476. left--;
  477. break;
  478. }
  479. /* desired type? */
  480. if (item < 0)
  481. itemtype = map->buckets[-1-item]->type;
  482. else
  483. itemtype = 0;
  484. dprintk(" item %d type %d\n", item, itemtype);
  485. /* keep going? */
  486. if (itemtype != type) {
  487. if (item >= 0 ||
  488. (-1-item) >= map->max_buckets) {
  489. dprintk(" bad item type %d\n", type);
  490. out[rep] = CRUSH_ITEM_NONE;
  491. if (out2)
  492. out2[rep] =
  493. CRUSH_ITEM_NONE;
  494. left--;
  495. break;
  496. }
  497. in = map->buckets[-1-item];
  498. continue;
  499. }
  500. /* collision? */
  501. collide = 0;
  502. for (i = outpos; i < endpos; i++) {
  503. if (out[i] == item) {
  504. collide = 1;
  505. break;
  506. }
  507. }
  508. if (collide)
  509. break;
  510. if (recurse_to_leaf) {
  511. if (item < 0) {
  512. crush_choose_indep(map,
  513. map->buckets[-1-item],
  514. weight, weight_max,
  515. x, 1, numrep, 0,
  516. out2, rep,
  517. recurse_tries, 0,
  518. 0, NULL, r);
  519. if (out2[rep] == CRUSH_ITEM_NONE) {
  520. /* placed nothing; no leaf */
  521. break;
  522. }
  523. } else {
  524. /* we already have a leaf! */
  525. out2[rep] = item;
  526. }
  527. }
  528. /* out? */
  529. if (itemtype == 0 &&
  530. is_out(map, weight, weight_max, item, x))
  531. break;
  532. /* yay! */
  533. out[rep] = item;
  534. left--;
  535. break;
  536. }
  537. }
  538. }
  539. for (rep = outpos; rep < endpos; rep++) {
  540. if (out[rep] == CRUSH_ITEM_UNDEF) {
  541. out[rep] = CRUSH_ITEM_NONE;
  542. }
  543. if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
  544. out2[rep] = CRUSH_ITEM_NONE;
  545. }
  546. }
  547. }
  548. /**
  549. * crush_do_rule - calculate a mapping with the given input and rule
  550. * @map: the crush_map
  551. * @ruleno: the rule id
  552. * @x: hash input
  553. * @result: pointer to result vector
  554. * @result_max: maximum result size
  555. * @weight: weight vector (for map leaves)
  556. * @weight_max: size of weight vector
  557. * @scratch: scratch vector for private use; must be >= 3 * result_max
  558. */
  559. int crush_do_rule(const struct crush_map *map,
  560. int ruleno, int x, int *result, int result_max,
  561. const __u32 *weight, int weight_max,
  562. int *scratch)
  563. {
  564. int result_len;
  565. int *a = scratch;
  566. int *b = scratch + result_max;
  567. int *c = scratch + result_max*2;
  568. int recurse_to_leaf;
  569. int *w;
  570. int wsize = 0;
  571. int *o;
  572. int osize;
  573. int *tmp;
  574. struct crush_rule *rule;
  575. __u32 step;
  576. int i, j;
  577. int numrep;
  578. int choose_tries = map->choose_total_tries;
  579. int choose_local_tries = map->choose_local_tries;
  580. int choose_local_fallback_tries = map->choose_local_fallback_tries;
  581. int choose_leaf_tries = 0;
  582. if ((__u32)ruleno >= map->max_rules) {
  583. dprintk(" bad ruleno %d\n", ruleno);
  584. return 0;
  585. }
  586. rule = map->rules[ruleno];
  587. result_len = 0;
  588. w = a;
  589. o = b;
  590. for (step = 0; step < rule->len; step++) {
  591. int firstn = 0;
  592. struct crush_rule_step *curstep = &rule->steps[step];
  593. switch (curstep->op) {
  594. case CRUSH_RULE_TAKE:
  595. w[0] = curstep->arg1;
  596. wsize = 1;
  597. break;
  598. case CRUSH_RULE_SET_CHOOSE_TRIES:
  599. if (curstep->arg1 > 0)
  600. choose_tries = curstep->arg1;
  601. break;
  602. case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
  603. if (curstep->arg1 > 0)
  604. choose_leaf_tries = curstep->arg1;
  605. break;
  606. case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
  607. if (curstep->arg1 > 0)
  608. choose_local_tries = curstep->arg1;
  609. break;
  610. case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
  611. if (curstep->arg1 > 0)
  612. choose_local_fallback_tries = curstep->arg1;
  613. break;
  614. case CRUSH_RULE_CHOOSELEAF_FIRSTN:
  615. case CRUSH_RULE_CHOOSE_FIRSTN:
  616. firstn = 1;
  617. /* fall through */
  618. case CRUSH_RULE_CHOOSELEAF_INDEP:
  619. case CRUSH_RULE_CHOOSE_INDEP:
  620. if (wsize == 0)
  621. break;
  622. recurse_to_leaf =
  623. curstep->op ==
  624. CRUSH_RULE_CHOOSELEAF_FIRSTN ||
  625. curstep->op ==
  626. CRUSH_RULE_CHOOSELEAF_INDEP;
  627. /* reset output */
  628. osize = 0;
  629. for (i = 0; i < wsize; i++) {
  630. /*
  631. * see CRUSH_N, CRUSH_N_MINUS macros.
  632. * basically, numrep <= 0 means relative to
  633. * the provided result_max
  634. */
  635. numrep = curstep->arg1;
  636. if (numrep <= 0) {
  637. numrep += result_max;
  638. if (numrep <= 0)
  639. continue;
  640. }
  641. j = 0;
  642. if (firstn) {
  643. int recurse_tries;
  644. if (choose_leaf_tries)
  645. recurse_tries =
  646. choose_leaf_tries;
  647. else if (map->chooseleaf_descend_once)
  648. recurse_tries = 1;
  649. else
  650. recurse_tries = choose_tries;
  651. osize += crush_choose_firstn(
  652. map,
  653. map->buckets[-1-w[i]],
  654. weight, weight_max,
  655. x, numrep,
  656. curstep->arg2,
  657. o+osize, j,
  658. choose_tries,
  659. recurse_tries,
  660. choose_local_tries,
  661. choose_local_fallback_tries,
  662. recurse_to_leaf,
  663. c+osize);
  664. } else {
  665. crush_choose_indep(
  666. map,
  667. map->buckets[-1-w[i]],
  668. weight, weight_max,
  669. x, numrep, numrep,
  670. curstep->arg2,
  671. o+osize, j,
  672. choose_tries,
  673. choose_leaf_tries ?
  674. choose_leaf_tries : 1,
  675. recurse_to_leaf,
  676. c+osize,
  677. 0);
  678. osize += numrep;
  679. }
  680. }
  681. if (recurse_to_leaf)
  682. /* copy final _leaf_ values to output set */
  683. memcpy(o, c, osize*sizeof(*o));
  684. /* swap o and w arrays */
  685. tmp = o;
  686. o = w;
  687. w = tmp;
  688. wsize = osize;
  689. break;
  690. case CRUSH_RULE_EMIT:
  691. for (i = 0; i < wsize && result_len < result_max; i++) {
  692. result[result_len] = w[i];
  693. result_len++;
  694. }
  695. wsize = 0;
  696. break;
  697. default:
  698. dprintk(" unknown op %d at step %d\n",
  699. curstep->op, step);
  700. break;
  701. }
  702. }
  703. return result_len;
  704. }