mapper.c 13 KB

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