162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#ifdef __KERNEL__
362306a36Sopenharmony_ci# include <linux/slab.h>
462306a36Sopenharmony_ci# include <linux/crush/crush.h>
562306a36Sopenharmony_ci#else
662306a36Sopenharmony_ci# include "crush_compat.h"
762306a36Sopenharmony_ci# include "crush.h"
862306a36Sopenharmony_ci#endif
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciconst char *crush_bucket_alg_name(int alg)
1162306a36Sopenharmony_ci{
1262306a36Sopenharmony_ci	switch (alg) {
1362306a36Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM: return "uniform";
1462306a36Sopenharmony_ci	case CRUSH_BUCKET_LIST: return "list";
1562306a36Sopenharmony_ci	case CRUSH_BUCKET_TREE: return "tree";
1662306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW: return "straw";
1762306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW2: return "straw2";
1862306a36Sopenharmony_ci	default: return "unknown";
1962306a36Sopenharmony_ci	}
2062306a36Sopenharmony_ci}
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/**
2362306a36Sopenharmony_ci * crush_get_bucket_item_weight - Get weight of an item in given bucket
2462306a36Sopenharmony_ci * @b: bucket pointer
2562306a36Sopenharmony_ci * @p: item index in bucket
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ciint crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	if ((__u32)p >= b->size)
3062306a36Sopenharmony_ci		return 0;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	switch (b->alg) {
3362306a36Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM:
3462306a36Sopenharmony_ci		return ((struct crush_bucket_uniform *)b)->item_weight;
3562306a36Sopenharmony_ci	case CRUSH_BUCKET_LIST:
3662306a36Sopenharmony_ci		return ((struct crush_bucket_list *)b)->item_weights[p];
3762306a36Sopenharmony_ci	case CRUSH_BUCKET_TREE:
3862306a36Sopenharmony_ci		return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
3962306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW:
4062306a36Sopenharmony_ci		return ((struct crush_bucket_straw *)b)->item_weights[p];
4162306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW2:
4262306a36Sopenharmony_ci		return ((struct crush_bucket_straw2 *)b)->item_weights[p];
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci	return 0;
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_civoid crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	kfree(b->h.items);
5062306a36Sopenharmony_ci	kfree(b);
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_civoid crush_destroy_bucket_list(struct crush_bucket_list *b)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	kfree(b->item_weights);
5662306a36Sopenharmony_ci	kfree(b->sum_weights);
5762306a36Sopenharmony_ci	kfree(b->h.items);
5862306a36Sopenharmony_ci	kfree(b);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_civoid crush_destroy_bucket_tree(struct crush_bucket_tree *b)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	kfree(b->h.items);
6462306a36Sopenharmony_ci	kfree(b->node_weights);
6562306a36Sopenharmony_ci	kfree(b);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_civoid crush_destroy_bucket_straw(struct crush_bucket_straw *b)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	kfree(b->straws);
7162306a36Sopenharmony_ci	kfree(b->item_weights);
7262306a36Sopenharmony_ci	kfree(b->h.items);
7362306a36Sopenharmony_ci	kfree(b);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_civoid crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	kfree(b->item_weights);
7962306a36Sopenharmony_ci	kfree(b->h.items);
8062306a36Sopenharmony_ci	kfree(b);
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_civoid crush_destroy_bucket(struct crush_bucket *b)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	switch (b->alg) {
8662306a36Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM:
8762306a36Sopenharmony_ci		crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b);
8862306a36Sopenharmony_ci		break;
8962306a36Sopenharmony_ci	case CRUSH_BUCKET_LIST:
9062306a36Sopenharmony_ci		crush_destroy_bucket_list((struct crush_bucket_list *)b);
9162306a36Sopenharmony_ci		break;
9262306a36Sopenharmony_ci	case CRUSH_BUCKET_TREE:
9362306a36Sopenharmony_ci		crush_destroy_bucket_tree((struct crush_bucket_tree *)b);
9462306a36Sopenharmony_ci		break;
9562306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW:
9662306a36Sopenharmony_ci		crush_destroy_bucket_straw((struct crush_bucket_straw *)b);
9762306a36Sopenharmony_ci		break;
9862306a36Sopenharmony_ci	case CRUSH_BUCKET_STRAW2:
9962306a36Sopenharmony_ci		crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b);
10062306a36Sopenharmony_ci		break;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/**
10562306a36Sopenharmony_ci * crush_destroy - Destroy a crush_map
10662306a36Sopenharmony_ci * @map: crush_map pointer
10762306a36Sopenharmony_ci */
10862306a36Sopenharmony_civoid crush_destroy(struct crush_map *map)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	/* buckets */
11162306a36Sopenharmony_ci	if (map->buckets) {
11262306a36Sopenharmony_ci		__s32 b;
11362306a36Sopenharmony_ci		for (b = 0; b < map->max_buckets; b++) {
11462306a36Sopenharmony_ci			if (map->buckets[b] == NULL)
11562306a36Sopenharmony_ci				continue;
11662306a36Sopenharmony_ci			crush_destroy_bucket(map->buckets[b]);
11762306a36Sopenharmony_ci		}
11862306a36Sopenharmony_ci		kfree(map->buckets);
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* rules */
12262306a36Sopenharmony_ci	if (map->rules) {
12362306a36Sopenharmony_ci		__u32 b;
12462306a36Sopenharmony_ci		for (b = 0; b < map->max_rules; b++)
12562306a36Sopenharmony_ci			crush_destroy_rule(map->rules[b]);
12662306a36Sopenharmony_ci		kfree(map->rules);
12762306a36Sopenharmony_ci	}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#ifndef __KERNEL__
13062306a36Sopenharmony_ci	kfree(map->choose_tries);
13162306a36Sopenharmony_ci#else
13262306a36Sopenharmony_ci	clear_crush_names(&map->type_names);
13362306a36Sopenharmony_ci	clear_crush_names(&map->names);
13462306a36Sopenharmony_ci	clear_choose_args(map);
13562306a36Sopenharmony_ci#endif
13662306a36Sopenharmony_ci	kfree(map);
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_civoid crush_destroy_rule(struct crush_rule *rule)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	kfree(rule);
14262306a36Sopenharmony_ci}
143