mapper.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * Ceph - scalable distributed file system
  3. *
  4. * Copyright (C) 2015 Intel Corporation All Rights Reserved
  5. *
  6. * This is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License version 2.1, as published by the Free Software
  9. * Foundation. See file COPYING.
  10. *
  11. */
  12. #ifdef __KERNEL__
  13. # include <linux/string.h>
  14. # include <linux/slab.h>
  15. # include <linux/bug.h>
  16. # include <linux/kernel.h>
  17. # include <linux/crush/crush.h>
  18. # include <linux/crush/hash.h>
  19. #else
  20. # include "crush_compat.h"
  21. # include "crush.h"
  22. # include "hash.h"
  23. #endif
  24. #include "crush_ln_table.h"
  25. #define dprintk(args...) /* printf(args) */
  26. /*
  27. * Implement the core CRUSH mapping algorithm.
  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. */
  36. int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
  37. {
  38. __u32 i;
  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. * bucket choose methods
  51. *
  52. * For each bucket algorithm, we have a "choose" method that, given a
  53. * crush input @x and replica position (usually, position in output set) @r,
  54. * will produce an item in the bucket.
  55. */
  56. /*
  57. * Choose based on a random permutation of the bucket.
  58. *
  59. * We used to use some prime number arithmetic to do this, but it
  60. * wasn't very random, and had some other bad behaviors. Instead, we
  61. * calculate an actual random permutation of the bucket members.
  62. * Since this is expensive, we optimize for the r=0 case, which
  63. * captures the vast majority of calls.
  64. */
  65. static int bucket_perm_choose(struct crush_bucket *bucket,
  66. int x, int r)
  67. {
  68. unsigned int pr = r % bucket->size;
  69. unsigned int i, s;
  70. /* start a new permutation if @x has changed */
  71. if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
  72. dprintk("bucket %d new x=%d\n", bucket->id, x);
  73. bucket->perm_x = x;
  74. /* optimize common r=0 case */
  75. if (pr == 0) {
  76. s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
  77. bucket->size;
  78. bucket->perm[0] = s;
  79. bucket->perm_n = 0xffff; /* magic value, see below */
  80. goto out;
  81. }
  82. for (i = 0; i < bucket->size; i++)
  83. bucket->perm[i] = i;
  84. bucket->perm_n = 0;
  85. } else if (bucket->perm_n == 0xffff) {
  86. /* clean up after the r=0 case above */
  87. for (i = 1; i < bucket->size; i++)
  88. bucket->perm[i] = i;
  89. bucket->perm[bucket->perm[0]] = 0;
  90. bucket->perm_n = 1;
  91. }
  92. /* calculate permutation up to pr */
  93. for (i = 0; i < bucket->perm_n; i++)
  94. dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
  95. while (bucket->perm_n <= pr) {
  96. unsigned int p = bucket->perm_n;
  97. /* no point in swapping the final entry */
  98. if (p < bucket->size - 1) {
  99. i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
  100. (bucket->size - p);
  101. if (i) {
  102. unsigned int t = bucket->perm[p + i];
  103. bucket->perm[p + i] = bucket->perm[p];
  104. bucket->perm[p] = t;
  105. }
  106. dprintk(" perm_choose swap %d with %d\n", p, p+i);
  107. }
  108. bucket->perm_n++;
  109. }
  110. for (i = 0; i < bucket->size; i++)
  111. dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
  112. s = bucket->perm[pr];
  113. out:
  114. dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
  115. bucket->size, x, r, pr, s);
  116. return bucket->items[s];
  117. }
  118. /* uniform */
  119. static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
  120. int x, int r)
  121. {
  122. return bucket_perm_choose(&bucket->h, x, r);
  123. }
  124. /* list */
  125. static int bucket_list_choose(struct crush_bucket_list *bucket,
  126. int x, int r)
  127. {
  128. int i;
  129. for (i = bucket->h.size-1; i >= 0; i--) {
  130. __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
  131. r, bucket->h.id);
  132. w &= 0xffff;
  133. dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
  134. "sw %x rand %llx",
  135. i, x, r, bucket->h.items[i], bucket->item_weights[i],
  136. bucket->sum_weights[i], w);
  137. w *= bucket->sum_weights[i];
  138. w = w >> 16;
  139. /*dprintk(" scaled %llx\n", w);*/
  140. if (w < bucket->item_weights[i])
  141. return bucket->h.items[i];
  142. }
  143. dprintk("bad list sums for bucket %d\n", bucket->h.id);
  144. return bucket->h.items[0];
  145. }
  146. /* (binary) tree */
  147. static int height(int n)
  148. {
  149. int h = 0;
  150. while ((n & 1) == 0) {
  151. h++;
  152. n = n >> 1;
  153. }
  154. return h;
  155. }
  156. static int left(int x)
  157. {
  158. int h = height(x);
  159. return x - (1 << (h-1));
  160. }
  161. static int right(int x)
  162. {
  163. int h = height(x);
  164. return x + (1 << (h-1));
  165. }
  166. static int terminal(int x)
  167. {
  168. return x & 1;
  169. }
  170. static int bucket_tree_choose(struct crush_bucket_tree *bucket,
  171. int x, int r)
  172. {
  173. int n;
  174. __u32 w;
  175. __u64 t;
  176. /* start at root */
  177. n = bucket->num_nodes >> 1;
  178. while (!terminal(n)) {
  179. int l;
  180. /* pick point in [0, w) */
  181. w = bucket->node_weights[n];
  182. t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
  183. bucket->h.id) * (__u64)w;
  184. t = t >> 32;
  185. /* descend to the left or right? */
  186. l = left(n);
  187. if (t < bucket->node_weights[l])
  188. n = l;
  189. else
  190. n = right(n);
  191. }
  192. return bucket->h.items[n >> 1];
  193. }
  194. /* straw */
  195. static int bucket_straw_choose(struct crush_bucket_straw *bucket,
  196. int x, int r)
  197. {
  198. __u32 i;
  199. int high = 0;
  200. __u64 high_draw = 0;
  201. __u64 draw;
  202. for (i = 0; i < bucket->h.size; i++) {
  203. draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
  204. draw &= 0xffff;
  205. draw *= bucket->straws[i];
  206. if (i == 0 || draw > high_draw) {
  207. high = i;
  208. high_draw = draw;
  209. }
  210. }
  211. return bucket->h.items[high];
  212. }
  213. /* compute 2^44*log2(input+1) */
  214. static __u64 crush_ln(unsigned int xin)
  215. {
  216. unsigned int x = xin, x1;
  217. int iexpon, index1, index2;
  218. __u64 RH, LH, LL, xl64, result;
  219. x++;
  220. /* normalize input */
  221. iexpon = 15;
  222. while (!(x & 0x18000)) {
  223. x <<= 1;
  224. iexpon--;
  225. }
  226. index1 = (x >> 8) << 1;
  227. /* RH ~ 2^56/index1 */
  228. RH = __RH_LH_tbl[index1 - 256];
  229. /* LH ~ 2^48 * log2(index1/256) */
  230. LH = __RH_LH_tbl[index1 + 1 - 256];
  231. /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
  232. xl64 = (__s64)x * RH;
  233. xl64 >>= 48;
  234. x1 = xl64;
  235. result = iexpon;
  236. result <<= (12 + 32);
  237. index2 = x1 & 0xff;
  238. /* LL ~ 2^48*log2(1.0+index2/2^15) */
  239. LL = __LL_tbl[index2];
  240. LH = LH + LL;
  241. LH >>= (48 - 12 - 32);
  242. result += LH;
  243. return result;
  244. }
  245. /*
  246. * straw2
  247. *
  248. * for reference, see:
  249. *
  250. * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
  251. *
  252. */
  253. static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket,
  254. int x, int r)
  255. {
  256. unsigned int i, high = 0;
  257. unsigned int u;
  258. unsigned int w;
  259. __s64 ln, draw, high_draw = 0;
  260. for (i = 0; i < bucket->h.size; i++) {
  261. w = bucket->item_weights[i];
  262. if (w) {
  263. u = crush_hash32_3(bucket->h.hash, x,
  264. bucket->h.items[i], r);
  265. u &= 0xffff;
  266. /*
  267. * for some reason slightly less than 0x10000 produces
  268. * a slightly more accurate distribution... probably a
  269. * rounding effect.
  270. *
  271. * the natural log lookup table maps [0,0xffff]
  272. * (corresponding to real numbers [1/0x10000, 1] to
  273. * [0, 0xffffffffffff] (corresponding to real numbers
  274. * [-11.090355,0]).
  275. */
  276. ln = crush_ln(u) - 0x1000000000000ll;
  277. /*
  278. * divide by 16.16 fixed-point weight. note
  279. * that the ln value is negative, so a larger
  280. * weight means a larger (less negative) value
  281. * for draw.
  282. */
  283. draw = div64_s64(ln, w);
  284. } else {
  285. draw = S64_MIN;
  286. }
  287. if (i == 0 || draw > high_draw) {
  288. high = i;
  289. high_draw = draw;
  290. }
  291. }
  292. return bucket->h.items[high];
  293. }
  294. static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
  295. {
  296. dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
  297. BUG_ON(in->size == 0);
  298. switch (in->alg) {
  299. case CRUSH_BUCKET_UNIFORM:
  300. return bucket_uniform_choose((struct crush_bucket_uniform *)in,
  301. x, r);
  302. case CRUSH_BUCKET_LIST:
  303. return bucket_list_choose((struct crush_bucket_list *)in,
  304. x, r);
  305. case CRUSH_BUCKET_TREE:
  306. return bucket_tree_choose((struct crush_bucket_tree *)in,
  307. x, r);
  308. case CRUSH_BUCKET_STRAW:
  309. return bucket_straw_choose((struct crush_bucket_straw *)in,
  310. x, r);
  311. case CRUSH_BUCKET_STRAW2:
  312. return bucket_straw2_choose((struct crush_bucket_straw2 *)in,
  313. x, r);
  314. default:
  315. dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
  316. return in->items[0];
  317. }
  318. }
  319. /*
  320. * true if device is marked "out" (failed, fully offloaded)
  321. * of the cluster
  322. */
  323. static int is_out(const struct crush_map *map,
  324. const __u32 *weight, int weight_max,
  325. int item, int x)
  326. {
  327. if (item >= weight_max)
  328. return 1;
  329. if (weight[item] >= 0x10000)
  330. return 0;
  331. if (weight[item] == 0)
  332. return 1;
  333. if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
  334. < weight[item])
  335. return 0;
  336. return 1;
  337. }
  338. /**
  339. * crush_choose_firstn - choose numrep distinct items of given type
  340. * @map: the crush_map
  341. * @bucket: the bucket we are choose an item from
  342. * @x: crush input value
  343. * @numrep: the number of items to choose
  344. * @type: the type of item to choose
  345. * @out: pointer to output vector
  346. * @outpos: our position in that vector
  347. * @out_size: size of the out vector
  348. * @tries: number of attempts to make
  349. * @recurse_tries: number of attempts to have recursive chooseleaf make
  350. * @local_retries: localized retries
  351. * @local_fallback_retries: localized fallback retries
  352. * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
  353. * @vary_r: pass r to recursive calls
  354. * @out2: second output vector for leaf items (if @recurse_to_leaf)
  355. * @parent_r: r value passed from the parent
  356. */
  357. static int crush_choose_firstn(const struct crush_map *map,
  358. struct crush_bucket *bucket,
  359. const __u32 *weight, int weight_max,
  360. int x, int numrep, int type,
  361. int *out, int outpos,
  362. int out_size,
  363. unsigned int tries,
  364. unsigned int recurse_tries,
  365. unsigned int local_retries,
  366. unsigned int local_fallback_retries,
  367. int recurse_to_leaf,
  368. unsigned int vary_r,
  369. int *out2,
  370. int parent_r)
  371. {
  372. int rep;
  373. unsigned int ftotal, flocal;
  374. int retry_descent, retry_bucket, skip_rep;
  375. struct crush_bucket *in = bucket;
  376. int r;
  377. int i;
  378. int item = 0;
  379. int itemtype;
  380. int collide, reject;
  381. int count = out_size;
  382. 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",
  383. recurse_to_leaf ? "_LEAF" : "",
  384. bucket->id, x, outpos, numrep,
  385. tries, recurse_tries, local_retries, local_fallback_retries,
  386. parent_r);
  387. for (rep = outpos; rep < numrep && count > 0 ; rep++) {
  388. /* keep trying until we get a non-out, non-colliding item */
  389. ftotal = 0;
  390. skip_rep = 0;
  391. do {
  392. retry_descent = 0;
  393. in = bucket; /* initial bucket */
  394. /* choose through intervening buckets */
  395. flocal = 0;
  396. do {
  397. collide = 0;
  398. retry_bucket = 0;
  399. r = rep + parent_r;
  400. /* r' = r + f_total */
  401. r += ftotal;
  402. /* bucket choose */
  403. if (in->size == 0) {
  404. reject = 1;
  405. goto reject;
  406. }
  407. if (local_fallback_retries > 0 &&
  408. flocal >= (in->size>>1) &&
  409. flocal > local_fallback_retries)
  410. item = bucket_perm_choose(in, x, r);
  411. else
  412. item = crush_bucket_choose(in, x, r);
  413. if (item >= map->max_devices) {
  414. dprintk(" bad item %d\n", item);
  415. skip_rep = 1;
  416. break;
  417. }
  418. /* desired type? */
  419. if (item < 0)
  420. itemtype = map->buckets[-1-item]->type;
  421. else
  422. itemtype = 0;
  423. dprintk(" item %d type %d\n", item, itemtype);
  424. /* keep going? */
  425. if (itemtype != type) {
  426. if (item >= 0 ||
  427. (-1-item) >= map->max_buckets) {
  428. dprintk(" bad item type %d\n", type);
  429. skip_rep = 1;
  430. break;
  431. }
  432. in = map->buckets[-1-item];
  433. retry_bucket = 1;
  434. continue;
  435. }
  436. /* collision? */
  437. for (i = 0; i < outpos; i++) {
  438. if (out[i] == item) {
  439. collide = 1;
  440. break;
  441. }
  442. }
  443. reject = 0;
  444. if (!collide && recurse_to_leaf) {
  445. if (item < 0) {
  446. int sub_r;
  447. if (vary_r)
  448. sub_r = r >> (vary_r-1);
  449. else
  450. sub_r = 0;
  451. if (crush_choose_firstn(map,
  452. map->buckets[-1-item],
  453. weight, weight_max,
  454. x, outpos+1, 0,
  455. out2, outpos, count,
  456. recurse_tries, 0,
  457. local_retries,
  458. local_fallback_retries,
  459. 0,
  460. vary_r,
  461. NULL,
  462. sub_r) <= outpos)
  463. /* didn't get leaf */
  464. reject = 1;
  465. } else {
  466. /* we already have a leaf! */
  467. out2[outpos] = item;
  468. }
  469. }
  470. if (!reject) {
  471. /* out? */
  472. if (itemtype == 0)
  473. reject = is_out(map, weight,
  474. weight_max,
  475. item, x);
  476. else
  477. reject = 0;
  478. }
  479. reject:
  480. if (reject || collide) {
  481. ftotal++;
  482. flocal++;
  483. if (collide && flocal <= local_retries)
  484. /* retry locally a few times */
  485. retry_bucket = 1;
  486. else if (local_fallback_retries > 0 &&
  487. flocal <= in->size + local_fallback_retries)
  488. /* exhaustive bucket search */
  489. retry_bucket = 1;
  490. else if (ftotal < tries)
  491. /* then retry descent */
  492. retry_descent = 1;
  493. else
  494. /* else give up */
  495. skip_rep = 1;
  496. dprintk(" reject %d collide %d "
  497. "ftotal %u flocal %u\n",
  498. reject, collide, ftotal,
  499. flocal);
  500. }
  501. } while (retry_bucket);
  502. } while (retry_descent);
  503. if (skip_rep) {
  504. dprintk("skip rep\n");
  505. continue;
  506. }
  507. dprintk("CHOOSE got %d\n", item);
  508. out[outpos] = item;
  509. outpos++;
  510. count--;
  511. #ifndef __KERNEL__
  512. if (map->choose_tries && ftotal <= map->choose_total_tries)
  513. map->choose_tries[ftotal]++;
  514. #endif
  515. }
  516. dprintk("CHOOSE returns %d\n", outpos);
  517. return outpos;
  518. }
  519. /**
  520. * crush_choose_indep: alternative breadth-first positionally stable mapping
  521. *
  522. */
  523. static void crush_choose_indep(const struct crush_map *map,
  524. struct crush_bucket *bucket,
  525. const __u32 *weight, int weight_max,
  526. int x, int left, int numrep, int type,
  527. int *out, int outpos,
  528. unsigned int tries,
  529. unsigned int recurse_tries,
  530. int recurse_to_leaf,
  531. int *out2,
  532. int parent_r)
  533. {
  534. struct crush_bucket *in = bucket;
  535. int endpos = outpos + left;
  536. int rep;
  537. unsigned int ftotal;
  538. int r;
  539. int i;
  540. int item = 0;
  541. int itemtype;
  542. int collide;
  543. dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
  544. bucket->id, x, outpos, numrep);
  545. /* initially my result is undefined */
  546. for (rep = outpos; rep < endpos; rep++) {
  547. out[rep] = CRUSH_ITEM_UNDEF;
  548. if (out2)
  549. out2[rep] = CRUSH_ITEM_UNDEF;
  550. }
  551. for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
  552. #ifdef DEBUG_INDEP
  553. if (out2 && ftotal) {
  554. dprintk("%u %d a: ", ftotal, left);
  555. for (rep = outpos; rep < endpos; rep++) {
  556. dprintk(" %d", out[rep]);
  557. }
  558. dprintk("\n");
  559. dprintk("%u %d b: ", ftotal, left);
  560. for (rep = outpos; rep < endpos; rep++) {
  561. dprintk(" %d", out2[rep]);
  562. }
  563. dprintk("\n");
  564. }
  565. #endif
  566. for (rep = outpos; rep < endpos; rep++) {
  567. if (out[rep] != CRUSH_ITEM_UNDEF)
  568. continue;
  569. in = bucket; /* initial bucket */
  570. /* choose through intervening buckets */
  571. for (;;) {
  572. /* note: we base the choice on the position
  573. * even in the nested call. that means that
  574. * if the first layer chooses the same bucket
  575. * in a different position, we will tend to
  576. * choose a different item in that bucket.
  577. * this will involve more devices in data
  578. * movement and tend to distribute the load.
  579. */
  580. r = rep + parent_r;
  581. /* be careful */
  582. if (in->alg == CRUSH_BUCKET_UNIFORM &&
  583. in->size % numrep == 0)
  584. /* r'=r+(n+1)*f_total */
  585. r += (numrep+1) * ftotal;
  586. else
  587. /* r' = r + n*f_total */
  588. r += numrep * ftotal;
  589. /* bucket choose */
  590. if (in->size == 0) {
  591. dprintk(" empty bucket\n");
  592. break;
  593. }
  594. item = crush_bucket_choose(in, x, r);
  595. if (item >= map->max_devices) {
  596. dprintk(" bad item %d\n", item);
  597. out[rep] = CRUSH_ITEM_NONE;
  598. if (out2)
  599. out2[rep] = CRUSH_ITEM_NONE;
  600. left--;
  601. break;
  602. }
  603. /* desired type? */
  604. if (item < 0)
  605. itemtype = map->buckets[-1-item]->type;
  606. else
  607. itemtype = 0;
  608. dprintk(" item %d type %d\n", item, itemtype);
  609. /* keep going? */
  610. if (itemtype != type) {
  611. if (item >= 0 ||
  612. (-1-item) >= map->max_buckets) {
  613. dprintk(" bad item type %d\n", type);
  614. out[rep] = CRUSH_ITEM_NONE;
  615. if (out2)
  616. out2[rep] =
  617. CRUSH_ITEM_NONE;
  618. left--;
  619. break;
  620. }
  621. in = map->buckets[-1-item];
  622. continue;
  623. }
  624. /* collision? */
  625. collide = 0;
  626. for (i = outpos; i < endpos; i++) {
  627. if (out[i] == item) {
  628. collide = 1;
  629. break;
  630. }
  631. }
  632. if (collide)
  633. break;
  634. if (recurse_to_leaf) {
  635. if (item < 0) {
  636. crush_choose_indep(map,
  637. map->buckets[-1-item],
  638. weight, weight_max,
  639. x, 1, numrep, 0,
  640. out2, rep,
  641. recurse_tries, 0,
  642. 0, NULL, r);
  643. if (out2[rep] == CRUSH_ITEM_NONE) {
  644. /* placed nothing; no leaf */
  645. break;
  646. }
  647. } else {
  648. /* we already have a leaf! */
  649. out2[rep] = item;
  650. }
  651. }
  652. /* out? */
  653. if (itemtype == 0 &&
  654. is_out(map, weight, weight_max, item, x))
  655. break;
  656. /* yay! */
  657. out[rep] = item;
  658. left--;
  659. break;
  660. }
  661. }
  662. }
  663. for (rep = outpos; rep < endpos; rep++) {
  664. if (out[rep] == CRUSH_ITEM_UNDEF) {
  665. out[rep] = CRUSH_ITEM_NONE;
  666. }
  667. if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
  668. out2[rep] = CRUSH_ITEM_NONE;
  669. }
  670. }
  671. #ifndef __KERNEL__
  672. if (map->choose_tries && ftotal <= map->choose_total_tries)
  673. map->choose_tries[ftotal]++;
  674. #endif
  675. #ifdef DEBUG_INDEP
  676. if (out2) {
  677. dprintk("%u %d a: ", ftotal, left);
  678. for (rep = outpos; rep < endpos; rep++) {
  679. dprintk(" %d", out[rep]);
  680. }
  681. dprintk("\n");
  682. dprintk("%u %d b: ", ftotal, left);
  683. for (rep = outpos; rep < endpos; rep++) {
  684. dprintk(" %d", out2[rep]);
  685. }
  686. dprintk("\n");
  687. }
  688. #endif
  689. }
  690. /**
  691. * crush_do_rule - calculate a mapping with the given input and rule
  692. * @map: the crush_map
  693. * @ruleno: the rule id
  694. * @x: hash input
  695. * @result: pointer to result vector
  696. * @result_max: maximum result size
  697. * @weight: weight vector (for map leaves)
  698. * @weight_max: size of weight vector
  699. * @scratch: scratch vector for private use; must be >= 3 * result_max
  700. */
  701. int crush_do_rule(const struct crush_map *map,
  702. int ruleno, int x, int *result, int result_max,
  703. const __u32 *weight, int weight_max,
  704. int *scratch)
  705. {
  706. int result_len;
  707. int *a = scratch;
  708. int *b = scratch + result_max;
  709. int *c = scratch + result_max*2;
  710. int recurse_to_leaf;
  711. int *w;
  712. int wsize = 0;
  713. int *o;
  714. int osize;
  715. int *tmp;
  716. struct crush_rule *rule;
  717. __u32 step;
  718. int i, j;
  719. int numrep;
  720. int out_size;
  721. /*
  722. * the original choose_total_tries value was off by one (it
  723. * counted "retries" and not "tries"). add one.
  724. */
  725. int choose_tries = map->choose_total_tries + 1;
  726. int choose_leaf_tries = 0;
  727. /*
  728. * the local tries values were counted as "retries", though,
  729. * and need no adjustment
  730. */
  731. int choose_local_retries = map->choose_local_tries;
  732. int choose_local_fallback_retries = map->choose_local_fallback_tries;
  733. int vary_r = map->chooseleaf_vary_r;
  734. if ((__u32)ruleno >= map->max_rules) {
  735. dprintk(" bad ruleno %d\n", ruleno);
  736. return 0;
  737. }
  738. rule = map->rules[ruleno];
  739. result_len = 0;
  740. w = a;
  741. o = b;
  742. for (step = 0; step < rule->len; step++) {
  743. int firstn = 0;
  744. struct crush_rule_step *curstep = &rule->steps[step];
  745. switch (curstep->op) {
  746. case CRUSH_RULE_TAKE:
  747. if ((curstep->arg1 >= 0 &&
  748. curstep->arg1 < map->max_devices) ||
  749. (-1-curstep->arg1 < map->max_buckets &&
  750. map->buckets[-1-curstep->arg1])) {
  751. w[0] = curstep->arg1;
  752. wsize = 1;
  753. } else {
  754. dprintk(" bad take value %d\n", curstep->arg1);
  755. }
  756. break;
  757. case CRUSH_RULE_SET_CHOOSE_TRIES:
  758. if (curstep->arg1 > 0)
  759. choose_tries = curstep->arg1;
  760. break;
  761. case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
  762. if (curstep->arg1 > 0)
  763. choose_leaf_tries = curstep->arg1;
  764. break;
  765. case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
  766. if (curstep->arg1 >= 0)
  767. choose_local_retries = curstep->arg1;
  768. break;
  769. case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
  770. if (curstep->arg1 >= 0)
  771. choose_local_fallback_retries = curstep->arg1;
  772. break;
  773. case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
  774. if (curstep->arg1 >= 0)
  775. vary_r = curstep->arg1;
  776. break;
  777. case CRUSH_RULE_CHOOSELEAF_FIRSTN:
  778. case CRUSH_RULE_CHOOSE_FIRSTN:
  779. firstn = 1;
  780. /* fall through */
  781. case CRUSH_RULE_CHOOSELEAF_INDEP:
  782. case CRUSH_RULE_CHOOSE_INDEP:
  783. if (wsize == 0)
  784. break;
  785. recurse_to_leaf =
  786. curstep->op ==
  787. CRUSH_RULE_CHOOSELEAF_FIRSTN ||
  788. curstep->op ==
  789. CRUSH_RULE_CHOOSELEAF_INDEP;
  790. /* reset output */
  791. osize = 0;
  792. for (i = 0; i < wsize; i++) {
  793. /*
  794. * see CRUSH_N, CRUSH_N_MINUS macros.
  795. * basically, numrep <= 0 means relative to
  796. * the provided result_max
  797. */
  798. numrep = curstep->arg1;
  799. if (numrep <= 0) {
  800. numrep += result_max;
  801. if (numrep <= 0)
  802. continue;
  803. }
  804. j = 0;
  805. if (firstn) {
  806. int recurse_tries;
  807. if (choose_leaf_tries)
  808. recurse_tries =
  809. choose_leaf_tries;
  810. else if (map->chooseleaf_descend_once)
  811. recurse_tries = 1;
  812. else
  813. recurse_tries = choose_tries;
  814. osize += crush_choose_firstn(
  815. map,
  816. map->buckets[-1-w[i]],
  817. weight, weight_max,
  818. x, numrep,
  819. curstep->arg2,
  820. o+osize, j,
  821. result_max-osize,
  822. choose_tries,
  823. recurse_tries,
  824. choose_local_retries,
  825. choose_local_fallback_retries,
  826. recurse_to_leaf,
  827. vary_r,
  828. c+osize,
  829. 0);
  830. } else {
  831. out_size = ((numrep < (result_max-osize)) ?
  832. numrep : (result_max-osize));
  833. crush_choose_indep(
  834. map,
  835. map->buckets[-1-w[i]],
  836. weight, weight_max,
  837. x, out_size, numrep,
  838. curstep->arg2,
  839. o+osize, j,
  840. choose_tries,
  841. choose_leaf_tries ?
  842. choose_leaf_tries : 1,
  843. recurse_to_leaf,
  844. c+osize,
  845. 0);
  846. osize += out_size;
  847. }
  848. }
  849. if (recurse_to_leaf)
  850. /* copy final _leaf_ values to output set */
  851. memcpy(o, c, osize*sizeof(*o));
  852. /* swap o and w arrays */
  853. tmp = o;
  854. o = w;
  855. w = tmp;
  856. wsize = osize;
  857. break;
  858. case CRUSH_RULE_EMIT:
  859. for (i = 0; i < wsize && result_len < result_max; i++) {
  860. result[result_len] = w[i];
  861. result_len++;
  862. }
  863. wsize = 0;
  864. break;
  865. default:
  866. dprintk(" unknown op %d at step %d\n",
  867. curstep->op, step);
  868. break;
  869. }
  870. }
  871. return result_len;
  872. }