mapper.c 23 KB

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