WIP FPC-III support
[linux/fpc-iii.git] / net / ceph / crush / crush.c
blob254ded0b05f6a1434e82e67e503914593633e499
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
10 const char *crush_bucket_alg_name(int alg)
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";
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
27 int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
29 if ((__u32)p >= b->size)
30 return 0;
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];
44 return 0;
47 void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
49 kfree(b->h.items);
50 kfree(b);
53 void crush_destroy_bucket_list(struct crush_bucket_list *b)
55 kfree(b->item_weights);
56 kfree(b->sum_weights);
57 kfree(b->h.items);
58 kfree(b);
61 void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
63 kfree(b->h.items);
64 kfree(b->node_weights);
65 kfree(b);
68 void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
70 kfree(b->straws);
71 kfree(b->item_weights);
72 kfree(b->h.items);
73 kfree(b);
76 void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
78 kfree(b->item_weights);
79 kfree(b->h.items);
80 kfree(b);
83 void crush_destroy_bucket(struct crush_bucket *b)
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;
105 * crush_destroy - Destroy a crush_map
106 * @map: crush_map pointer
108 void crush_destroy(struct crush_map *map)
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]);
118 kfree(map->buckets);
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);
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);
139 void crush_destroy_rule(struct crush_rule *rule)
141 kfree(rule);