dm-btree.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #ifndef _LINUX_DM_BTREE_H
  7. #define _LINUX_DM_BTREE_H
  8. #include "dm-block-manager.h"
  9. struct dm_transaction_manager;
  10. /*----------------------------------------------------------------*/
  11. /*
  12. * Annotations used to check on-disk metadata is handled as little-endian.
  13. */
  14. #ifdef __CHECKER__
  15. # define __dm_written_to_disk(x) __releases(x)
  16. # define __dm_reads_from_disk(x) __acquires(x)
  17. # define __dm_bless_for_disk(x) __acquire(x)
  18. # define __dm_unbless_for_disk(x) __release(x)
  19. #else
  20. # define __dm_written_to_disk(x)
  21. # define __dm_reads_from_disk(x)
  22. # define __dm_bless_for_disk(x)
  23. # define __dm_unbless_for_disk(x)
  24. #endif
  25. /*----------------------------------------------------------------*/
  26. /*
  27. * Manipulates hierarchical B+ trees with 64-bit keys and arbitrary-sized
  28. * values.
  29. */
  30. /*
  31. * Infomation about the values stored within the btree.
  32. */
  33. struct dm_btree_value_type {
  34. void *context;
  35. /*
  36. * The size in bytes of each value.
  37. */
  38. uint32_t size;
  39. /*
  40. * Any of these methods can be safely set to NULL if you do not
  41. * need the corresponding feature.
  42. */
  43. /*
  44. * The btree is making a duplicate of the value, for instance
  45. * because previously-shared btree nodes have now diverged.
  46. * @value argument is the new copy that the copy function may modify.
  47. * (Probably it just wants to increment a reference count
  48. * somewhere.) This method is _not_ called for insertion of a new
  49. * value: It is assumed the ref count is already 1.
  50. */
  51. void (*inc)(void *context, void *value);
  52. /*
  53. * This value is being deleted. The btree takes care of freeing
  54. * the memory pointed to by @value. Often the del function just
  55. * needs to decrement a reference count somewhere.
  56. */
  57. void (*dec)(void *context, void *value);
  58. /*
  59. * A test for equality between two values. When a value is
  60. * overwritten with a new one, the old one has the dec method
  61. * called _unless_ the new and old value are deemed equal.
  62. */
  63. int (*equal)(void *context, void *value1, void *value2);
  64. };
  65. /*
  66. * The shape and contents of a btree.
  67. */
  68. struct dm_btree_info {
  69. struct dm_transaction_manager *tm;
  70. /*
  71. * Number of nested btrees. (Not the depth of a single tree.)
  72. */
  73. unsigned levels;
  74. struct dm_btree_value_type value_type;
  75. };
  76. /*
  77. * Set up an empty tree. O(1).
  78. */
  79. int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root);
  80. /*
  81. * Delete a tree. O(n) - this is the slow one! It can also block, so
  82. * please don't call it on an IO path.
  83. */
  84. int dm_btree_del(struct dm_btree_info *info, dm_block_t root);
  85. /*
  86. * All the lookup functions return -ENODATA if the key cannot be found.
  87. */
  88. /*
  89. * Tries to find a key that matches exactly. O(ln(n))
  90. */
  91. int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
  92. uint64_t *keys, void *value_le);
  93. /*
  94. * Insertion (or overwrite an existing value). O(ln(n))
  95. */
  96. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  97. uint64_t *keys, void *value, dm_block_t *new_root)
  98. __dm_written_to_disk(value);
  99. /*
  100. * A variant of insert that indicates whether it actually inserted or just
  101. * overwrote. Useful if you're keeping track of the number of entries in a
  102. * tree.
  103. */
  104. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  105. uint64_t *keys, void *value, dm_block_t *new_root,
  106. int *inserted)
  107. __dm_written_to_disk(value);
  108. /*
  109. * Remove a key if present. This doesn't remove empty sub trees. Normally
  110. * subtrees represent a separate entity, like a snapshot map, so this is
  111. * correct behaviour. O(ln(n)).
  112. */
  113. int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
  114. uint64_t *keys, dm_block_t *new_root);
  115. /*
  116. * Returns < 0 on failure. Otherwise the number of key entries that have
  117. * been filled out. Remember trees can have zero entries, and as such have
  118. * no highest key.
  119. */
  120. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  121. uint64_t *result_keys);
  122. #endif /* _LINUX_DM_BTREE_H */