diff options
author | Joe Thornber <ejt@redhat.com> | 2016-10-03 14:15:02 -0400 |
---|---|---|
committer | Mike Snitzer <snitzer@redhat.com> | 2017-02-16 13:12:45 -0500 |
commit | 6fe28dbf05e329ce136f38219d95a1826ceafebd (patch) | |
tree | e5619c3b907f006f72257fc74eccb941e1ae646b /drivers/md/persistent-data | |
parent | 9f9ef0657d53d988dc07b096052b3dd07d6e3c46 (diff) |
dm bitset: introduce cursor api
Signed-off-by: Joe Thornber <ejt@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Diffstat (limited to 'drivers/md/persistent-data')
-rw-r--r-- | drivers/md/persistent-data/dm-bitset.c | 69 | ||||
-rw-r--r-- | drivers/md/persistent-data/dm-bitset.h | 22 |
2 files changed, 91 insertions, 0 deletions
diff --git a/drivers/md/persistent-data/dm-bitset.c b/drivers/md/persistent-data/dm-bitset.c index 36f7cc2c7109..7cb28516cc19 100644 --- a/drivers/md/persistent-data/dm-bitset.c +++ b/drivers/md/persistent-data/dm-bitset.c | |||
@@ -168,4 +168,73 @@ int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root, | |||
168 | } | 168 | } |
169 | EXPORT_SYMBOL_GPL(dm_bitset_test_bit); | 169 | EXPORT_SYMBOL_GPL(dm_bitset_test_bit); |
170 | 170 | ||
171 | static int cursor_next_array_entry(struct dm_bitset_cursor *c) | ||
172 | { | ||
173 | int r; | ||
174 | __le64 *value; | ||
175 | |||
176 | r = dm_array_cursor_next(&c->cursor); | ||
177 | if (r) | ||
178 | return r; | ||
179 | |||
180 | dm_array_cursor_get_value(&c->cursor, (void **) &value); | ||
181 | c->array_index++; | ||
182 | c->bit_index = 0; | ||
183 | c->current_bits = le64_to_cpu(*value); | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | int dm_bitset_cursor_begin(struct dm_disk_bitset *info, | ||
188 | dm_block_t root, uint32_t nr_entries, | ||
189 | struct dm_bitset_cursor *c) | ||
190 | { | ||
191 | int r; | ||
192 | __le64 *value; | ||
193 | |||
194 | if (!nr_entries) | ||
195 | return -ENODATA; | ||
196 | |||
197 | c->info = info; | ||
198 | c->entries_remaining = nr_entries; | ||
199 | |||
200 | r = dm_array_cursor_begin(&info->array_info, root, &c->cursor); | ||
201 | if (r) | ||
202 | return r; | ||
203 | |||
204 | dm_array_cursor_get_value(&c->cursor, (void **) &value); | ||
205 | c->array_index = 0; | ||
206 | c->bit_index = 0; | ||
207 | c->current_bits = le64_to_cpu(*value); | ||
208 | |||
209 | return r; | ||
210 | } | ||
211 | EXPORT_SYMBOL_GPL(dm_bitset_cursor_begin); | ||
212 | |||
213 | void dm_bitset_cursor_end(struct dm_bitset_cursor *c) | ||
214 | { | ||
215 | return dm_array_cursor_end(&c->cursor); | ||
216 | } | ||
217 | EXPORT_SYMBOL_GPL(dm_bitset_cursor_end); | ||
218 | |||
219 | int dm_bitset_cursor_next(struct dm_bitset_cursor *c) | ||
220 | { | ||
221 | int r = 0; | ||
222 | |||
223 | if (!c->entries_remaining) | ||
224 | return -ENODATA; | ||
225 | |||
226 | c->entries_remaining--; | ||
227 | if (++c->bit_index > 63) | ||
228 | r = cursor_next_array_entry(c); | ||
229 | |||
230 | return r; | ||
231 | } | ||
232 | EXPORT_SYMBOL_GPL(dm_bitset_cursor_next); | ||
233 | |||
234 | bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c) | ||
235 | { | ||
236 | return test_bit(c->bit_index, (unsigned long *) &c->current_bits); | ||
237 | } | ||
238 | EXPORT_SYMBOL_GPL(dm_bitset_cursor_get_value); | ||
239 | |||
171 | /*----------------------------------------------------------------*/ | 240 | /*----------------------------------------------------------------*/ |
diff --git a/drivers/md/persistent-data/dm-bitset.h b/drivers/md/persistent-data/dm-bitset.h index c2287d672ef5..017c0d42cdbf 100644 --- a/drivers/md/persistent-data/dm-bitset.h +++ b/drivers/md/persistent-data/dm-bitset.h | |||
@@ -161,6 +161,28 @@ int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root, | |||
161 | int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root, | 161 | int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root, |
162 | dm_block_t *new_root); | 162 | dm_block_t *new_root); |
163 | 163 | ||
164 | struct dm_bitset_cursor { | ||
165 | struct dm_disk_bitset *info; | ||
166 | struct dm_array_cursor cursor; | ||
167 | |||
168 | uint32_t entries_remaining; | ||
169 | uint32_t array_index; | ||
170 | uint32_t bit_index; | ||
171 | uint64_t current_bits; | ||
172 | }; | ||
173 | |||
174 | /* | ||
175 | * Make sure you've flush any dm_disk_bitset and updated the root before | ||
176 | * using this. | ||
177 | */ | ||
178 | int dm_bitset_cursor_begin(struct dm_disk_bitset *info, | ||
179 | dm_block_t root, uint32_t nr_entries, | ||
180 | struct dm_bitset_cursor *c); | ||
181 | void dm_bitset_cursor_end(struct dm_bitset_cursor *c); | ||
182 | |||
183 | int dm_bitset_cursor_next(struct dm_bitset_cursor *c); | ||
184 | bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c); | ||
185 | |||
164 | /*----------------------------------------------------------------*/ | 186 | /*----------------------------------------------------------------*/ |
165 | 187 | ||
166 | #endif /* _LINUX_DM_BITSET_H */ | 188 | #endif /* _LINUX_DM_BITSET_H */ |