crush.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifdef __KERNEL__
  2. # include <linux/slab.h>
  3. #else
  4. # include <stdlib.h>
  5. # include <assert.h>
  6. # define kfree(x) do { if (x) free(x); } while (0)
  7. # define BUG_ON(x) assert(!(x))
  8. #endif
  9. #include <linux/crush/crush.h>
  10. const char *crush_bucket_alg_name(int alg)
  11. {
  12. switch (alg) {
  13. case CRUSH_BUCKET_UNIFORM: return "uniform";
  14. case CRUSH_BUCKET_LIST: return "list";
  15. case CRUSH_BUCKET_TREE: return "tree";
  16. case CRUSH_BUCKET_STRAW: return "straw";
  17. default: return "unknown";
  18. }
  19. }
  20. /**
  21. * crush_get_bucket_item_weight - Get weight of an item in given bucket
  22. * @b: bucket pointer
  23. * @p: item index in bucket
  24. */
  25. int crush_get_bucket_item_weight(struct crush_bucket *b, int p)
  26. {
  27. if (p >= b->size)
  28. return 0;
  29. switch (b->alg) {
  30. case CRUSH_BUCKET_UNIFORM:
  31. return ((struct crush_bucket_uniform *)b)->item_weight;
  32. case CRUSH_BUCKET_LIST:
  33. return ((struct crush_bucket_list *)b)->item_weights[p];
  34. case CRUSH_BUCKET_TREE:
  35. if (p & 1)
  36. return ((struct crush_bucket_tree *)b)->node_weights[p];
  37. return 0;
  38. case CRUSH_BUCKET_STRAW:
  39. return ((struct crush_bucket_straw *)b)->item_weights[p];
  40. }
  41. return 0;
  42. }
  43. /**
  44. * crush_calc_parents - Calculate parent vectors for the given crush map.
  45. * @map: crush_map pointer
  46. */
  47. void crush_calc_parents(struct crush_map *map)
  48. {
  49. int i, b, c;
  50. for (b = 0; b < map->max_buckets; b++) {
  51. if (map->buckets[b] == NULL)
  52. continue;
  53. for (i = 0; i < map->buckets[b]->size; i++) {
  54. c = map->buckets[b]->items[i];
  55. BUG_ON(c >= map->max_devices ||
  56. c < -map->max_buckets);
  57. if (c >= 0)
  58. map->device_parents[c] = map->buckets[b]->id;
  59. else
  60. map->bucket_parents[-1-c] = map->buckets[b]->id;
  61. }
  62. }
  63. }
  64. void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
  65. {
  66. kfree(b->h.perm);
  67. kfree(b->h.items);
  68. kfree(b);
  69. }
  70. void crush_destroy_bucket_list(struct crush_bucket_list *b)
  71. {
  72. kfree(b->item_weights);
  73. kfree(b->sum_weights);
  74. kfree(b->h.perm);
  75. kfree(b->h.items);
  76. kfree(b);
  77. }
  78. void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
  79. {
  80. kfree(b->node_weights);
  81. kfree(b);
  82. }
  83. void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
  84. {
  85. kfree(b->straws);
  86. kfree(b->item_weights);
  87. kfree(b->h.perm);
  88. kfree(b->h.items);
  89. kfree(b);
  90. }
  91. void crush_destroy_bucket(struct crush_bucket *b)
  92. {
  93. switch (b->alg) {
  94. case CRUSH_BUCKET_UNIFORM:
  95. crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b);
  96. break;
  97. case CRUSH_BUCKET_LIST:
  98. crush_destroy_bucket_list((struct crush_bucket_list *)b);
  99. break;
  100. case CRUSH_BUCKET_TREE:
  101. crush_destroy_bucket_tree((struct crush_bucket_tree *)b);
  102. break;
  103. case CRUSH_BUCKET_STRAW:
  104. crush_destroy_bucket_straw((struct crush_bucket_straw *)b);
  105. break;
  106. }
  107. }
  108. /**
  109. * crush_destroy - Destroy a crush_map
  110. * @map: crush_map pointer
  111. */
  112. void crush_destroy(struct crush_map *map)
  113. {
  114. int b;
  115. /* buckets */
  116. if (map->buckets) {
  117. for (b = 0; b < map->max_buckets; b++) {
  118. if (map->buckets[b] == NULL)
  119. continue;
  120. crush_destroy_bucket(map->buckets[b]);
  121. }
  122. kfree(map->buckets);
  123. }
  124. /* rules */
  125. if (map->rules) {
  126. for (b = 0; b < map->max_rules; b++)
  127. kfree(map->rules[b]);
  128. kfree(map->rules);
  129. }
  130. kfree(map->bucket_parents);
  131. kfree(map->device_parents);
  132. kfree(map);
  133. }