xref: /kernel/linux/linux-5.10/net/ceph/crush/crush.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#ifdef __KERNEL__
38c2ecf20Sopenharmony_ci# include <linux/slab.h>
48c2ecf20Sopenharmony_ci# include <linux/crush/crush.h>
58c2ecf20Sopenharmony_ci#else
68c2ecf20Sopenharmony_ci# include "crush_compat.h"
78c2ecf20Sopenharmony_ci# include "crush.h"
88c2ecf20Sopenharmony_ci#endif
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciconst char *crush_bucket_alg_name(int alg)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	switch (alg) {
138c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM: return "uniform";
148c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_LIST: return "list";
158c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_TREE: return "tree";
168c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW: return "straw";
178c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW2: return "straw2";
188c2ecf20Sopenharmony_ci	default: return "unknown";
198c2ecf20Sopenharmony_ci	}
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * crush_get_bucket_item_weight - Get weight of an item in given bucket
248c2ecf20Sopenharmony_ci * @b: bucket pointer
258c2ecf20Sopenharmony_ci * @p: item index in bucket
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ciint crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	if ((__u32)p >= b->size)
308c2ecf20Sopenharmony_ci		return 0;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	switch (b->alg) {
338c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM:
348c2ecf20Sopenharmony_ci		return ((struct crush_bucket_uniform *)b)->item_weight;
358c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_LIST:
368c2ecf20Sopenharmony_ci		return ((struct crush_bucket_list *)b)->item_weights[p];
378c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_TREE:
388c2ecf20Sopenharmony_ci		return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
398c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW:
408c2ecf20Sopenharmony_ci		return ((struct crush_bucket_straw *)b)->item_weights[p];
418c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW2:
428c2ecf20Sopenharmony_ci		return ((struct crush_bucket_straw2 *)b)->item_weights[p];
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci	return 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_civoid crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	kfree(b->h.items);
508c2ecf20Sopenharmony_ci	kfree(b);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_civoid crush_destroy_bucket_list(struct crush_bucket_list *b)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	kfree(b->item_weights);
568c2ecf20Sopenharmony_ci	kfree(b->sum_weights);
578c2ecf20Sopenharmony_ci	kfree(b->h.items);
588c2ecf20Sopenharmony_ci	kfree(b);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_civoid crush_destroy_bucket_tree(struct crush_bucket_tree *b)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	kfree(b->h.items);
648c2ecf20Sopenharmony_ci	kfree(b->node_weights);
658c2ecf20Sopenharmony_ci	kfree(b);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_civoid crush_destroy_bucket_straw(struct crush_bucket_straw *b)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	kfree(b->straws);
718c2ecf20Sopenharmony_ci	kfree(b->item_weights);
728c2ecf20Sopenharmony_ci	kfree(b->h.items);
738c2ecf20Sopenharmony_ci	kfree(b);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_civoid crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	kfree(b->item_weights);
798c2ecf20Sopenharmony_ci	kfree(b->h.items);
808c2ecf20Sopenharmony_ci	kfree(b);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid crush_destroy_bucket(struct crush_bucket *b)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	switch (b->alg) {
868c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_UNIFORM:
878c2ecf20Sopenharmony_ci		crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b);
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_LIST:
908c2ecf20Sopenharmony_ci		crush_destroy_bucket_list((struct crush_bucket_list *)b);
918c2ecf20Sopenharmony_ci		break;
928c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_TREE:
938c2ecf20Sopenharmony_ci		crush_destroy_bucket_tree((struct crush_bucket_tree *)b);
948c2ecf20Sopenharmony_ci		break;
958c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW:
968c2ecf20Sopenharmony_ci		crush_destroy_bucket_straw((struct crush_bucket_straw *)b);
978c2ecf20Sopenharmony_ci		break;
988c2ecf20Sopenharmony_ci	case CRUSH_BUCKET_STRAW2:
998c2ecf20Sopenharmony_ci		crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b);
1008c2ecf20Sopenharmony_ci		break;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/**
1058c2ecf20Sopenharmony_ci * crush_destroy - Destroy a crush_map
1068c2ecf20Sopenharmony_ci * @map: crush_map pointer
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_civoid crush_destroy(struct crush_map *map)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	/* buckets */
1118c2ecf20Sopenharmony_ci	if (map->buckets) {
1128c2ecf20Sopenharmony_ci		__s32 b;
1138c2ecf20Sopenharmony_ci		for (b = 0; b < map->max_buckets; b++) {
1148c2ecf20Sopenharmony_ci			if (map->buckets[b] == NULL)
1158c2ecf20Sopenharmony_ci				continue;
1168c2ecf20Sopenharmony_ci			crush_destroy_bucket(map->buckets[b]);
1178c2ecf20Sopenharmony_ci		}
1188c2ecf20Sopenharmony_ci		kfree(map->buckets);
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* rules */
1228c2ecf20Sopenharmony_ci	if (map->rules) {
1238c2ecf20Sopenharmony_ci		__u32 b;
1248c2ecf20Sopenharmony_ci		for (b = 0; b < map->max_rules; b++)
1258c2ecf20Sopenharmony_ci			crush_destroy_rule(map->rules[b]);
1268c2ecf20Sopenharmony_ci		kfree(map->rules);
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#ifndef __KERNEL__
1308c2ecf20Sopenharmony_ci	kfree(map->choose_tries);
1318c2ecf20Sopenharmony_ci#else
1328c2ecf20Sopenharmony_ci	clear_crush_names(&map->type_names);
1338c2ecf20Sopenharmony_ci	clear_crush_names(&map->names);
1348c2ecf20Sopenharmony_ci	clear_choose_args(map);
1358c2ecf20Sopenharmony_ci#endif
1368c2ecf20Sopenharmony_ci	kfree(map);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_civoid crush_destroy_rule(struct crush_rule *rule)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	kfree(rule);
1428c2ecf20Sopenharmony_ci}
143