mapper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #ifdef __KERNEL__
  2. # include <linux/string.h>
  3. # include <linux/slab.h>
  4. # include <linux/bug.h>
  5. # include <linux/kernel.h>
  6. # ifndef dprintk
  7. # define dprintk(args...)
  8. # endif
  9. #else
  10. # include <string.h>
  11. # include <stdio.h>
  12. # include <stdlib.h>
  13. # include <assert.h>
  14. # define BUG_ON(x) assert(!(x))
  15. # define dprintk(args...) /* printf(args) */
  16. # define kmalloc(x, f) malloc(x)
  17. # define kfree(x) free(x)
  18. #endif
  19. #include <linux/crush/crush.h>
  20. #include <linux/crush/hash.h>
  21. #include <linux/crush/mapper.h>
  22. /*
  23. * Implement the core CRUSH mapping algorithm.
  24. */
  25. /**
  26. * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
  27. * @map: the crush_map
  28. * @ruleset: the storage ruleset id (user defined)
  29. * @type: storage ruleset type (user defined)
  30. * @size: output set size
  31. */
  32. int crush_find_rule(struct crush_map *map, int ruleset, int type, int size)
  33. {
  34. int i;
  35. for (i = 0; i < map->max_rules; i++) {
  36. if (map->rules[i] &&
  37. map->rules[i]->mask.ruleset == ruleset &&
  38. map->rules[i]->mask.type == type &&
  39. map->rules[i]->mask.min_size <= size &&
  40. map->rules[i]->mask.max_size >= size)
  41. return i;
  42. }
  43. return -1;
  44. }
  45. /*
  46. * bucket choose methods
  47. *
  48. * For each bucket algorithm, we have a "choose" method that, given a
  49. * crush input @x and replica position (usually, position in output set) @r,
  50. * will produce an item in the bucket.
  51. */
  52. /*
  53. * Choose based on a random permutation of the bucket.
  54. *
  55. * We used to use some prime number arithmetic to do this, but it
  56. * wasn't very random, and had some other bad behaviors. Instead, we
  57. * calculate an actual random permutation of the bucket members.
  58. * Since this is expensive, we optimize for the r=0 case, which
  59. * captures the vast majority of calls.
  60. */
  61. static int bucket_perm_choose(struct crush_bucket *bucket,
  62. int x, int r)
  63. {
  64. unsigned int pr = r % bucket->size;
  65. unsigned int i, s;
  66. /* start a new permutation if @x has changed */
  67. if (bucket->perm_x != x || bucket->perm_n == 0) {
  68. dprintk("bucket %d new x=%d\n", bucket->id, x);
  69. bucket->perm_x = x;
  70. /* optimize common r=0 case */
  71. if (pr == 0) {
  72. s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
  73. bucket->size;
  74. bucket->perm[0] = s;
  75. bucket->perm_n = 0xffff; /* magic value, see below */
  76. goto out;
  77. }
  78. for (i = 0; i < bucket->size; i++)
  79. bucket->perm[i] = i;
  80. bucket->perm_n = 0;
  81. } else if (bucket->perm_n == 0xffff) {
  82. /* clean up after the r=0 case above */
  83. for (i = 1; i < bucket->size; i++)
  84. bucket->perm[i] = i;
  85. bucket->perm[bucket->perm[0]] = 0;
  86. bucket->perm_n = 1;
  87. }
  88. /* calculate permutation up to pr */
  89. for (i = 0; i < bucket->perm_n; i++)
  90. dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
  91. while (bucket->perm_n <= pr) {
  92. unsigned int p = bucket->perm_n;
  93. /* no point in swapping the final entry */
  94. if (p < bucket->size - 1) {
  95. i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
  96. (bucket->size - p);
  97. if (i) {
  98. unsigned int t = bucket->perm[p + i];
  99. bucket->perm[p + i] = bucket->perm[p];
  100. bucket->perm[p] = t;
  101. }
  102. dprintk(" perm_choose swap %d with %d\n", p, p+i);
  103. }
  104. bucket->perm_n++;
  105. }
  106. for (i = 0; i < bucket->size; i++)
  107. dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
  108. s = bucket->perm[pr];
  109. out:
  110. dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
  111. bucket->size, x, r, pr, s);
  112. return bucket->items[s];
  113. }
  114. /* uniform */
  115. static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
  116. int x, int r)
  117. {
  118. return bucket_perm_choose(&bucket->h, x, r);
  119. }
  120. /* list */
  121. static int bucket_list_choose(struct crush_bucket_list *bucket,
  122. int x, int r)
  123. {
  124. int i;
  125. for (i = bucket->h.size-1; i >= 0; i--) {
  126. __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
  127. r, bucket->h.id);
  128. w &= 0xffff;
  129. dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
  130. "sw %x rand %llx",
  131. i, x, r, bucket->h.items[i], bucket->item_weights[i],
  132. bucket->sum_weights[i], w);
  133. w *= bucket->sum_weights[i];
  134. w = w >> 16;
  135. /*dprintk(" scaled %llx\n", w);*/
  136. if (w < bucket->item_weights[i])
  137. return bucket->h.items[i];
  138. }
  139. BUG_ON(1);
  140. return 0;
  141. }
  142. /* (binary) tree */
  143. static int height(int n)
  144. {
  145. int h = 0;
  146. while ((n & 1) == 0) {
  147. h++;
  148. n = n >> 1;
  149. }
  150. return h;
  151. }
  152. static int left(int x)
  153. {
  154. int h = height(x);
  155. return x - (1 << (h-1));
  156. }
  157. static int right(int x)
  158. {
  159. int h = height(x);
  160. return x + (1 << (h-1));
  161. }
  162. static int terminal(int x)
  163. {
  164. return x & 1;
  165. }
  166. static int bucket_tree_choose(struct crush_bucket_tree *bucket,
  167. int x, int r)
  168. {
  169. int n, l;
  170. __u32 w;
  171. __u64 t;
  172. /* start at root */
  173. n = bucket->num_nodes >> 1;
  174. while (!terminal(n)) {
  175. /* pick point in [0, w) */
  176. w = bucket->node_weights[n];
  177. t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
  178. bucket->h.id) * (__u64)w;
  179. t = t >> 32;
  180. /* descend to the left or right? */
  181. l = left(n);
  182. if (t < bucket->node_weights[l])
  183. n = l;
  184. else
  185. n = right(n);
  186. }
  187. return bucket->h.items[n >> 1];
  188. }
  189. /* straw */
  190. static int bucket_straw_choose(struct crush_bucket_straw *bucket,
  191. int x, int r)
  192. {
  193. int i;
  194. int high = 0;
  195. __u64 high_draw = 0;
  196. __u64 draw;
  197. for (i = 0; i < bucket->h.size; i++) {
  198. draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
  199. draw &= 0xffff;
  200. draw *= bucket->straws[i];
  201. if (i == 0 || draw > high_draw) {
  202. high = i;
  203. high_draw = draw;
  204. }
  205. }
  206. return bucket->h.items[high];
  207. }
  208. static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
  209. {
  210. dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
  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. BUG_ON(1);
  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(struct crush_map *map, __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(struct crush_map *map,
  258. struct crush_bucket *bucket,
  259. __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. 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 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 || 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. BUG_ON(item >= map->max_devices);
  321. /* desired type? */
  322. if (item < 0)
  323. itemtype = map->buckets[-1-item]->type;
  324. else
  325. itemtype = 0;
  326. dprintk(" item %d type %d\n", item, itemtype);
  327. /* keep going? */
  328. if (itemtype != type) {
  329. BUG_ON(item >= 0 ||
  330. (-1-item) >= map->max_buckets);
  331. in = map->buckets[-1-item];
  332. retry_bucket = 1;
  333. continue;
  334. }
  335. /* collision? */
  336. for (i = 0; i < outpos; i++) {
  337. if (out[i] == item) {
  338. collide = 1;
  339. break;
  340. }
  341. }
  342. reject = 0;
  343. if (recurse_to_leaf) {
  344. if (item < 0) {
  345. if (crush_choose(map,
  346. map->buckets[-1-item],
  347. weight,
  348. x, outpos+1, 0,
  349. out2, outpos,
  350. firstn, 0,
  351. NULL) <= outpos)
  352. /* didn't get leaf */
  353. reject = 1;
  354. } else {
  355. /* we already have a leaf! */
  356. out2[outpos] = item;
  357. }
  358. }
  359. if (!reject) {
  360. /* out? */
  361. if (itemtype == 0)
  362. reject = is_out(map, weight,
  363. item, x);
  364. else
  365. reject = 0;
  366. }
  367. reject:
  368. if (reject || collide) {
  369. ftotal++;
  370. flocal++;
  371. if (collide && flocal < 3)
  372. /* retry locally a few times */
  373. retry_bucket = 1;
  374. else if (flocal < in->size + orig_tries)
  375. /* exhaustive bucket search */
  376. retry_bucket = 1;
  377. else if (ftotal < 20)
  378. /* then retry descent */
  379. retry_descent = 1;
  380. else
  381. /* else give up */
  382. skip_rep = 1;
  383. dprintk(" reject %d collide %d "
  384. "ftotal %d flocal %d\n",
  385. reject, collide, ftotal,
  386. flocal);
  387. }
  388. } while (retry_bucket);
  389. } while (retry_descent);
  390. if (skip_rep) {
  391. dprintk("skip rep\n");
  392. continue;
  393. }
  394. dprintk("CHOOSE got %d\n", item);
  395. out[outpos] = item;
  396. outpos++;
  397. }
  398. dprintk("CHOOSE returns %d\n", outpos);
  399. return outpos;
  400. }
  401. /**
  402. * crush_do_rule - calculate a mapping with the given input and rule
  403. * @map: the crush_map
  404. * @ruleno: the rule id
  405. * @x: hash input
  406. * @result: pointer to result vector
  407. * @result_max: maximum result size
  408. * @force: force initial replica choice; -1 for none
  409. */
  410. int crush_do_rule(struct crush_map *map,
  411. int ruleno, int x, int *result, int result_max,
  412. int force, __u32 *weight)
  413. {
  414. int result_len;
  415. int force_context[CRUSH_MAX_DEPTH];
  416. int force_pos = -1;
  417. int a[CRUSH_MAX_SET];
  418. int b[CRUSH_MAX_SET];
  419. int c[CRUSH_MAX_SET];
  420. int recurse_to_leaf;
  421. int *w;
  422. int wsize = 0;
  423. int *o;
  424. int osize;
  425. int *tmp;
  426. struct crush_rule *rule;
  427. int step;
  428. int i, j;
  429. int numrep;
  430. int firstn;
  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. force < map->max_devices &&
  443. map->device_parents[force] != 0 &&
  444. !is_out(map, weight, force, x)) {
  445. while (1) {
  446. force_context[++force_pos] = force;
  447. if (force >= 0)
  448. force = map->device_parents[force];
  449. else
  450. force = map->bucket_parents[-1-force];
  451. if (force == 0)
  452. break;
  453. }
  454. }
  455. for (step = 0; step < rule->len; step++) {
  456. firstn = 0;
  457. switch (rule->steps[step].op) {
  458. case CRUSH_RULE_TAKE:
  459. w[0] = rule->steps[step].arg1;
  460. /* find position in force_context/hierarchy */
  461. while (force_pos >= 0 &&
  462. force_context[force_pos] != w[0])
  463. force_pos--;
  464. /* and move past it */
  465. if (force_pos >= 0)
  466. force_pos--;
  467. wsize = 1;
  468. break;
  469. case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
  470. case CRUSH_RULE_CHOOSE_FIRSTN:
  471. firstn = 1;
  472. case CRUSH_RULE_CHOOSE_LEAF_INDEP:
  473. case CRUSH_RULE_CHOOSE_INDEP:
  474. BUG_ON(wsize == 0);
  475. recurse_to_leaf =
  476. rule->steps[step].op ==
  477. CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
  478. rule->steps[step].op ==
  479. CRUSH_RULE_CHOOSE_LEAF_INDEP;
  480. /* reset output */
  481. osize = 0;
  482. for (i = 0; i < wsize; i++) {
  483. /*
  484. * see CRUSH_N, CRUSH_N_MINUS macros.
  485. * basically, numrep <= 0 means relative to
  486. * the provided result_max
  487. */
  488. numrep = rule->steps[step].arg1;
  489. if (numrep <= 0) {
  490. numrep += result_max;
  491. if (numrep <= 0)
  492. continue;
  493. }
  494. j = 0;
  495. if (osize == 0 && force_pos >= 0) {
  496. /* skip any intermediate types */
  497. while (force_pos &&
  498. force_context[force_pos] < 0 &&
  499. rule->steps[step].arg2 !=
  500. map->buckets[-1 -
  501. force_context[force_pos]]->type)
  502. force_pos--;
  503. o[osize] = force_context[force_pos];
  504. if (recurse_to_leaf)
  505. c[osize] = force_context[0];
  506. j++;
  507. force_pos--;
  508. }
  509. osize += crush_choose(map,
  510. map->buckets[-1-w[i]],
  511. weight,
  512. x, numrep,
  513. rule->steps[step].arg2,
  514. o+osize, j,
  515. firstn,
  516. recurse_to_leaf, c+osize);
  517. }
  518. if (recurse_to_leaf)
  519. /* copy final _leaf_ values to output set */
  520. memcpy(o, c, osize*sizeof(*o));
  521. /* swap t and w arrays */
  522. tmp = o;
  523. o = w;
  524. w = tmp;
  525. wsize = osize;
  526. break;
  527. case CRUSH_RULE_EMIT:
  528. for (i = 0; i < wsize && result_len < result_max; i++) {
  529. result[result_len] = w[i];
  530. result_len++;
  531. }
  532. wsize = 0;
  533. break;
  534. default:
  535. BUG_ON(1);
  536. }
  537. }
  538. return result_len;
  539. }