crush.c 3.0 KB

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