dm-btree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef _LINUX_DM_BTREE_H
  8. #define _LINUX_DM_BTREE_H
  9. #include "dm-block-manager.h"
  10. struct dm_transaction_manager;
  11. /*----------------------------------------------------------------*/
  12. /*
  13. * Annotations used to check on-disk metadata is handled as little-endian.
  14. */
  15. #ifdef __CHECKER__
  16. # define __dm_written_to_disk(x) __releases(x)
  17. # define __dm_reads_from_disk(x) __acquires(x)
  18. # define __dm_bless_for_disk(x) __acquire(x)
  19. # define __dm_unbless_for_disk(x) __release(x)
  20. #else
  21. # define __dm_written_to_disk(x)
  22. # define __dm_reads_from_disk(x)
  23. # define __dm_bless_for_disk(x)
  24. # define __dm_unbless_for_disk(x)
  25. #endif
  26. /*----------------------------------------------------------------*/
  27. /*
  28. * Manipulates hierarchical B+ trees with 64-bit keys and arbitrary-sized
  29. * values.
  30. */
  31. /*
  32. * Information about the values stored within the btree.
  33. */
  34. struct dm_btree_value_type {
  35. void *context;
  36. /*
  37. * The size in bytes of each value.
  38. */
  39. uint32_t size;
  40. /*
  41. * Any of these methods can be safely set to NULL if you do not
  42. * need the corresponding feature.
  43. */
  44. /*
  45. * The btree is making a duplicate of a run of values, for instance
  46. * because previously-shared btree nodes have now diverged.
  47. * @value argument is the new copy that the copy function may modify.
  48. * (Probably it just wants to increment a reference count
  49. * somewhere.) This method is _not_ called for insertion of a new
  50. * value: It is assumed the ref count is already 1.
  51. */
  52. void (*inc)(void *context, const void *value, unsigned int count);
  53. /*
  54. * These values are being deleted. The btree takes care of freeing
  55. * the memory pointed to by @value. Often the del function just
  56. * needs to decrement a reference counts somewhere.
  57. */
  58. void (*dec)(void *context, const void *value, unsigned int count);
  59. /*
  60. * A test for equality between two values. When a value is
  61. * overwritten with a new one, the old one has the dec method
  62. * called _unless_ the new and old value are deemed equal.
  63. */
  64. int (*equal)(void *context, const void *value1, const void *value2);
  65. };
  66. /*
  67. * The shape and contents of a btree.
  68. */
  69. struct dm_btree_info {
  70. struct dm_transaction_manager *tm;
  71. /*
  72. * Number of nested btrees. (Not the depth of a single tree.)
  73. */
  74. unsigned int levels;
  75. struct dm_btree_value_type value_type;
  76. };
  77. /*
  78. * Set up an empty tree. O(1).
  79. */
  80. int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root);
  81. /*
  82. * Delete a tree. O(n) - this is the slow one! It can also block, so
  83. * please don't call it on an IO path.
  84. */
  85. int dm_btree_del(struct dm_btree_info *info, dm_block_t root);
  86. /*
  87. * All the lookup functions return -ENODATA if the key cannot be found.
  88. */
  89. /*
  90. * Tries to find a key that matches exactly. O(ln(n))
  91. */
  92. int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
  93. uint64_t *keys, void *value_le);
  94. /*
  95. * Tries to find the first key where the bottom level key is >= to that
  96. * given. Useful for skipping empty sections of the btree.
  97. */
  98. int dm_btree_lookup_next(struct dm_btree_info *info, dm_block_t root,
  99. uint64_t *keys, uint64_t *rkey, void *value_le);
  100. /*
  101. * Insertion (or overwrite an existing value). O(ln(n))
  102. */
  103. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  104. uint64_t *keys, void *value, dm_block_t *new_root)
  105. __dm_written_to_disk(value);
  106. /*
  107. * A variant of insert that indicates whether it actually inserted or just
  108. * overwrote. Useful if you're keeping track of the number of entries in a
  109. * tree.
  110. */
  111. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  112. uint64_t *keys, void *value, dm_block_t *new_root,
  113. int *inserted)
  114. __dm_written_to_disk(value);
  115. /*
  116. * Remove a key if present. This doesn't remove empty sub trees. Normally
  117. * subtrees represent a separate entity, like a snapshot map, so this is
  118. * correct behaviour. O(ln(n)).
  119. */
  120. int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
  121. uint64_t *keys, dm_block_t *new_root);
  122. /*
  123. * Removes a _contiguous_ run of values starting from 'keys' and not
  124. * reaching keys2 (where keys2 is keys with the final key replaced with
  125. * 'end_key'). 'end_key' is the one-past-the-end value. 'keys' may be
  126. * altered.
  127. */
  128. int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,
  129. uint64_t *keys, uint64_t end_key,
  130. dm_block_t *new_root, unsigned int *nr_removed);
  131. /*
  132. * Returns < 0 on failure. Otherwise the number of key entries that have
  133. * been filled out. Remember trees can have zero entries, and as such have
  134. * no lowest key.
  135. */
  136. int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
  137. uint64_t *result_keys);
  138. /*
  139. * Returns < 0 on failure. Otherwise the number of key entries that have
  140. * been filled out. Remember trees can have zero entries, and as such have
  141. * no highest key.
  142. */
  143. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  144. uint64_t *result_keys);
  145. /*
  146. * Iterate through the a btree, calling fn() on each entry.
  147. * It only works for single level trees and is internally recursive, so
  148. * monitor stack usage carefully.
  149. */
  150. int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
  151. int (*fn)(void *context, uint64_t *keys, void *leaf),
  152. void *context);
  153. /*----------------------------------------------------------------*/
  154. /*
  155. * Cursor API. This does not follow the rolling lock convention. Since we
  156. * know the order that values are required we can issue prefetches to speed
  157. * up iteration. Use on a single level btree only.
  158. */
  159. #define DM_BTREE_CURSOR_MAX_DEPTH 16
  160. struct cursor_node {
  161. struct dm_block *b;
  162. unsigned int index;
  163. };
  164. struct dm_btree_cursor {
  165. struct dm_btree_info *info;
  166. dm_block_t root;
  167. bool prefetch_leaves;
  168. unsigned int depth;
  169. struct cursor_node nodes[DM_BTREE_CURSOR_MAX_DEPTH];
  170. };
  171. /*
  172. * Creates a fresh cursor. If prefetch_leaves is set then it is assumed
  173. * the btree contains block indexes that will be prefetched. The cursor is
  174. * quite large, so you probably don't want to put it on the stack.
  175. */
  176. int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
  177. bool prefetch_leaves, struct dm_btree_cursor *c);
  178. void dm_btree_cursor_end(struct dm_btree_cursor *c);
  179. int dm_btree_cursor_next(struct dm_btree_cursor *c);
  180. int dm_btree_cursor_skip(struct dm_btree_cursor *c, uint32_t count);
  181. int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le);
  182. #endif /* _LINUX_DM_BTREE_H */