Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / ceph / crush / crush.c
blob3d70244bc1b63ffd6c470f5d425a4c86d959629f
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
11 const char *crush_bucket_alg_name(int alg)
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";
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
28 int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
30 if ((__u32)p >= b->size)
31 return 0;
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];
45 return 0;
48 void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
50 kfree(b->h.items);
51 kfree(b);
54 void crush_destroy_bucket_list(struct crush_bucket_list *b)
56 kfree(b->item_weights);
57 kfree(b->sum_weights);
58 kfree(b->h.items);
59 kfree(b);
62 void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
64 kfree(b->h.items);
65 kfree(b->node_weights);
66 kfree(b);
69 void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
71 kfree(b->straws);
72 kfree(b->item_weights);
73 kfree(b->h.items);
74 kfree(b);
77 void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
79 kfree(b->item_weights);
80 kfree(b->h.items);
81 kfree(b);
84 void crush_destroy_bucket(struct crush_bucket *b)
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;
106 * crush_destroy - Destroy a crush_map
107 * @map: crush_map pointer
109 void crush_destroy(struct crush_map *map)
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]);
119 kfree(map->buckets);
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);
130 #ifndef __KERNEL__
131 kfree(map->choose_tries);
132 #else
133 clear_choose_args(map);
134 #endif
135 kfree(map);
138 void crush_destroy_rule(struct crush_rule *rule)
140 kfree(rule);