2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
7 #ifndef DM_BTREE_INTERNAL_H
8 #define DM_BTREE_INTERNAL_H
12 /*----------------------------------------------------------------*/
15 * We'll need 2 accessor functions for n->csum and n->blocknr
16 * to support dm-btree-spine.c in that case.
25 * Every btree node begins with this structure. Make sure it's a multiple
26 * of 8-bytes in size, otherwise the 64bit keys will be mis-aligned.
31 __le64 blocknr; /* Block this node is supposed to live in. */
37 } __attribute__((packed, aligned(8)));
40 struct node_header header;
42 } __attribute__((packed, aligned(8)));
46 * Locks a block using the btree node validator.
48 int bn_read_lock(struct dm_btree_info *info, dm_block_t b,
49 struct dm_block **result);
51 void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
52 struct dm_btree_value_type *vt);
54 int new_block(struct dm_btree_info *info, struct dm_block **result);
55 void unlock_block(struct dm_btree_info *info, struct dm_block *b);
58 * Spines keep track of the rolling locks. There are 2 variants, read-only
59 * and one that uses shadowing. These are separate structs to allow the
60 * type checker to spot misuse, for example accidentally calling read_lock
64 struct dm_btree_info *info;
67 struct dm_block *nodes[2];
70 void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info);
71 int exit_ro_spine(struct ro_spine *s);
72 int ro_step(struct ro_spine *s, dm_block_t new_child);
73 void ro_pop(struct ro_spine *s);
74 struct btree_node *ro_node(struct ro_spine *s);
77 struct dm_btree_info *info;
80 struct dm_block *nodes[2];
85 void init_shadow_spine(struct shadow_spine *s, struct dm_btree_info *info);
86 int exit_shadow_spine(struct shadow_spine *s);
88 int shadow_step(struct shadow_spine *s, dm_block_t b,
89 struct dm_btree_value_type *vt);
92 * The spine must have at least one entry before calling this.
94 struct dm_block *shadow_current(struct shadow_spine *s);
97 * The spine must have at least two entries before calling this.
99 struct dm_block *shadow_parent(struct shadow_spine *s);
101 int shadow_has_parent(struct shadow_spine *s);
103 int shadow_root(struct shadow_spine *s);
108 static inline __le64 *key_ptr(struct btree_node *n, uint32_t index)
110 return n->keys + index;
113 static inline void *value_base(struct btree_node *n)
115 return &n->keys[le32_to_cpu(n->header.max_entries)];
118 static inline void *value_ptr(struct btree_node *n, uint32_t index)
120 uint32_t value_size = le32_to_cpu(n->header.value_size);
121 return value_base(n) + (value_size * index);
125 * Assumes the values are suitably-aligned and converts to core format.
127 static inline uint64_t value64(struct btree_node *n, uint32_t index)
129 __le64 *values_le = value_base(n);
131 return le64_to_cpu(values_le[index]);
135 * Searching for a key within a single node.
137 int lower_bound(struct btree_node *n, uint64_t key);
139 extern struct dm_block_validator btree_node_validator;
142 * Value type for upper levels of multi-level btrees.
144 extern void init_le64_type(struct dm_transaction_manager *tm,
145 struct dm_btree_value_type *vt);
147 #endif /* DM_BTREE_INTERNAL_H */