mapper.c 13 KB

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