mapper.c 21 KB

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