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