crush.c 3.0 KB

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