mapper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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(struct crush_map *map, int ruleset, int type, int size)
  32. {
  33. int 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 != 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. BUG_ON(1);
  139. return 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. int 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. switch (in->alg) {
  211. case CRUSH_BUCKET_UNIFORM:
  212. return bucket_uniform_choose((struct crush_bucket_uniform *)in,
  213. x, r);
  214. case CRUSH_BUCKET_LIST:
  215. return bucket_list_choose((struct crush_bucket_list *)in,
  216. x, r);
  217. case CRUSH_BUCKET_TREE:
  218. return bucket_tree_choose((struct crush_bucket_tree *)in,
  219. x, r);
  220. case CRUSH_BUCKET_STRAW:
  221. return bucket_straw_choose((struct crush_bucket_straw *)in,
  222. x, r);
  223. default:
  224. BUG_ON(1);
  225. return in->items[0];
  226. }
  227. }
  228. /*
  229. * true if device is marked "out" (failed, fully offloaded)
  230. * of the cluster
  231. */
  232. static int is_out(struct crush_map *map, __u32 *weight, int item, int x)
  233. {
  234. if (weight[item] >= 0x10000)
  235. return 0;
  236. if (weight[item] == 0)
  237. return 1;
  238. if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
  239. < weight[item])
  240. return 0;
  241. return 1;
  242. }
  243. /**
  244. * crush_choose - choose numrep distinct items of given type
  245. * @map: the crush_map
  246. * @bucket: the bucket we are choose an item from
  247. * @x: crush input value
  248. * @numrep: the number of items to choose
  249. * @type: the type of item to choose
  250. * @out: pointer to output vector
  251. * @outpos: our position in that vector
  252. * @firstn: true if choosing "first n" items, false if choosing "indep"
  253. * @recurse_to_leaf: true if we want one device under each item of given type
  254. * @out2: second output vector for leaf items (if @recurse_to_leaf)
  255. */
  256. static int crush_choose(struct crush_map *map,
  257. struct crush_bucket *bucket,
  258. __u32 *weight,
  259. int x, int numrep, int type,
  260. int *out, int outpos,
  261. int firstn, int recurse_to_leaf,
  262. int *out2)
  263. {
  264. int rep;
  265. int ftotal, flocal;
  266. int retry_descent, retry_bucket, skip_rep;
  267. struct crush_bucket *in = bucket;
  268. int r;
  269. int i;
  270. int item = 0;
  271. int itemtype;
  272. int collide, reject;
  273. const int orig_tries = 5; /* attempts before we fall back to search */
  274. dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
  275. bucket->id, x, outpos, numrep);
  276. for (rep = outpos; rep < numrep; rep++) {
  277. /* keep trying until we get a non-out, non-colliding item */
  278. ftotal = 0;
  279. skip_rep = 0;
  280. do {
  281. retry_descent = 0;
  282. in = bucket; /* initial bucket */
  283. /* choose through intervening buckets */
  284. flocal = 0;
  285. do {
  286. collide = 0;
  287. retry_bucket = 0;
  288. r = rep;
  289. if (in->alg == CRUSH_BUCKET_UNIFORM) {
  290. /* be careful */
  291. if (firstn || numrep >= in->size)
  292. /* r' = r + f_total */
  293. r += ftotal;
  294. else if (in->size % numrep == 0)
  295. /* r'=r+(n+1)*f_local */
  296. r += (numrep+1) *
  297. (flocal+ftotal);
  298. else
  299. /* r' = r + n*f_local */
  300. r += numrep * (flocal+ftotal);
  301. } else {
  302. if (firstn)
  303. /* r' = r + f_total */
  304. r += ftotal;
  305. else
  306. /* r' = r + n*f_local */
  307. r += numrep * (flocal+ftotal);
  308. }
  309. /* bucket choose */
  310. if (in->size == 0) {
  311. reject = 1;
  312. goto reject;
  313. }
  314. if (flocal >= (in->size>>1) &&
  315. flocal > orig_tries)
  316. item = bucket_perm_choose(in, x, r);
  317. else
  318. item = crush_bucket_choose(in, x, r);
  319. BUG_ON(item >= map->max_devices);
  320. /* desired type? */
  321. if (item < 0)
  322. itemtype = map->buckets[-1-item]->type;
  323. else
  324. itemtype = 0;
  325. dprintk(" item %d type %d\n", item, itemtype);
  326. /* keep going? */
  327. if (itemtype != type) {
  328. BUG_ON(item >= 0 ||
  329. (-1-item) >= map->max_buckets);
  330. in = map->buckets[-1-item];
  331. retry_bucket = 1;
  332. continue;
  333. }
  334. /* collision? */
  335. for (i = 0; i < outpos; i++) {
  336. if (out[i] == item) {
  337. collide = 1;
  338. break;
  339. }
  340. }
  341. reject = 0;
  342. if (recurse_to_leaf) {
  343. if (item < 0) {
  344. if (crush_choose(map,
  345. map->buckets[-1-item],
  346. weight,
  347. x, outpos+1, 0,
  348. out2, outpos,
  349. firstn, 0,
  350. NULL) <= outpos)
  351. /* didn't get leaf */
  352. reject = 1;
  353. } else {
  354. /* we already have a leaf! */
  355. out2[outpos] = item;
  356. }
  357. }
  358. if (!reject) {
  359. /* out? */
  360. if (itemtype == 0)
  361. reject = is_out(map, weight,
  362. item, x);
  363. else
  364. reject = 0;
  365. }
  366. reject:
  367. if (reject || collide) {
  368. ftotal++;
  369. flocal++;
  370. if (collide && flocal < 3)
  371. /* retry locally a few times */
  372. retry_bucket = 1;
  373. else if (flocal < in->size + orig_tries)
  374. /* exhaustive bucket search */
  375. retry_bucket = 1;
  376. else if (ftotal < 20)
  377. /* then retry descent */
  378. retry_descent = 1;
  379. else
  380. /* else give up */
  381. skip_rep = 1;
  382. dprintk(" reject %d collide %d "
  383. "ftotal %d flocal %d\n",
  384. reject, collide, ftotal,
  385. flocal);
  386. }
  387. } while (retry_bucket);
  388. } while (retry_descent);
  389. if (skip_rep) {
  390. dprintk("skip rep\n");
  391. continue;
  392. }
  393. dprintk("CHOOSE got %d\n", item);
  394. out[outpos] = item;
  395. outpos++;
  396. }
  397. dprintk("CHOOSE returns %d\n", outpos);
  398. return outpos;
  399. }
  400. /**
  401. * crush_do_rule - calculate a mapping with the given input and rule
  402. * @map: the crush_map
  403. * @ruleno: the rule id
  404. * @x: hash input
  405. * @result: pointer to result vector
  406. * @result_max: maximum result size
  407. * @force: force initial replica choice; -1 for none
  408. */
  409. int crush_do_rule(struct crush_map *map,
  410. int ruleno, int x, int *result, int result_max,
  411. int force, __u32 *weight)
  412. {
  413. int result_len;
  414. int force_context[CRUSH_MAX_DEPTH];
  415. int force_pos = -1;
  416. int a[CRUSH_MAX_SET];
  417. int b[CRUSH_MAX_SET];
  418. int c[CRUSH_MAX_SET];
  419. int recurse_to_leaf;
  420. int *w;
  421. int wsize = 0;
  422. int *o;
  423. int osize;
  424. int *tmp;
  425. struct crush_rule *rule;
  426. int step;
  427. int i, j;
  428. int numrep;
  429. int firstn;
  430. int rc = -1;
  431. BUG_ON(ruleno >= map->max_rules);
  432. rule = map->rules[ruleno];
  433. result_len = 0;
  434. w = a;
  435. o = b;
  436. /*
  437. * determine hierarchical context of force, if any. note
  438. * that this may or may not correspond to the specific types
  439. * referenced by the crush rule.
  440. */
  441. if (force >= 0) {
  442. if (force >= map->max_devices ||
  443. map->device_parents[force] == 0) {
  444. /*dprintk("CRUSH: forcefed device dne\n");*/
  445. rc = -1; /* force fed device dne */
  446. goto out;
  447. }
  448. if (!is_out(map, weight, force, x)) {
  449. while (1) {
  450. force_context[++force_pos] = force;
  451. if (force >= 0)
  452. force = map->device_parents[force];
  453. else
  454. force = map->bucket_parents[-1-force];
  455. if (force == 0)
  456. break;
  457. }
  458. }
  459. }
  460. for (step = 0; step < rule->len; step++) {
  461. firstn = 0;
  462. switch (rule->steps[step].op) {
  463. case CRUSH_RULE_TAKE:
  464. w[0] = rule->steps[step].arg1;
  465. if (force_pos >= 0) {
  466. BUG_ON(force_context[force_pos] != w[0]);
  467. force_pos--;
  468. }
  469. wsize = 1;
  470. break;
  471. case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
  472. case CRUSH_RULE_CHOOSE_FIRSTN:
  473. firstn = 1;
  474. case CRUSH_RULE_CHOOSE_LEAF_INDEP:
  475. case CRUSH_RULE_CHOOSE_INDEP:
  476. BUG_ON(wsize == 0);
  477. recurse_to_leaf =
  478. rule->steps[step].op ==
  479. CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
  480. rule->steps[step].op ==
  481. CRUSH_RULE_CHOOSE_LEAF_INDEP;
  482. /* reset output */
  483. osize = 0;
  484. for (i = 0; i < wsize; i++) {
  485. /*
  486. * see CRUSH_N, CRUSH_N_MINUS macros.
  487. * basically, numrep <= 0 means relative to
  488. * the provided result_max
  489. */
  490. numrep = rule->steps[step].arg1;
  491. if (numrep <= 0) {
  492. numrep += result_max;
  493. if (numrep <= 0)
  494. continue;
  495. }
  496. j = 0;
  497. if (osize == 0 && force_pos >= 0) {
  498. /* skip any intermediate types */
  499. while (force_pos &&
  500. force_context[force_pos] < 0 &&
  501. rule->steps[step].arg2 !=
  502. map->buckets[-1 -
  503. force_context[force_pos]]->type)
  504. force_pos--;
  505. o[osize] = force_context[force_pos];
  506. if (recurse_to_leaf)
  507. c[osize] = force_context[0];
  508. j++;
  509. force_pos--;
  510. }
  511. osize += crush_choose(map,
  512. map->buckets[-1-w[i]],
  513. weight,
  514. x, numrep,
  515. rule->steps[step].arg2,
  516. o+osize, j,
  517. firstn,
  518. recurse_to_leaf, c+osize);
  519. }
  520. if (recurse_to_leaf)
  521. /* copy final _leaf_ values to output set */
  522. memcpy(o, c, osize*sizeof(*o));
  523. /* swap t and w arrays */
  524. tmp = o;
  525. o = w;
  526. w = tmp;
  527. wsize = osize;
  528. break;
  529. case CRUSH_RULE_EMIT:
  530. for (i = 0; i < wsize && result_len < result_max; i++) {
  531. result[result_len] = w[i];
  532. result_len++;
  533. }
  534. wsize = 0;
  535. break;
  536. default:
  537. BUG_ON(1);
  538. }
  539. }
  540. rc = result_len;
  541. out:
  542. return rc;
  543. }