Skip to content

Commit 2151249

Browse files
jthornbersnitm
authored andcommitted
dm bitset: add dm_bitset_new()
A more efficient way of creating a populated bitset. Signed-off-by: Joe Thornber <ejt@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent 4855105 commit 2151249

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎drivers/md/persistent-data/dm-bitset.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,48 @@ int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *root)
3939
}
4040
EXPORT_SYMBOL_GPL(dm_bitset_empty);
4141

42+
struct packer_context {
43+
bit_value_fn fn;
44+
unsigned nr_bits;
45+
void *context;
46+
};
47+
48+
static int pack_bits(uint32_t index, void *value, void *context)
49+
{
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;
55+
56+
for (bit = 0; bit < nr; bit++) {
57+
r = p->fn(index * 64 + bit, &bv, p->context);
58+
if (r)
59+
return r;
60+
61+
if (bv)
62+
set_bit(bit, (unsigned long *) &word);
63+
else
64+
clear_bit(bit, (unsigned long *) &word);
65+
}
66+
67+
*((__le64 *) value) = cpu_to_le64(word);
68+
69+
return 0;
70+
}
71+
72+
int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
73+
uint32_t size, bit_value_fn fn, void *context)
74+
{
75+
struct packer_context p;
76+
p.fn = fn;
77+
p.nr_bits = size;
78+
p.context = context;
79+
80+
return dm_array_new(&info->array_info, root, dm_div_up(size, 64), pack_bits, &p);
81+
}
82+
EXPORT_SYMBOL_GPL(dm_bitset_new);
83+
4284
int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t root,
4385
uint32_t old_nr_entries, uint32_t new_nr_entries,
4486
bool default_value, dm_block_t *new_root)

‎drivers/md/persistent-data/dm-bitset.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ void dm_disk_bitset_init(struct dm_transaction_manager *tm,
9292
*/
9393
int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *new_root);
9494

95+
/*
96+
* Creates a new bitset populated with values provided by a callback
97+
* function. This is more efficient than creating an empty bitset,
98+
* resizing, and then setting values since that process incurs a lot of
99+
* copying.
100+
*
101+
* info - describes the array
102+
* root - the root block of the array on disk
103+
* size - the number of entries in the array
104+
* fn - the callback
105+
* context - passed to the callback
106+
*/
107+
typedef int (*bit_value_fn)(uint32_t index, bool *value, void *context);
108+
int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
109+
uint32_t size, bit_value_fn fn, void *context);
110+
95111
/*
96112
* Resize the bitset.
97113
*

0 commit comments

Comments
 (0)