blob: 3d70244bc1b63ffd6c470f5d425a4c86d959629f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weil5ecc0a02009-10-06 11:31:11 -07002#ifdef __KERNEL__
3# include <linux/slab.h>
Ilya Dryomovb459be72015-06-12 13:21:07 +03004# include <linux/crush/crush.h>
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02005void clear_choose_args(struct crush_map *c);
Sage Weil5ecc0a02009-10-06 11:31:11 -07006#else
Ilya Dryomovb459be72015-06-12 13:21:07 +03007# include "crush_compat.h"
8# include "crush.h"
Sage Weil5ecc0a02009-10-06 11:31:11 -07009#endif
10
Sage Weilc6cf7262009-11-06 16:39:26 -080011const 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";
Ilya Dryomov958a2762015-04-14 16:54:52 +030018 case CRUSH_BUCKET_STRAW2: return "straw2";
Sage Weilc6cf7262009-11-06 16:39:26 -080019 default: return "unknown";
20 }
21}
22
Sage Weil5ecc0a02009-10-06 11:31:11 -070023/**
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 */
Sage Weil8b12d472012-05-07 15:38:35 -070028int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
Sage Weil5ecc0a02009-10-06 11:31:11 -070029{
Sage Weil8b12d472012-05-07 15:38:35 -070030 if ((__u32)p >= b->size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070031 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:
Sage Weilf671d4c2012-05-07 15:36:49 -070039 return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
Sage Weil5ecc0a02009-10-06 11:31:11 -070040 case CRUSH_BUCKET_STRAW:
41 return ((struct crush_bucket_straw *)b)->item_weights[p];
Ilya Dryomov958a2762015-04-14 16:54:52 +030042 case CRUSH_BUCKET_STRAW2:
43 return ((struct crush_bucket_straw2 *)b)->item_weights[p];
Sage Weil5ecc0a02009-10-06 11:31:11 -070044 }
45 return 0;
46}
47
Sage Weil5ecc0a02009-10-06 11:31:11 -070048void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
49{
Sage Weil5ecc0a02009-10-06 11:31:11 -070050 kfree(b->h.items);
51 kfree(b);
52}
53
54void crush_destroy_bucket_list(struct crush_bucket_list *b)
55{
56 kfree(b->item_weights);
57 kfree(b->sum_weights);
Sage Weil5ecc0a02009-10-06 11:31:11 -070058 kfree(b->h.items);
59 kfree(b);
60}
61
62void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
63{
Sage Weil6eb43f42012-05-07 15:37:05 -070064 kfree(b->h.items);
Sage Weil5ecc0a02009-10-06 11:31:11 -070065 kfree(b->node_weights);
66 kfree(b);
67}
68
69void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
70{
71 kfree(b->straws);
72 kfree(b->item_weights);
Sage Weil5ecc0a02009-10-06 11:31:11 -070073 kfree(b->h.items);
74 kfree(b);
75}
76
Ilya Dryomov958a2762015-04-14 16:54:52 +030077void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
78{
79 kfree(b->item_weights);
Ilya Dryomov958a2762015-04-14 16:54:52 +030080 kfree(b->h.items);
81 kfree(b);
82}
83
Sage Weil5ecc0a02009-10-06 11:31:11 -070084void 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;
Ilya Dryomov958a2762015-04-14 16:54:52 +030099 case CRUSH_BUCKET_STRAW2:
100 crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b);
101 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700102 }
103}
104
105/**
106 * crush_destroy - Destroy a crush_map
107 * @map: crush_map pointer
108 */
109void crush_destroy(struct crush_map *map)
110{
Sage Weil5ecc0a02009-10-06 11:31:11 -0700111 /* buckets */
112 if (map->buckets) {
Sage Weil8b12d472012-05-07 15:38:35 -0700113 __s32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700114 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) {
Sage Weil8b12d472012-05-07 15:38:35 -0700124 __u32 b;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700125 for (b = 0; b < map->max_rules; b++)
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200126 crush_destroy_rule(map->rules[b]);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700127 kfree(map->rules);
128 }
129
Ilya Dryomovb459be72015-06-12 13:21:07 +0300130#ifndef __KERNEL__
131 kfree(map->choose_tries);
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200132#else
133 clear_choose_args(map);
Ilya Dryomovb459be72015-06-12 13:21:07 +0300134#endif
Sage Weil5ecc0a02009-10-06 11:31:11 -0700135 kfree(map);
136}
137
Ilya Dryomovbfb16d72013-12-24 21:19:24 +0200138void crush_destroy_rule(struct crush_rule *rule)
139{
140 kfree(rule);
141}