blob: b93575f4eb13bcee249f0c9df9c9247a9a7905ff [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";
20 default: return "unknown";
21 }
22}
23
Sage Weil5ecc0a02009-10-06 11:31:11 -070024/**
25 * crush_get_bucket_item_weight - Get weight of an item in given bucket
26 * @b: bucket pointer
27 * @p: item index in bucket
28 */
Sage Weil8b12d472012-05-07 15:38:35 -070029int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
Sage Weil5ecc0a02009-10-06 11:31:11 -070030{
Sage Weil8b12d472012-05-07 15:38:35 -070031 if ((__u32)p >= b->size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070032 return 0;
33
34 switch (b->alg) {
35 case CRUSH_BUCKET_UNIFORM:
36 return ((struct crush_bucket_uniform *)b)->item_weight;
37 case CRUSH_BUCKET_LIST:
38 return ((struct crush_bucket_list *)b)->item_weights[p];
39 case CRUSH_BUCKET_TREE:
Sage Weilf671d4c2012-05-07 15:36:49 -070040 return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
Sage Weil5ecc0a02009-10-06 11:31:11 -070041 case CRUSH_BUCKET_STRAW:
42 return ((struct crush_bucket_straw *)b)->item_weights[p];
43 }
44 return 0;
45}
46
Sage Weil5ecc0a02009-10-06 11:31:11 -070047void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
48{
49 kfree(b->h.perm);
50 kfree(b->h.items);
51 kfree(b);
52}
53
54void crush_destroy_bucket_list(struct crush_bucket_list *b)
55{
56 kfree(b->item_weights);
57 kfree(b->sum_weights);
58 kfree(b->h.perm);
59 kfree(b->h.items);
60 kfree(b);
61}
62
63void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
64{
65 kfree(b->node_weights);
66 kfree(b);
67}
68
69void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
70{
71 kfree(b->straws);
72 kfree(b->item_weights);
73 kfree(b->h.perm);
74 kfree(b->h.items);
75 kfree(b);
76}
77
78void 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 }
94}
95
96/**
97 * crush_destroy - Destroy a crush_map
98 * @map: crush_map pointer
99 */
100void crush_destroy(struct crush_map *map)
101{
Sage Weil5ecc0a02009-10-06 11:31:11 -0700102 /* buckets */
103 if (map->buckets) {
Sage Weil8b12d472012-05-07 15:38:35 -0700104 __s32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700105 for (b = 0; b < map->max_buckets; b++) {
106 if (map->buckets[b] == NULL)
107 continue;
108 crush_destroy_bucket(map->buckets[b]);
109 }
110 kfree(map->buckets);
111 }
112
113 /* rules */
114 if (map->rules) {
Sage Weil8b12d472012-05-07 15:38:35 -0700115 __u32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700116 for (b = 0; b < map->max_rules; b++)
117 kfree(map->rules[b]);
118 kfree(map->rules);
119 }
120
Sage Weil5ecc0a02009-10-06 11:31:11 -0700121 kfree(map);
122}
123
124