1 
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 
11 #include <linux/crush/crush.h>
12 
crush_bucket_alg_name(int alg)13 const 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 
24 /**
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  */
crush_get_bucket_item_weight(const struct crush_bucket * b,int p)29 int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
30 {
31 	if ((__u32)p >= b->size)
32 		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:
40 		return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
41 	case CRUSH_BUCKET_STRAW:
42 		return ((struct crush_bucket_straw *)b)->item_weights[p];
43 	}
44 	return 0;
45 }
46 
47 /**
48  * crush_calc_parents - Calculate parent vectors for the given crush map.
49  * @map: crush_map pointer
50  */
crush_calc_parents(struct crush_map * map)51 void crush_calc_parents(struct crush_map *map)
52 {
53 	int i, b, c;
54 
55 	for (b = 0; b < map->max_buckets; b++) {
56 		if (map->buckets[b] == NULL)
57 			continue;
58 		for (i = 0; i < map->buckets[b]->size; i++) {
59 			c = map->buckets[b]->items[i];
60 			BUG_ON(c >= map->max_devices ||
61 			       c < -map->max_buckets);
62 			if (c >= 0)
63 				map->device_parents[c] = map->buckets[b]->id;
64 			else
65 				map->bucket_parents[-1-c] = map->buckets[b]->id;
66 		}
67 	}
68 }
69 
crush_destroy_bucket_uniform(struct crush_bucket_uniform * b)70 void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
71 {
72 	kfree(b->h.perm);
73 	kfree(b->h.items);
74 	kfree(b);
75 }
76 
crush_destroy_bucket_list(struct crush_bucket_list * b)77 void crush_destroy_bucket_list(struct crush_bucket_list *b)
78 {
79 	kfree(b->item_weights);
80 	kfree(b->sum_weights);
81 	kfree(b->h.perm);
82 	kfree(b->h.items);
83 	kfree(b);
84 }
85 
crush_destroy_bucket_tree(struct crush_bucket_tree * b)86 void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
87 {
88 	kfree(b->h.perm);
89 	kfree(b->h.items);
90 	kfree(b->node_weights);
91 	kfree(b);
92 }
93 
crush_destroy_bucket_straw(struct crush_bucket_straw * b)94 void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
95 {
96 	kfree(b->straws);
97 	kfree(b->item_weights);
98 	kfree(b->h.perm);
99 	kfree(b->h.items);
100 	kfree(b);
101 }
102 
crush_destroy_bucket(struct crush_bucket * b)103 void crush_destroy_bucket(struct crush_bucket *b)
104 {
105 	switch (b->alg) {
106 	case CRUSH_BUCKET_UNIFORM:
107 		crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b);
108 		break;
109 	case CRUSH_BUCKET_LIST:
110 		crush_destroy_bucket_list((struct crush_bucket_list *)b);
111 		break;
112 	case CRUSH_BUCKET_TREE:
113 		crush_destroy_bucket_tree((struct crush_bucket_tree *)b);
114 		break;
115 	case CRUSH_BUCKET_STRAW:
116 		crush_destroy_bucket_straw((struct crush_bucket_straw *)b);
117 		break;
118 	}
119 }
120 
121 /**
122  * crush_destroy - Destroy a crush_map
123  * @map: crush_map pointer
124  */
crush_destroy(struct crush_map * map)125 void crush_destroy(struct crush_map *map)
126 {
127 	/* buckets */
128 	if (map->buckets) {
129 		__s32 b;
130 		for (b = 0; b < map->max_buckets; b++) {
131 			if (map->buckets[b] == NULL)
132 				continue;
133 			crush_destroy_bucket(map->buckets[b]);
134 		}
135 		kfree(map->buckets);
136 	}
137 
138 	/* rules */
139 	if (map->rules) {
140 		__u32 b;
141 		for (b = 0; b < map->max_rules; b++)
142 			kfree(map->rules[b]);
143 		kfree(map->rules);
144 	}
145 
146 	kfree(map->bucket_parents);
147 	kfree(map->device_parents);
148 	kfree(map);
149 }
150 
151 
152