3 # include <linux/slab.h>
7 # define kfree(x) do { if (x) free(x); } while (0)
8 # define BUG_ON(x) assert(!(x))
11 #include <linux/crush/crush.h>
13 const char *crush_bucket_alg_name(int 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";
25 * crush_get_bucket_item_weight - Get weight of an item in given bucket
27 * @p: item index in bucket
29 int crush_get_bucket_item_weight(struct crush_bucket
*b
, int p
)
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
:
41 return ((struct crush_bucket_tree
*)b
)->node_weights
[p
];
43 case CRUSH_BUCKET_STRAW
:
44 return ((struct crush_bucket_straw
*)b
)->item_weights
[p
];
50 * crush_calc_parents - Calculate parent vectors for the given crush map.
51 * @map: crush_map pointer
53 void crush_calc_parents(struct crush_map
*map
)
57 for (b
= 0; b
< map
->max_buckets
; b
++) {
58 if (map
->buckets
[b
] == NULL
)
60 for (i
= 0; i
< map
->buckets
[b
]->size
; i
++) {
61 c
= map
->buckets
[b
]->items
[i
];
62 BUG_ON(c
>= map
->max_devices
||
63 c
< -map
->max_buckets
);
65 map
->device_parents
[c
] = map
->buckets
[b
]->id
;
67 map
->bucket_parents
[-1-c
] = map
->buckets
[b
]->id
;
72 void crush_destroy_bucket_uniform(struct crush_bucket_uniform
*b
)
79 void crush_destroy_bucket_list(struct crush_bucket_list
*b
)
81 kfree(b
->item_weights
);
82 kfree(b
->sum_weights
);
88 void crush_destroy_bucket_tree(struct crush_bucket_tree
*b
)
90 kfree(b
->node_weights
);
94 void crush_destroy_bucket_straw(struct crush_bucket_straw
*b
)
97 kfree(b
->item_weights
);
103 void crush_destroy_bucket(struct crush_bucket
*b
)
106 case CRUSH_BUCKET_UNIFORM
:
107 crush_destroy_bucket_uniform((struct crush_bucket_uniform
*)b
);
109 case CRUSH_BUCKET_LIST
:
110 crush_destroy_bucket_list((struct crush_bucket_list
*)b
);
112 case CRUSH_BUCKET_TREE
:
113 crush_destroy_bucket_tree((struct crush_bucket_tree
*)b
);
115 case CRUSH_BUCKET_STRAW
:
116 crush_destroy_bucket_straw((struct crush_bucket_straw
*)b
);
122 * crush_destroy - Destroy a crush_map
123 * @map: crush_map pointer
125 void crush_destroy(struct crush_map
*map
)
131 for (b
= 0; b
< map
->max_buckets
; b
++) {
132 if (map
->buckets
[b
] == NULL
)
134 crush_destroy_bucket(map
->buckets
[b
]);
141 for (b
= 0; b
< map
->max_rules
; b
++)
142 kfree(map
->rules
[b
]);
146 kfree(map
->bucket_parents
);
147 kfree(map
->device_parents
);