Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 1 | |
| 2 | #ifdef __KERNEL__ |
| 3 | # include <linux/slab.h> |
| 4 | #else |
| 5 | # include <stdlib.h> |
| 6 | # include <assert.h> |
| 7 | # define kfree(x) do { if (x) free(x); } while (0) |
| 8 | # define BUG_ON(x) assert(!(x)) |
| 9 | #endif |
| 10 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 11 | #include <linux/crush/crush.h> |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 12 | |
Sage Weil | c6cf726 | 2009-11-06 16:39:26 -0800 | [diff] [blame] | 13 | const char *crush_bucket_alg_name(int alg) |
| 14 | { |
| 15 | switch (alg) { |
| 16 | case CRUSH_BUCKET_UNIFORM: return "uniform"; |
| 17 | case CRUSH_BUCKET_LIST: return "list"; |
| 18 | case CRUSH_BUCKET_TREE: return "tree"; |
| 19 | case CRUSH_BUCKET_STRAW: return "straw"; |
| 20 | default: return "unknown"; |
| 21 | } |
| 22 | } |
| 23 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 24 | /** |
| 25 | * crush_get_bucket_item_weight - Get weight of an item in given bucket |
| 26 | * @b: bucket pointer |
| 27 | * @p: item index in bucket |
| 28 | */ |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 29 | 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] | 30 | { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 31 | if ((__u32)p >= b->size) |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | |
| 34 | switch (b->alg) { |
| 35 | case CRUSH_BUCKET_UNIFORM: |
| 36 | return ((struct crush_bucket_uniform *)b)->item_weight; |
| 37 | case CRUSH_BUCKET_LIST: |
| 38 | return ((struct crush_bucket_list *)b)->item_weights[p]; |
| 39 | case CRUSH_BUCKET_TREE: |
Sage Weil | f671d4c | 2012-05-07 15:36:49 -0700 | [diff] [blame] | 40 | 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] | 41 | case CRUSH_BUCKET_STRAW: |
| 42 | return ((struct crush_bucket_straw *)b)->item_weights[p]; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 47 | void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b) |
| 48 | { |
| 49 | kfree(b->h.perm); |
| 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); |
| 58 | kfree(b->h.perm); |
| 59 | kfree(b->h.items); |
| 60 | kfree(b); |
| 61 | } |
| 62 | |
| 63 | void crush_destroy_bucket_tree(struct crush_bucket_tree *b) |
| 64 | { |
Sage Weil | 6eb43f4 | 2012-05-07 15:37:05 -0700 | [diff] [blame] | 65 | kfree(b->h.perm); |
| 66 | kfree(b->h.items); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 67 | kfree(b->node_weights); |
| 68 | kfree(b); |
| 69 | } |
| 70 | |
| 71 | void crush_destroy_bucket_straw(struct crush_bucket_straw *b) |
| 72 | { |
| 73 | kfree(b->straws); |
| 74 | kfree(b->item_weights); |
| 75 | kfree(b->h.perm); |
| 76 | kfree(b->h.items); |
| 77 | kfree(b); |
| 78 | } |
| 79 | |
| 80 | void crush_destroy_bucket(struct crush_bucket *b) |
| 81 | { |
| 82 | switch (b->alg) { |
| 83 | case CRUSH_BUCKET_UNIFORM: |
| 84 | crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b); |
| 85 | break; |
| 86 | case CRUSH_BUCKET_LIST: |
| 87 | crush_destroy_bucket_list((struct crush_bucket_list *)b); |
| 88 | break; |
| 89 | case CRUSH_BUCKET_TREE: |
| 90 | crush_destroy_bucket_tree((struct crush_bucket_tree *)b); |
| 91 | break; |
| 92 | case CRUSH_BUCKET_STRAW: |
| 93 | crush_destroy_bucket_straw((struct crush_bucket_straw *)b); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * crush_destroy - Destroy a crush_map |
| 100 | * @map: crush_map pointer |
| 101 | */ |
| 102 | void crush_destroy(struct crush_map *map) |
| 103 | { |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 104 | /* buckets */ |
| 105 | if (map->buckets) { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 106 | __s32 b; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 107 | for (b = 0; b < map->max_buckets; b++) { |
| 108 | if (map->buckets[b] == NULL) |
| 109 | continue; |
| 110 | crush_destroy_bucket(map->buckets[b]); |
| 111 | } |
| 112 | kfree(map->buckets); |
| 113 | } |
| 114 | |
| 115 | /* rules */ |
| 116 | if (map->rules) { |
Sage Weil | 8b12d47 | 2012-05-07 15:38:35 -0700 | [diff] [blame] | 117 | __u32 b; |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 118 | for (b = 0; b < map->max_rules; b++) |
Ilya Dryomov | bfb16d7 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 119 | crush_destroy_rule(map->rules[b]); |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 120 | kfree(map->rules); |
| 121 | } |
| 122 | |
Sage Weil | 5ecc0a0 | 2009-10-06 11:31:11 -0700 | [diff] [blame] | 123 | kfree(map); |
| 124 | } |
| 125 | |
Ilya Dryomov | bfb16d7 | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 126 | void crush_destroy_rule(struct crush_rule *rule) |
| 127 | { |
| 128 | kfree(rule); |
| 129 | } |