dm-btree-internal.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 DM_BTREE_INTERNAL_H
  8. #define DM_BTREE_INTERNAL_H
  9. #include "dm-btree.h"
  10. /*----------------------------------------------------------------*/
  11. /*
  12. * We'll need 2 accessor functions for n->csum and n->blocknr
  13. * to support dm-btree-spine.c in that case.
  14. */
  15. enum node_flags {
  16. INTERNAL_NODE = 1,
  17. LEAF_NODE = 1 << 1
  18. };
  19. /*
  20. * Every btree node begins with this structure. Make sure it's a multiple
  21. * of 8-bytes in size, otherwise the 64bit keys will be mis-aligned.
  22. */
  23. struct node_header {
  24. __le32 csum;
  25. __le32 flags;
  26. __le64 blocknr; /* Block this node is supposed to live in. */
  27. __le32 nr_entries;
  28. __le32 max_entries;
  29. __le32 value_size;
  30. __le32 padding;
  31. } __packed __aligned(8);
  32. struct btree_node {
  33. struct node_header header;
  34. __le64 keys[];
  35. } __packed __aligned(8);
  36. /*
  37. * Locks a block using the btree node validator.
  38. */
  39. int bn_read_lock(struct dm_btree_info *info, dm_block_t b,
  40. struct dm_block **result);
  41. void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
  42. struct dm_btree_value_type *vt);
  43. int new_block(struct dm_btree_info *info, struct dm_block **result);
  44. void unlock_block(struct dm_btree_info *info, struct dm_block *b);
  45. /*
  46. * Spines keep track of the rolling locks. There are 2 variants, read-only
  47. * and one that uses shadowing. These are separate structs to allow the
  48. * type checker to spot misuse, for example accidentally calling read_lock
  49. * on a shadow spine.
  50. */
  51. struct ro_spine {
  52. struct dm_btree_info *info;
  53. int count;
  54. struct dm_block *nodes[2];
  55. };
  56. void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info);
  57. void exit_ro_spine(struct ro_spine *s);
  58. int ro_step(struct ro_spine *s, dm_block_t new_child);
  59. void ro_pop(struct ro_spine *s);
  60. struct btree_node *ro_node(struct ro_spine *s);
  61. struct shadow_spine {
  62. struct dm_btree_info *info;
  63. int count;
  64. struct dm_block *nodes[2];
  65. dm_block_t root;
  66. };
  67. void init_shadow_spine(struct shadow_spine *s, struct dm_btree_info *info);
  68. void exit_shadow_spine(struct shadow_spine *s);
  69. int shadow_step(struct shadow_spine *s, dm_block_t b,
  70. struct dm_btree_value_type *vt);
  71. /*
  72. * The spine must have at least one entry before calling this.
  73. */
  74. struct dm_block *shadow_current(struct shadow_spine *s);
  75. /*
  76. * The spine must have at least two entries before calling this.
  77. */
  78. struct dm_block *shadow_parent(struct shadow_spine *s);
  79. int shadow_has_parent(struct shadow_spine *s);
  80. dm_block_t shadow_root(struct shadow_spine *s);
  81. /*
  82. * Some inlines.
  83. */
  84. static inline __le64 *key_ptr(struct btree_node *n, uint32_t index)
  85. {
  86. return n->keys + index;
  87. }
  88. static inline void *value_base(struct btree_node *n)
  89. {
  90. return &n->keys[le32_to_cpu(n->header.max_entries)];
  91. }
  92. static inline void *value_ptr(struct btree_node *n, uint32_t index)
  93. {
  94. uint32_t value_size = le32_to_cpu(n->header.value_size);
  95. return value_base(n) + (value_size * index);
  96. }
  97. /*
  98. * Assumes the values are suitably-aligned and converts to core format.
  99. */
  100. static inline uint64_t value64(struct btree_node *n, uint32_t index)
  101. {
  102. __le64 *values_le = value_base(n);
  103. return le64_to_cpu(values_le[index]);
  104. }
  105. /*
  106. * Searching for a key within a single node.
  107. */
  108. int lower_bound(struct btree_node *n, uint64_t key);
  109. extern const struct dm_block_validator btree_node_validator;
  110. /*
  111. * Value type for upper levels of multi-level btrees.
  112. */
  113. extern void init_le64_type(struct dm_transaction_manager *tm,
  114. struct dm_btree_value_type *vt);
  115. /*
  116. * This returns a shadowed btree leaf that you may modify. In practise
  117. * this means overwrites only, since an insert could cause a node to
  118. * be split. Useful if you need access to the old value to calculate the
  119. * new one.
  120. *
  121. * This only works with single level btrees. The given key must be present in
  122. * the tree, otherwise -EINVAL will be returned.
  123. */
  124. int btree_get_overwrite_leaf(struct dm_btree_info *info, dm_block_t root,
  125. uint64_t key, int *index,
  126. dm_block_t *new_root, struct dm_block **leaf);
  127. #endif /* DM_BTREE_INTERNAL_H */