Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Red Hat, Inc.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-bitset.h"
9#include "dm-transaction-manager.h"
10
11#include <linux/export.h>
12#include <linux/device-mapper.h>
13
14#define DM_MSG_PREFIX "bitset"
15#define BITS_PER_ARRAY_ENTRY 64
16
17/*----------------------------------------------------------------*/
18
19static struct dm_btree_value_type bitset_bvt = {
20 .context = NULL,
21 .size = sizeof(__le64),
22 .inc = NULL,
23 .dec = NULL,
24 .equal = NULL,
25};
26
27/*----------------------------------------------------------------*/
28
29void dm_disk_bitset_init(struct dm_transaction_manager *tm,
30 struct dm_disk_bitset *info)
31{
32 dm_array_info_init(&info->array_info, tm, &bitset_bvt);
33 info->current_index_set = false;
34}
35EXPORT_SYMBOL_GPL(dm_disk_bitset_init);
36
37int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *root)
38{
39 return dm_array_empty(&info->array_info, root);
40}
41EXPORT_SYMBOL_GPL(dm_bitset_empty);
42
43struct packer_context {
44 bit_value_fn fn;
45 unsigned int nr_bits;
46 void *context;
47};
48
49static int pack_bits(uint32_t index, void *value, void *context)
50{
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;
56
57 for (bit = 0; bit < nr; bit++) {
58 r = p->fn(index * 64 + bit, &bv, p->context);
59 if (r)
60 return r;
61
62 if (bv)
63 set_bit(bit, (unsigned long *) &word);
64 else
65 clear_bit(bit, (unsigned long *) &word);
66 }
67
68 *((__le64 *) value) = cpu_to_le64(word);
69
70 return 0;
71}
72
73int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
74 uint32_t size, bit_value_fn fn, void *context)
75{
76 struct packer_context p;
77
78 p.fn = fn;
79 p.nr_bits = size;
80 p.context = context;
81
82 return dm_array_new(&info->array_info, root, dm_div_up(size, 64), pack_bits, &p);
83}
84EXPORT_SYMBOL_GPL(dm_bitset_new);
85
86int 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)
89{
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);
93
94 __dm_bless_for_disk(&value);
95 return dm_array_resize(&info->array_info, root, old_blocks, new_blocks,
96 &value, new_root);
97}
98EXPORT_SYMBOL_GPL(dm_bitset_resize);
99
100int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root)
101{
102 return dm_array_del(&info->array_info, root);
103}
104EXPORT_SYMBOL_GPL(dm_bitset_del);
105
106int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
107 dm_block_t *new_root)
108{
109 int r;
110 __le64 value;
111
112 if (!info->current_index_set || !info->dirty)
113 return 0;
114
115 value = cpu_to_le64(info->current_bits);
116
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;
122
123 info->current_index_set = false;
124 info->dirty = false;
125
126 return 0;
127}
128EXPORT_SYMBOL_GPL(dm_bitset_flush);
129
130static int read_bits(struct dm_disk_bitset *info, dm_block_t root,
131 uint32_t array_index)
132{
133 int r;
134 __le64 value;
135
136 r = dm_array_get_value(&info->array_info, root, array_index, &value);
137 if (r)
138 return r;
139
140 info->current_bits = le64_to_cpu(value);
141 info->current_index_set = true;
142 info->current_index = array_index;
143 info->dirty = false;
144
145 return 0;
146}
147
148static int get_array_entry(struct dm_disk_bitset *info, dm_block_t root,
149 uint32_t index, dm_block_t *new_root)
150{
151 int r;
152 unsigned int array_index = index / BITS_PER_ARRAY_ENTRY;
153
154 if (info->current_index_set) {
155 if (info->current_index == array_index)
156 return 0;
157
158 r = dm_bitset_flush(info, root, new_root);
159 if (r)
160 return r;
161 }
162
163 return read_bits(info, root, array_index);
164}
165
166int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
167 uint32_t index, dm_block_t *new_root)
168{
169 int r;
170 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
171
172 r = get_array_entry(info, root, index, new_root);
173 if (r)
174 return r;
175
176 set_bit(b, (unsigned long *) &info->current_bits);
177 info->dirty = true;
178
179 return 0;
180}
181EXPORT_SYMBOL_GPL(dm_bitset_set_bit);
182
183int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
184 uint32_t index, dm_block_t *new_root)
185{
186 int r;
187 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
188
189 r = get_array_entry(info, root, index, new_root);
190 if (r)
191 return r;
192
193 clear_bit(b, (unsigned long *) &info->current_bits);
194 info->dirty = true;
195
196 return 0;
197}
198EXPORT_SYMBOL_GPL(dm_bitset_clear_bit);
199
200int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
201 uint32_t index, dm_block_t *new_root, bool *result)
202{
203 int r;
204 unsigned int b = index % BITS_PER_ARRAY_ENTRY;
205
206 r = get_array_entry(info, root, index, new_root);
207 if (r)
208 return r;
209
210 *result = test_bit(b, (unsigned long *) &info->current_bits);
211 return 0;
212}
213EXPORT_SYMBOL_GPL(dm_bitset_test_bit);
214
215static int cursor_next_array_entry(struct dm_bitset_cursor *c)
216{
217 int r;
218 __le64 *value;
219
220 r = dm_array_cursor_next(&c->cursor);
221 if (r)
222 return r;
223
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;
229}
230
231int dm_bitset_cursor_begin(struct dm_disk_bitset *info,
232 dm_block_t root, uint32_t nr_entries,
233 struct dm_bitset_cursor *c)
234{
235 int r;
236 __le64 *value;
237
238 if (!nr_entries)
239 return -ENODATA;
240
241 c->info = info;
242 c->entries_remaining = nr_entries;
243
244 r = dm_array_cursor_begin(&info->array_info, root, &c->cursor);
245 if (r)
246 return r;
247
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);
252
253 return r;
254}
255EXPORT_SYMBOL_GPL(dm_bitset_cursor_begin);
256
257void dm_bitset_cursor_end(struct dm_bitset_cursor *c)
258{
259 return dm_array_cursor_end(&c->cursor);
260}
261EXPORT_SYMBOL_GPL(dm_bitset_cursor_end);
262
263int dm_bitset_cursor_next(struct dm_bitset_cursor *c)
264{
265 int r = 0;
266
267 if (!c->entries_remaining)
268 return -ENODATA;
269
270 c->entries_remaining--;
271 if (++c->bit_index > 63)
272 r = cursor_next_array_entry(c);
273
274 return r;
275}
276EXPORT_SYMBOL_GPL(dm_bitset_cursor_next);
277
278int dm_bitset_cursor_skip(struct dm_bitset_cursor *c, uint32_t count)
279{
280 int r;
281 __le64 *value;
282 uint32_t nr_array_skip;
283 uint32_t remaining_in_word = 64 - c->bit_index;
284
285 if (c->entries_remaining < count)
286 return -ENODATA;
287
288 if (count < remaining_in_word) {
289 c->bit_index += count;
290 c->entries_remaining -= count;
291 return 0;
292
293 } else {
294 c->entries_remaining -= remaining_in_word;
295 count -= remaining_in_word;
296 }
297
298 nr_array_skip = (count / 64) + 1;
299 r = dm_array_cursor_skip(&c->cursor, nr_array_skip);
300 if (r)
301 return r;
302
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);
308
309 return 0;
310}
311EXPORT_SYMBOL_GPL(dm_bitset_cursor_skip);
312
313bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c)
314{
315 return test_bit(c->bit_index, (unsigned long *) &c->current_bits);
316}
317EXPORT_SYMBOL_GPL(dm_bitset_cursor_get_value);
318
319/*----------------------------------------------------------------*/
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-bitset.h"
8#include "dm-transaction-manager.h"
9
10#include <linux/export.h>
11#include <linux/device-mapper.h>
12
13#define DM_MSG_PREFIX "bitset"
14#define BITS_PER_ARRAY_ENTRY 64
15
16/*----------------------------------------------------------------*/
17
18static struct dm_btree_value_type bitset_bvt = {
19 .context = NULL,
20 .size = sizeof(__le64),
21 .inc = NULL,
22 .dec = NULL,
23 .equal = NULL,
24};
25
26/*----------------------------------------------------------------*/
27
28void dm_disk_bitset_init(struct dm_transaction_manager *tm,
29 struct dm_disk_bitset *info)
30{
31 dm_array_info_init(&info->array_info, tm, &bitset_bvt);
32 info->current_index_set = false;
33}
34EXPORT_SYMBOL_GPL(dm_disk_bitset_init);
35
36int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *root)
37{
38 return dm_array_empty(&info->array_info, root);
39}
40EXPORT_SYMBOL_GPL(dm_bitset_empty);
41
42int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t root,
43 uint32_t old_nr_entries, uint32_t new_nr_entries,
44 bool default_value, dm_block_t *new_root)
45{
46 uint32_t old_blocks = dm_div_up(old_nr_entries, BITS_PER_ARRAY_ENTRY);
47 uint32_t new_blocks = dm_div_up(new_nr_entries, BITS_PER_ARRAY_ENTRY);
48 __le64 value = default_value ? cpu_to_le64(~0) : cpu_to_le64(0);
49
50 __dm_bless_for_disk(&value);
51 return dm_array_resize(&info->array_info, root, old_blocks, new_blocks,
52 &value, new_root);
53}
54EXPORT_SYMBOL_GPL(dm_bitset_resize);
55
56int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root)
57{
58 return dm_array_del(&info->array_info, root);
59}
60EXPORT_SYMBOL_GPL(dm_bitset_del);
61
62int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
63 dm_block_t *new_root)
64{
65 int r;
66 __le64 value;
67
68 if (!info->current_index_set || !info->dirty)
69 return 0;
70
71 value = cpu_to_le64(info->current_bits);
72
73 __dm_bless_for_disk(&value);
74 r = dm_array_set_value(&info->array_info, root, info->current_index,
75 &value, new_root);
76 if (r)
77 return r;
78
79 info->current_index_set = false;
80 info->dirty = false;
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(dm_bitset_flush);
85
86static int read_bits(struct dm_disk_bitset *info, dm_block_t root,
87 uint32_t array_index)
88{
89 int r;
90 __le64 value;
91
92 r = dm_array_get_value(&info->array_info, root, array_index, &value);
93 if (r)
94 return r;
95
96 info->current_bits = le64_to_cpu(value);
97 info->current_index_set = true;
98 info->current_index = array_index;
99 info->dirty = false;
100
101 return 0;
102}
103
104static int get_array_entry(struct dm_disk_bitset *info, dm_block_t root,
105 uint32_t index, dm_block_t *new_root)
106{
107 int r;
108 unsigned array_index = index / BITS_PER_ARRAY_ENTRY;
109
110 if (info->current_index_set) {
111 if (info->current_index == array_index)
112 return 0;
113
114 r = dm_bitset_flush(info, root, new_root);
115 if (r)
116 return r;
117 }
118
119 return read_bits(info, root, array_index);
120}
121
122int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
123 uint32_t index, dm_block_t *new_root)
124{
125 int r;
126 unsigned b = index % BITS_PER_ARRAY_ENTRY;
127
128 r = get_array_entry(info, root, index, new_root);
129 if (r)
130 return r;
131
132 set_bit(b, (unsigned long *) &info->current_bits);
133 info->dirty = true;
134
135 return 0;
136}
137EXPORT_SYMBOL_GPL(dm_bitset_set_bit);
138
139int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
140 uint32_t index, dm_block_t *new_root)
141{
142 int r;
143 unsigned b = index % BITS_PER_ARRAY_ENTRY;
144
145 r = get_array_entry(info, root, index, new_root);
146 if (r)
147 return r;
148
149 clear_bit(b, (unsigned long *) &info->current_bits);
150 info->dirty = true;
151
152 return 0;
153}
154EXPORT_SYMBOL_GPL(dm_bitset_clear_bit);
155
156int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
157 uint32_t index, dm_block_t *new_root, bool *result)
158{
159 int r;
160 unsigned b = index % BITS_PER_ARRAY_ENTRY;
161
162 r = get_array_entry(info, root, index, new_root);
163 if (r)
164 return r;
165
166 *result = test_bit(b, (unsigned long *) &info->current_bits);
167 return 0;
168}
169EXPORT_SYMBOL_GPL(dm_bitset_test_bit);
170
171/*----------------------------------------------------------------*/