Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / md / persistent-data / dm-bitset.c
blob00c0a3f186b7189104687b9b4efc2049e90459b1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Red Hat, Inc.
5 * This file is released under the GPL.
6 */
8 #include "dm-bitset.h"
9 #include "dm-transaction-manager.h"
11 #include <linux/export.h>
12 #include <linux/device-mapper.h>
14 #define DM_MSG_PREFIX "bitset"
15 #define BITS_PER_ARRAY_ENTRY 64
17 /*----------------------------------------------------------------*/
19 static struct dm_btree_value_type bitset_bvt = {
20 .context = NULL,
21 .size = sizeof(__le64),
22 .inc = NULL,
23 .dec = NULL,
24 .equal = NULL,
27 /*----------------------------------------------------------------*/
29 void dm_disk_bitset_init(struct dm_transaction_manager *tm,
30 struct dm_disk_bitset *info)
32 dm_array_info_init(&info->array_info, tm, &bitset_bvt);
33 info->current_index_set = false;
35 EXPORT_SYMBOL_GPL(dm_disk_bitset_init);
37 int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *root)
39 return dm_array_empty(&info->array_info, root);
41 EXPORT_SYMBOL_GPL(dm_bitset_empty);
43 struct packer_context {
44 bit_value_fn fn;
45 unsigned int nr_bits;
46 void *context;
49 static int pack_bits(uint32_t index, void *value, void *context)
51 int r;
52 struct packer_context *p = context;
53 unsigned int bit, nr = min(64u, p->nr_bits - (index * 64));
54 uint64_t word = 0;
55 bool bv;
57 for (bit = 0; bit < nr; bit++) {
58 r = p->fn(index * 64 + bit, &bv, p->context);
59 if (r)
60 return r;
62 if (bv)
63 set_bit(bit, (unsigned long *) &word);
64 else
65 clear_bit(bit, (unsigned long *) &word);
68 *((__le64 *) value) = cpu_to_le64(word);
70 return 0;
73 int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
74 uint32_t size, bit_value_fn fn, void *context)
76 struct packer_context p;
78 p.fn = fn;
79 p.nr_bits = size;
80 p.context = context;
82 return dm_array_new(&info->array_info, root, dm_div_up(size, 64), pack_bits, &p);
84 EXPORT_SYMBOL_GPL(dm_bitset_new);
86 int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t root,
87 uint32_t old_nr_entries, uint32_t new_nr_entries,
88 bool default_value, dm_block_t *new_root)
90 uint32_t old_blocks = dm_div_up(old_nr_entries, BITS_PER_ARRAY_ENTRY);
91 uint32_t new_blocks = dm_div_up(new_nr_entries, BITS_PER_ARRAY_ENTRY);
92 __le64 value = default_value ? cpu_to_le64(~0) : cpu_to_le64(0);
94 __dm_bless_for_disk(&value);
95 return dm_array_resize(&info->array_info, root, old_blocks, new_blocks,
96 &value, new_root);
98 EXPORT_SYMBOL_GPL(dm_bitset_resize);
100 int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root)
102 return dm_array_del(&info->array_info, root);
104 EXPORT_SYMBOL_GPL(dm_bitset_del);
106 int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
107 dm_block_t *new_root)
109 int r;
110 __le64 value;
112 if (!info->current_index_set || !info->dirty)
113 return 0;
115 value = cpu_to_le64(info->current_bits);
117 __dm_bless_for_disk(&value);
118 r = dm_array_set_value(&info->array_info, root, info->current_index,
119 &value, new_root);
120 if (r)
121 return r;
123 info->current_index_set = false;
124 info->dirty = false;
126 return 0;
128 EXPORT_SYMBOL_GPL(dm_bitset_flush);
130 static int read_bits(struct dm_disk_bitset *info, dm_block_t root,
131 uint32_t array_index)
133 int r;
134 __le64 value;
136 r = dm_array_get_value(&info->array_info, root, array_index, &value);
137 if (r)
138 return r;
140 info->current_bits = le64_to_cpu(value);
141 info->current_index_set = true;
142 info->current_index = array_index;
143 info->dirty = false;
145 return 0;
148 static int get_array_entry(struct dm_disk_bitset *info, dm_block_t root,
149 uint32_t index, dm_block_t *new_root)
151 int r;
152 unsigned int array_index = index / BITS_PER_ARRAY_ENTRY;
154 if (info->current_index_set) {
155 if (info->current_index == array_index)
156 return 0;
158 r = dm_bitset_flush(info, root, new_root);
159 if (r)
160 return r;
163 return read_bits(info, root, array_index);
166 int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
167 uint32_t index, dm_block_t *new_root)
169 int r;
170 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
172 r = get_array_entry(info, root, index, new_root);
173 if (r)
174 return r;
176 set_bit(b, (unsigned long *) &info->current_bits);
177 info->dirty = true;
179 return 0;
181 EXPORT_SYMBOL_GPL(dm_bitset_set_bit);
183 int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
184 uint32_t index, dm_block_t *new_root)
186 int r;
187 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
189 r = get_array_entry(info, root, index, new_root);
190 if (r)
191 return r;
193 clear_bit(b, (unsigned long *) &info->current_bits);
194 info->dirty = true;
196 return 0;
198 EXPORT_SYMBOL_GPL(dm_bitset_clear_bit);
200 int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
201 uint32_t index, dm_block_t *new_root, bool *result)
203 int r;
204 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
206 r = get_array_entry(info, root, index, new_root);
207 if (r)
208 return r;
210 *result = test_bit(b, (unsigned long *) &info->current_bits);
211 return 0;
213 EXPORT_SYMBOL_GPL(dm_bitset_test_bit);
215 static int cursor_next_array_entry(struct dm_bitset_cursor *c)
217 int r;
218 __le64 *value;
220 r = dm_array_cursor_next(&c->cursor);
221 if (r)
222 return r;
224 dm_array_cursor_get_value(&c->cursor, (void **) &value);
225 c->array_index++;
226 c->bit_index = 0;
227 c->current_bits = le64_to_cpu(*value);
228 return 0;
231 int dm_bitset_cursor_begin(struct dm_disk_bitset *info,
232 dm_block_t root, uint32_t nr_entries,
233 struct dm_bitset_cursor *c)
235 int r;
236 __le64 *value;
238 if (!nr_entries)
239 return -ENODATA;
241 c->info = info;
242 c->entries_remaining = nr_entries;
244 r = dm_array_cursor_begin(&info->array_info, root, &c->cursor);
245 if (r)
246 return r;
248 dm_array_cursor_get_value(&c->cursor, (void **) &value);
249 c->array_index = 0;
250 c->bit_index = 0;
251 c->current_bits = le64_to_cpu(*value);
253 return r;
255 EXPORT_SYMBOL_GPL(dm_bitset_cursor_begin);
257 void dm_bitset_cursor_end(struct dm_bitset_cursor *c)
259 return dm_array_cursor_end(&c->cursor);
261 EXPORT_SYMBOL_GPL(dm_bitset_cursor_end);
263 int dm_bitset_cursor_next(struct dm_bitset_cursor *c)
265 int r = 0;
267 if (!c->entries_remaining)
268 return -ENODATA;
270 c->entries_remaining--;
271 if (++c->bit_index > 63)
272 r = cursor_next_array_entry(c);
274 return r;
276 EXPORT_SYMBOL_GPL(dm_bitset_cursor_next);
278 int dm_bitset_cursor_skip(struct dm_bitset_cursor *c, uint32_t count)
280 int r;
281 __le64 *value;
282 uint32_t nr_array_skip;
283 uint32_t remaining_in_word = 64 - c->bit_index;
285 if (c->entries_remaining < count)
286 return -ENODATA;
288 if (count < remaining_in_word) {
289 c->bit_index += count;
290 c->entries_remaining -= count;
291 return 0;
293 } else {
294 c->entries_remaining -= remaining_in_word;
295 count -= remaining_in_word;
298 nr_array_skip = (count / 64) + 1;
299 r = dm_array_cursor_skip(&c->cursor, nr_array_skip);
300 if (r)
301 return r;
303 dm_array_cursor_get_value(&c->cursor, (void **) &value);
304 c->entries_remaining -= count;
305 c->array_index += nr_array_skip;
306 c->bit_index = count & 63;
307 c->current_bits = le64_to_cpu(*value);
309 return 0;
311 EXPORT_SYMBOL_GPL(dm_bitset_cursor_skip);
313 bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c)
315 return test_bit(c->bit_index, (unsigned long *) &c->current_bits);
317 EXPORT_SYMBOL_GPL(dm_bitset_cursor_get_value);
319 /*----------------------------------------------------------------*/