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.59 2008/06/30 02:45:30 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 void hammer_make_separator(hammer_base_elm_t key1
,
91 hammer_base_elm_t key2
, hammer_base_elm_t dest
);
94 * Iterate records after a search. The cursor is iterated forwards past
95 * the current record until a record matching the key-range requirements
96 * is found. ENOENT is returned if the iteration goes past the ending
99 * The iteration is inclusive of key_beg and can be inclusive or exclusive
100 * of key_end depending on whether HAMMER_CURSOR_END_INCLUSIVE is set.
102 * When doing an as-of search (cursor->asof != 0), key_beg.create_tid
103 * may be modified by B-Tree functions.
105 * cursor->key_beg may or may not be modified by this function during
106 * the iteration. XXX future - in case of an inverted lock we may have
107 * to reinitiate the lookup and set key_beg to properly pick up where we
110 * NOTE! EDEADLK *CANNOT* be returned by this procedure.
113 hammer_btree_iterate(hammer_cursor_t cursor
)
115 hammer_node_ondisk_t node
;
116 hammer_btree_elm_t elm
;
122 * Skip past the current record
124 node
= cursor
->node
->ondisk
;
127 if (cursor
->index
< node
->count
&&
128 (cursor
->flags
& HAMMER_CURSOR_ATEDISK
)) {
133 * Loop until an element is found or we are done.
137 * We iterate up the tree and then index over one element
138 * while we are at the last element in the current node.
140 * If we are at the root of the filesystem, cursor_up
143 * XXX this could be optimized by storing the information in
144 * the parent reference.
146 * XXX we can lose the node lock temporarily, this could mess
149 ++hammer_stats_btree_iterations
;
150 if (cursor
->index
== node
->count
) {
151 if (hammer_debug_btree
) {
152 kprintf("BRACKETU %016llx[%d] -> %016llx[%d] (td=%p)\n",
153 cursor
->node
->node_offset
,
155 (cursor
->parent
? cursor
->parent
->node_offset
: -1),
156 cursor
->parent_index
,
159 KKASSERT(cursor
->parent
== NULL
|| cursor
->parent
->ondisk
->elms
[cursor
->parent_index
].internal
.subtree_offset
== cursor
->node
->node_offset
);
160 error
= hammer_cursor_up(cursor
);
163 /* reload stale pointer */
164 node
= cursor
->node
->ondisk
;
165 KKASSERT(cursor
->index
!= node
->count
);
168 * If we are reblocking we want to return internal
171 if (cursor
->flags
& HAMMER_CURSOR_REBLOCKING
) {
172 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
180 * Check internal or leaf element. Determine if the record
181 * at the cursor has gone beyond the end of our range.
183 * We recurse down through internal nodes.
185 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
186 elm
= &node
->elms
[cursor
->index
];
188 r
= hammer_btree_cmp(&cursor
->key_end
, &elm
[0].base
);
189 s
= hammer_btree_cmp(&cursor
->key_beg
, &elm
[1].base
);
190 if (hammer_debug_btree
) {
191 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d (td=%p)\n",
192 cursor
->node
->node_offset
,
194 elm
[0].internal
.base
.obj_id
,
195 elm
[0].internal
.base
.rec_type
,
196 elm
[0].internal
.base
.key
,
197 elm
[0].internal
.base
.localization
,
201 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
202 cursor
->node
->node_offset
,
204 elm
[1].internal
.base
.obj_id
,
205 elm
[1].internal
.base
.rec_type
,
206 elm
[1].internal
.base
.key
,
207 elm
[1].internal
.base
.localization
,
216 if (r
== 0 && (cursor
->flags
&
217 HAMMER_CURSOR_END_INCLUSIVE
) == 0) {
226 KKASSERT(elm
->internal
.subtree_offset
!= 0);
229 * If running the mirror filter see if we can skip
230 * the entire sub-tree.
232 if (cursor
->flags
& HAMMER_CURSOR_MIRROR_FILTERED
) {
233 if (elm
->internal
.mirror_tid
<
234 cursor
->mirror_tid
) {
240 error
= hammer_cursor_down(cursor
);
243 KKASSERT(cursor
->index
== 0);
244 /* reload stale pointer */
245 node
= cursor
->node
->ondisk
;
248 elm
= &node
->elms
[cursor
->index
];
249 r
= hammer_btree_cmp(&cursor
->key_end
, &elm
->base
);
250 if (hammer_debug_btree
) {
251 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
252 cursor
->node
->node_offset
,
254 (elm
[0].leaf
.base
.btype
?
255 elm
[0].leaf
.base
.btype
: '?'),
256 elm
[0].leaf
.base
.obj_id
,
257 elm
[0].leaf
.base
.rec_type
,
258 elm
[0].leaf
.base
.key
,
259 elm
[0].leaf
.base
.localization
,
269 * We support both end-inclusive and
270 * end-exclusive searches.
273 (cursor
->flags
& HAMMER_CURSOR_END_INCLUSIVE
) == 0) {
278 switch(elm
->leaf
.base
.btype
) {
279 case HAMMER_BTREE_TYPE_RECORD
:
280 if ((cursor
->flags
& HAMMER_CURSOR_ASOF
) &&
281 hammer_btree_chkts(cursor
->asof
, &elm
->base
)) {
294 * node pointer invalid after loop
300 if (hammer_debug_btree
) {
301 int i
= cursor
->index
;
302 hammer_btree_elm_t elm
= &cursor
->node
->ondisk
->elms
[i
];
303 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
305 elm
->internal
.base
.obj_id
,
306 elm
->internal
.base
.rec_type
,
307 elm
->internal
.base
.key
,
308 elm
->internal
.base
.localization
311 hammer_flusher_clean_loose_ios(cursor
->trans
->hmp
);
318 * Iterate in the reverse direction. This is used by the pruning code to
319 * avoid overlapping records.
322 hammer_btree_iterate_reverse(hammer_cursor_t cursor
)
324 hammer_node_ondisk_t node
;
325 hammer_btree_elm_t elm
;
331 * Skip past the current record. For various reasons the cursor
332 * may end up set to -1 or set to point at the end of the current
333 * node. These cases must be addressed.
335 node
= cursor
->node
->ondisk
;
338 if (cursor
->index
!= -1 &&
339 (cursor
->flags
& HAMMER_CURSOR_ATEDISK
)) {
342 if (cursor
->index
== cursor
->node
->ondisk
->count
)
346 * Loop until an element is found or we are done.
350 * We iterate up the tree and then index over one element
351 * while we are at the last element in the current node.
353 if (cursor
->index
== -1) {
354 error
= hammer_cursor_up(cursor
);
356 cursor
->index
= 0; /* sanity */
359 /* reload stale pointer */
360 node
= cursor
->node
->ondisk
;
361 KKASSERT(cursor
->index
!= node
->count
);
367 * Check internal or leaf element. Determine if the record
368 * at the cursor has gone beyond the end of our range.
370 * We recurse down through internal nodes.
372 KKASSERT(cursor
->index
!= node
->count
);
373 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
374 elm
= &node
->elms
[cursor
->index
];
375 r
= hammer_btree_cmp(&cursor
->key_end
, &elm
[0].base
);
376 s
= hammer_btree_cmp(&cursor
->key_beg
, &elm
[1].base
);
377 if (hammer_debug_btree
) {
378 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
379 cursor
->node
->node_offset
,
381 elm
[0].internal
.base
.obj_id
,
382 elm
[0].internal
.base
.rec_type
,
383 elm
[0].internal
.base
.key
,
384 elm
[0].internal
.base
.localization
,
387 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
388 cursor
->node
->node_offset
,
390 elm
[1].internal
.base
.obj_id
,
391 elm
[1].internal
.base
.rec_type
,
392 elm
[1].internal
.base
.key
,
393 elm
[1].internal
.base
.localization
,
407 KKASSERT(elm
->internal
.subtree_offset
!= 0);
409 error
= hammer_cursor_down(cursor
);
412 KKASSERT(cursor
->index
== 0);
413 /* reload stale pointer */
414 node
= cursor
->node
->ondisk
;
416 /* this can assign -1 if the leaf was empty */
417 cursor
->index
= node
->count
- 1;
420 elm
= &node
->elms
[cursor
->index
];
421 s
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->base
);
422 if (hammer_debug_btree
) {
423 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
424 cursor
->node
->node_offset
,
426 (elm
[0].leaf
.base
.btype
?
427 elm
[0].leaf
.base
.btype
: '?'),
428 elm
[0].leaf
.base
.obj_id
,
429 elm
[0].leaf
.base
.rec_type
,
430 elm
[0].leaf
.base
.key
,
431 elm
[0].leaf
.base
.localization
,
440 switch(elm
->leaf
.base
.btype
) {
441 case HAMMER_BTREE_TYPE_RECORD
:
442 if ((cursor
->flags
& HAMMER_CURSOR_ASOF
) &&
443 hammer_btree_chkts(cursor
->asof
, &elm
->base
)) {
456 * node pointer invalid after loop
462 if (hammer_debug_btree
) {
463 int i
= cursor
->index
;
464 hammer_btree_elm_t elm
= &cursor
->node
->ondisk
->elms
[i
];
465 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
467 elm
->internal
.base
.obj_id
,
468 elm
->internal
.base
.rec_type
,
469 elm
->internal
.base
.key
,
470 elm
->internal
.base
.localization
473 hammer_flusher_clean_loose_ios(cursor
->trans
->hmp
);
480 * Lookup cursor->key_beg. 0 is returned on success, ENOENT if the entry
481 * could not be found, EDEADLK if inserting and a retry is needed, and a
482 * fatal error otherwise. When retrying, the caller must terminate the
483 * cursor and reinitialize it. EDEADLK cannot be returned if not inserting.
485 * The cursor is suitably positioned for a deletion on success, and suitably
486 * positioned for an insertion on ENOENT if HAMMER_CURSOR_INSERT was
489 * The cursor may begin anywhere, the search will traverse the tree in
490 * either direction to locate the requested element.
492 * Most of the logic implementing historical searches is handled here. We
493 * do an initial lookup with create_tid set to the asof TID. Due to the
494 * way records are laid out, a backwards iteration may be required if
495 * ENOENT is returned to locate the historical record. Here's the
498 * create_tid: 10 15 20
502 * Lets say we want to do a lookup AS-OF timestamp 17. We will traverse
503 * LEAF2 but the only record in LEAF2 has a create_tid of 18, which is
504 * not visible and thus causes ENOENT to be returned. We really need
505 * to check record 11 in LEAF1. If it also fails then the search fails
506 * (e.g. it might represent the range 11-16 and thus still not match our
507 * AS-OF timestamp of 17). Note that LEAF1 could be empty, requiring
508 * further iterations.
510 * If this case occurs btree_search() will set HAMMER_CURSOR_CREATE_CHECK
511 * and the cursor->create_check TID if an iteration might be needed.
512 * In the above example create_check would be set to 14.
515 hammer_btree_lookup(hammer_cursor_t cursor
)
519 ++hammer_stats_btree_lookups
;
520 if (cursor
->flags
& HAMMER_CURSOR_ASOF
) {
521 KKASSERT((cursor
->flags
& HAMMER_CURSOR_INSERT
) == 0);
522 cursor
->key_beg
.create_tid
= cursor
->asof
;
524 cursor
->flags
&= ~HAMMER_CURSOR_CREATE_CHECK
;
525 error
= btree_search(cursor
, 0);
526 if (error
!= ENOENT
||
527 (cursor
->flags
& HAMMER_CURSOR_CREATE_CHECK
) == 0) {
530 * Stop if error other then ENOENT.
531 * Stop if ENOENT and not special case.
535 if (hammer_debug_btree
) {
536 kprintf("CREATE_CHECK %016llx\n",
537 cursor
->create_check
);
539 cursor
->key_beg
.create_tid
= cursor
->create_check
;
543 error
= btree_search(cursor
, 0);
546 error
= hammer_btree_extract(cursor
, cursor
->flags
);
551 * Execute the logic required to start an iteration. The first record
552 * located within the specified range is returned and iteration control
553 * flags are adjusted for successive hammer_btree_iterate() calls.
556 hammer_btree_first(hammer_cursor_t cursor
)
560 error
= hammer_btree_lookup(cursor
);
561 if (error
== ENOENT
) {
562 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
563 error
= hammer_btree_iterate(cursor
);
565 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
570 * Similarly but for an iteration in the reverse direction.
572 * Set ATEDISK when iterating backwards to skip the current entry,
573 * which after an ENOENT lookup will be pointing beyond our end point.
576 hammer_btree_last(hammer_cursor_t cursor
)
578 struct hammer_base_elm save
;
581 save
= cursor
->key_beg
;
582 cursor
->key_beg
= cursor
->key_end
;
583 error
= hammer_btree_lookup(cursor
);
584 cursor
->key_beg
= save
;
585 if (error
== ENOENT
||
586 (cursor
->flags
& HAMMER_CURSOR_END_INCLUSIVE
) == 0) {
587 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
588 error
= hammer_btree_iterate_reverse(cursor
);
590 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
595 * Extract the record and/or data associated with the cursor's current
596 * position. Any prior record or data stored in the cursor is replaced.
597 * The cursor must be positioned at a leaf node.
599 * NOTE: All extractions occur at the leaf of the B-Tree.
602 hammer_btree_extract(hammer_cursor_t cursor
, int flags
)
605 hammer_node_ondisk_t node
;
606 hammer_btree_elm_t elm
;
607 hammer_off_t data_off
;
612 * The case where the data reference resolves to the same buffer
613 * as the record reference must be handled.
615 node
= cursor
->node
->ondisk
;
616 elm
= &node
->elms
[cursor
->index
];
618 hmp
= cursor
->node
->hmp
;
621 * There is nothing to extract for an internal element.
623 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
)
627 * Only record types have data.
629 KKASSERT(node
->type
== HAMMER_BTREE_TYPE_LEAF
);
630 cursor
->leaf
= &elm
->leaf
;
632 if ((flags
& HAMMER_CURSOR_GET_DATA
) == 0)
634 if (elm
->leaf
.base
.btype
!= HAMMER_BTREE_TYPE_RECORD
)
636 data_off
= elm
->leaf
.data_offset
;
637 data_len
= elm
->leaf
.data_len
;
644 KKASSERT(data_len
>= 0 && data_len
<= HAMMER_XBUFSIZE
);
645 cursor
->data
= hammer_bread_ext(hmp
, data_off
, data_len
,
646 &error
, &cursor
->data_buffer
);
647 if (hammer_crc_test_leaf(cursor
->data
, &elm
->leaf
) == 0)
648 Debugger("CRC FAILED: DATA");
654 * Insert a leaf element into the B-Tree at the current cursor position.
655 * The cursor is positioned such that the element at and beyond the cursor
656 * are shifted to make room for the new record.
658 * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT
659 * flag set and that call must return ENOENT before this function can be
662 * The caller may depend on the cursor's exclusive lock after return to
663 * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE).
665 * ENOSPC is returned if there is no room to insert a new record.
668 hammer_btree_insert(hammer_cursor_t cursor
, hammer_btree_leaf_elm_t elm
)
670 hammer_node_ondisk_t node
;
674 if ((error
= hammer_cursor_upgrade_node(cursor
)) != 0)
676 ++hammer_stats_btree_inserts
;
679 * Insert the element at the leaf node and update the count in the
680 * parent. It is possible for parent to be NULL, indicating that
681 * the filesystem's ROOT B-Tree node is a leaf itself, which is
682 * possible. The root inode can never be deleted so the leaf should
685 * Remember that the right-hand boundary is not included in the
688 hammer_modify_node_all(cursor
->trans
, cursor
->node
);
689 node
= cursor
->node
->ondisk
;
691 KKASSERT(elm
->base
.btype
!= 0);
692 KKASSERT(node
->type
== HAMMER_BTREE_TYPE_LEAF
);
693 KKASSERT(node
->count
< HAMMER_BTREE_LEAF_ELMS
);
694 if (i
!= node
->count
) {
695 bcopy(&node
->elms
[i
], &node
->elms
[i
+1],
696 (node
->count
- i
) * sizeof(*elm
));
698 node
->elms
[i
].leaf
= *elm
;
702 * Update the leaf node's aggregate mirror_tid for mirroring
705 if (node
->mirror_tid
< elm
->base
.delete_tid
)
706 node
->mirror_tid
= elm
->base
.delete_tid
;
707 if (node
->mirror_tid
< elm
->base
.create_tid
)
708 node
->mirror_tid
= elm
->base
.create_tid
;
709 hammer_modify_node_done(cursor
->node
);
712 * What we really want to do is propogate mirror_tid all the way
713 * up the parent chain to the B-Tree root. That would be
714 * ultra-expensive, though.
716 if (cursor
->parent
&&
717 (cursor
->trans
->hmp
->hflags
& (HMNT_MASTERID
|HMNT_SLAVE
))) {
718 hammer_btree_mirror_propagate(cursor
->trans
, cursor
->parent
,
719 cursor
->parent_index
,
724 * Debugging sanity checks.
726 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
->base
) <= 0);
727 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
->base
) > 0);
729 KKASSERT(hammer_btree_cmp(&node
->elms
[i
-1].leaf
.base
, &elm
->base
) < 0);
731 if (i
!= node
->count
- 1)
732 KKASSERT(hammer_btree_cmp(&node
->elms
[i
+1].leaf
.base
, &elm
->base
) > 0);
738 * Delete a record from the B-Tree at the current cursor position.
739 * The cursor is positioned such that the current element is the one
742 * On return the cursor will be positioned after the deleted element and
743 * MAY point to an internal node. It will be suitable for the continuation
744 * of an iteration but not for an insertion or deletion.
746 * Deletions will attempt to partially rebalance the B-Tree in an upward
747 * direction, but will terminate rather then deadlock. Empty internal nodes
748 * are never allowed by a deletion which deadlocks may end up giving us an
749 * empty leaf. The pruner will clean up and rebalance the tree.
751 * This function can return EDEADLK, requiring the caller to retry the
752 * operation after clearing the deadlock.
755 hammer_btree_delete(hammer_cursor_t cursor
)
757 hammer_node_ondisk_t ondisk
;
759 hammer_node_t parent
;
763 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
765 ++hammer_stats_btree_deletes
;
768 * Delete the element from the leaf node.
770 * Remember that leaf nodes do not have boundaries.
773 ondisk
= node
->ondisk
;
776 KKASSERT(ondisk
->type
== HAMMER_BTREE_TYPE_LEAF
);
777 KKASSERT(i
>= 0 && i
< ondisk
->count
);
778 hammer_modify_node_all(cursor
->trans
, node
);
779 if (i
+ 1 != ondisk
->count
) {
780 bcopy(&ondisk
->elms
[i
+1], &ondisk
->elms
[i
],
781 (ondisk
->count
- i
- 1) * sizeof(ondisk
->elms
[0]));
784 hammer_modify_node_done(node
);
787 * Validate local parent
789 if (ondisk
->parent
) {
790 parent
= cursor
->parent
;
792 KKASSERT(parent
!= NULL
);
793 KKASSERT(parent
->node_offset
== ondisk
->parent
);
797 * If the leaf becomes empty it must be detached from the parent,
798 * potentially recursing through to the filesystem root.
800 * This may reposition the cursor at one of the parent's of the
803 * Ignore deadlock errors, that simply means that btree_remove
804 * was unable to recurse and had to leave us with an empty leaf.
806 KKASSERT(cursor
->index
<= ondisk
->count
);
807 if (ondisk
->count
== 0) {
808 error
= btree_remove(cursor
);
809 if (error
== EDEADLK
)
814 KKASSERT(cursor
->parent
== NULL
||
815 cursor
->parent_index
< cursor
->parent
->ondisk
->count
);
820 * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE
822 * Search the filesystem B-Tree for cursor->key_beg, return the matching node.
824 * The search can begin ANYWHERE in the B-Tree. As a first step the search
825 * iterates up the tree as necessary to properly position itself prior to
826 * actually doing the sarch.
828 * INSERTIONS: The search will split full nodes and leaves on its way down
829 * and guarentee that the leaf it ends up on is not full. If we run out
830 * of space the search continues to the leaf (to position the cursor for
831 * the spike), but ENOSPC is returned.
833 * The search is only guarenteed to end up on a leaf if an error code of 0
834 * is returned, or if inserting and an error code of ENOENT is returned.
835 * Otherwise it can stop at an internal node. On success a search returns
838 * COMPLEXITY WARNING! This is the core B-Tree search code for the entire
839 * filesystem, and it is not simple code. Please note the following facts:
841 * - Internal node recursions have a boundary on the left AND right. The
842 * right boundary is non-inclusive. The create_tid is a generic part
843 * of the key for internal nodes.
845 * - Leaf nodes contain terminal elements only now.
847 * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a
848 * historical search. ASOF and INSERT are mutually exclusive. When
849 * doing an as-of lookup btree_search() checks for a right-edge boundary
850 * case. If while recursing down the left-edge differs from the key
851 * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along
852 * with cursor->create_check. This is used by btree_lookup() to iterate.
853 * The iteration backwards because as-of searches can wind up going
854 * down the wrong branch of the B-Tree.
858 btree_search(hammer_cursor_t cursor
, int flags
)
860 hammer_node_ondisk_t node
;
861 hammer_btree_elm_t elm
;
868 flags
|= cursor
->flags
;
869 ++hammer_stats_btree_searches
;
871 if (hammer_debug_btree
) {
872 kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx lo=%02x (td = %p)\n",
873 cursor
->node
->node_offset
,
875 cursor
->key_beg
.obj_id
,
876 cursor
->key_beg
.rec_type
,
878 cursor
->key_beg
.create_tid
,
879 cursor
->key_beg
.localization
,
883 kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n",
884 cursor
->parent
->node_offset
, cursor
->parent_index
,
885 cursor
->left_bound
->obj_id
,
886 cursor
->parent
->ondisk
->elms
[cursor
->parent_index
].internal
.base
.obj_id
,
887 cursor
->right_bound
->obj_id
,
888 cursor
->parent
->ondisk
->elms
[cursor
->parent_index
+1].internal
.base
.obj_id
,
890 &cursor
->parent
->ondisk
->elms
[cursor
->parent_index
],
892 &cursor
->parent
->ondisk
->elms
[cursor
->parent_index
+1]
897 * Move our cursor up the tree until we find a node whos range covers
898 * the key we are trying to locate.
900 * The left bound is inclusive, the right bound is non-inclusive.
901 * It is ok to cursor up too far.
904 r
= hammer_btree_cmp(&cursor
->key_beg
, cursor
->left_bound
);
905 s
= hammer_btree_cmp(&cursor
->key_beg
, cursor
->right_bound
);
908 KKASSERT(cursor
->parent
);
909 ++hammer_stats_btree_iterations
;
910 error
= hammer_cursor_up(cursor
);
916 * The delete-checks below are based on node, not parent. Set the
917 * initial delete-check based on the parent.
920 KKASSERT(cursor
->left_bound
->create_tid
!= 1);
921 cursor
->create_check
= cursor
->left_bound
->create_tid
- 1;
922 cursor
->flags
|= HAMMER_CURSOR_CREATE_CHECK
;
926 * We better have ended up with a node somewhere.
928 KKASSERT(cursor
->node
!= NULL
);
931 * If we are inserting we can't start at a full node if the parent
932 * is also full (because there is no way to split the node),
933 * continue running up the tree until the requirement is satisfied
934 * or we hit the root of the filesystem.
936 * (If inserting we aren't doing an as-of search so we don't have
937 * to worry about create_check).
939 while ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0) {
940 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
941 if (btree_node_is_full(cursor
->node
->ondisk
) == 0)
944 if (btree_node_is_full(cursor
->node
->ondisk
) ==0)
947 if (cursor
->node
->ondisk
->parent
== 0 ||
948 cursor
->parent
->ondisk
->count
!= HAMMER_BTREE_INT_ELMS
) {
951 ++hammer_stats_btree_iterations
;
952 error
= hammer_cursor_up(cursor
);
953 /* node may have become stale */
959 * Push down through internal nodes to locate the requested key.
961 node
= cursor
->node
->ondisk
;
962 while (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
964 * Scan the node to find the subtree index to push down into.
965 * We go one-past, then back-up.
967 * We must proactively remove deleted elements which may
968 * have been left over from a deadlocked btree_remove().
970 * The left and right boundaries are included in the loop
971 * in order to detect edge cases.
973 * If the separator only differs by create_tid (r == 1)
974 * and we are doing an as-of search, we may end up going
975 * down a branch to the left of the one containing the
976 * desired key. This requires numerous special cases.
978 ++hammer_stats_btree_iterations
;
979 if (hammer_debug_btree
) {
980 kprintf("SEARCH-I %016llx count=%d\n",
981 cursor
->node
->node_offset
,
986 * Try to shortcut the search before dropping into the
987 * linear loop. Locate the first node where r <= 1.
989 i
= hammer_btree_search_node(&cursor
->key_beg
, node
);
990 while (i
<= node
->count
) {
991 ++hammer_stats_btree_elements
;
992 elm
= &node
->elms
[i
];
993 r
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->base
);
994 if (hammer_debug_btree
> 2) {
995 kprintf(" IELM %p %d r=%d\n",
996 &node
->elms
[i
], i
, r
);
1001 KKASSERT(elm
->base
.create_tid
!= 1);
1002 cursor
->create_check
= elm
->base
.create_tid
- 1;
1003 cursor
->flags
|= HAMMER_CURSOR_CREATE_CHECK
;
1007 if (hammer_debug_btree
) {
1008 kprintf("SEARCH-I preI=%d/%d r=%d\n",
1013 * These cases occur when the parent's idea of the boundary
1014 * is wider then the child's idea of the boundary, and
1015 * require special handling. If not inserting we can
1016 * terminate the search early for these cases but the
1017 * child's boundaries cannot be unconditionally modified.
1021 * If i == 0 the search terminated to the LEFT of the
1022 * left_boundary but to the RIGHT of the parent's left
1027 elm
= &node
->elms
[0];
1030 * If we aren't inserting we can stop here.
1032 if ((flags
& (HAMMER_CURSOR_INSERT
|
1033 HAMMER_CURSOR_PRUNING
)) == 0) {
1039 * Correct a left-hand boundary mismatch.
1041 * We can only do this if we can upgrade the lock,
1042 * and synchronized as a background cursor (i.e.
1043 * inserting or pruning).
1045 * WARNING: We can only do this if inserting, i.e.
1046 * we are running on the backend.
1048 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1050 KKASSERT(cursor
->flags
& HAMMER_CURSOR_BACKEND
);
1051 hammer_modify_node_field(cursor
->trans
, cursor
->node
,
1053 save
= node
->elms
[0].base
.btype
;
1054 node
->elms
[0].base
= *cursor
->left_bound
;
1055 node
->elms
[0].base
.btype
= save
;
1056 hammer_modify_node_done(cursor
->node
);
1057 } else if (i
== node
->count
+ 1) {
1059 * If i == node->count + 1 the search terminated to
1060 * the RIGHT of the right boundary but to the LEFT
1061 * of the parent's right boundary. If we aren't
1062 * inserting we can stop here.
1064 * Note that the last element in this case is
1065 * elms[i-2] prior to adjustments to 'i'.
1068 if ((flags
& (HAMMER_CURSOR_INSERT
|
1069 HAMMER_CURSOR_PRUNING
)) == 0) {
1075 * Correct a right-hand boundary mismatch.
1076 * (actual push-down record is i-2 prior to
1077 * adjustments to i).
1079 * We can only do this if we can upgrade the lock,
1080 * and synchronized as a background cursor (i.e.
1081 * inserting or pruning).
1083 * WARNING: We can only do this if inserting, i.e.
1084 * we are running on the backend.
1086 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1088 elm
= &node
->elms
[i
];
1089 KKASSERT(cursor
->flags
& HAMMER_CURSOR_BACKEND
);
1090 hammer_modify_node(cursor
->trans
, cursor
->node
,
1091 &elm
->base
, sizeof(elm
->base
));
1092 elm
->base
= *cursor
->right_bound
;
1093 hammer_modify_node_done(cursor
->node
);
1097 * The push-down index is now i - 1. If we had
1098 * terminated on the right boundary this will point
1099 * us at the last element.
1104 elm
= &node
->elms
[i
];
1106 if (hammer_debug_btree
) {
1107 kprintf("RESULT-I %016llx[%d] %016llx %02x "
1108 "key=%016llx cre=%016llx lo=%02x\n",
1109 cursor
->node
->node_offset
,
1111 elm
->internal
.base
.obj_id
,
1112 elm
->internal
.base
.rec_type
,
1113 elm
->internal
.base
.key
,
1114 elm
->internal
.base
.create_tid
,
1115 elm
->internal
.base
.localization
1120 * We better have a valid subtree offset.
1122 KKASSERT(elm
->internal
.subtree_offset
!= 0);
1125 * Handle insertion and deletion requirements.
1127 * If inserting split full nodes. The split code will
1128 * adjust cursor->node and cursor->index if the current
1129 * index winds up in the new node.
1131 * If inserting and a left or right edge case was detected,
1132 * we cannot correct the left or right boundary and must
1133 * prepend and append an empty leaf node in order to make
1134 * the boundary correction.
1136 * If we run out of space we set enospc and continue on
1137 * to a leaf to provide the spike code with a good point
1140 if ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0) {
1141 if (btree_node_is_full(node
)) {
1142 error
= btree_split_internal(cursor
);
1144 if (error
!= ENOSPC
)
1149 * reload stale pointers
1152 node
= cursor
->node
->ondisk
;
1157 * Push down (push into new node, existing node becomes
1158 * the parent) and continue the search.
1160 error
= hammer_cursor_down(cursor
);
1161 /* node may have become stale */
1164 node
= cursor
->node
->ondisk
;
1168 * We are at a leaf, do a linear search of the key array.
1170 * On success the index is set to the matching element and 0
1173 * On failure the index is set to the insertion point and ENOENT
1176 * Boundaries are not stored in leaf nodes, so the index can wind
1177 * up to the left of element 0 (index == 0) or past the end of
1178 * the array (index == node->count). It is also possible that the
1179 * leaf might be empty.
1181 ++hammer_stats_btree_iterations
;
1182 KKASSERT (node
->type
== HAMMER_BTREE_TYPE_LEAF
);
1183 KKASSERT(node
->count
<= HAMMER_BTREE_LEAF_ELMS
);
1184 if (hammer_debug_btree
) {
1185 kprintf("SEARCH-L %016llx count=%d\n",
1186 cursor
->node
->node_offset
,
1191 * Try to shortcut the search before dropping into the
1192 * linear loop. Locate the first node where r <= 1.
1194 i
= hammer_btree_search_node(&cursor
->key_beg
, node
);
1195 while (i
< node
->count
) {
1196 ++hammer_stats_btree_elements
;
1197 elm
= &node
->elms
[i
];
1199 r
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->leaf
.base
);
1201 if (hammer_debug_btree
> 1)
1202 kprintf(" ELM %p %d r=%d\n", &node
->elms
[i
], i
, r
);
1205 * We are at a record element. Stop if we've flipped past
1206 * key_beg, not counting the create_tid test. Allow the
1207 * r == 1 case (key_beg > element but differs only by its
1208 * create_tid) to fall through to the AS-OF check.
1210 KKASSERT (elm
->leaf
.base
.btype
== HAMMER_BTREE_TYPE_RECORD
);
1220 * Check our as-of timestamp against the element.
1222 if (flags
& HAMMER_CURSOR_ASOF
) {
1223 if (hammer_btree_chkts(cursor
->asof
,
1224 &node
->elms
[i
].base
) != 0) {
1230 if (r
> 0) { /* can only be +1 */
1238 if (hammer_debug_btree
) {
1239 kprintf("RESULT-L %016llx[%d] (SUCCESS)\n",
1240 cursor
->node
->node_offset
, i
);
1246 * The search of the leaf node failed. i is the insertion point.
1249 if (hammer_debug_btree
) {
1250 kprintf("RESULT-L %016llx[%d] (FAILED)\n",
1251 cursor
->node
->node_offset
, i
);
1255 * No exact match was found, i is now at the insertion point.
1257 * If inserting split a full leaf before returning. This
1258 * may have the side effect of adjusting cursor->node and
1262 if ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0 &&
1263 btree_node_is_full(node
)) {
1264 error
= btree_split_leaf(cursor
);
1266 if (error
!= ENOSPC
)
1271 * reload stale pointers
1275 node = &cursor->node->internal;
1280 * We reached a leaf but did not find the key we were looking for.
1281 * If this is an insert we will be properly positioned for an insert
1282 * (ENOENT) or spike (ENOSPC) operation.
1284 error
= enospc
? ENOSPC
: ENOENT
;
1290 * Heuristical search for the first element whos comparison is <= 1. May
1291 * return an index whos compare result is > 1 but may only return an index
1292 * whos compare result is <= 1 if it is the first element with that result.
1295 hammer_btree_search_node(hammer_base_elm_t elm
, hammer_node_ondisk_t node
)
1303 * Don't bother if the node does not have very many elements
1308 i
= b
+ (s
- b
) / 2;
1309 ++hammer_stats_btree_elements
;
1310 r
= hammer_btree_cmp(elm
, &node
->elms
[i
].leaf
.base
);
1321 /************************************************************************
1322 * SPLITTING AND MERGING *
1323 ************************************************************************
1325 * These routines do all the dirty work required to split and merge nodes.
1329 * Split an internal node into two nodes and move the separator at the split
1330 * point to the parent.
1332 * (cursor->node, cursor->index) indicates the element the caller intends
1333 * to push into. We will adjust node and index if that element winds
1334 * up in the split node.
1336 * If we are at the root of the filesystem a new root must be created with
1337 * two elements, one pointing to the original root and one pointing to the
1338 * newly allocated split node.
1342 btree_split_internal(hammer_cursor_t cursor
)
1344 hammer_node_ondisk_t ondisk
;
1346 hammer_node_t parent
;
1347 hammer_node_t new_node
;
1348 hammer_btree_elm_t elm
;
1349 hammer_btree_elm_t parent_elm
;
1350 hammer_node_locklist_t locklist
= NULL
;
1351 hammer_mount_t hmp
= cursor
->trans
->hmp
;
1357 const int esize
= sizeof(*elm
);
1359 error
= hammer_btree_lock_children(cursor
, &locklist
);
1362 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1364 ++hammer_stats_btree_splits
;
1367 * We are splitting but elms[split] will be promoted to the parent,
1368 * leaving the right hand node with one less element. If the
1369 * insertion point will be on the left-hand side adjust the split
1370 * point to give the right hand side one additional node.
1372 node
= cursor
->node
;
1373 ondisk
= node
->ondisk
;
1374 split
= (ondisk
->count
+ 1) / 2;
1375 if (cursor
->index
<= split
)
1379 * If we are at the root of the filesystem, create a new root node
1380 * with 1 element and split normally. Avoid making major
1381 * modifications until we know the whole operation will work.
1383 if (ondisk
->parent
== 0) {
1384 parent
= hammer_alloc_btree(cursor
->trans
, &error
);
1387 hammer_lock_ex(&parent
->lock
);
1388 hammer_modify_node_noundo(cursor
->trans
, parent
);
1389 ondisk
= parent
->ondisk
;
1392 ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1393 ondisk
->elms
[0].base
= hmp
->root_btree_beg
;
1394 ondisk
->elms
[0].base
.btype
= node
->ondisk
->type
;
1395 ondisk
->elms
[0].internal
.subtree_offset
= node
->node_offset
;
1396 ondisk
->elms
[1].base
= hmp
->root_btree_end
;
1397 hammer_modify_node_done(parent
);
1398 /* ondisk->elms[1].base.btype - not used */
1400 parent_index
= 0; /* index of current node in parent */
1403 parent
= cursor
->parent
;
1404 parent_index
= cursor
->parent_index
;
1408 * Split node into new_node at the split point.
1410 * B O O O P N N B <-- P = node->elms[split]
1411 * 0 1 2 3 4 5 6 <-- subtree indices
1416 * B O O O B B N N B <--- inner boundary points are 'P'
1420 new_node
= hammer_alloc_btree(cursor
->trans
, &error
);
1421 if (new_node
== NULL
) {
1423 hammer_unlock(&parent
->lock
);
1424 hammer_delete_node(cursor
->trans
, parent
);
1425 hammer_rel_node(parent
);
1429 hammer_lock_ex(&new_node
->lock
);
1432 * Create the new node. P becomes the left-hand boundary in the
1433 * new node. Copy the right-hand boundary as well.
1435 * elm is the new separator.
1437 hammer_modify_node_noundo(cursor
->trans
, new_node
);
1438 hammer_modify_node_all(cursor
->trans
, node
);
1439 ondisk
= node
->ondisk
;
1440 elm
= &ondisk
->elms
[split
];
1441 bcopy(elm
, &new_node
->ondisk
->elms
[0],
1442 (ondisk
->count
- split
+ 1) * esize
);
1443 new_node
->ondisk
->count
= ondisk
->count
- split
;
1444 new_node
->ondisk
->parent
= parent
->node_offset
;
1445 new_node
->ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1446 KKASSERT(ondisk
->type
== new_node
->ondisk
->type
);
1449 * Cleanup the original node. Elm (P) becomes the new boundary,
1450 * its subtree_offset was moved to the new node. If we had created
1451 * a new root its parent pointer may have changed.
1453 elm
->internal
.subtree_offset
= 0;
1454 ondisk
->count
= split
;
1457 * Insert the separator into the parent, fixup the parent's
1458 * reference to the original node, and reference the new node.
1459 * The separator is P.
1461 * Remember that base.count does not include the right-hand boundary.
1463 hammer_modify_node_all(cursor
->trans
, parent
);
1464 ondisk
= parent
->ondisk
;
1465 KKASSERT(ondisk
->count
!= HAMMER_BTREE_INT_ELMS
);
1466 parent_elm
= &ondisk
->elms
[parent_index
+1];
1467 bcopy(parent_elm
, parent_elm
+ 1,
1468 (ondisk
->count
- parent_index
) * esize
);
1469 parent_elm
->internal
.base
= elm
->base
; /* separator P */
1470 parent_elm
->internal
.base
.btype
= new_node
->ondisk
->type
;
1471 parent_elm
->internal
.subtree_offset
= new_node
->node_offset
;
1473 hammer_modify_node_done(parent
);
1476 * The children of new_node need their parent pointer set to new_node.
1477 * The children have already been locked by
1478 * hammer_btree_lock_children().
1480 for (i
= 0; i
< new_node
->ondisk
->count
; ++i
) {
1481 elm
= &new_node
->ondisk
->elms
[i
];
1482 error
= btree_set_parent(cursor
->trans
, new_node
, elm
);
1484 panic("btree_split_internal: btree-fixup problem");
1487 hammer_modify_node_done(new_node
);
1490 * The filesystem's root B-Tree pointer may have to be updated.
1493 hammer_volume_t volume
;
1495 volume
= hammer_get_root_volume(hmp
, &error
);
1496 KKASSERT(error
== 0);
1498 hammer_modify_volume_field(cursor
->trans
, volume
,
1500 volume
->ondisk
->vol0_btree_root
= parent
->node_offset
;
1501 hammer_modify_volume_done(volume
);
1502 node
->ondisk
->parent
= parent
->node_offset
;
1503 if (cursor
->parent
) {
1504 hammer_unlock(&cursor
->parent
->lock
);
1505 hammer_rel_node(cursor
->parent
);
1507 cursor
->parent
= parent
; /* lock'd and ref'd */
1508 hammer_rel_volume(volume
, 0);
1510 hammer_modify_node_done(node
);
1514 * Ok, now adjust the cursor depending on which element the original
1515 * index was pointing at. If we are >= the split point the push node
1516 * is now in the new node.
1518 * NOTE: If we are at the split point itself we cannot stay with the
1519 * original node because the push index will point at the right-hand
1520 * boundary, which is illegal.
1522 * NOTE: The cursor's parent or parent_index must be adjusted for
1523 * the case where a new parent (new root) was created, and the case
1524 * where the cursor is now pointing at the split node.
1526 if (cursor
->index
>= split
) {
1527 cursor
->parent_index
= parent_index
+ 1;
1528 cursor
->index
-= split
;
1529 hammer_unlock(&cursor
->node
->lock
);
1530 hammer_rel_node(cursor
->node
);
1531 cursor
->node
= new_node
; /* locked and ref'd */
1533 cursor
->parent_index
= parent_index
;
1534 hammer_unlock(&new_node
->lock
);
1535 hammer_rel_node(new_node
);
1539 * Fixup left and right bounds
1541 parent_elm
= &parent
->ondisk
->elms
[cursor
->parent_index
];
1542 cursor
->left_bound
= &parent_elm
[0].internal
.base
;
1543 cursor
->right_bound
= &parent_elm
[1].internal
.base
;
1544 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1545 &cursor
->node
->ondisk
->elms
[0].internal
.base
) <= 0);
1546 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1547 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
].internal
.base
) >= 0);
1550 hammer_btree_unlock_children(&locklist
);
1551 hammer_cursor_downgrade(cursor
);
1556 * Same as the above, but splits a full leaf node.
1562 btree_split_leaf(hammer_cursor_t cursor
)
1564 hammer_node_ondisk_t ondisk
;
1565 hammer_node_t parent
;
1568 hammer_node_t new_leaf
;
1569 hammer_btree_elm_t elm
;
1570 hammer_btree_elm_t parent_elm
;
1571 hammer_base_elm_t mid_boundary
;
1576 const size_t esize
= sizeof(*elm
);
1578 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1580 ++hammer_stats_btree_splits
;
1582 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1583 &cursor
->node
->ondisk
->elms
[0].leaf
.base
) <= 0);
1584 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1585 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
-1].leaf
.base
) > 0);
1588 * Calculate the split point. If the insertion point will be on
1589 * the left-hand side adjust the split point to give the right
1590 * hand side one additional node.
1592 * Spikes are made up of two leaf elements which cannot be
1595 leaf
= cursor
->node
;
1596 ondisk
= leaf
->ondisk
;
1597 split
= (ondisk
->count
+ 1) / 2;
1598 if (cursor
->index
<= split
)
1603 elm
= &ondisk
->elms
[split
];
1605 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
[-1].leaf
.base
) <= 0);
1606 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
->leaf
.base
) <= 0);
1607 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
->leaf
.base
) > 0);
1608 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
[1].leaf
.base
) > 0);
1611 * If we are at the root of the tree, create a new root node with
1612 * 1 element and split normally. Avoid making major modifications
1613 * until we know the whole operation will work.
1615 if (ondisk
->parent
== 0) {
1616 parent
= hammer_alloc_btree(cursor
->trans
, &error
);
1619 hammer_lock_ex(&parent
->lock
);
1620 hammer_modify_node_noundo(cursor
->trans
, parent
);
1621 ondisk
= parent
->ondisk
;
1624 ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1625 ondisk
->elms
[0].base
= hmp
->root_btree_beg
;
1626 ondisk
->elms
[0].base
.btype
= leaf
->ondisk
->type
;
1627 ondisk
->elms
[0].internal
.subtree_offset
= leaf
->node_offset
;
1628 ondisk
->elms
[1].base
= hmp
->root_btree_end
;
1629 /* ondisk->elms[1].base.btype = not used */
1630 hammer_modify_node_done(parent
);
1632 parent_index
= 0; /* insertion point in parent */
1635 parent
= cursor
->parent
;
1636 parent_index
= cursor
->parent_index
;
1640 * Split leaf into new_leaf at the split point. Select a separator
1641 * value in-between the two leafs but with a bent towards the right
1642 * leaf since comparisons use an 'elm >= separator' inequality.
1651 new_leaf
= hammer_alloc_btree(cursor
->trans
, &error
);
1652 if (new_leaf
== NULL
) {
1654 hammer_unlock(&parent
->lock
);
1655 hammer_delete_node(cursor
->trans
, parent
);
1656 hammer_rel_node(parent
);
1660 hammer_lock_ex(&new_leaf
->lock
);
1663 * Create the new node and copy the leaf elements from the split
1664 * point on to the new node.
1666 hammer_modify_node_all(cursor
->trans
, leaf
);
1667 hammer_modify_node_noundo(cursor
->trans
, new_leaf
);
1668 ondisk
= leaf
->ondisk
;
1669 elm
= &ondisk
->elms
[split
];
1670 bcopy(elm
, &new_leaf
->ondisk
->elms
[0], (ondisk
->count
- split
) * esize
);
1671 new_leaf
->ondisk
->count
= ondisk
->count
- split
;
1672 new_leaf
->ondisk
->parent
= parent
->node_offset
;
1673 new_leaf
->ondisk
->type
= HAMMER_BTREE_TYPE_LEAF
;
1674 KKASSERT(ondisk
->type
== new_leaf
->ondisk
->type
);
1675 hammer_modify_node_done(new_leaf
);
1678 * Cleanup the original node. Because this is a leaf node and
1679 * leaf nodes do not have a right-hand boundary, there
1680 * aren't any special edge cases to clean up. We just fixup the
1683 ondisk
->count
= split
;
1686 * Insert the separator into the parent, fixup the parent's
1687 * reference to the original node, and reference the new node.
1688 * The separator is P.
1690 * Remember that base.count does not include the right-hand boundary.
1691 * We are copying parent_index+1 to parent_index+2, not +0 to +1.
1693 hammer_modify_node_all(cursor
->trans
, parent
);
1694 ondisk
= parent
->ondisk
;
1695 KKASSERT(split
!= 0);
1696 KKASSERT(ondisk
->count
!= HAMMER_BTREE_INT_ELMS
);
1697 parent_elm
= &ondisk
->elms
[parent_index
+1];
1698 bcopy(parent_elm
, parent_elm
+ 1,
1699 (ondisk
->count
- parent_index
) * esize
);
1701 hammer_make_separator(&elm
[-1].base
, &elm
[0].base
, &parent_elm
->base
);
1702 parent_elm
->internal
.base
.btype
= new_leaf
->ondisk
->type
;
1703 parent_elm
->internal
.subtree_offset
= new_leaf
->node_offset
;
1704 mid_boundary
= &parent_elm
->base
;
1706 hammer_modify_node_done(parent
);
1709 * The filesystem's root B-Tree pointer may have to be updated.
1712 hammer_volume_t volume
;
1714 volume
= hammer_get_root_volume(hmp
, &error
);
1715 KKASSERT(error
== 0);
1717 hammer_modify_volume_field(cursor
->trans
, volume
,
1719 volume
->ondisk
->vol0_btree_root
= parent
->node_offset
;
1720 hammer_modify_volume_done(volume
);
1721 leaf
->ondisk
->parent
= parent
->node_offset
;
1722 if (cursor
->parent
) {
1723 hammer_unlock(&cursor
->parent
->lock
);
1724 hammer_rel_node(cursor
->parent
);
1726 cursor
->parent
= parent
; /* lock'd and ref'd */
1727 hammer_rel_volume(volume
, 0);
1729 hammer_modify_node_done(leaf
);
1732 * Ok, now adjust the cursor depending on which element the original
1733 * index was pointing at. If we are >= the split point the push node
1734 * is now in the new node.
1736 * NOTE: If we are at the split point itself we need to select the
1737 * old or new node based on where key_beg's insertion point will be.
1738 * If we pick the wrong side the inserted element will wind up in
1739 * the wrong leaf node and outside that node's bounds.
1741 if (cursor
->index
> split
||
1742 (cursor
->index
== split
&&
1743 hammer_btree_cmp(&cursor
->key_beg
, mid_boundary
) >= 0)) {
1744 cursor
->parent_index
= parent_index
+ 1;
1745 cursor
->index
-= split
;
1746 hammer_unlock(&cursor
->node
->lock
);
1747 hammer_rel_node(cursor
->node
);
1748 cursor
->node
= new_leaf
;
1750 cursor
->parent_index
= parent_index
;
1751 hammer_unlock(&new_leaf
->lock
);
1752 hammer_rel_node(new_leaf
);
1756 * Fixup left and right bounds
1758 parent_elm
= &parent
->ondisk
->elms
[cursor
->parent_index
];
1759 cursor
->left_bound
= &parent_elm
[0].internal
.base
;
1760 cursor
->right_bound
= &parent_elm
[1].internal
.base
;
1763 * Assert that the bounds are correct.
1765 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1766 &cursor
->node
->ondisk
->elms
[0].leaf
.base
) <= 0);
1767 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1768 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
-1].leaf
.base
) > 0);
1769 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &cursor
->key_beg
) <= 0);
1770 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &cursor
->key_beg
) > 0);
1773 hammer_cursor_downgrade(cursor
);
1778 * Recursively correct the right-hand boundary's create_tid to (tid) as
1779 * long as the rest of the key matches. We have to recurse upward in
1780 * the tree as well as down the left side of each parent's right node.
1782 * Return EDEADLK if we were only partially successful, forcing the caller
1783 * to try again. The original cursor is not modified. This routine can
1784 * also fail with EDEADLK if it is forced to throw away a portion of its
1787 * The caller must pass a downgraded cursor to us (otherwise we can't dup it).
1790 TAILQ_ENTRY(hammer_rhb
) entry
;
1795 TAILQ_HEAD(hammer_rhb_list
, hammer_rhb
);
1798 hammer_btree_correct_rhb(hammer_cursor_t cursor
, hammer_tid_t tid
)
1800 struct hammer_rhb_list rhb_list
;
1801 hammer_base_elm_t elm
;
1802 hammer_node_t orig_node
;
1803 struct hammer_rhb
*rhb
;
1807 TAILQ_INIT(&rhb_list
);
1810 * Save our position so we can restore it on return. This also
1811 * gives us a stable 'elm'.
1813 orig_node
= cursor
->node
;
1814 hammer_ref_node(orig_node
);
1815 hammer_lock_sh(&orig_node
->lock
);
1816 orig_index
= cursor
->index
;
1817 elm
= &orig_node
->ondisk
->elms
[orig_index
].base
;
1820 * Now build a list of parents going up, allocating a rhb
1821 * structure for each one.
1823 while (cursor
->parent
) {
1825 * Stop if we no longer have any right-bounds to fix up
1827 if (elm
->obj_id
!= cursor
->right_bound
->obj_id
||
1828 elm
->rec_type
!= cursor
->right_bound
->rec_type
||
1829 elm
->key
!= cursor
->right_bound
->key
) {
1834 * Stop if the right-hand bound's create_tid does not
1835 * need to be corrected.
1837 if (cursor
->right_bound
->create_tid
>= tid
)
1840 rhb
= kmalloc(sizeof(*rhb
), M_HAMMER
, M_WAITOK
|M_ZERO
);
1841 rhb
->node
= cursor
->parent
;
1842 rhb
->index
= cursor
->parent_index
;
1843 hammer_ref_node(rhb
->node
);
1844 hammer_lock_sh(&rhb
->node
->lock
);
1845 TAILQ_INSERT_HEAD(&rhb_list
, rhb
, entry
);
1847 hammer_cursor_up(cursor
);
1851 * now safely adjust the right hand bound for each rhb. This may
1852 * also require taking the right side of the tree and iterating down
1856 while (error
== 0 && (rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1857 error
= hammer_cursor_seek(cursor
, rhb
->node
, rhb
->index
);
1860 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1861 hammer_unlock(&rhb
->node
->lock
);
1862 hammer_rel_node(rhb
->node
);
1863 kfree(rhb
, M_HAMMER
);
1865 switch (cursor
->node
->ondisk
->type
) {
1866 case HAMMER_BTREE_TYPE_INTERNAL
:
1868 * Right-boundary for parent at internal node
1869 * is one element to the right of the element whos
1870 * right boundary needs adjusting. We must then
1871 * traverse down the left side correcting any left
1872 * bounds (which may now be too far to the left).
1875 error
= hammer_btree_correct_lhb(cursor
, tid
);
1878 panic("hammer_btree_correct_rhb(): Bad node type");
1887 while ((rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1888 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1889 hammer_unlock(&rhb
->node
->lock
);
1890 hammer_rel_node(rhb
->node
);
1891 kfree(rhb
, M_HAMMER
);
1893 error
= hammer_cursor_seek(cursor
, orig_node
, orig_index
);
1894 hammer_unlock(&orig_node
->lock
);
1895 hammer_rel_node(orig_node
);
1900 * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand
1901 * bound going downward starting at the current cursor position.
1903 * This function does not restore the cursor after use.
1906 hammer_btree_correct_lhb(hammer_cursor_t cursor
, hammer_tid_t tid
)
1908 struct hammer_rhb_list rhb_list
;
1909 hammer_base_elm_t elm
;
1910 hammer_base_elm_t cmp
;
1911 struct hammer_rhb
*rhb
;
1914 TAILQ_INIT(&rhb_list
);
1916 cmp
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
1919 * Record the node and traverse down the left-hand side for all
1920 * matching records needing a boundary correction.
1924 rhb
= kmalloc(sizeof(*rhb
), M_HAMMER
, M_WAITOK
|M_ZERO
);
1925 rhb
->node
= cursor
->node
;
1926 rhb
->index
= cursor
->index
;
1927 hammer_ref_node(rhb
->node
);
1928 hammer_lock_sh(&rhb
->node
->lock
);
1929 TAILQ_INSERT_HEAD(&rhb_list
, rhb
, entry
);
1931 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
1933 * Nothing to traverse down if we are at the right
1934 * boundary of an internal node.
1936 if (cursor
->index
== cursor
->node
->ondisk
->count
)
1939 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
1940 if (elm
->btype
== HAMMER_BTREE_TYPE_RECORD
)
1942 panic("Illegal leaf record type %02x", elm
->btype
);
1944 error
= hammer_cursor_down(cursor
);
1948 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
1949 if (elm
->obj_id
!= cmp
->obj_id
||
1950 elm
->rec_type
!= cmp
->rec_type
||
1951 elm
->key
!= cmp
->key
) {
1954 if (elm
->create_tid
>= tid
)
1960 * Now we can safely adjust the left-hand boundary from the bottom-up.
1961 * The last element we remove from the list is the caller's right hand
1962 * boundary, which must also be adjusted.
1964 while (error
== 0 && (rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1965 error
= hammer_cursor_seek(cursor
, rhb
->node
, rhb
->index
);
1968 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1969 hammer_unlock(&rhb
->node
->lock
);
1970 hammer_rel_node(rhb
->node
);
1971 kfree(rhb
, M_HAMMER
);
1973 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
1974 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
1975 hammer_modify_node(cursor
->trans
, cursor
->node
,
1977 sizeof(elm
->create_tid
));
1978 elm
->create_tid
= tid
;
1979 hammer_modify_node_done(cursor
->node
);
1981 panic("hammer_btree_correct_lhb(): Bad element type");
1988 while ((rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1989 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1990 hammer_unlock(&rhb
->node
->lock
);
1991 hammer_rel_node(rhb
->node
);
1992 kfree(rhb
, M_HAMMER
);
1998 * Attempt to remove the locked, empty or want-to-be-empty B-Tree node at
1999 * (cursor->node). Returns 0 on success, EDEADLK if we could not complete
2000 * the operation due to a deadlock, or some other error.
2002 * This routine is always called with an empty, locked leaf but may recurse
2003 * into want-to-be-empty parents as part of its operation.
2005 * It should also be noted that when removing empty leaves we must be sure
2006 * to test and update mirror_tid because another thread may have deadlocked
2007 * against us (or someone) trying to propogate it up and cannot retry once
2008 * the node has been deleted.
2010 * On return the cursor may end up pointing to an internal node, suitable
2011 * for further iteration but not for an immediate insertion or deletion.
2014 btree_remove(hammer_cursor_t cursor
)
2016 hammer_node_ondisk_t ondisk
;
2017 hammer_btree_elm_t elm
;
2019 hammer_node_t parent
;
2020 const int esize
= sizeof(*elm
);
2023 node
= cursor
->node
;
2026 * When deleting the root of the filesystem convert it to
2027 * an empty leaf node. Internal nodes cannot be empty.
2029 ondisk
= node
->ondisk
;
2030 if (ondisk
->parent
== 0) {
2031 KKASSERT(cursor
->parent
== NULL
);
2032 hammer_modify_node_all(cursor
->trans
, node
);
2033 KKASSERT(ondisk
== node
->ondisk
);
2034 ondisk
->type
= HAMMER_BTREE_TYPE_LEAF
;
2036 hammer_modify_node_done(node
);
2041 parent
= cursor
->parent
;
2044 * If another thread deadlocked trying to propogate mirror_tid up
2045 * we have to finish the job before deleting node. XXX
2047 if (parent
->ondisk
->mirror_tid
< node
->ondisk
->mirror_tid
&&
2048 (cursor
->trans
->hmp
->hflags
& (HMNT_MASTERID
|HMNT_SLAVE
))) {
2049 hammer_btree_mirror_propagate(cursor
->trans
,
2051 cursor
->parent_index
,
2052 node
->ondisk
->mirror_tid
);
2057 * Attempt to remove the parent's reference to the child. If the
2058 * parent would become empty we have to recurse. If we fail we
2059 * leave the parent pointing to an empty leaf node.
2061 if (parent
->ondisk
->count
== 1) {
2063 * This special cursor_up_locked() call leaves the original
2064 * node exclusively locked and referenced, leaves the
2065 * original parent locked (as the new node), and locks the
2066 * new parent. It can return EDEADLK.
2068 error
= hammer_cursor_up_locked(cursor
);
2070 error
= btree_remove(cursor
);
2072 hammer_modify_node_all(cursor
->trans
, node
);
2073 ondisk
= node
->ondisk
;
2074 ondisk
->type
= HAMMER_BTREE_TYPE_DELETED
;
2076 hammer_modify_node_done(node
);
2077 hammer_flush_node(node
);
2078 hammer_delete_node(cursor
->trans
, node
);
2080 kprintf("Warning: BTREE_REMOVE: Defering "
2081 "parent removal1 @ %016llx, skipping\n",
2084 hammer_unlock(&node
->lock
);
2085 hammer_rel_node(node
);
2087 kprintf("Warning: BTREE_REMOVE: Defering parent "
2088 "removal2 @ %016llx, skipping\n",
2092 KKASSERT(parent
->ondisk
->count
> 1);
2095 * Delete the subtree reference in the parent
2097 hammer_modify_node_all(cursor
->trans
, parent
);
2098 ondisk
= parent
->ondisk
;
2099 KKASSERT(ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
2101 elm
= &ondisk
->elms
[cursor
->parent_index
];
2102 KKASSERT(elm
->internal
.subtree_offset
== node
->node_offset
);
2103 KKASSERT(ondisk
->count
> 0);
2104 bcopy(&elm
[1], &elm
[0],
2105 (ondisk
->count
- cursor
->parent_index
) * esize
);
2107 hammer_modify_node_done(parent
);
2108 hammer_flush_node(node
);
2109 hammer_delete_node(cursor
->trans
, node
);
2112 * cursor->node is invalid, cursor up to make the cursor
2115 error
= hammer_cursor_up(cursor
);
2121 * Propagate a mirror TID update upwards through the B-Tree to the root.
2123 * A locked internal node must be passed in. The node will remain locked
2126 * This function syncs mirror_tid at the specified internal node's element,
2127 * adjusts the node's aggregation mirror_tid, and then recurses upwards.
2130 hammer_btree_mirror_propagate(hammer_transaction_t trans
, hammer_node_t node
,
2131 int index
, hammer_tid_t mirror_tid
)
2133 hammer_btree_internal_elm_t elm
;
2134 hammer_node_t parent
;
2138 KKASSERT (node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
2141 * Adjust the node's element
2143 elm
= &node
->ondisk
->elms
[index
].internal
;
2144 if (elm
->mirror_tid
>= mirror_tid
)
2146 hammer_modify_node(trans
, node
, &elm
->mirror_tid
,
2147 sizeof(elm
->mirror_tid
));
2148 elm
->mirror_tid
= mirror_tid
;
2149 hammer_modify_node_done(node
);
2152 * Adjust the node's mirror_tid aggragator
2154 if (node
->ondisk
->mirror_tid
>= mirror_tid
)
2156 hammer_modify_node_field(trans
, node
, mirror_tid
);
2157 node
->ondisk
->mirror_tid
= mirror_tid
;
2158 hammer_modify_node_done(node
);
2162 if (node
->ondisk
->parent
&&
2163 (trans
->hmp
->hflags
& (HMNT_MASTERID
|HMNT_SLAVE
))) {
2164 parent
= hammer_btree_get_parent(node
, &parent_index
,
2167 hammer_btree_mirror_propagate(trans
, parent
,
2168 parent_index
, mirror_tid
);
2169 hammer_unlock(&parent
->lock
);
2170 hammer_rel_node(parent
);
2177 hammer_btree_get_parent(hammer_node_t node
, int *parent_indexp
, int *errorp
,
2180 hammer_node_t parent
;
2181 hammer_btree_elm_t elm
;
2187 parent
= hammer_get_node(node
->hmp
, node
->ondisk
->parent
, 0, errorp
);
2189 KKASSERT(parent
== NULL
);
2192 KKASSERT ((parent
->flags
& HAMMER_NODE_DELETED
) == 0);
2197 if (try_exclusive
) {
2198 if (hammer_lock_ex_try(&parent
->lock
)) {
2199 hammer_rel_node(parent
);
2204 hammer_lock_sh(&parent
->lock
);
2208 * Figure out which element in the parent is pointing to the
2211 if (node
->ondisk
->count
) {
2212 i
= hammer_btree_search_node(&node
->ondisk
->elms
[0].base
,
2217 while (i
< parent
->ondisk
->count
) {
2218 elm
= &parent
->ondisk
->elms
[i
];
2219 if (elm
->internal
.subtree_offset
== node
->node_offset
)
2223 if (i
== parent
->ondisk
->count
) {
2224 hammer_unlock(&parent
->lock
);
2225 panic("Bad B-Tree link: parent %p node %p\n", parent
, node
);
2228 KKASSERT(*errorp
== 0);
2233 * The element (elm) has been moved to a new internal node (node).
2235 * If the element represents a pointer to an internal node that node's
2236 * parent must be adjusted to the element's new location.
2238 * XXX deadlock potential here with our exclusive locks
2241 btree_set_parent(hammer_transaction_t trans
, hammer_node_t node
,
2242 hammer_btree_elm_t elm
)
2244 hammer_node_t child
;
2249 switch(elm
->base
.btype
) {
2250 case HAMMER_BTREE_TYPE_INTERNAL
:
2251 case HAMMER_BTREE_TYPE_LEAF
:
2252 child
= hammer_get_node(node
->hmp
, elm
->internal
.subtree_offset
,
2255 hammer_modify_node_field(trans
, child
, parent
);
2256 child
->ondisk
->parent
= node
->node_offset
;
2257 hammer_modify_node_done(child
);
2258 hammer_rel_node(child
);
2268 * Exclusively lock all the children of node. This is used by the split
2269 * code to prevent anyone from accessing the children of a cursor node
2270 * while we fix-up its parent offset.
2272 * If we don't lock the children we can really mess up cursors which block
2273 * trying to cursor-up into our node.
2275 * On failure EDEADLK (or some other error) is returned. If a deadlock
2276 * error is returned the cursor is adjusted to block on termination.
2279 hammer_btree_lock_children(hammer_cursor_t cursor
,
2280 struct hammer_node_locklist
**locklistp
)
2283 hammer_node_locklist_t item
;
2284 hammer_node_ondisk_t ondisk
;
2285 hammer_btree_elm_t elm
;
2286 hammer_node_t child
;
2290 node
= cursor
->node
;
2291 ondisk
= node
->ondisk
;
2295 * We really do not want to block on I/O with exclusive locks held,
2296 * pre-get the children before trying to lock the mess.
2298 for (i
= 0; i
< ondisk
->count
; ++i
) {
2299 ++hammer_stats_btree_elements
;
2300 elm
= &ondisk
->elms
[i
];
2301 if (elm
->base
.btype
!= HAMMER_BTREE_TYPE_LEAF
&&
2302 elm
->base
.btype
!= HAMMER_BTREE_TYPE_INTERNAL
) {
2305 child
= hammer_get_node(node
->hmp
,
2306 elm
->internal
.subtree_offset
,
2309 hammer_rel_node(child
);
2315 for (i
= 0; error
== 0 && i
< ondisk
->count
; ++i
) {
2316 ++hammer_stats_btree_elements
;
2317 elm
= &ondisk
->elms
[i
];
2319 switch(elm
->base
.btype
) {
2320 case HAMMER_BTREE_TYPE_INTERNAL
:
2321 case HAMMER_BTREE_TYPE_LEAF
:
2322 KKASSERT(elm
->internal
.subtree_offset
!= 0);
2323 child
= hammer_get_node(node
->hmp
,
2324 elm
->internal
.subtree_offset
,
2332 if (hammer_lock_ex_try(&child
->lock
) != 0) {
2333 if (cursor
->deadlk_node
== NULL
) {
2334 cursor
->deadlk_node
= child
;
2335 hammer_ref_node(cursor
->deadlk_node
);
2338 hammer_rel_node(child
);
2340 item
= kmalloc(sizeof(*item
),
2341 M_HAMMER
, M_WAITOK
);
2342 item
->next
= *locklistp
;
2349 hammer_btree_unlock_children(locklistp
);
2355 * Release previously obtained node locks.
2358 hammer_btree_unlock_children(struct hammer_node_locklist
**locklistp
)
2360 hammer_node_locklist_t item
;
2362 while ((item
= *locklistp
) != NULL
) {
2363 *locklistp
= item
->next
;
2364 hammer_unlock(&item
->node
->lock
);
2365 hammer_rel_node(item
->node
);
2366 kfree(item
, M_HAMMER
);
2370 /************************************************************************
2371 * MISCELLANIOUS SUPPORT *
2372 ************************************************************************/
2375 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
2377 * Note that for this particular function a return value of -1, 0, or +1
2378 * can denote a match if create_tid is otherwise discounted. A create_tid
2379 * of zero is considered to be 'infinity' in comparisons.
2381 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
2384 hammer_btree_cmp(hammer_base_elm_t key1
, hammer_base_elm_t key2
)
2386 if (key1
->localization
< key2
->localization
)
2388 if (key1
->localization
> key2
->localization
)
2391 if (key1
->obj_id
< key2
->obj_id
)
2393 if (key1
->obj_id
> key2
->obj_id
)
2396 if (key1
->rec_type
< key2
->rec_type
)
2398 if (key1
->rec_type
> key2
->rec_type
)
2401 if (key1
->key
< key2
->key
)
2403 if (key1
->key
> key2
->key
)
2407 * A create_tid of zero indicates a record which is undeletable
2408 * and must be considered to have a value of positive infinity.
2410 if (key1
->create_tid
== 0) {
2411 if (key2
->create_tid
== 0)
2415 if (key2
->create_tid
== 0)
2417 if (key1
->create_tid
< key2
->create_tid
)
2419 if (key1
->create_tid
> key2
->create_tid
)
2425 * Test a timestamp against an element to determine whether the
2426 * element is visible. A timestamp of 0 means 'infinity'.
2429 hammer_btree_chkts(hammer_tid_t asof
, hammer_base_elm_t base
)
2432 if (base
->delete_tid
)
2436 if (asof
< base
->create_tid
)
2438 if (base
->delete_tid
&& asof
>= base
->delete_tid
)
2444 * Create a separator half way inbetween key1 and key2. For fields just
2445 * one unit apart, the separator will match key2. key1 is on the left-hand
2446 * side and key2 is on the right-hand side.
2448 * key2 must be >= the separator. It is ok for the separator to match key2.
2450 * NOTE: Even if key1 does not match key2, the separator may wind up matching
2453 * NOTE: It might be beneficial to just scrap this whole mess and just
2454 * set the separator to key2.
2456 #define MAKE_SEPARATOR(key1, key2, dest, field) \
2457 dest->field = key1->field + ((key2->field - key1->field + 1) >> 1);
2460 hammer_make_separator(hammer_base_elm_t key1
, hammer_base_elm_t key2
,
2461 hammer_base_elm_t dest
)
2463 bzero(dest
, sizeof(*dest
));
2465 dest
->rec_type
= key2
->rec_type
;
2466 dest
->key
= key2
->key
;
2467 dest
->obj_id
= key2
->obj_id
;
2468 dest
->create_tid
= key2
->create_tid
;
2470 MAKE_SEPARATOR(key1
, key2
, dest
, localization
);
2471 if (key1
->localization
== key2
->localization
) {
2472 MAKE_SEPARATOR(key1
, key2
, dest
, obj_id
);
2473 if (key1
->obj_id
== key2
->obj_id
) {
2474 MAKE_SEPARATOR(key1
, key2
, dest
, rec_type
);
2475 if (key1
->rec_type
== key2
->rec_type
) {
2476 MAKE_SEPARATOR(key1
, key2
, dest
, key
);
2478 * Don't bother creating a separator for
2479 * create_tid, which also conveniently avoids
2480 * having to handle the create_tid == 0
2481 * (infinity) case. Just leave create_tid
2484 * Worst case, dest matches key2 exactly,
2485 * which is acceptable.
2492 #undef MAKE_SEPARATOR
2495 * Return whether a generic internal or leaf node is full
2498 btree_node_is_full(hammer_node_ondisk_t node
)
2500 switch(node
->type
) {
2501 case HAMMER_BTREE_TYPE_INTERNAL
:
2502 if (node
->count
== HAMMER_BTREE_INT_ELMS
)
2505 case HAMMER_BTREE_TYPE_LEAF
:
2506 if (node
->count
== HAMMER_BTREE_LEAF_ELMS
)
2510 panic("illegal btree subtype");
2517 btree_max_elements(u_int8_t type
)
2519 if (type
== HAMMER_BTREE_TYPE_LEAF
)
2520 return(HAMMER_BTREE_LEAF_ELMS
);
2521 if (type
== HAMMER_BTREE_TYPE_INTERNAL
)
2522 return(HAMMER_BTREE_INT_ELMS
);
2523 panic("btree_max_elements: bad type %d\n", type
);
2528 hammer_print_btree_node(hammer_node_ondisk_t ondisk
)
2530 hammer_btree_elm_t elm
;
2533 kprintf("node %p count=%d parent=%016llx type=%c\n",
2534 ondisk
, ondisk
->count
, ondisk
->parent
, ondisk
->type
);
2537 * Dump both boundary elements if an internal node
2539 if (ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
2540 for (i
= 0; i
<= ondisk
->count
; ++i
) {
2541 elm
= &ondisk
->elms
[i
];
2542 hammer_print_btree_elm(elm
, ondisk
->type
, i
);
2545 for (i
= 0; i
< ondisk
->count
; ++i
) {
2546 elm
= &ondisk
->elms
[i
];
2547 hammer_print_btree_elm(elm
, ondisk
->type
, i
);
2553 hammer_print_btree_elm(hammer_btree_elm_t elm
, u_int8_t type
, int i
)
2556 kprintf("\tobj_id = %016llx\n", elm
->base
.obj_id
);
2557 kprintf("\tkey = %016llx\n", elm
->base
.key
);
2558 kprintf("\tcreate_tid = %016llx\n", elm
->base
.create_tid
);
2559 kprintf("\tdelete_tid = %016llx\n", elm
->base
.delete_tid
);
2560 kprintf("\trec_type = %04x\n", elm
->base
.rec_type
);
2561 kprintf("\tobj_type = %02x\n", elm
->base
.obj_type
);
2562 kprintf("\tbtype = %02x (%c)\n",
2564 (elm
->base
.btype
? elm
->base
.btype
: '?'));
2565 kprintf("\tlocalization = %02x\n", elm
->base
.localization
);
2568 case HAMMER_BTREE_TYPE_INTERNAL
:
2569 kprintf("\tsubtree_off = %016llx\n",
2570 elm
->internal
.subtree_offset
);
2572 case HAMMER_BTREE_TYPE_RECORD
:
2573 kprintf("\tdata_offset = %016llx\n", elm
->leaf
.data_offset
);
2574 kprintf("\tdata_len = %08x\n", elm
->leaf
.data_len
);
2575 kprintf("\tdata_crc = %08x\n", elm
->leaf
.data_crc
);