dm-bitset.c 6.9 KB

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