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