2 * Copyright (c) 2007-2008 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.c,v 1.69 2008/07/10 21:23:58 dillon Exp $
40 * HAMMER implements a modified B+Tree. In documentation this will
41 * simply be refered to as the HAMMER B-Tree. Basically a HAMMER B-Tree
42 * looks like a B+Tree (A B-Tree which stores its records only at the leafs
43 * of the tree), but adds two additional boundary elements which describe
44 * the left-most and right-most element a node is able to represent. In
45 * otherwords, we have boundary elements at the two ends of a B-Tree node
46 * instead of sub-tree pointers.
48 * A B-Tree internal node looks like this:
50 * B N N N N N N B <-- boundary and internal elements
51 * S S S S S S S <-- subtree pointers
53 * A B-Tree leaf node basically looks like this:
55 * L L L L L L L L <-- leaf elemenets
57 * The radix for an internal node is 1 less then a leaf but we get a
58 * number of significant benefits for our troubles.
60 * The big benefit to using a B-Tree containing boundary information
61 * is that it is possible to cache pointers into the middle of the tree
62 * and not have to start searches, insertions, OR deletions at the root
63 * node. In particular, searches are able to progress in a definitive
64 * direction from any point in the tree without revisting nodes. This
65 * greatly improves the efficiency of many operations, most especially
68 * B-Trees also make the stacking of trees fairly straightforward.
70 * INSERTIONS: A search performed with the intention of doing
71 * an insert will guarantee that the terminal leaf node is not full by
72 * splitting full nodes. Splits occur top-down during the dive down the
75 * DELETIONS: A deletion makes no attempt to proactively balance the
76 * tree and will recursively remove nodes that become empty. If a
77 * deadlock occurs a deletion may not be able to remove an empty leaf.
78 * Deletions never allow internal nodes to become empty (that would blow
85 static int btree_search(hammer_cursor_t cursor, int flags);
86 static int btree_split_internal(hammer_cursor_t cursor);
87 static int btree_split_leaf(hammer_cursor_t cursor);
88 static int btree_remove(hammer_cursor_t cursor);
89 static int btree_node_is_full(hammer_node_ondisk_t node);
90 static int hammer_btree_mirror_propagate(hammer_cursor_t cursor,
91 hammer_tid_t mirror_tid);
92 static void hammer_make_separator(hammer_base_elm_t key1,
93 hammer_base_elm_t key2, hammer_base_elm_t dest);
94 static void hammer_cursor_mirror_filter(hammer_cursor_t cursor);
97 * Iterate records after a search. The cursor is iterated forwards past
98 * the current record until a record matching the key-range requirements
99 * is found. ENOENT is returned if the iteration goes past the ending
102 * The iteration is inclusive of key_beg and can be inclusive or exclusive
103 * of key_end depending on whether HAMMER_CURSOR_END_INCLUSIVE is set.
105 * When doing an as-of search (cursor->asof != 0), key_beg.create_tid
106 * may be modified by B-Tree functions.
108 * cursor->key_beg may or may not be modified by this function during
109 * the iteration. XXX future - in case of an inverted lock we may have
110 * to reinitiate the lookup and set key_beg to properly pick up where we
113 * NOTE! EDEADLK *CANNOT* be returned by this procedure.
116 hammer_btree_iterate(hammer_cursor_t cursor)
118 hammer_node_ondisk_t node;
119 hammer_btree_elm_t elm;
125 * Skip past the current record
127 node = cursor->node->ondisk;
130 if (cursor->index < node->count &&
131 (cursor->flags & HAMMER_CURSOR_ATEDISK)) {
136 * Loop until an element is found or we are done.
140 * We iterate up the tree and then index over one element
141 * while we are at the last element in the current node.
143 * If we are at the root of the filesystem, cursor_up
146 * XXX this could be optimized by storing the information in
147 * the parent reference.
149 * XXX we can lose the node lock temporarily, this could mess
152 ++hammer_stats_btree_iterations;
153 hammer_flusher_clean_loose_ios(cursor->trans->hmp);
155 if (cursor->index == node->count) {
156 if (hammer_debug_btree) {
157 kprintf("BRACKETU %016llx[%d] -> %016llx[%d] (td=%p)\n",
158 cursor->node->node_offset,
160 (cursor->parent ? cursor->parent->node_offset : -1),
161 cursor->parent_index,
164 KKASSERT(cursor->parent == NULL || cursor->parent->ondisk->elms[cursor->parent_index].internal.subtree_offset == cursor->node->node_offset);
165 error = hammer_cursor_up(cursor);
168 /* reload stale pointer */
169 node = cursor->node->ondisk;
170 KKASSERT(cursor->index != node->count);
173 * If we are reblocking we want to return internal
176 if (cursor->flags & HAMMER_CURSOR_REBLOCKING) {
177 cursor->flags |= HAMMER_CURSOR_ATEDISK;
185 * Check internal or leaf element. Determine if the record
186 * at the cursor has gone beyond the end of our range.
188 * We recurse down through internal nodes.
190 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
191 elm = &node->elms[cursor->index];
193 r = hammer_btree_cmp(&cursor->key_end, &elm[0].base);
194 s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base);
195 if (hammer_debug_btree) {
196 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d (td=%p)\n",
197 cursor->node->node_offset,
199 elm[0].internal.base.obj_id,
200 elm[0].internal.base.rec_type,
201 elm[0].internal.base.key,
202 elm[0].internal.base.localization,
206 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
207 cursor->node->node_offset,
209 elm[1].internal.base.obj_id,
210 elm[1].internal.base.rec_type,
211 elm[1].internal.base.key,
212 elm[1].internal.base.localization,
221 if (r == 0 && (cursor->flags &
222 HAMMER_CURSOR_END_INCLUSIVE) == 0) {
231 KKASSERT(elm->internal.subtree_offset != 0);
234 * If running the mirror filter see if we can skip
235 * one or more entire sub-trees. If we can we
236 * return the internal mode and the caller processes
237 * the skipped range (see mirror_read)
239 if (cursor->flags & HAMMER_CURSOR_MIRROR_FILTERED) {
240 if (elm->internal.mirror_tid <
241 cursor->cmirror->mirror_tid) {
242 hammer_cursor_mirror_filter(cursor);
247 error = hammer_cursor_down(cursor);
250 KKASSERT(cursor->index == 0);
251 /* reload stale pointer */
252 node = cursor->node->ondisk;
255 elm = &node->elms[cursor->index];
256 r = hammer_btree_cmp(&cursor->key_end, &elm->base);
257 if (hammer_debug_btree) {
258 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
259 cursor->node->node_offset,
261 (elm[0].leaf.base.btype ?
262 elm[0].leaf.base.btype : '?'),
263 elm[0].leaf.base.obj_id,
264 elm[0].leaf.base.rec_type,
265 elm[0].leaf.base.key,
266 elm[0].leaf.base.localization,
276 * We support both end-inclusive and
277 * end-exclusive searches.
280 (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) {
285 switch(elm->leaf.base.btype) {
286 case HAMMER_BTREE_TYPE_RECORD:
287 if ((cursor->flags & HAMMER_CURSOR_ASOF) &&
288 hammer_btree_chkts(cursor->asof, &elm->base)) {
301 * node pointer invalid after loop
307 if (hammer_debug_btree) {
308 int i = cursor->index;
309 hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i];
310 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
312 elm->internal.base.obj_id,
313 elm->internal.base.rec_type,
314 elm->internal.base.key,
315 elm->internal.base.localization
324 * We hit an internal element that we could skip as part of a mirroring
325 * scan. Calculate the entire range being skipped.
327 * It is important to include any gaps between the parent's left_bound
328 * and the node's left_bound, and same goes for the right side.
331 hammer_cursor_mirror_filter(hammer_cursor_t cursor)
333 struct hammer_cmirror *cmirror;
334 hammer_node_ondisk_t ondisk;
335 hammer_btree_elm_t elm;
337 ondisk = cursor->node->ondisk;
338 cmirror = cursor->cmirror;
341 * Calculate the skipped range
343 elm = &ondisk->elms[cursor->index];
344 if (cursor->index == 0)
345 cmirror->skip_beg = *cursor->left_bound;
347 cmirror->skip_beg = elm->internal.base;
348 while (cursor->index < ondisk->count) {
349 if (elm->internal.mirror_tid >= cmirror->mirror_tid)
354 if (cursor->index == ondisk->count)
355 cmirror->skip_end = *cursor->right_bound;
357 cmirror->skip_end = elm->internal.base;
360 * clip the returned result.
362 if (hammer_btree_cmp(&cmirror->skip_beg, &cursor->key_beg) < 0)
363 cmirror->skip_beg = cursor->key_beg;
364 if (hammer_btree_cmp(&cmirror->skip_end, &cursor->key_end) > 0)
365 cmirror->skip_end = cursor->key_end;
369 * Iterate in the reverse direction. This is used by the pruning code to
370 * avoid overlapping records.
373 hammer_btree_iterate_reverse(hammer_cursor_t cursor)
375 hammer_node_ondisk_t node;
376 hammer_btree_elm_t elm;
381 /* mirror filtering not supported for reverse iteration */
382 KKASSERT ((cursor->flags & HAMMER_CURSOR_MIRROR_FILTERED) == 0);
385 * Skip past the current record. For various reasons the cursor
386 * may end up set to -1 or set to point at the end of the current
387 * node. These cases must be addressed.
389 node = cursor->node->ondisk;
392 if (cursor->index != -1 &&
393 (cursor->flags & HAMMER_CURSOR_ATEDISK)) {
396 if (cursor->index == cursor->node->ondisk->count)
400 * Loop until an element is found or we are done.
403 ++hammer_stats_btree_iterations;
404 hammer_flusher_clean_loose_ios(cursor->trans->hmp);
407 * We iterate up the tree and then index over one element
408 * while we are at the last element in the current node.
410 if (cursor->index == -1) {
411 error = hammer_cursor_up(cursor);
413 cursor->index = 0; /* sanity */
416 /* reload stale pointer */
417 node = cursor->node->ondisk;
418 KKASSERT(cursor->index != node->count);
424 * Check internal or leaf element. Determine if the record
425 * at the cursor has gone beyond the end of our range.
427 * We recurse down through internal nodes.
429 KKASSERT(cursor->index != node->count);
430 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
431 elm = &node->elms[cursor->index];
432 r = hammer_btree_cmp(&cursor->key_end, &elm[0].base);
433 s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base);
434 if (hammer_debug_btree) {
435 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
436 cursor->node->node_offset,
438 elm[0].internal.base.obj_id,
439 elm[0].internal.base.rec_type,
440 elm[0].internal.base.key,
441 elm[0].internal.base.localization,
444 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
445 cursor->node->node_offset,
447 elm[1].internal.base.obj_id,
448 elm[1].internal.base.rec_type,
449 elm[1].internal.base.key,
450 elm[1].internal.base.localization,
464 KKASSERT(elm->internal.subtree_offset != 0);
466 error = hammer_cursor_down(cursor);
469 KKASSERT(cursor->index == 0);
470 /* reload stale pointer */
471 node = cursor->node->ondisk;
473 /* this can assign -1 if the leaf was empty */
474 cursor->index = node->count - 1;
477 elm = &node->elms[cursor->index];
478 s = hammer_btree_cmp(&cursor->key_beg, &elm->base);
479 if (hammer_debug_btree) {
480 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
481 cursor->node->node_offset,
483 (elm[0].leaf.base.btype ?
484 elm[0].leaf.base.btype : '?'),
485 elm[0].leaf.base.obj_id,
486 elm[0].leaf.base.rec_type,
487 elm[0].leaf.base.key,
488 elm[0].leaf.base.localization,
497 switch(elm->leaf.base.btype) {
498 case HAMMER_BTREE_TYPE_RECORD:
499 if ((cursor->flags & HAMMER_CURSOR_ASOF) &&
500 hammer_btree_chkts(cursor->asof, &elm->base)) {
513 * node pointer invalid after loop
519 if (hammer_debug_btree) {
520 int i = cursor->index;
521 hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i];
522 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
524 elm->internal.base.obj_id,
525 elm->internal.base.rec_type,
526 elm->internal.base.key,
527 elm->internal.base.localization
536 * Lookup cursor->key_beg. 0 is returned on success, ENOENT if the entry
537 * could not be found, EDEADLK if inserting and a retry is needed, and a
538 * fatal error otherwise. When retrying, the caller must terminate the
539 * cursor and reinitialize it. EDEADLK cannot be returned if not inserting.
541 * The cursor is suitably positioned for a deletion on success, and suitably
542 * positioned for an insertion on ENOENT if HAMMER_CURSOR_INSERT was
545 * The cursor may begin anywhere, the search will traverse the tree in
546 * either direction to locate the requested element.
548 * Most of the logic implementing historical searches is handled here. We
549 * do an initial lookup with create_tid set to the asof TID. Due to the
550 * way records are laid out, a backwards iteration may be required if
551 * ENOENT is returned to locate the historical record. Here's the
554 * create_tid: 10 15 20
558 * Lets say we want to do a lookup AS-OF timestamp 17. We will traverse
559 * LEAF2 but the only record in LEAF2 has a create_tid of 18, which is
560 * not visible and thus causes ENOENT to be returned. We really need
561 * to check record 11 in LEAF1. If it also fails then the search fails
562 * (e.g. it might represent the range 11-16 and thus still not match our
563 * AS-OF timestamp of 17). Note that LEAF1 could be empty, requiring
564 * further iterations.
566 * If this case occurs btree_search() will set HAMMER_CURSOR_CREATE_CHECK
567 * and the cursor->create_check TID if an iteration might be needed.
568 * In the above example create_check would be set to 14.
571 hammer_btree_lookup(hammer_cursor_t cursor)
575 ++hammer_stats_btree_lookups;
576 if (cursor->flags & HAMMER_CURSOR_ASOF) {
577 KKASSERT((cursor->flags & HAMMER_CURSOR_INSERT) == 0);
578 cursor->key_beg.create_tid = cursor->asof;
580 cursor->flags &= ~HAMMER_CURSOR_CREATE_CHECK;
581 error = btree_search(cursor, 0);
582 if (error != ENOENT ||
583 (cursor->flags & HAMMER_CURSOR_CREATE_CHECK) == 0) {
586 * Stop if error other then ENOENT.
587 * Stop if ENOENT and not special case.
591 if (hammer_debug_btree) {
592 kprintf("CREATE_CHECK %016llx\n",
593 cursor->create_check);
595 cursor->key_beg.create_tid = cursor->create_check;
599 error = btree_search(cursor, 0);
602 error = hammer_btree_extract(cursor, cursor->flags);
607 * Execute the logic required to start an iteration. The first record
608 * located within the specified range is returned and iteration control
609 * flags are adjusted for successive hammer_btree_iterate() calls.
612 hammer_btree_first(hammer_cursor_t cursor)
616 error = hammer_btree_lookup(cursor);
617 if (error == ENOENT) {
618 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
619 error = hammer_btree_iterate(cursor);
621 cursor->flags |= HAMMER_CURSOR_ATEDISK;
626 * Similarly but for an iteration in the reverse direction.
628 * Set ATEDISK when iterating backwards to skip the current entry,
629 * which after an ENOENT lookup will be pointing beyond our end point.
632 hammer_btree_last(hammer_cursor_t cursor)
634 struct hammer_base_elm save;
637 save = cursor->key_beg;
638 cursor->key_beg = cursor->key_end;
639 error = hammer_btree_lookup(cursor);
640 cursor->key_beg = save;
641 if (error == ENOENT ||
642 (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) {
643 cursor->flags |= HAMMER_CURSOR_ATEDISK;
644 error = hammer_btree_iterate_reverse(cursor);
646 cursor->flags |= HAMMER_CURSOR_ATEDISK;
651 * Extract the record and/or data associated with the cursor's current
652 * position. Any prior record or data stored in the cursor is replaced.
653 * The cursor must be positioned at a leaf node.
655 * NOTE: All extractions occur at the leaf of the B-Tree.
658 hammer_btree_extract(hammer_cursor_t cursor, int flags)
661 hammer_node_ondisk_t node;
662 hammer_btree_elm_t elm;
663 hammer_off_t data_off;
668 * The case where the data reference resolves to the same buffer
669 * as the record reference must be handled.
671 node = cursor->node->ondisk;
672 elm = &node->elms[cursor->index];
674 hmp = cursor->node->hmp;
677 * There is nothing to extract for an internal element.
679 if (node->type == HAMMER_BTREE_TYPE_INTERNAL)
683 * Only record types have data.
685 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
686 cursor->leaf = &elm->leaf;
688 if ((flags & HAMMER_CURSOR_GET_DATA) == 0)
690 if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD)
692 data_off = elm->leaf.data_offset;
693 data_len = elm->leaf.data_len;
700 KKASSERT(data_len >= 0 && data_len <= HAMMER_XBUFSIZE);
701 cursor->data = hammer_bread_ext(hmp, data_off, data_len,
702 &error, &cursor->data_buffer);
703 if (hammer_crc_test_leaf(cursor->data, &elm->leaf) == 0)
704 Debugger("CRC FAILED: DATA");
710 * Insert a leaf element into the B-Tree at the current cursor position.
711 * The cursor is positioned such that the element at and beyond the cursor
712 * are shifted to make room for the new record.
714 * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT
715 * flag set and that call must return ENOENT before this function can be
718 * The caller may depend on the cursor's exclusive lock after return to
719 * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE).
721 * ENOSPC is returned if there is no room to insert a new record.
724 hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm,
727 hammer_node_ondisk_t node;
732 if ((error = hammer_cursor_upgrade_node(cursor)) != 0)
734 ++hammer_stats_btree_inserts;
737 * Insert the element at the leaf node and update the count in the
738 * parent. It is possible for parent to be NULL, indicating that
739 * the filesystem's ROOT B-Tree node is a leaf itself, which is
740 * possible. The root inode can never be deleted so the leaf should
743 * Remember that the right-hand boundary is not included in the
746 hammer_modify_node_all(cursor->trans, cursor->node);
747 node = cursor->node->ondisk;
749 KKASSERT(elm->base.btype != 0);
750 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
751 KKASSERT(node->count < HAMMER_BTREE_LEAF_ELMS);
752 if (i != node->count) {
753 bcopy(&node->elms[i], &node->elms[i+1],
754 (node->count - i) * sizeof(*elm));
756 node->elms[i].leaf = *elm;
758 hammer_cursor_inserted_element(cursor->node, i);
761 * Update the leaf node's aggregate mirror_tid for mirroring
764 if (node->mirror_tid < elm->base.delete_tid) {
765 node->mirror_tid = elm->base.delete_tid;
768 if (node->mirror_tid < elm->base.create_tid) {
769 node->mirror_tid = elm->base.create_tid;
772 hammer_modify_node_done(cursor->node);
775 * Debugging sanity checks.
777 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->base) <= 0);
778 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->base) > 0);
780 KKASSERT(hammer_btree_cmp(&node->elms[i-1].leaf.base, &elm->base) < 0);
782 if (i != node->count - 1)
783 KKASSERT(hammer_btree_cmp(&node->elms[i+1].leaf.base, &elm->base) > 0);
789 * Delete a record from the B-Tree at the current cursor position.
790 * The cursor is positioned such that the current element is the one
793 * On return the cursor will be positioned after the deleted element and
794 * MAY point to an internal node. It will be suitable for the continuation
795 * of an iteration but not for an insertion or deletion.
797 * Deletions will attempt to partially rebalance the B-Tree in an upward
798 * direction, but will terminate rather then deadlock. Empty internal nodes
799 * are never allowed by a deletion which deadlocks may end up giving us an
800 * empty leaf. The pruner will clean up and rebalance the tree.
802 * This function can return EDEADLK, requiring the caller to retry the
803 * operation after clearing the deadlock.
806 hammer_btree_delete(hammer_cursor_t cursor)
808 hammer_node_ondisk_t ondisk;
810 hammer_node_t parent;
814 if ((error = hammer_cursor_upgrade(cursor)) != 0)
816 ++hammer_stats_btree_deletes;
819 * Delete the element from the leaf node.
821 * Remember that leaf nodes do not have boundaries.
824 ondisk = node->ondisk;
827 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_LEAF);
828 KKASSERT(i >= 0 && i < ondisk->count);
829 hammer_modify_node_all(cursor->trans, node);
830 if (i + 1 != ondisk->count) {
831 bcopy(&ondisk->elms[i+1], &ondisk->elms[i],
832 (ondisk->count - i - 1) * sizeof(ondisk->elms[0]));
835 hammer_modify_node_done(node);
836 hammer_cursor_deleted_element(node, i);
839 * Validate local parent
841 if (ondisk->parent) {
842 parent = cursor->parent;
844 KKASSERT(parent != NULL);
845 KKASSERT(parent->node_offset == ondisk->parent);
849 * If the leaf becomes empty it must be detached from the parent,
850 * potentially recursing through to the filesystem root.
852 * This may reposition the cursor at one of the parent's of the
855 * Ignore deadlock errors, that simply means that btree_remove
856 * was unable to recurse and had to leave us with an empty leaf.
858 KKASSERT(cursor->index <= ondisk->count);
859 if (ondisk->count == 0) {
860 error = btree_remove(cursor);
861 if (error == EDEADLK)
866 KKASSERT(cursor->parent == NULL ||
867 cursor->parent_index < cursor->parent->ondisk->count);
872 * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE
874 * Search the filesystem B-Tree for cursor->key_beg, return the matching node.
876 * The search can begin ANYWHERE in the B-Tree. As a first step the search
877 * iterates up the tree as necessary to properly position itself prior to
878 * actually doing the sarch.
880 * INSERTIONS: The search will split full nodes and leaves on its way down
881 * and guarentee that the leaf it ends up on is not full. If we run out
882 * of space the search continues to the leaf (to position the cursor for
883 * the spike), but ENOSPC is returned.
885 * The search is only guarenteed to end up on a leaf if an error code of 0
886 * is returned, or if inserting and an error code of ENOENT is returned.
887 * Otherwise it can stop at an internal node. On success a search returns
890 * COMPLEXITY WARNING! This is the core B-Tree search code for the entire
891 * filesystem, and it is not simple code. Please note the following facts:
893 * - Internal node recursions have a boundary on the left AND right. The
894 * right boundary is non-inclusive. The create_tid is a generic part
895 * of the key for internal nodes.
897 * - Leaf nodes contain terminal elements only now.
899 * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a
900 * historical search. ASOF and INSERT are mutually exclusive. When
901 * doing an as-of lookup btree_search() checks for a right-edge boundary
902 * case. If while recursing down the left-edge differs from the key
903 * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along
904 * with cursor->create_check. This is used by btree_lookup() to iterate.
905 * The iteration backwards because as-of searches can wind up going
906 * down the wrong branch of the B-Tree.
910 btree_search(hammer_cursor_t cursor, int flags)
912 hammer_node_ondisk_t node;
913 hammer_btree_elm_t elm;
920 flags |= cursor->flags;
921 ++hammer_stats_btree_searches;
923 if (hammer_debug_btree) {
924 kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx lo=%02x (td = %p)\n",
925 cursor->node->node_offset,
927 cursor->key_beg.obj_id,
928 cursor->key_beg.rec_type,
930 cursor->key_beg.create_tid,
931 cursor->key_beg.localization,
935 kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n",
936 cursor->parent->node_offset, cursor->parent_index,
937 cursor->left_bound->obj_id,
938 cursor->parent->ondisk->elms[cursor->parent_index].internal.base.obj_id,
939 cursor->right_bound->obj_id,
940 cursor->parent->ondisk->elms[cursor->parent_index+1].internal.base.obj_id,
942 &cursor->parent->ondisk->elms[cursor->parent_index],
944 &cursor->parent->ondisk->elms[cursor->parent_index+1]
949 * Move our cursor up the tree until we find a node whos range covers
950 * the key we are trying to locate.
952 * The left bound is inclusive, the right bound is non-inclusive.
953 * It is ok to cursor up too far.
956 r = hammer_btree_cmp(&cursor->key_beg, cursor->left_bound);
957 s = hammer_btree_cmp(&cursor->key_beg, cursor->right_bound);
960 KKASSERT(cursor->parent);
961 ++hammer_stats_btree_iterations;
962 error = hammer_cursor_up(cursor);
968 * The delete-checks below are based on node, not parent. Set the
969 * initial delete-check based on the parent.
972 KKASSERT(cursor->left_bound->create_tid != 1);
973 cursor->create_check = cursor->left_bound->create_tid - 1;
974 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
978 * We better have ended up with a node somewhere.
980 KKASSERT(cursor->node != NULL);
983 * If we are inserting we can't start at a full node if the parent
984 * is also full (because there is no way to split the node),
985 * continue running up the tree until the requirement is satisfied
986 * or we hit the root of the filesystem.
988 * (If inserting we aren't doing an as-of search so we don't have
989 * to worry about create_check).
991 while ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
992 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
993 if (btree_node_is_full(cursor->node->ondisk) == 0)
996 if (btree_node_is_full(cursor->node->ondisk) ==0)
999 if (cursor->node->ondisk->parent == 0 ||
1000 cursor->parent->ondisk->count != HAMMER_BTREE_INT_ELMS) {
1003 ++hammer_stats_btree_iterations;
1004 error = hammer_cursor_up(cursor);
1005 /* node may have become stale */
1011 * Push down through internal nodes to locate the requested key.
1013 node = cursor->node->ondisk;
1014 while (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
1016 * Scan the node to find the subtree index to push down into.
1017 * We go one-past, then back-up.
1019 * We must proactively remove deleted elements which may
1020 * have been left over from a deadlocked btree_remove().
1022 * The left and right boundaries are included in the loop
1023 * in order to detect edge cases.
1025 * If the separator only differs by create_tid (r == 1)
1026 * and we are doing an as-of search, we may end up going
1027 * down a branch to the left of the one containing the
1028 * desired key. This requires numerous special cases.
1030 ++hammer_stats_btree_iterations;
1031 if (hammer_debug_btree) {
1032 kprintf("SEARCH-I %016llx count=%d\n",
1033 cursor->node->node_offset,
1038 * Try to shortcut the search before dropping into the
1039 * linear loop. Locate the first node where r <= 1.
1041 i = hammer_btree_search_node(&cursor->key_beg, node);
1042 while (i <= node->count) {
1043 ++hammer_stats_btree_elements;
1044 elm = &node->elms[i];
1045 r = hammer_btree_cmp(&cursor->key_beg, &elm->base);
1046 if (hammer_debug_btree > 2) {
1047 kprintf(" IELM %p %d r=%d\n",
1048 &node->elms[i], i, r);
1053 KKASSERT(elm->base.create_tid != 1);
1054 cursor->create_check = elm->base.create_tid - 1;
1055 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
1059 if (hammer_debug_btree) {
1060 kprintf("SEARCH-I preI=%d/%d r=%d\n",
1065 * These cases occur when the parent's idea of the boundary
1066 * is wider then the child's idea of the boundary, and
1067 * require special handling. If not inserting we can
1068 * terminate the search early for these cases but the
1069 * child's boundaries cannot be unconditionally modified.
1073 * If i == 0 the search terminated to the LEFT of the
1074 * left_boundary but to the RIGHT of the parent's left
1079 elm = &node->elms[0];
1082 * If we aren't inserting we can stop here.
1084 if ((flags & (HAMMER_CURSOR_INSERT |
1085 HAMMER_CURSOR_PRUNING)) == 0) {
1091 * Correct a left-hand boundary mismatch.
1093 * We can only do this if we can upgrade the lock,
1094 * and synchronized as a background cursor (i.e.
1095 * inserting or pruning).
1097 * WARNING: We can only do this if inserting, i.e.
1098 * we are running on the backend.
1100 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1102 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1103 hammer_modify_node_field(cursor->trans, cursor->node,
1105 save = node->elms[0].base.btype;
1106 node->elms[0].base = *cursor->left_bound;
1107 node->elms[0].base.btype = save;
1108 hammer_modify_node_done(cursor->node);
1109 } else if (i == node->count + 1) {
1111 * If i == node->count + 1 the search terminated to
1112 * the RIGHT of the right boundary but to the LEFT
1113 * of the parent's right boundary. If we aren't
1114 * inserting we can stop here.
1116 * Note that the last element in this case is
1117 * elms[i-2] prior to adjustments to 'i'.
1120 if ((flags & (HAMMER_CURSOR_INSERT |
1121 HAMMER_CURSOR_PRUNING)) == 0) {
1127 * Correct a right-hand boundary mismatch.
1128 * (actual push-down record is i-2 prior to
1129 * adjustments to i).
1131 * We can only do this if we can upgrade the lock,
1132 * and synchronized as a background cursor (i.e.
1133 * inserting or pruning).
1135 * WARNING: We can only do this if inserting, i.e.
1136 * we are running on the backend.
1138 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1140 elm = &node->elms[i];
1141 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1142 hammer_modify_node(cursor->trans, cursor->node,
1143 &elm->base, sizeof(elm->base));
1144 elm->base = *cursor->right_bound;
1145 hammer_modify_node_done(cursor->node);
1149 * The push-down index is now i - 1. If we had
1150 * terminated on the right boundary this will point
1151 * us at the last element.
1156 elm = &node->elms[i];
1158 if (hammer_debug_btree) {
1159 kprintf("RESULT-I %016llx[%d] %016llx %02x "
1160 "key=%016llx cre=%016llx lo=%02x\n",
1161 cursor->node->node_offset,
1163 elm->internal.base.obj_id,
1164 elm->internal.base.rec_type,
1165 elm->internal.base.key,
1166 elm->internal.base.create_tid,
1167 elm->internal.base.localization
1172 * We better have a valid subtree offset.
1174 KKASSERT(elm->internal.subtree_offset != 0);
1177 * Handle insertion and deletion requirements.
1179 * If inserting split full nodes. The split code will
1180 * adjust cursor->node and cursor->index if the current
1181 * index winds up in the new node.
1183 * If inserting and a left or right edge case was detected,
1184 * we cannot correct the left or right boundary and must
1185 * prepend and append an empty leaf node in order to make
1186 * the boundary correction.
1188 * If we run out of space we set enospc and continue on
1189 * to a leaf to provide the spike code with a good point
1192 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
1193 if (btree_node_is_full(node)) {
1194 error = btree_split_internal(cursor);
1196 if (error != ENOSPC)
1201 * reload stale pointers
1204 node = cursor->node->ondisk;
1209 * Push down (push into new node, existing node becomes
1210 * the parent) and continue the search.
1212 error = hammer_cursor_down(cursor);
1213 /* node may have become stale */
1216 node = cursor->node->ondisk;
1220 * We are at a leaf, do a linear search of the key array.
1222 * On success the index is set to the matching element and 0
1225 * On failure the index is set to the insertion point and ENOENT
1228 * Boundaries are not stored in leaf nodes, so the index can wind
1229 * up to the left of element 0 (index == 0) or past the end of
1230 * the array (index == node->count). It is also possible that the
1231 * leaf might be empty.
1233 ++hammer_stats_btree_iterations;
1234 KKASSERT (node->type == HAMMER_BTREE_TYPE_LEAF);
1235 KKASSERT(node->count <= HAMMER_BTREE_LEAF_ELMS);
1236 if (hammer_debug_btree) {
1237 kprintf("SEARCH-L %016llx count=%d\n",
1238 cursor->node->node_offset,
1243 * Try to shortcut the search before dropping into the
1244 * linear loop. Locate the first node where r <= 1.
1246 i = hammer_btree_search_node(&cursor->key_beg, node);
1247 while (i < node->count) {
1248 ++hammer_stats_btree_elements;
1249 elm = &node->elms[i];
1251 r = hammer_btree_cmp(&cursor->key_beg, &elm->leaf.base);
1253 if (hammer_debug_btree > 1)
1254 kprintf(" ELM %p %d r=%d\n", &node->elms[i], i, r);
1257 * We are at a record element. Stop if we've flipped past
1258 * key_beg, not counting the create_tid test. Allow the
1259 * r == 1 case (key_beg > element but differs only by its
1260 * create_tid) to fall through to the AS-OF check.
1262 KKASSERT (elm->leaf.base.btype == HAMMER_BTREE_TYPE_RECORD);
1272 * Check our as-of timestamp against the element.
1274 if (flags & HAMMER_CURSOR_ASOF) {
1275 if (hammer_btree_chkts(cursor->asof,
1276 &node->elms[i].base) != 0) {
1282 if (r > 0) { /* can only be +1 */
1290 if (hammer_debug_btree) {
1291 kprintf("RESULT-L %016llx[%d] (SUCCESS)\n",
1292 cursor->node->node_offset, i);
1298 * The search of the leaf node failed. i is the insertion point.
1301 if (hammer_debug_btree) {
1302 kprintf("RESULT-L %016llx[%d] (FAILED)\n",
1303 cursor->node->node_offset, i);
1307 * No exact match was found, i is now at the insertion point.
1309 * If inserting split a full leaf before returning. This
1310 * may have the side effect of adjusting cursor->node and
1314 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0 &&
1315 btree_node_is_full(node)) {
1316 error = btree_split_leaf(cursor);
1318 if (error != ENOSPC)
1323 * reload stale pointers
1327 node = &cursor->node->internal;
1332 * We reached a leaf but did not find the key we were looking for.
1333 * If this is an insert we will be properly positioned for an insert
1334 * (ENOENT) or spike (ENOSPC) operation.
1336 error = enospc ? ENOSPC : ENOENT;
1342 * Heuristical search for the first element whos comparison is <= 1. May
1343 * return an index whos compare result is > 1 but may only return an index
1344 * whos compare result is <= 1 if it is the first element with that result.
1347 hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node)
1355 * Don't bother if the node does not have very many elements
1360 i = b + (s - b) / 2;
1361 ++hammer_stats_btree_elements;
1362 r = hammer_btree_cmp(elm, &node->elms[i].leaf.base);
1373 /************************************************************************
1374 * SPLITTING AND MERGING *
1375 ************************************************************************
1377 * These routines do all the dirty work required to split and merge nodes.
1381 * Split an internal node into two nodes and move the separator at the split
1382 * point to the parent.
1384 * (cursor->node, cursor->index) indicates the element the caller intends
1385 * to push into. We will adjust node and index if that element winds
1386 * up in the split node.
1388 * If we are at the root of the filesystem a new root must be created with
1389 * two elements, one pointing to the original root and one pointing to the
1390 * newly allocated split node.
1394 btree_split_internal(hammer_cursor_t cursor)
1396 hammer_node_ondisk_t ondisk;
1398 hammer_node_t parent;
1399 hammer_node_t new_node;
1400 hammer_btree_elm_t elm;
1401 hammer_btree_elm_t parent_elm;
1402 hammer_node_locklist_t locklist = NULL;
1403 hammer_mount_t hmp = cursor->trans->hmp;
1409 const int esize = sizeof(*elm);
1411 error = hammer_btree_lock_children(cursor, &locklist);
1414 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1416 ++hammer_stats_btree_splits;
1419 * We are splitting but elms[split] will be promoted to the parent,
1420 * leaving the right hand node with one less element. If the
1421 * insertion point will be on the left-hand side adjust the split
1422 * point to give the right hand side one additional node.
1424 node = cursor->node;
1425 ondisk = node->ondisk;
1426 split = (ondisk->count + 1) / 2;
1427 if (cursor->index <= split)
1431 * If we are at the root of the filesystem, create a new root node
1432 * with 1 element and split normally. Avoid making major
1433 * modifications until we know the whole operation will work.
1435 if (ondisk->parent == 0) {
1436 parent = hammer_alloc_btree(cursor->trans, &error);
1439 hammer_lock_ex(&parent->lock);
1440 hammer_modify_node_noundo(cursor->trans, parent);
1441 ondisk = parent->ondisk;
1444 ondisk->mirror_tid = node->ondisk->mirror_tid;
1445 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1446 ondisk->elms[0].base = hmp->root_btree_beg;
1447 ondisk->elms[0].base.btype = node->ondisk->type;
1448 ondisk->elms[0].internal.subtree_offset = node->node_offset;
1449 ondisk->elms[1].base = hmp->root_btree_end;
1450 hammer_modify_node_done(parent);
1451 /* ondisk->elms[1].base.btype - not used */
1453 parent_index = 0; /* index of current node in parent */
1456 parent = cursor->parent;
1457 parent_index = cursor->parent_index;
1461 * Split node into new_node at the split point.
1463 * B O O O P N N B <-- P = node->elms[split]
1464 * 0 1 2 3 4 5 6 <-- subtree indices
1469 * B O O O B B N N B <--- inner boundary points are 'P'
1473 new_node = hammer_alloc_btree(cursor->trans, &error);
1474 if (new_node == NULL) {
1476 hammer_unlock(&parent->lock);
1477 hammer_delete_node(cursor->trans, parent);
1478 hammer_rel_node(parent);
1482 hammer_lock_ex(&new_node->lock);
1485 * Create the new node. P becomes the left-hand boundary in the
1486 * new node. Copy the right-hand boundary as well.
1488 * elm is the new separator.
1490 hammer_modify_node_noundo(cursor->trans, new_node);
1491 hammer_modify_node_all(cursor->trans, node);
1492 ondisk = node->ondisk;
1493 elm = &ondisk->elms[split];
1494 bcopy(elm, &new_node->ondisk->elms[0],
1495 (ondisk->count - split + 1) * esize);
1496 new_node->ondisk->count = ondisk->count - split;
1497 new_node->ondisk->parent = parent->node_offset;
1498 new_node->ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1499 new_node->ondisk->mirror_tid = ondisk->mirror_tid;
1500 KKASSERT(ondisk->type == new_node->ondisk->type);
1501 hammer_cursor_split_node(node, new_node, split);
1504 * Cleanup the original node. Elm (P) becomes the new boundary,
1505 * its subtree_offset was moved to the new node. If we had created
1506 * a new root its parent pointer may have changed.
1508 elm->internal.subtree_offset = 0;
1509 ondisk->count = split;
1512 * Insert the separator into the parent, fixup the parent's
1513 * reference to the original node, and reference the new node.
1514 * The separator is P.
1516 * Remember that base.count does not include the right-hand boundary.
1518 hammer_modify_node_all(cursor->trans, parent);
1519 ondisk = parent->ondisk;
1520 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
1521 parent_elm = &ondisk->elms[parent_index+1];
1522 bcopy(parent_elm, parent_elm + 1,
1523 (ondisk->count - parent_index) * esize);
1524 parent_elm->internal.base = elm->base; /* separator P */
1525 parent_elm->internal.base.btype = new_node->ondisk->type;
1526 parent_elm->internal.subtree_offset = new_node->node_offset;
1527 parent_elm->internal.mirror_tid = new_node->ondisk->mirror_tid;
1529 hammer_modify_node_done(parent);
1530 hammer_cursor_inserted_element(parent, parent_index + 1);
1533 * The children of new_node need their parent pointer set to new_node.
1534 * The children have already been locked by
1535 * hammer_btree_lock_children().
1537 for (i = 0; i < new_node->ondisk->count; ++i) {
1538 elm = &new_node->ondisk->elms[i];
1539 error = btree_set_parent(cursor->trans, new_node, elm);
1541 panic("btree_split_internal: btree-fixup problem");
1544 hammer_modify_node_done(new_node);
1547 * The filesystem's root B-Tree pointer may have to be updated.
1550 hammer_volume_t volume;
1552 volume = hammer_get_root_volume(hmp, &error);
1553 KKASSERT(error == 0);
1555 hammer_modify_volume_field(cursor->trans, volume,
1557 volume->ondisk->vol0_btree_root = parent->node_offset;
1558 hammer_modify_volume_done(volume);
1559 node->ondisk->parent = parent->node_offset;
1560 if (cursor->parent) {
1561 hammer_unlock(&cursor->parent->lock);
1562 hammer_rel_node(cursor->parent);
1564 cursor->parent = parent; /* lock'd and ref'd */
1565 hammer_rel_volume(volume, 0);
1567 hammer_modify_node_done(node);
1570 * Ok, now adjust the cursor depending on which element the original
1571 * index was pointing at. If we are >= the split point the push node
1572 * is now in the new node.
1574 * NOTE: If we are at the split point itself we cannot stay with the
1575 * original node because the push index will point at the right-hand
1576 * boundary, which is illegal.
1578 * NOTE: The cursor's parent or parent_index must be adjusted for
1579 * the case where a new parent (new root) was created, and the case
1580 * where the cursor is now pointing at the split node.
1582 if (cursor->index >= split) {
1583 cursor->parent_index = parent_index + 1;
1584 cursor->index -= split;
1585 hammer_unlock(&cursor->node->lock);
1586 hammer_rel_node(cursor->node);
1587 cursor->node = new_node; /* locked and ref'd */
1589 cursor->parent_index = parent_index;
1590 hammer_unlock(&new_node->lock);
1591 hammer_rel_node(new_node);
1595 * Fixup left and right bounds
1597 parent_elm = &parent->ondisk->elms[cursor->parent_index];
1598 cursor->left_bound = &parent_elm[0].internal.base;
1599 cursor->right_bound = &parent_elm[1].internal.base;
1600 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1601 &cursor->node->ondisk->elms[0].internal.base) <= 0);
1602 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1603 &cursor->node->ondisk->elms[cursor->node->ondisk->count].internal.base) >= 0);
1606 hammer_btree_unlock_children(&locklist);
1607 hammer_cursor_downgrade(cursor);
1612 * Same as the above, but splits a full leaf node.
1618 btree_split_leaf(hammer_cursor_t cursor)
1620 hammer_node_ondisk_t ondisk;
1621 hammer_node_t parent;
1624 hammer_node_t new_leaf;
1625 hammer_btree_elm_t elm;
1626 hammer_btree_elm_t parent_elm;
1627 hammer_base_elm_t mid_boundary;
1632 const size_t esize = sizeof(*elm);
1634 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1636 ++hammer_stats_btree_splits;
1638 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1639 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
1640 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1641 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
1644 * Calculate the split point. If the insertion point will be on
1645 * the left-hand side adjust the split point to give the right
1646 * hand side one additional node.
1648 * Spikes are made up of two leaf elements which cannot be
1651 leaf = cursor->node;
1652 ondisk = leaf->ondisk;
1653 split = (ondisk->count + 1) / 2;
1654 if (cursor->index <= split)
1659 elm = &ondisk->elms[split];
1661 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm[-1].leaf.base) <= 0);
1662 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->leaf.base) <= 0);
1663 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->leaf.base) > 0);
1664 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm[1].leaf.base) > 0);
1667 * If we are at the root of the tree, create a new root node with
1668 * 1 element and split normally. Avoid making major modifications
1669 * until we know the whole operation will work.
1671 if (ondisk->parent == 0) {
1672 parent = hammer_alloc_btree(cursor->trans, &error);
1675 hammer_lock_ex(&parent->lock);
1676 hammer_modify_node_noundo(cursor->trans, parent);
1677 ondisk = parent->ondisk;
1680 ondisk->mirror_tid = leaf->ondisk->mirror_tid;
1681 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1682 ondisk->elms[0].base = hmp->root_btree_beg;
1683 ondisk->elms[0].base.btype = leaf->ondisk->type;
1684 ondisk->elms[0].internal.subtree_offset = leaf->node_offset;
1685 ondisk->elms[1].base = hmp->root_btree_end;
1686 /* ondisk->elms[1].base.btype = not used */
1687 hammer_modify_node_done(parent);
1689 parent_index = 0; /* insertion point in parent */
1692 parent = cursor->parent;
1693 parent_index = cursor->parent_index;
1697 * Split leaf into new_leaf at the split point. Select a separator
1698 * value in-between the two leafs but with a bent towards the right
1699 * leaf since comparisons use an 'elm >= separator' inequality.
1708 new_leaf = hammer_alloc_btree(cursor->trans, &error);
1709 if (new_leaf == NULL) {
1711 hammer_unlock(&parent->lock);
1712 hammer_delete_node(cursor->trans, parent);
1713 hammer_rel_node(parent);
1717 hammer_lock_ex(&new_leaf->lock);
1720 * Create the new node and copy the leaf elements from the split
1721 * point on to the new node.
1723 hammer_modify_node_all(cursor->trans, leaf);
1724 hammer_modify_node_noundo(cursor->trans, new_leaf);
1725 ondisk = leaf->ondisk;
1726 elm = &ondisk->elms[split];
1727 bcopy(elm, &new_leaf->ondisk->elms[0], (ondisk->count - split) * esize);
1728 new_leaf->ondisk->count = ondisk->count - split;
1729 new_leaf->ondisk->parent = parent->node_offset;
1730 new_leaf->ondisk->type = HAMMER_BTREE_TYPE_LEAF;
1731 new_leaf->ondisk->mirror_tid = ondisk->mirror_tid;
1732 KKASSERT(ondisk->type == new_leaf->ondisk->type);
1733 hammer_modify_node_done(new_leaf);
1734 hammer_cursor_split_node(leaf, new_leaf, split);
1737 * Cleanup the original node. Because this is a leaf node and
1738 * leaf nodes do not have a right-hand boundary, there
1739 * aren't any special edge cases to clean up. We just fixup the
1742 ondisk->count = split;
1745 * Insert the separator into the parent, fixup the parent's
1746 * reference to the original node, and reference the new node.
1747 * The separator is P.
1749 * Remember that base.count does not include the right-hand boundary.
1750 * We are copying parent_index+1 to parent_index+2, not +0 to +1.
1752 hammer_modify_node_all(cursor->trans, parent);
1753 ondisk = parent->ondisk;
1754 KKASSERT(split != 0);
1755 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
1756 parent_elm = &ondisk->elms[parent_index+1];
1757 bcopy(parent_elm, parent_elm + 1,
1758 (ondisk->count - parent_index) * esize);
1760 hammer_make_separator(&elm[-1].base, &elm[0].base, &parent_elm->base);
1761 parent_elm->internal.base.btype = new_leaf->ondisk->type;
1762 parent_elm->internal.subtree_offset = new_leaf->node_offset;
1763 parent_elm->internal.mirror_tid = new_leaf->ondisk->mirror_tid;
1764 mid_boundary = &parent_elm->base;
1766 hammer_modify_node_done(parent);
1767 hammer_cursor_inserted_element(parent, parent_index + 1);
1770 * The filesystem's root B-Tree pointer may have to be updated.
1773 hammer_volume_t volume;
1775 volume = hammer_get_root_volume(hmp, &error);
1776 KKASSERT(error == 0);
1778 hammer_modify_volume_field(cursor->trans, volume,
1780 volume->ondisk->vol0_btree_root = parent->node_offset;
1781 hammer_modify_volume_done(volume);
1782 leaf->ondisk->parent = parent->node_offset;
1783 if (cursor->parent) {
1784 hammer_unlock(&cursor->parent->lock);
1785 hammer_rel_node(cursor->parent);
1787 cursor->parent = parent; /* lock'd and ref'd */
1788 hammer_rel_volume(volume, 0);
1790 hammer_modify_node_done(leaf);
1793 * Ok, now adjust the cursor depending on which element the original
1794 * index was pointing at. If we are >= the split point the push node
1795 * is now in the new node.
1797 * NOTE: If we are at the split point itself we need to select the
1798 * old or new node based on where key_beg's insertion point will be.
1799 * If we pick the wrong side the inserted element will wind up in
1800 * the wrong leaf node and outside that node's bounds.
1802 if (cursor->index > split ||
1803 (cursor->index == split &&
1804 hammer_btree_cmp(&cursor->key_beg, mid_boundary) >= 0)) {
1805 cursor->parent_index = parent_index + 1;
1806 cursor->index -= split;
1807 hammer_unlock(&cursor->node->lock);
1808 hammer_rel_node(cursor->node);
1809 cursor->node = new_leaf;
1811 cursor->parent_index = parent_index;
1812 hammer_unlock(&new_leaf->lock);
1813 hammer_rel_node(new_leaf);
1817 * Fixup left and right bounds
1819 parent_elm = &parent->ondisk->elms[cursor->parent_index];
1820 cursor->left_bound = &parent_elm[0].internal.base;
1821 cursor->right_bound = &parent_elm[1].internal.base;
1824 * Assert that the bounds are correct.
1826 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1827 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
1828 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1829 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
1830 KKASSERT(hammer_btree_cmp(cursor->left_bound, &cursor->key_beg) <= 0);
1831 KKASSERT(hammer_btree_cmp(cursor->right_bound, &cursor->key_beg) > 0);
1834 hammer_cursor_downgrade(cursor);
1841 * Recursively correct the right-hand boundary's create_tid to (tid) as
1842 * long as the rest of the key matches. We have to recurse upward in
1843 * the tree as well as down the left side of each parent's right node.
1845 * Return EDEADLK if we were only partially successful, forcing the caller
1846 * to try again. The original cursor is not modified. This routine can
1847 * also fail with EDEADLK if it is forced to throw away a portion of its
1850 * The caller must pass a downgraded cursor to us (otherwise we can't dup it).
1853 TAILQ_ENTRY(hammer_rhb) entry;
1858 TAILQ_HEAD(hammer_rhb_list, hammer_rhb);
1861 hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid)
1863 struct hammer_rhb_list rhb_list;
1864 hammer_base_elm_t elm;
1865 hammer_node_t orig_node;
1866 struct hammer_rhb *rhb;
1870 TAILQ_INIT(&rhb_list);
1873 * Save our position so we can restore it on return. This also
1874 * gives us a stable 'elm'.
1876 orig_node = cursor->node;
1877 hammer_ref_node(orig_node);
1878 hammer_lock_sh(&orig_node->lock);
1879 orig_index = cursor->index;
1880 elm = &orig_node->ondisk->elms[orig_index].base;
1883 * Now build a list of parents going up, allocating a rhb
1884 * structure for each one.
1886 while (cursor->parent) {
1888 * Stop if we no longer have any right-bounds to fix up
1890 if (elm->obj_id != cursor->right_bound->obj_id ||
1891 elm->rec_type != cursor->right_bound->rec_type ||
1892 elm->key != cursor->right_bound->key) {
1897 * Stop if the right-hand bound's create_tid does not
1898 * need to be corrected.
1900 if (cursor->right_bound->create_tid >= tid)
1903 rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO);
1904 rhb->node = cursor->parent;
1905 rhb->index = cursor->parent_index;
1906 hammer_ref_node(rhb->node);
1907 hammer_lock_sh(&rhb->node->lock);
1908 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
1910 hammer_cursor_up(cursor);
1914 * now safely adjust the right hand bound for each rhb. This may
1915 * also require taking the right side of the tree and iterating down
1919 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1920 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
1923 TAILQ_REMOVE(&rhb_list, rhb, entry);
1924 hammer_unlock(&rhb->node->lock);
1925 hammer_rel_node(rhb->node);
1926 kfree(rhb, M_HAMMER);
1928 switch (cursor->node->ondisk->type) {
1929 case HAMMER_BTREE_TYPE_INTERNAL:
1931 * Right-boundary for parent at internal node
1932 * is one element to the right of the element whos
1933 * right boundary needs adjusting. We must then
1934 * traverse down the left side correcting any left
1935 * bounds (which may now be too far to the left).
1938 error = hammer_btree_correct_lhb(cursor, tid);
1941 panic("hammer_btree_correct_rhb(): Bad node type");
1950 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1951 TAILQ_REMOVE(&rhb_list, rhb, entry);
1952 hammer_unlock(&rhb->node->lock);
1953 hammer_rel_node(rhb->node);
1954 kfree(rhb, M_HAMMER);
1956 error = hammer_cursor_seek(cursor, orig_node, orig_index);
1957 hammer_unlock(&orig_node->lock);
1958 hammer_rel_node(orig_node);
1963 * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand
1964 * bound going downward starting at the current cursor position.
1966 * This function does not restore the cursor after use.
1969 hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid)
1971 struct hammer_rhb_list rhb_list;
1972 hammer_base_elm_t elm;
1973 hammer_base_elm_t cmp;
1974 struct hammer_rhb *rhb;
1977 TAILQ_INIT(&rhb_list);
1979 cmp = &cursor->node->ondisk->elms[cursor->index].base;
1982 * Record the node and traverse down the left-hand side for all
1983 * matching records needing a boundary correction.
1987 rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO);
1988 rhb->node = cursor->node;
1989 rhb->index = cursor->index;
1990 hammer_ref_node(rhb->node);
1991 hammer_lock_sh(&rhb->node->lock);
1992 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
1994 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
1996 * Nothing to traverse down if we are at the right
1997 * boundary of an internal node.
1999 if (cursor->index == cursor->node->ondisk->count)
2002 elm = &cursor->node->ondisk->elms[cursor->index].base;
2003 if (elm->btype == HAMMER_BTREE_TYPE_RECORD)
2005 panic("Illegal leaf record type %02x", elm->btype);
2007 error = hammer_cursor_down(cursor);
2011 elm = &cursor->node->ondisk->elms[cursor->index].base;
2012 if (elm->obj_id != cmp->obj_id ||
2013 elm->rec_type != cmp->rec_type ||
2014 elm->key != cmp->key) {
2017 if (elm->create_tid >= tid)
2023 * Now we can safely adjust the left-hand boundary from the bottom-up.
2024 * The last element we remove from the list is the caller's right hand
2025 * boundary, which must also be adjusted.
2027 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2028 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
2031 TAILQ_REMOVE(&rhb_list, rhb, entry);
2032 hammer_unlock(&rhb->node->lock);
2033 hammer_rel_node(rhb->node);
2034 kfree(rhb, M_HAMMER);
2036 elm = &cursor->node->ondisk->elms[cursor->index].base;
2037 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
2038 hammer_modify_node(cursor->trans, cursor->node,
2040 sizeof(elm->create_tid));
2041 elm->create_tid = tid;
2042 hammer_modify_node_done(cursor->node);
2044 panic("hammer_btree_correct_lhb(): Bad element type");
2051 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2052 TAILQ_REMOVE(&rhb_list, rhb, entry);
2053 hammer_unlock(&rhb->node->lock);
2054 hammer_rel_node(rhb->node);
2055 kfree(rhb, M_HAMMER);
2063 * Attempt to remove the locked, empty or want-to-be-empty B-Tree node at
2064 * (cursor->node). Returns 0 on success, EDEADLK if we could not complete
2065 * the operation due to a deadlock, or some other error.
2067 * This routine is always called with an empty, locked leaf but may recurse
2068 * into want-to-be-empty parents as part of its operation.
2070 * It should also be noted that when removing empty leaves we must be sure
2071 * to test and update mirror_tid because another thread may have deadlocked
2072 * against us (or someone) trying to propagate it up and cannot retry once
2073 * the node has been deleted.
2075 * On return the cursor may end up pointing to an internal node, suitable
2076 * for further iteration but not for an immediate insertion or deletion.
2079 btree_remove(hammer_cursor_t cursor)
2081 hammer_node_ondisk_t ondisk;
2082 hammer_btree_elm_t elm;
2084 hammer_node_t parent;
2085 const int esize = sizeof(*elm);
2088 node = cursor->node;
2091 * When deleting the root of the filesystem convert it to
2092 * an empty leaf node. Internal nodes cannot be empty.
2094 ondisk = node->ondisk;
2095 if (ondisk->parent == 0) {
2096 KKASSERT(cursor->parent == NULL);
2097 hammer_modify_node_all(cursor->trans, node);
2098 KKASSERT(ondisk == node->ondisk);
2099 ondisk->type = HAMMER_BTREE_TYPE_LEAF;
2101 hammer_modify_node_done(node);
2106 parent = cursor->parent;
2107 hammer_cursor_removed_node(node, parent, cursor->parent_index);
2110 * Attempt to remove the parent's reference to the child. If the
2111 * parent would become empty we have to recurse. If we fail we
2112 * leave the parent pointing to an empty leaf node.
2114 if (parent->ondisk->count == 1) {
2116 * This special cursor_up_locked() call leaves the original
2117 * node exclusively locked and referenced, leaves the
2118 * original parent locked (as the new node), and locks the
2119 * new parent. It can return EDEADLK.
2121 error = hammer_cursor_up_locked(cursor);
2123 error = btree_remove(cursor);
2125 hammer_modify_node_all(cursor->trans, node);
2126 ondisk = node->ondisk;
2127 ondisk->type = HAMMER_BTREE_TYPE_DELETED;
2129 hammer_modify_node_done(node);
2130 hammer_flush_node(node);
2131 hammer_delete_node(cursor->trans, node);
2133 kprintf("Warning: BTREE_REMOVE: Defering "
2134 "parent removal1 @ %016llx, skipping\n",
2137 hammer_unlock(&node->lock);
2138 hammer_rel_node(node);
2140 kprintf("Warning: BTREE_REMOVE: Defering parent "
2141 "removal2 @ %016llx, skipping\n",
2145 KKASSERT(parent->ondisk->count > 1);
2147 hammer_modify_node_all(cursor->trans, parent);
2148 ondisk = parent->ondisk;
2149 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
2151 elm = &ondisk->elms[cursor->parent_index];
2152 KKASSERT(elm->internal.subtree_offset == node->node_offset);
2153 KKASSERT(ondisk->count > 0);
2156 * We must retain the highest mirror_tid. The deleted
2157 * range is now encompassed by the element to the left.
2158 * If we are already at the left edge the new left edge
2159 * inherits mirror_tid.
2161 * Note that bounds of the parent to our parent may create
2162 * a gap to the left of our left-most node or to the right
2163 * of our right-most node. The gap is silently included
2164 * in the mirror_tid's area of effect from the point of view
2167 if (cursor->parent_index) {
2168 if (elm[-1].internal.mirror_tid <
2169 elm[0].internal.mirror_tid) {
2170 elm[-1].internal.mirror_tid =
2171 elm[0].internal.mirror_tid;
2174 if (elm[1].internal.mirror_tid <
2175 elm[0].internal.mirror_tid) {
2176 elm[1].internal.mirror_tid =
2177 elm[0].internal.mirror_tid;
2182 * Delete the subtree reference in the parent
2184 bcopy(&elm[1], &elm[0],
2185 (ondisk->count - cursor->parent_index) * esize);
2187 hammer_modify_node_done(parent);
2188 hammer_cursor_deleted_element(parent, cursor->parent_index);
2189 hammer_flush_node(node);
2190 hammer_delete_node(cursor->trans, node);
2193 * cursor->node is invalid, cursor up to make the cursor
2196 error = hammer_cursor_up(cursor);
2202 * Propagate cursor->trans->tid up the B-Tree starting at the current
2203 * cursor position using pseudofs info gleaned from the passed inode.
2205 * The passed inode has no relationship to the cursor position other
2206 * then being in the same pseudofs as the insertion or deletion we
2207 * are propagating the mirror_tid for.
2210 hammer_btree_do_propagation(hammer_cursor_t cursor,
2211 hammer_pseudofs_inmem_t pfsm,
2212 hammer_btree_leaf_elm_t leaf)
2214 hammer_cursor_t ncursor;
2215 hammer_tid_t mirror_tid;
2219 * We only propagate the mirror_tid up if we are in master or slave
2220 * mode. We do not bother if we are in no-mirror mode.
2222 * If pfsm is NULL we propagate (from mirror_write).
2225 pfsm->pfsd.master_id < 0 &&
2226 (pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
2231 * This is a bit of a hack because we cannot deadlock or return
2232 * EDEADLK here. The related operation has already completed and
2233 * we must propagate the mirror_tid now regardless.
2235 * Generate a new cursor which inherits the original's locks and
2236 * unlock the original. Use the new cursor to propagate the
2237 * mirror_tid. Then clean up the new cursor and reacquire locks
2240 * hammer_dup_cursor() cannot dup locks. The dup inherits the
2241 * original's locks and the original is tracked and must be
2244 mirror_tid = cursor->node->ondisk->mirror_tid;
2245 KKASSERT(mirror_tid != 0);
2246 ncursor = hammer_push_cursor(cursor);
2247 error = hammer_btree_mirror_propagate(ncursor, mirror_tid);
2248 KKASSERT(error == 0);
2249 hammer_pop_cursor(cursor, ncursor);
2254 * Propagate a mirror TID update upwards through the B-Tree to the root.
2256 * A locked internal node must be passed in. The node will remain locked
2259 * This function syncs mirror_tid at the specified internal node's element,
2260 * adjusts the node's aggregation mirror_tid, and then recurses upwards.
2263 hammer_btree_mirror_propagate(hammer_cursor_t cursor, hammer_tid_t mirror_tid)
2265 hammer_btree_internal_elm_t elm;
2270 error = hammer_cursor_up(cursor);
2272 error = hammer_cursor_upgrade(cursor);
2273 while (error == EDEADLK) {
2274 hammer_recover_cursor(cursor);
2275 error = hammer_cursor_upgrade(cursor);
2279 node = cursor->node;
2280 KKASSERT (node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
2283 * Adjust the node's element
2285 elm = &node->ondisk->elms[cursor->index].internal;
2286 if (elm->mirror_tid >= mirror_tid)
2288 hammer_modify_node(cursor->trans, node, &elm->mirror_tid,
2289 sizeof(elm->mirror_tid));
2290 elm->mirror_tid = mirror_tid;
2291 hammer_modify_node_done(node);
2294 * Adjust the node's mirror_tid aggregator
2296 if (node->ondisk->mirror_tid >= mirror_tid)
2298 hammer_modify_node_field(cursor->trans, node, mirror_tid);
2299 node->ondisk->mirror_tid = mirror_tid;
2300 hammer_modify_node_done(node);
2302 if (error == ENOENT)
2308 hammer_btree_get_parent(hammer_node_t node, int *parent_indexp, int *errorp,
2311 hammer_node_t parent;
2312 hammer_btree_elm_t elm;
2318 parent = hammer_get_node(node->hmp, node->ondisk->parent, 0, errorp);
2320 KKASSERT(parent == NULL);
2323 KKASSERT ((parent->flags & HAMMER_NODE_DELETED) == 0);
2328 if (try_exclusive) {
2329 if (hammer_lock_ex_try(&parent->lock)) {
2330 hammer_rel_node(parent);
2335 hammer_lock_sh(&parent->lock);
2339 * Figure out which element in the parent is pointing to the
2342 if (node->ondisk->count) {
2343 i = hammer_btree_search_node(&node->ondisk->elms[0].base,
2348 while (i < parent->ondisk->count) {
2349 elm = &parent->ondisk->elms[i];
2350 if (elm->internal.subtree_offset == node->node_offset)
2354 if (i == parent->ondisk->count) {
2355 hammer_unlock(&parent->lock);
2356 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
2359 KKASSERT(*errorp == 0);
2364 * The element (elm) has been moved to a new internal node (node).
2366 * If the element represents a pointer to an internal node that node's
2367 * parent must be adjusted to the element's new location.
2369 * XXX deadlock potential here with our exclusive locks
2372 btree_set_parent(hammer_transaction_t trans, hammer_node_t node,
2373 hammer_btree_elm_t elm)
2375 hammer_node_t child;
2380 switch(elm->base.btype) {
2381 case HAMMER_BTREE_TYPE_INTERNAL:
2382 case HAMMER_BTREE_TYPE_LEAF:
2383 child = hammer_get_node(node->hmp, elm->internal.subtree_offset,
2386 hammer_modify_node_field(trans, child, parent);
2387 child->ondisk->parent = node->node_offset;
2388 hammer_modify_node_done(child);
2389 hammer_rel_node(child);
2399 * Exclusively lock all the children of node. This is used by the split
2400 * code to prevent anyone from accessing the children of a cursor node
2401 * while we fix-up its parent offset.
2403 * If we don't lock the children we can really mess up cursors which block
2404 * trying to cursor-up into our node.
2406 * On failure EDEADLK (or some other error) is returned. If a deadlock
2407 * error is returned the cursor is adjusted to block on termination.
2410 hammer_btree_lock_children(hammer_cursor_t cursor,
2411 struct hammer_node_locklist **locklistp)
2414 hammer_node_locklist_t item;
2415 hammer_node_ondisk_t ondisk;
2416 hammer_btree_elm_t elm;
2417 hammer_node_t child;
2421 node = cursor->node;
2422 ondisk = node->ondisk;
2426 * We really do not want to block on I/O with exclusive locks held,
2427 * pre-get the children before trying to lock the mess.
2429 for (i = 0; i < ondisk->count; ++i) {
2430 ++hammer_stats_btree_elements;
2431 elm = &ondisk->elms[i];
2432 if (elm->base.btype != HAMMER_BTREE_TYPE_LEAF &&
2433 elm->base.btype != HAMMER_BTREE_TYPE_INTERNAL) {
2436 child = hammer_get_node(node->hmp,
2437 elm->internal.subtree_offset,
2440 hammer_rel_node(child);
2446 for (i = 0; error == 0 && i < ondisk->count; ++i) {
2447 ++hammer_stats_btree_elements;
2448 elm = &ondisk->elms[i];
2450 switch(elm->base.btype) {
2451 case HAMMER_BTREE_TYPE_INTERNAL:
2452 case HAMMER_BTREE_TYPE_LEAF:
2453 KKASSERT(elm->internal.subtree_offset != 0);
2454 child = hammer_get_node(node->hmp,
2455 elm->internal.subtree_offset,
2463 if (hammer_lock_ex_try(&child->lock) != 0) {
2464 if (cursor->deadlk_node == NULL) {
2465 cursor->deadlk_node = child;
2466 hammer_ref_node(cursor->deadlk_node);
2469 hammer_rel_node(child);
2471 item = kmalloc(sizeof(*item),
2472 M_HAMMER, M_WAITOK);
2473 item->next = *locklistp;
2480 hammer_btree_unlock_children(locklistp);
2486 * Release previously obtained node locks.
2489 hammer_btree_unlock_children(struct hammer_node_locklist **locklistp)
2491 hammer_node_locklist_t item;
2493 while ((item = *locklistp) != NULL) {
2494 *locklistp = item->next;
2495 hammer_unlock(&item->node->lock);
2496 hammer_rel_node(item->node);
2497 kfree(item, M_HAMMER);
2501 /************************************************************************
2502 * MISCELLANIOUS SUPPORT *
2503 ************************************************************************/
2506 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
2508 * Note that for this particular function a return value of -1, 0, or +1
2509 * can denote a match if create_tid is otherwise discounted. A create_tid
2510 * of zero is considered to be 'infinity' in comparisons.
2512 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
2515 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
2517 if (key1->localization < key2->localization)
2519 if (key1->localization > key2->localization)
2522 if (key1->obj_id < key2->obj_id)
2524 if (key1->obj_id > key2->obj_id)
2527 if (key1->rec_type < key2->rec_type)
2529 if (key1->rec_type > key2->rec_type)
2532 if (key1->key < key2->key)
2534 if (key1->key > key2->key)
2538 * A create_tid of zero indicates a record which is undeletable
2539 * and must be considered to have a value of positive infinity.
2541 if (key1->create_tid == 0) {
2542 if (key2->create_tid == 0)
2546 if (key2->create_tid == 0)
2548 if (key1->create_tid < key2->create_tid)
2550 if (key1->create_tid > key2->create_tid)
2556 * Test a timestamp against an element to determine whether the
2557 * element is visible. A timestamp of 0 means 'infinity'.
2560 hammer_btree_chkts(hammer_tid_t asof, hammer_base_elm_t base)
2563 if (base->delete_tid)
2567 if (asof < base->create_tid)
2569 if (base->delete_tid && asof >= base->delete_tid)
2575 * Create a separator half way inbetween key1 and key2. For fields just
2576 * one unit apart, the separator will match key2. key1 is on the left-hand
2577 * side and key2 is on the right-hand side.
2579 * key2 must be >= the separator. It is ok for the separator to match key2.
2581 * NOTE: Even if key1 does not match key2, the separator may wind up matching
2584 * NOTE: It might be beneficial to just scrap this whole mess and just
2585 * set the separator to key2.
2587 #define MAKE_SEPARATOR(key1, key2, dest, field) \
2588 dest->field = key1->field + ((key2->field - key1->field + 1) >> 1);
2591 hammer_make_separator(hammer_base_elm_t key1, hammer_base_elm_t key2,
2592 hammer_base_elm_t dest)
2594 bzero(dest, sizeof(*dest));
2596 dest->rec_type = key2->rec_type;
2597 dest->key = key2->key;
2598 dest->obj_id = key2->obj_id;
2599 dest->create_tid = key2->create_tid;
2601 MAKE_SEPARATOR(key1, key2, dest, localization);
2602 if (key1->localization == key2->localization) {
2603 MAKE_SEPARATOR(key1, key2, dest, obj_id);
2604 if (key1->obj_id == key2->obj_id) {
2605 MAKE_SEPARATOR(key1, key2, dest, rec_type);
2606 if (key1->rec_type == key2->rec_type) {
2607 MAKE_SEPARATOR(key1, key2, dest, key);
2609 * Don't bother creating a separator for
2610 * create_tid, which also conveniently avoids
2611 * having to handle the create_tid == 0
2612 * (infinity) case. Just leave create_tid
2615 * Worst case, dest matches key2 exactly,
2616 * which is acceptable.
2623 #undef MAKE_SEPARATOR
2626 * Return whether a generic internal or leaf node is full
2629 btree_node_is_full(hammer_node_ondisk_t node)
2631 switch(node->type) {
2632 case HAMMER_BTREE_TYPE_INTERNAL:
2633 if (node->count == HAMMER_BTREE_INT_ELMS)
2636 case HAMMER_BTREE_TYPE_LEAF:
2637 if (node->count == HAMMER_BTREE_LEAF_ELMS)
2641 panic("illegal btree subtype");
2648 btree_max_elements(u_int8_t type)
2650 if (type == HAMMER_BTREE_TYPE_LEAF)
2651 return(HAMMER_BTREE_LEAF_ELMS);
2652 if (type == HAMMER_BTREE_TYPE_INTERNAL)
2653 return(HAMMER_BTREE_INT_ELMS);
2654 panic("btree_max_elements: bad type %d\n", type);
2659 hammer_print_btree_node(hammer_node_ondisk_t ondisk)
2661 hammer_btree_elm_t elm;
2664 kprintf("node %p count=%d parent=%016llx type=%c\n",
2665 ondisk, ondisk->count, ondisk->parent, ondisk->type);
2668 * Dump both boundary elements if an internal node
2670 if (ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
2671 for (i = 0; i <= ondisk->count; ++i) {
2672 elm = &ondisk->elms[i];
2673 hammer_print_btree_elm(elm, ondisk->type, i);
2676 for (i = 0; i < ondisk->count; ++i) {
2677 elm = &ondisk->elms[i];
2678 hammer_print_btree_elm(elm, ondisk->type, i);
2684 hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i)
2687 kprintf("\tobj_id = %016llx\n", elm->base.obj_id);
2688 kprintf("\tkey = %016llx\n", elm->base.key);
2689 kprintf("\tcreate_tid = %016llx\n", elm->base.create_tid);
2690 kprintf("\tdelete_tid = %016llx\n", elm->base.delete_tid);
2691 kprintf("\trec_type = %04x\n", elm->base.rec_type);
2692 kprintf("\tobj_type = %02x\n", elm->base.obj_type);
2693 kprintf("\tbtype = %02x (%c)\n",
2695 (elm->base.btype ? elm->base.btype : '?'));
2696 kprintf("\tlocalization = %02x\n", elm->base.localization);
2699 case HAMMER_BTREE_TYPE_INTERNAL:
2700 kprintf("\tsubtree_off = %016llx\n",
2701 elm->internal.subtree_offset);
2703 case HAMMER_BTREE_TYPE_RECORD:
2704 kprintf("\tdata_offset = %016llx\n", elm->leaf.data_offset);
2705 kprintf("\tdata_len = %08x\n", elm->leaf.data_len);
2706 kprintf("\tdata_crc = %08x\n", elm->leaf.data_crc);