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