dm-bitset.h 6.4 KB

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