^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifdef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # include <linux/crush/crush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # include "crush_compat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # include "crush.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) const char *crush_bucket_alg_name(int alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) switch (alg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) case CRUSH_BUCKET_UNIFORM: return "uniform";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) case CRUSH_BUCKET_LIST: return "list";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) case CRUSH_BUCKET_TREE: return "tree";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) case CRUSH_BUCKET_STRAW: return "straw";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) case CRUSH_BUCKET_STRAW2: return "straw2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) default: return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * crush_get_bucket_item_weight - Get weight of an item in given bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @b: bucket pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @p: item index in bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int crush_get_bucket_item_weight(const struct crush_bucket *b, int p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if ((__u32)p >= b->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) switch (b->alg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) case CRUSH_BUCKET_UNIFORM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return ((struct crush_bucket_uniform *)b)->item_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) case CRUSH_BUCKET_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return ((struct crush_bucket_list *)b)->item_weights[p];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) case CRUSH_BUCKET_TREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case CRUSH_BUCKET_STRAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return ((struct crush_bucket_straw *)b)->item_weights[p];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) case CRUSH_BUCKET_STRAW2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return ((struct crush_bucket_straw2 *)b)->item_weights[p];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kfree(b->h.items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void crush_destroy_bucket_list(struct crush_bucket_list *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfree(b->item_weights);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) kfree(b->sum_weights);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) kfree(b->h.items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void crush_destroy_bucket_tree(struct crush_bucket_tree *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) kfree(b->h.items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree(b->node_weights);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void crush_destroy_bucket_straw(struct crush_bucket_straw *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kfree(b->straws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kfree(b->item_weights);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kfree(b->h.items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) kfree(b->item_weights);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) kfree(b->h.items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) void crush_destroy_bucket(struct crush_bucket *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) switch (b->alg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case CRUSH_BUCKET_UNIFORM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case CRUSH_BUCKET_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) crush_destroy_bucket_list((struct crush_bucket_list *)b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case CRUSH_BUCKET_TREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) crush_destroy_bucket_tree((struct crush_bucket_tree *)b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) case CRUSH_BUCKET_STRAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) crush_destroy_bucket_straw((struct crush_bucket_straw *)b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case CRUSH_BUCKET_STRAW2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * crush_destroy - Destroy a crush_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * @map: crush_map pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void crush_destroy(struct crush_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* buckets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (map->buckets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) __s32 b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) for (b = 0; b < map->max_buckets; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (map->buckets[b] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) crush_destroy_bucket(map->buckets[b]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kfree(map->buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* rules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (map->rules) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) __u32 b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) for (b = 0; b < map->max_rules; b++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) crush_destroy_rule(map->rules[b]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) kfree(map->rules);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #ifndef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kfree(map->choose_tries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) clear_crush_names(&map->type_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) clear_crush_names(&map->names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) clear_choose_args(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) kfree(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void crush_destroy_rule(struct crush_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) kfree(rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }