dm-bitset.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef _LINUX_DM_BITSET_H
  8. #define _LINUX_DM_BITSET_H
  9. #include "dm-array.h"
  10. /*----------------------------------------------------------------*/
  11. /*
  12. * This bitset type is a thin wrapper round a dm_array of 64bit words. It
  13. * uses a tiny, one word cache to reduce the number of array lookups and so
  14. * increase performance.
  15. *
  16. * Like the dm-array that it's based on, the caller needs to keep track of
  17. * the size of the bitset separately. The underlying dm-array implicitly
  18. * knows how many words it's storing and will return -ENODATA if you try
  19. * and access an out of bounds word. However, an out of bounds bit in the
  20. * final word will _not_ be detected, you have been warned.
  21. *
  22. * Bits are indexed from zero.
  23. * Typical use:
  24. *
  25. * a) Initialise a dm_disk_bitset structure with dm_disk_bitset_init().
  26. * This describes the bitset and includes the cache. It's not called it
  27. * dm_bitset_info in line with other data structures because it does
  28. * include instance data.
  29. *
  30. * b) Get yourself a root. The root is the index of a block of data on the
  31. * disk that holds a particular instance of an bitset. You may have a
  32. * pre existing root in your metadata that you wish to use, or you may
  33. * want to create a brand new, empty bitset with dm_bitset_empty().
  34. *
  35. * Like the other data structures in this library, dm_bitset objects are
  36. * immutable between transactions. Update functions will return you the
  37. * root for a _new_ array. If you've incremented the old root, via
  38. * dm_tm_inc(), before calling the update function you may continue to use
  39. * it in parallel with the new root.
  40. *
  41. * Even read operations may trigger the cache to be flushed and as such
  42. * return a root for a new, updated bitset.
  43. *
  44. * c) resize a bitset with dm_bitset_resize().
  45. *
  46. * d) Set a bit with dm_bitset_set_bit().
  47. *
  48. * e) Clear a bit with dm_bitset_clear_bit().
  49. *
  50. * f) Test a bit with dm_bitset_test_bit().
  51. *
  52. * g) Flush all updates from the cache with dm_bitset_flush().
  53. *
  54. * h) Destroy the bitset with dm_bitset_del(). This tells the transaction
  55. * manager that you're no longer using this data structure so it can
  56. * recycle it's blocks. (dm_bitset_dec() would be a better name for it,
  57. * but del is in keeping with dm_btree_del()).
  58. */
  59. /*
  60. * Opaque object. Unlike dm_array_info, you should have one of these per
  61. * bitset. Initialise with dm_disk_bitset_init().
  62. */
  63. struct dm_disk_bitset {
  64. struct dm_array_info array_info;
  65. uint32_t current_index;
  66. uint64_t current_bits;
  67. bool current_index_set:1;
  68. bool dirty:1;
  69. };
  70. /*
  71. * Sets up a dm_disk_bitset structure. You don't need to do anything with
  72. * this structure when you finish using it.
  73. *
  74. * tm - the transaction manager that should supervise this structure
  75. * info - the structure being initialised
  76. */
  77. void dm_disk_bitset_init(struct dm_transaction_manager *tm,
  78. struct dm_disk_bitset *info);
  79. /*
  80. * Create an empty, zero length bitset.
  81. *
  82. * info - describes the bitset
  83. * new_root - on success, points to the new root block
  84. */
  85. int dm_bitset_empty(struct dm_disk_bitset *info, dm_block_t *new_root);
  86. /*
  87. * Creates a new bitset populated with values provided by a callback
  88. * function. This is more efficient than creating an empty bitset,
  89. * resizing, and then setting values since that process incurs a lot of
  90. * copying.
  91. *
  92. * info - describes the array
  93. * root - the root block of the array on disk
  94. * size - the number of entries in the array
  95. * fn - the callback
  96. * context - passed to the callback
  97. */
  98. typedef int (*bit_value_fn)(uint32_t index, bool *value, void *context);
  99. int dm_bitset_new(struct dm_disk_bitset *info, dm_block_t *root,
  100. uint32_t size, bit_value_fn fn, void *context);
  101. /*
  102. * Resize the bitset.
  103. *
  104. * info - describes the bitset
  105. * old_root - the root block of the array on disk
  106. * old_nr_entries - the number of bits in the old bitset
  107. * new_nr_entries - the number of bits you want in the new bitset
  108. * default_value - the value for any new bits
  109. * new_root - on success, points to the new root block
  110. */
  111. int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t old_root,
  112. uint32_t old_nr_entries, uint32_t new_nr_entries,
  113. bool default_value, dm_block_t *new_root);
  114. /*
  115. * Frees the bitset.
  116. */
  117. int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root);
  118. /*
  119. * Set a bit.
  120. *
  121. * info - describes the bitset
  122. * root - the root block of the bitset
  123. * index - the bit index
  124. * new_root - on success, points to the new root block
  125. *
  126. * -ENODATA will be returned if the index is out of bounds.
  127. */
  128. int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
  129. uint32_t index, dm_block_t *new_root);
  130. /*
  131. * Clears a bit.
  132. *
  133. * info - describes the bitset
  134. * root - the root block of the bitset
  135. * index - the bit index
  136. * new_root - on success, points to the new root block
  137. *
  138. * -ENODATA will be returned if the index is out of bounds.
  139. */
  140. int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
  141. uint32_t index, dm_block_t *new_root);
  142. /*
  143. * Tests a bit.
  144. *
  145. * info - describes the bitset
  146. * root - the root block of the bitset
  147. * index - the bit index
  148. * new_root - on success, points to the new root block (cached values may have been written)
  149. * result - the bit value you're after
  150. *
  151. * -ENODATA will be returned if the index is out of bounds.
  152. */
  153. int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
  154. uint32_t index, dm_block_t *new_root, bool *result);
  155. /*
  156. * Flush any cached changes to disk.
  157. *
  158. * info - describes the bitset
  159. * root - the root block of the bitset
  160. * new_root - on success, points to the new root block
  161. */
  162. int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
  163. dm_block_t *new_root);
  164. struct dm_bitset_cursor {
  165. struct dm_disk_bitset *info;
  166. struct dm_array_cursor cursor;
  167. uint32_t entries_remaining;
  168. uint32_t array_index;
  169. uint32_t bit_index;
  170. uint64_t current_bits;
  171. };
  172. /*
  173. * Make sure you've flush any dm_disk_bitset and updated the root before
  174. * using this.
  175. */
  176. int dm_bitset_cursor_begin(struct dm_disk_bitset *info,
  177. dm_block_t root, uint32_t nr_entries,
  178. struct dm_bitset_cursor *c);
  179. void dm_bitset_cursor_end(struct dm_bitset_cursor *c);
  180. int dm_bitset_cursor_next(struct dm_bitset_cursor *c);
  181. int dm_bitset_cursor_skip(struct dm_bitset_cursor *c, uint32_t count);
  182. bool dm_bitset_cursor_get_value(struct dm_bitset_cursor *c);
  183. /*----------------------------------------------------------------*/
  184. #endif /* _LINUX_DM_BITSET_H */