blob: 9d84ce4ea0dfa8928cc498b9f0515735666b7857 [file] [log] [blame]
Sage Weil5ecc0a02009-10-06 11:31:11 -07001
2#ifdef __KERNEL__
3# include <linux/slab.h>
4#else
5# include <stdlib.h>
6# include <assert.h>
7# define kfree(x) do { if (x) free(x); } while (0)
8# define BUG_ON(x) assert(!(x))
9#endif
10
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include <linux/crush/crush.h>
Sage Weil5ecc0a02009-10-06 11:31:11 -070012
Sage Weilc6cf7262009-11-06 16:39:26 -080013const char *crush_bucket_alg_name(int alg)
14{
15 switch (alg) {
16 case CRUSH_BUCKET_UNIFORM: return "uniform";
17 case CRUSH_BUCKET_LIST: return "list";
18 case CRUSH_BUCKET_TREE: return "tree";
19 case CRUSH_BUCKET_STRAW: return "straw";
Ilya Dryomov958a2762015-04-14 16:54:52 +030020 case CRUSH_BUCKET_STRAW2: return "straw2";
Sage Weilc6cf7262009-11-06 16:39:26 -080021 default: return "unknown";
22 }
23}
24
Sage Weil5ecc0a02009-10-06 11:31:11 -070025/**
26 * crush_get_bucket_item_weight - Get weight of an item in given bucket
27 * @b: bucket pointer
28 * @p: item index in bucket
29 */
Sage Weil8b12d472012-05-07 15:38:35 -070030int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
Sage Weil5ecc0a02009-10-06 11:31:11 -070031{
Sage Weil8b12d472012-05-07 15:38:35 -070032 if ((__u32)p >= b->size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070033 return 0;
34
35 switch (b->alg) {
36 case CRUSH_BUCKET_UNIFORM:
37 return ((struct crush_bucket_uniform *)b)->item_weight;
38 case CRUSH_BUCKET_LIST:
39 return ((struct crush_bucket_list *)b)->item_weights[p];
40 case CRUSH_BUCKET_TREE:
Sage Weilf671d4c2012-05-07 15:36:49 -070041 return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
Sage Weil5ecc0a02009-10-06 11:31:11 -070042 case CRUSH_BUCKET_STRAW:
43 return ((struct crush_bucket_straw *)b)->item_weights[p];
Ilya Dryomov958a2762015-04-14 16:54:52 +030044 case CRUSH_BUCKET_STRAW2:
45 return ((struct crush_bucket_straw2 *)b)->item_weights[p];
Sage Weil5ecc0a02009-10-06 11:31:11 -070046 }
47 return 0;
48}
49
Sage Weil5ecc0a02009-10-06 11:31:11 -070050void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
51{
52 kfree(b->h.perm);
53 kfree(b->h.items);
54 kfree(b);
55}
56
57void crush_destroy_bucket_list(struct crush_bucket_list *b)
58{
59 kfree(b->item_weights);
60 kfree(b->sum_weights);
61 kfree(b->h.perm);
62 kfree(b->h.items);
63 kfree(b);
64}
65
66void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
67{
Sage Weil6eb43f42012-05-07 15:37:05 -070068 kfree(b->h.perm);
69 kfree(b->h.items);
Sage Weil5ecc0a02009-10-06 11:31:11 -070070 kfree(b->node_weights);
71 kfree(b);
72}
73
74void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
75{
76 kfree(b->straws);
77 kfree(b->item_weights);
78 kfree(b->h.perm);
79 kfree(b->h.items);
80 kfree(b);
81}
82
Ilya Dryomov958a2762015-04-14 16:54:52 +030083void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
84{
85 kfree(b->item_weights);
86 kfree(b->h.perm);
87 kfree(b->h.items);
88 kfree(b);
89}
90
Sage Weil5ecc0a02009-10-06 11:31:11 -070091void 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;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300106 case CRUSH_BUCKET_STRAW2:
107 crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b);
108 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700109 }
110}
111
112/**
113 * crush_destroy - Destroy a crush_map
114 * @map: crush_map pointer
115 */
116void crush_destroy(struct crush_map *map)
117{
Sage Weil5ecc0a02009-10-06 11:31:11 -0700118 /* buckets */
119 if (map->buckets) {
Sage Weil8b12d472012-05-07 15:38:35 -0700120 __s32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700121 for (b = 0; b < map->max_buckets; b++) {
122 if (map->buckets[b] == NULL)
123 continue;
124 crush_destroy_bucket(map->buckets[b]);
125 }
126 kfree(map->buckets);
127 }
128
129 /* rules */
130 if (map->rules) {
Sage Weil8b12d472012-05-07 15:38:35 -0700131 __u32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700132 for (b = 0; b < map->max_rules; b++)
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200133 crush_destroy_rule(map->rules[b]);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700134 kfree(map->rules);
135 }
136
Sage Weil5ecc0a02009-10-06 11:31:11 -0700137 kfree(map);
138}
139
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200140void crush_destroy_rule(struct crush_rule *rule)
141{
142 kfree(rule);
143}