dm-bitset.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Resize the bitset.
  87. *
  88. * info - describes the bitset
  89. * old_root - the root block of the array on disk
  90. * old_nr_entries - the number of bits in the old bitset
  91. * new_nr_entries - the number of bits you want in the new bitset
  92. * default_value - the value for any new bits
  93. * new_root - on success, points to the new root block
  94. */
  95. int dm_bitset_resize(struct dm_disk_bitset *info, dm_block_t old_root,
  96. uint32_t old_nr_entries, uint32_t new_nr_entries,
  97. bool default_value, dm_block_t *new_root);
  98. /*
  99. * Frees the bitset.
  100. */
  101. int dm_bitset_del(struct dm_disk_bitset *info, dm_block_t root);
  102. /*
  103. * Set a bit.
  104. *
  105. * info - describes the bitset
  106. * root - the root block of the bitset
  107. * index - the bit index
  108. * new_root - on success, points to the new root block
  109. *
  110. * -ENODATA will be returned if the index is out of bounds.
  111. */
  112. int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
  113. uint32_t index, dm_block_t *new_root);
  114. /*
  115. * Clears a bit.
  116. *
  117. * info - describes the bitset
  118. * root - the root block of the bitset
  119. * index - the bit index
  120. * new_root - on success, points to the new root block
  121. *
  122. * -ENODATA will be returned if the index is out of bounds.
  123. */
  124. int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
  125. uint32_t index, dm_block_t *new_root);
  126. /*
  127. * Tests a bit.
  128. *
  129. * info - describes the bitset
  130. * root - the root block of the bitset
  131. * index - the bit index
  132. * new_root - on success, points to the new root block (cached values may have been written)
  133. * result - the bit value you're after
  134. *
  135. * -ENODATA will be returned if the index is out of bounds.
  136. */
  137. int dm_bitset_test_bit(struct dm_disk_bitset *info, dm_block_t root,
  138. uint32_t index, dm_block_t *new_root, bool *result);
  139. /*
  140. * Flush any cached changes to disk.
  141. *
  142. * info - describes the bitset
  143. * root - the root block of the bitset
  144. * new_root - on success, points to the new root block
  145. */
  146. int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
  147. dm_block_t *new_root);
  148. /*----------------------------------------------------------------*/
  149. #endif /* _LINUX_DM_BITSET_H */