2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_btree.h,v 1.24 2008/06/26 04:06:22 dillon Exp $
37 #ifndef VFS_HAMMER_BTREE_H_
38 #define VFS_HAMMER_BTREE_H_
43 * HAMMER implements a modified B+Tree. B+Trees store records only
44 * at their leaves and HAMMER's modification is to adjust the internal
45 * elements so there is a boundary element on each side instead of sub-tree
48 * We just call our modified B+Tree a 'B-Tree' in HAMMER documentation to
51 * A B-Tree internal node looks like this:
53 * B N N N N N N B <-- boundary and internal elements
54 * S S S S S S S <-- subtree pointers
56 * A B-Tree leaf node looks like this:
58 * L L L L L L L L <-- leaf elemenets
59 * (there is also a previous and next-leaf pointer)
61 * The recursion radix of an internal node is reduced by 1 relative to
62 * a normal B-Tree in order to accomodate the right-hand boundary.
63 * The left-hand boundary (B in the left) is integrated into the first
64 * element so it doesn't require 2 elements to accomodate boundaries.
66 * The big benefit to using a B-Tree with built-in bounds information is
67 * that it makes it possible to cache pointers into the middle of the tree
68 * and not have to start searches, insertions, OR deletions at the root node.
69 * The boundary elements allow searches to progress in a definitive direction
70 * from any point in the tree without revisting nodes. It is also possible
71 * to terminate searches early and make minor adjustments to the boundaries
72 * (within the confines of the parent's boundaries) on the fly. This greatly
73 * improves the efficiency of many operations.
75 * All of the structures below are on-disk structures.
79 * Common base for all B-Tree element types (40 bytes)
81 * btype field represents a type of B-Tree ondisk structure that this
82 * B-Tree element points to, but not a type of B-Tree node that this
83 * B-Tree element is a part of. btype could be HAMMER_BTREE_TYPE_RECORD
84 * as well as HAMMER_BTREE_TYPE_INTERNAL and HAMMER_BTREE_TYPE_LEAF,
85 * while B-Tree node type is never HAMMER_BTREE_TYPE_RECORD.
87 * The following fields are keys used by hammer_btree_cmp() to compare
88 * B-Tree elements listed from higher to lower priority on comparison.
89 * B-Tree elements are first grouped by localization value, and then
90 * obj_id within a subtree of the same localization value, and so on.
98 struct hammer_base_elm {
99 int64_t obj_id; /* 00 object record is associated with */
100 int64_t key; /* 08 indexing key (offset or namekey) */
102 hammer_tid_t create_tid; /* 10 transaction id for record creation */
103 hammer_tid_t delete_tid; /* 18 transaction id for record update/del */
105 uint16_t rec_type; /* 20 HAMMER_RECTYPE_ */
106 uint8_t obj_type; /* 22 HAMMER_OBJTYPE_ */
107 uint8_t btype; /* 23 B-Tree element type */
108 uint32_t localization; /* 24 B-Tree localization parameter */
112 typedef struct hammer_base_elm *hammer_base_elm_t;
115 * Localization has sorting priority over the obj_id,rec_type,key,tid
116 * and is used to localize inodes for very fast directory scans.
118 * Localization can also be used to create pseudo-filesystems within
119 * a HAMMER filesystem. Pseudo-filesystems would be suitable
120 * replication targets.
122 * Upper 16 bits of the localization field in struct hammer_base_elm
123 * represents pseudo-filesystem id ranging from default 0 to 65535,
124 * and lower 16 bits represents its localization type in bitfield
125 * where 0x1 means the element is for inode and 0x2 means the element
126 * is for anything other than inode. Note that 0x3 (0x1|0x2) is not
127 * a valid type, while 0 and 0xFFFF are valid types for some cases.
129 * The root inode (not the PFS root inode but the real root) uses
130 * HAMMER_DEF_LOCALIZATION for its incore ip->obj_localization.
131 * HAMMER_DEF_LOCALIZATION implies PFS 0 and no localization type.
133 #define HAMMER_LOCALIZE_INODE 0x00000001
134 #define HAMMER_LOCALIZE_MISC 0x00000002 /* not inode */
135 #define HAMMER_LOCALIZE_MASK 0x0000FFFF
136 #define HAMMER_LOCALIZE_PSEUDOFS_MASK 0xFFFF0000
138 #define HAMMER_MIN_LOCALIZATION 0x00000000U
139 #define HAMMER_MAX_LOCALIZATION 0x0000FFFFU
140 #define HAMMER_DEF_LOCALIZATION 0x00000000U
142 #define lo_to_pfs(lo) \
143 ((int)(((lo) & HAMMER_LOCALIZE_PSEUDOFS_MASK) >> 16))
144 #define pfs_to_lo(pfs) \
145 ((((uint32_t)(pfs)) << 16) & HAMMER_LOCALIZE_PSEUDOFS_MASK)
148 * Internal element (40 + 24 = 64 bytes).
150 * An internal element contains a recursion to another B-Tree node.
152 struct hammer_btree_internal_elm {
153 struct hammer_base_elm base;
154 hammer_tid_t mirror_tid; /* mirroring support */
155 hammer_off_t subtree_offset;
160 typedef struct hammer_btree_internal_elm *hammer_btree_internal_elm_t;
163 * Leaf B-Tree element (40 + 24 = 64 bytes).
165 * NOTE: create_ts/delete_ts are not used by any core algorithms, they are
166 * used only by userland to present nominal real-time date strings
169 struct hammer_btree_leaf_elm {
170 struct hammer_base_elm base;
173 hammer_off_t data_offset;
175 hammer_crc_t data_crc;
178 typedef struct hammer_btree_leaf_elm *hammer_btree_leaf_elm_t;
181 * Rollup btree leaf element types - 64 byte structure
183 union hammer_btree_elm {
184 struct hammer_base_elm base;
185 struct hammer_btree_leaf_elm leaf;
186 struct hammer_btree_internal_elm internal;
189 typedef union hammer_btree_elm *hammer_btree_elm_t;
192 * B-Tree node (64x64 = 4K structure)
194 * Each node contains 63 elements. The last element for an internal node
195 * is the right-boundary so internal nodes have one fewer logical elements
198 * 'count' always refers to the number of elements and is non-inclusive of
199 * the right-hand boundary for an internal node. For a leaf node, 'count'
200 * refers to the number of elements and there is no idea of boundaries.
202 * The use of a fairly large radix is designed to reduce the number of
203 * discrete disk accesses required to locate something. Keep in mind
204 * that nodes are allocated out of 16K hammer buffers so supported values
205 * are (256-1), (128-1), (64-1), (32-1), or (16-1). HAMMER uses 63-way
206 * so the node size is (64x(1+(64-1))) = 4KB.
208 * NOTE: FUTURE EXPANSION: The reserved fields in hammer_node_ondisk are
209 * reserved for left/right leaf linkage fields, flags, and other future
212 #define HAMMER_BTREE_TYPE_INTERNAL ((uint8_t)'I')
213 #define HAMMER_BTREE_TYPE_LEAF ((uint8_t)'L')
214 #define HAMMER_BTREE_TYPE_RECORD ((uint8_t)'R')
215 #define HAMMER_BTREE_TYPE_DELETED ((uint8_t)'D')
216 #define HAMMER_BTREE_TYPE_NONE ((uint8_t)0)
218 #define HAMMER_BTREE_LEAF_ELMS 63
219 #define HAMMER_BTREE_INT_ELMS (HAMMER_BTREE_LEAF_ELMS - 1)
221 struct hammer_node_ondisk {
223 * B-Tree node header (64 bytes)
225 hammer_crc_t crc; /* MUST BE FIRST FIELD OF STRUCTURE */
227 hammer_off_t parent; /* 0 if at root of B-Tree */
228 int32_t count; /* maximum 62 for INTERNAL, 63 for LEAF */
229 uint8_t type; /* B-Tree node type (INTERNAL or LEAF) */
232 hammer_off_t reserved03;
233 hammer_off_t reserved04;
234 hammer_off_t reserved05;
235 hammer_off_t reserved06;
236 hammer_tid_t mirror_tid; /* mirroring support (aggregator) */
239 * B-Tree node element array (64x63 bytes)
241 * Internal nodes have one less logical element
242 * (meaning: the same number of physical elements) in order to
243 * accomodate the right-hand boundary. The left-hand boundary
244 * is integrated into the first element. Leaf nodes have no
247 union hammer_btree_elm elms[HAMMER_BTREE_LEAF_ELMS];
250 typedef struct hammer_node_ondisk *hammer_node_ondisk_t;
252 #define HAMMER_BTREE_CRCSIZE \
253 (sizeof(struct hammer_node_ondisk) - sizeof(hammer_crc_t))
256 * Return 1 if elm is a node element of an internal node,
257 * otherwise return 0.
261 hammer_is_internal_node_elm(hammer_btree_elm_t elm)
263 switch (elm->base.btype) {
264 case HAMMER_BTREE_TYPE_INTERNAL:
265 case HAMMER_BTREE_TYPE_LEAF:
272 * Return 1 if elm is a node element of a leaf node,
273 * otherwise return 0.
277 hammer_is_leaf_node_elm(hammer_btree_elm_t elm)
279 switch (elm->base.btype) {
280 case HAMMER_BTREE_TYPE_RECORD:
288 hammer_node_max_elements(uint8_t type)
291 case HAMMER_BTREE_TYPE_LEAF:
292 return(HAMMER_BTREE_LEAF_ELMS);
293 case HAMMER_BTREE_TYPE_INTERNAL:
294 return(HAMMER_BTREE_INT_ELMS);
296 return(-1); /* invalid type */
301 hammer_elm_btype(hammer_btree_elm_t elm)
303 switch(elm->base.btype) {
304 case HAMMER_BTREE_TYPE_INTERNAL:
305 case HAMMER_BTREE_TYPE_LEAF:
306 case HAMMER_BTREE_TYPE_RECORD:
307 case HAMMER_BTREE_TYPE_DELETED:
308 return(elm->base.btype); /* ascii */
309 case HAMMER_BTREE_TYPE_NONE:
316 #endif /* !VFS_HAMMER_BTREE_H_ */