Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / md / persistent-data / dm-bitset.c
blobb7208d82e748a0992081e8e4fae88a6a3d306978
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-bitset.h"
8 #include "dm-transaction-manager.h"
10 #include <linux/export.h>
11 #include <linux/device-mapper.h>
13 #define DM_MSG_PREFIX "bitset"
14 #define BITS_PER_ARRAY_ENTRY 64
16 /*----------------------------------------------------------------*/
18 static struct dm_btree_value_type bitset_bvt = {
19 .context = NULL,
20 .size = sizeof(__le64),
21 .inc = NULL,
22 .dec = NULL,
23 .equal = NULL,
26 /*----------------------------------------------------------------*/
28 void dm_disk_bitset_init(struct dm_transaction_manager *tm,
29 struct dm_disk_bitset *info)
31 dm_array_info_init(&info->array_info, tm, &bitset_bvt);
32 info->current_index_set = false;
34 EXPORT_SYMBOL_GPL(dm_disk_bitset_init);
36 int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *root)
38 return dm_array_empty(&info->array_info, root);
40 EXPORT_SYMBOL_GPL(dm_bitset_empty);
42 struct packer_context {
43 bit_value_fn fn;
44 unsigned nr_bits;
45 void *context;
48 static int pack_bits(uint32_t index, void *value, void *context)
50 int r;
51 struct packer_context *p = context;
52 unsigned bit, nr = min(64u, p->nr_bits - (index * 64));
53 uint64_t word = 0;
54 bool bv;
56 for (bit = 0; bit < nr; bit++) {
57 r = p->fn(index * 64 + bit, &bv, p->context);
58 if (r)
59 return r;
61 if (bv)
62 set_bit(bit, (unsigned long *) &word);
63 else
64 clear_bit(bit, (unsigned long *) &word);
67 *((__le64 *) value) = cpu_to_le64(word);
69 return 0;
72 int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
73 uint32_t size, bit_value_fn fn, void *context)
75 struct packer_context p;
76 p.fn = fn;
77 p.nr_bits = size;
78 p.context = context;
80 return dm_array_new(&info->array_info, root, dm_div_up(size, 64), pack_bits, &p);
82 EXPORT_SYMBOL_GPL(dm_bitset_new);
84 int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t root,
85 uint32_t old_nr_entries, uint32_t new_nr_entries,
86 bool default_value, dm_block_t *new_root)
88 uint32_t old_blocks = dm_div_up(old_nr_entries, BITS_PER_ARRAY_ENTRY);
89 uint32_t new_blocks = dm_div_up(new_nr_entries, BITS_PER_ARRAY_ENTRY);
90 __le64 value = default_value ? cpu_to_le64(~0) : cpu_to_le64(0);
92 __dm_bless_for_disk(&value);
93 return dm_array_resize(&info->array_info, root, old_blocks, new_blocks,
94 &value, new_root);
96 EXPORT_SYMBOL_GPL(dm_bitset_resize);
98 int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root)
100 return dm_array_del(&info->array_info, root);
102 EXPORT_SYMBOL_GPL(dm_bitset_del);
104 int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
105 dm_block_t *new_root)
107 int r;
108 __le64 value;
110 if (!info->current_index_set || !info->dirty)
111 return 0;
113 value = cpu_to_le64(info->current_bits);
115 __dm_bless_for_disk(&value);
116 r = dm_array_set_value(&info->array_info, root, info->current_index,
117 &value, new_root);
118 if (r)
119 return r;
121 info->current_index_set = false;
122 info->dirty = false;
124 return 0;
126 EXPORT_SYMBOL_GPL(dm_bitset_flush);
128 static int read_bits(struct dm_disk_bitset *info, dm_block_t root,
129 uint32_t array_index)
131 int r;
132 __le64 value;
134 r = dm_array_get_value(&info->array_info, root, array_index, &value);
135 if (r)
136 return r;
138 info->current_bits = le64_to_cpu(value);
139 info->current_index_set = true;
140 info->current_index = array_index;
141 info->dirty = false;
143 return 0;
146 static int get_array_entry(struct dm_disk_bitset *info, dm_block_t root,
147 uint32_t index, dm_block_t *new_root)
149 int r;
150 unsigned array_index = index / BITS_PER_ARRAY_ENTRY;
152 if (info->current_index_set) {
153 if (info->current_index == array_index)
154 return 0;
156 r = dm_bitset_flush(info, root, new_root);
157 if (r)
158 return r;
161 return read_bits(info, root, array_index);
164 int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
165 uint32_t index, dm_block_t *new_root)
167 int r;
168 unsigned b = index % BITS_PER_ARRAY_ENTRY;
170 r = get_array_entry(info, root, index, new_root);
171 if (r)
172 return r;
174 set_bit(b, (unsigned long *) &info->current_bits);
175 info->dirty = true;
177 return 0;
179 EXPORT_SYMBOL_GPL(dm_bitset_set_bit);
181 int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
182 uint32_t index, dm_block_t *new_root)
184 int r;
185 unsigned b = index % BITS_PER_ARRAY_ENTRY;
187 r = get_array_entry(info, root, index, new_root);
188 if (r)
189 return r;
191 clear_bit(b, (unsigned long *) &info->current_bits);
192 info->dirty = true;
194 return 0;
196 EXPORT_SYMBOL_GPL(dm_bitset_clear_bit);
198 int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
199 uint32_t index, dm_block_t *new_root, bool *result)
201 int r;
202 unsigned b = index % BITS_PER_ARRAY_ENTRY;
204 r = get_array_entry(info, root, index, new_root);
205 if (r)
206 return r;
208 *result = test_bit(b, (unsigned long *) &info->current_bits);
209 return 0;
211 EXPORT_SYMBOL_GPL(dm_bitset_test_bit);
213 static int cursor_next_array_entry(struct dm_bitset_cursor *c)
215 int r;
216 __le64 *value;
218 r = dm_array_cursor_next(&c->cursor);
219 if (r)
220 return r;
222 dm_array_cursor_get_value(&c->cursor, (void **) &value);
223 c->array_index++;
224 c->bit_index = 0;
225 c->current_bits = le64_to_cpu(*value);
226 return 0;
229 int dm_bitset_cursor_begin(struct dm_disk_bitset *info,
230 dm_block_t root, uint32_t nr_entries,
231 struct dm_bitset_cursor *c)
233 int r;
234 __le64 *value;
236 if (!nr_entries)
237 return -ENODATA;
239 c->info = info;
240 c->entries_remaining = nr_entries;
242 r = dm_array_cursor_begin(&info->array_info, root, &c->cursor);
243 if (r)
244 return r;
246 dm_array_cursor_get_value(&c->cursor, (void **) &value);
247 c->array_index = 0;
248 c->bit_index = 0;
249 c->current_bits = le64_to_cpu(*value);
251 return r;
253 EXPORT_SYMBOL_GPL(dm_bitset_cursor_begin);
255 void dm_bitset_cursor_end(struct dm_bitset_cursor *c)
257 return dm_array_cursor_end(&c->cursor);
259 EXPORT_SYMBOL_GPL(dm_bitset_cursor_end);
261 int dm_bitset_cursor_next(struct dm_bitset_cursor *c)
263 int r = 0;
265 if (!c->entries_remaining)
266 return -ENODATA;
268 c->entries_remaining--;
269 if (++c->bit_index > 63)
270 r = cursor_next_array_entry(c);
272 return r;
274 EXPORT_SYMBOL_GPL(dm_bitset_cursor_next);
276 int dm_bitset_cursor_skip(struct dm_bitset_cursor *c, uint32_t count)
278 int r;
279 __le64 *value;
280 uint32_t nr_array_skip;
281 uint32_t remaining_in_word = 64 - c->bit_index;
283 if (c->entries_remaining < count)
284 return -ENODATA;
286 if (count < remaining_in_word) {
287 c->bit_index += count;
288 c->entries_remaining -= count;
289 return 0;
291 } else {
292 c->entries_remaining -= remaining_in_word;
293 count -= remaining_in_word;
296 nr_array_skip = (count / 64) + 1;
297 r = dm_array_cursor_skip(&c->cursor, nr_array_skip);
298 if (r)
299 return r;
301 dm_array_cursor_get_value(&c->cursor, (void **) &value);
302 c->entries_remaining -= count;
303 c->array_index += nr_array_skip;
304 c->bit_index = count & 63;
305 c->current_bits = le64_to_cpu(*value);
307 return 0;
309 EXPORT_SYMBOL_GPL(dm_bitset_cursor_skip);
311 bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c)
313 return test_bit(c->bit_index, (unsigned long *) &c->current_bits);
315 EXPORT_SYMBOL_GPL(dm_bitset_cursor_get_value);
317 /*----------------------------------------------------------------*/