Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 2 | #ifdef __KERNEL__ |
| 3 | # include <linux/slab.h> |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 4 | # include <linux/crush/crush.h> |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 5 | void clear_choose_args(struct crush_map *c); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 6 | #else |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 7 | # include "crush_compat.h" |
| 8 | # include "crush.h" |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 9 | #endif |
| 10 | |
Sage Weil | c6cf726 | 2009-11-06 16:39:26 -0800 | [diff] [blame] | 11 | const 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 Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 18 | case CRUSH_BUCKET_STRAW2: return "straw2"; |
Sage Weil | c6cf726 | 2009-11-06 16:39:26 -0800 | [diff] [blame] | 19 | default: return "unknown"; |
| 20 | } |
| 21 | } |
| 22 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 23 | /** |
| 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 Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 28 | int crush_get_bucket_item_weight(const struct crush_bucket *b, int p) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 29 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 30 | if ((__u32)p >= b->size) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 31 | 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 Weil | f671d4c | 2012-05-07 15:36:49 -0700 | [diff] [blame] | 39 | return ((struct crush_bucket_tree *)b)->node_weights[crush_calc_tree_node(p)]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 40 | case CRUSH_BUCKET_STRAW: |
| 41 | return ((struct crush_bucket_straw *)b)->item_weights[p]; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 42 | case CRUSH_BUCKET_STRAW2: |
| 43 | return ((struct crush_bucket_straw2 *)b)->item_weights[p]; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 44 | } |
| 45 | return 0; |
| 46 | } |
| 47 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 48 | void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b) |
| 49 | { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 50 | kfree(b->h.items); |
| 51 | kfree(b); |
| 52 | } |
| 53 | |
| 54 | void crush_destroy_bucket_list(struct crush_bucket_list *b) |
| 55 | { |
| 56 | kfree(b->item_weights); |
| 57 | kfree(b->sum_weights); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 58 | kfree(b->h.items); |
| 59 | kfree(b); |
| 60 | } |
| 61 | |
| 62 | void crush_destroy_bucket_tree(struct crush_bucket_tree *b) |
| 63 | { |
Sage Weil | 6eb43f4 | 2012-05-07 15:37:05 -0700 | [diff] [blame] | 64 | kfree(b->h.items); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 65 | kfree(b->node_weights); |
| 66 | kfree(b); |
| 67 | } |
| 68 | |
| 69 | void crush_destroy_bucket_straw(struct crush_bucket_straw *b) |
| 70 | { |
| 71 | kfree(b->straws); |
| 72 | kfree(b->item_weights); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 73 | kfree(b->h.items); |
| 74 | kfree(b); |
| 75 | } |
| 76 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 77 | void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b) |
| 78 | { |
| 79 | kfree(b->item_weights); |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 80 | kfree(b->h.items); |
| 81 | kfree(b); |
| 82 | } |
| 83 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 84 | void 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 Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 99 | case CRUSH_BUCKET_STRAW2: |
| 100 | crush_destroy_bucket_straw2((struct crush_bucket_straw2 *)b); |
| 101 | break; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * crush_destroy - Destroy a crush_map |
| 107 | * @map: crush_map pointer |
| 108 | */ |
| 109 | void crush_destroy(struct crush_map *map) |
| 110 | { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 111 | /* buckets */ |
| 112 | if (map->buckets) { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 113 | __s32 b; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 114 | 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 Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 124 | __u32 b; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 125 | for (b = 0; b < map->max_rules; b++) |
Ilya Dryomov | bfb16d7 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 126 | crush_destroy_rule(map->rules[b]); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 127 | kfree(map->rules); |
| 128 | } |
| 129 | |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 130 | #ifndef __KERNEL__ |
| 131 | kfree(map->choose_tries); |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 132 | #else |
| 133 | clear_choose_args(map); |
Ilya Dryomov | b459be7 | 2015-06-12 13:21:07 +0300 | [diff] [blame] | 134 | #endif |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 135 | kfree(map); |
| 136 | } |
| 137 | |
Ilya Dryomov | bfb16d7 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 138 | void crush_destroy_rule(struct crush_rule *rule) |
| 139 | { |
| 140 | kfree(rule); |
| 141 | } |