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.76 2008/08/06 15:38: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
)) {
302 * node pointer invalid after loop
308 if (hammer_debug_btree
) {
309 int i
= cursor
->index
;
310 hammer_btree_elm_t elm
= &cursor
->node
->ondisk
->elms
[i
];
311 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
313 elm
->internal
.base
.obj_id
,
314 elm
->internal
.base
.rec_type
,
315 elm
->internal
.base
.key
,
316 elm
->internal
.base
.localization
325 * We hit an internal element that we could skip as part of a mirroring
326 * scan. Calculate the entire range being skipped.
328 * It is important to include any gaps between the parent's left_bound
329 * and the node's left_bound, and same goes for the right side.
332 hammer_cursor_mirror_filter(hammer_cursor_t cursor
)
334 struct hammer_cmirror
*cmirror
;
335 hammer_node_ondisk_t ondisk
;
336 hammer_btree_elm_t elm
;
338 ondisk
= cursor
->node
->ondisk
;
339 cmirror
= cursor
->cmirror
;
342 * Calculate the skipped range
344 elm
= &ondisk
->elms
[cursor
->index
];
345 if (cursor
->index
== 0)
346 cmirror
->skip_beg
= *cursor
->left_bound
;
348 cmirror
->skip_beg
= elm
->internal
.base
;
349 while (cursor
->index
< ondisk
->count
) {
350 if (elm
->internal
.mirror_tid
>= cmirror
->mirror_tid
)
355 if (cursor
->index
== ondisk
->count
)
356 cmirror
->skip_end
= *cursor
->right_bound
;
358 cmirror
->skip_end
= elm
->internal
.base
;
361 * clip the returned result.
363 if (hammer_btree_cmp(&cmirror
->skip_beg
, &cursor
->key_beg
) < 0)
364 cmirror
->skip_beg
= cursor
->key_beg
;
365 if (hammer_btree_cmp(&cmirror
->skip_end
, &cursor
->key_end
) > 0)
366 cmirror
->skip_end
= cursor
->key_end
;
370 * Iterate in the reverse direction. This is used by the pruning code to
371 * avoid overlapping records.
374 hammer_btree_iterate_reverse(hammer_cursor_t cursor
)
376 hammer_node_ondisk_t node
;
377 hammer_btree_elm_t elm
;
382 /* mirror filtering not supported for reverse iteration */
383 KKASSERT ((cursor
->flags
& HAMMER_CURSOR_MIRROR_FILTERED
) == 0);
386 * Skip past the current record. For various reasons the cursor
387 * may end up set to -1 or set to point at the end of the current
388 * node. These cases must be addressed.
390 node
= cursor
->node
->ondisk
;
393 if (cursor
->index
!= -1 &&
394 (cursor
->flags
& HAMMER_CURSOR_ATEDISK
)) {
397 if (cursor
->index
== cursor
->node
->ondisk
->count
)
401 * Loop until an element is found or we are done.
404 ++hammer_stats_btree_iterations
;
405 hammer_flusher_clean_loose_ios(cursor
->trans
->hmp
);
408 * We iterate up the tree and then index over one element
409 * while we are at the last element in the current node.
411 if (cursor
->index
== -1) {
412 error
= hammer_cursor_up(cursor
);
414 cursor
->index
= 0; /* sanity */
417 /* reload stale pointer */
418 node
= cursor
->node
->ondisk
;
419 KKASSERT(cursor
->index
!= node
->count
);
425 * Check internal or leaf element. Determine if the record
426 * at the cursor has gone beyond the end of our range.
428 * We recurse down through internal nodes.
430 KKASSERT(cursor
->index
!= node
->count
);
431 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
432 elm
= &node
->elms
[cursor
->index
];
433 r
= hammer_btree_cmp(&cursor
->key_end
, &elm
[0].base
);
434 s
= hammer_btree_cmp(&cursor
->key_beg
, &elm
[1].base
);
435 if (hammer_debug_btree
) {
436 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
437 cursor
->node
->node_offset
,
439 elm
[0].internal
.base
.obj_id
,
440 elm
[0].internal
.base
.rec_type
,
441 elm
[0].internal
.base
.key
,
442 elm
[0].internal
.base
.localization
,
445 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
446 cursor
->node
->node_offset
,
448 elm
[1].internal
.base
.obj_id
,
449 elm
[1].internal
.base
.rec_type
,
450 elm
[1].internal
.base
.key
,
451 elm
[1].internal
.base
.localization
,
465 KKASSERT(elm
->internal
.subtree_offset
!= 0);
467 error
= hammer_cursor_down(cursor
);
470 KKASSERT(cursor
->index
== 0);
471 /* reload stale pointer */
472 node
= cursor
->node
->ondisk
;
474 /* this can assign -1 if the leaf was empty */
475 cursor
->index
= node
->count
- 1;
478 elm
= &node
->elms
[cursor
->index
];
479 s
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->base
);
480 if (hammer_debug_btree
) {
481 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
482 cursor
->node
->node_offset
,
484 (elm
[0].leaf
.base
.btype
?
485 elm
[0].leaf
.base
.btype
: '?'),
486 elm
[0].leaf
.base
.obj_id
,
487 elm
[0].leaf
.base
.rec_type
,
488 elm
[0].leaf
.base
.key
,
489 elm
[0].leaf
.base
.localization
,
498 switch(elm
->leaf
.base
.btype
) {
499 case HAMMER_BTREE_TYPE_RECORD
:
500 if ((cursor
->flags
& HAMMER_CURSOR_ASOF
) &&
501 hammer_btree_chkts(cursor
->asof
, &elm
->base
)) {
515 * node pointer invalid after loop
521 if (hammer_debug_btree
) {
522 int i
= cursor
->index
;
523 hammer_btree_elm_t elm
= &cursor
->node
->ondisk
->elms
[i
];
524 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
526 elm
->internal
.base
.obj_id
,
527 elm
->internal
.base
.rec_type
,
528 elm
->internal
.base
.key
,
529 elm
->internal
.base
.localization
538 * Lookup cursor->key_beg. 0 is returned on success, ENOENT if the entry
539 * could not be found, EDEADLK if inserting and a retry is needed, and a
540 * fatal error otherwise. When retrying, the caller must terminate the
541 * cursor and reinitialize it. EDEADLK cannot be returned if not inserting.
543 * The cursor is suitably positioned for a deletion on success, and suitably
544 * positioned for an insertion on ENOENT if HAMMER_CURSOR_INSERT was
547 * The cursor may begin anywhere, the search will traverse the tree in
548 * either direction to locate the requested element.
550 * Most of the logic implementing historical searches is handled here. We
551 * do an initial lookup with create_tid set to the asof TID. Due to the
552 * way records are laid out, a backwards iteration may be required if
553 * ENOENT is returned to locate the historical record. Here's the
556 * create_tid: 10 15 20
560 * Lets say we want to do a lookup AS-OF timestamp 17. We will traverse
561 * LEAF2 but the only record in LEAF2 has a create_tid of 18, which is
562 * not visible and thus causes ENOENT to be returned. We really need
563 * to check record 11 in LEAF1. If it also fails then the search fails
564 * (e.g. it might represent the range 11-16 and thus still not match our
565 * AS-OF timestamp of 17). Note that LEAF1 could be empty, requiring
566 * further iterations.
568 * If this case occurs btree_search() will set HAMMER_CURSOR_CREATE_CHECK
569 * and the cursor->create_check TID if an iteration might be needed.
570 * In the above example create_check would be set to 14.
573 hammer_btree_lookup(hammer_cursor_t cursor
)
577 KKASSERT ((cursor
->flags
& HAMMER_CURSOR_INSERT
) == 0 ||
578 cursor
->trans
->sync_lock_refs
> 0);
579 ++hammer_stats_btree_lookups
;
580 if (cursor
->flags
& HAMMER_CURSOR_ASOF
) {
581 KKASSERT((cursor
->flags
& HAMMER_CURSOR_INSERT
) == 0);
582 cursor
->key_beg
.create_tid
= cursor
->asof
;
584 cursor
->flags
&= ~HAMMER_CURSOR_CREATE_CHECK
;
585 error
= btree_search(cursor
, 0);
586 if (error
!= ENOENT
||
587 (cursor
->flags
& HAMMER_CURSOR_CREATE_CHECK
) == 0) {
590 * Stop if error other then ENOENT.
591 * Stop if ENOENT and not special case.
595 if (hammer_debug_btree
) {
596 kprintf("CREATE_CHECK %016llx\n",
597 cursor
->create_check
);
599 cursor
->key_beg
.create_tid
= cursor
->create_check
;
603 error
= btree_search(cursor
, 0);
606 error
= hammer_btree_extract(cursor
, cursor
->flags
);
611 * Execute the logic required to start an iteration. The first record
612 * located within the specified range is returned and iteration control
613 * flags are adjusted for successive hammer_btree_iterate() calls.
616 hammer_btree_first(hammer_cursor_t cursor
)
620 error
= hammer_btree_lookup(cursor
);
621 if (error
== ENOENT
) {
622 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
623 error
= hammer_btree_iterate(cursor
);
625 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
630 * Similarly but for an iteration in the reverse direction.
632 * Set ATEDISK when iterating backwards to skip the current entry,
633 * which after an ENOENT lookup will be pointing beyond our end point.
636 hammer_btree_last(hammer_cursor_t cursor
)
638 struct hammer_base_elm save
;
641 save
= cursor
->key_beg
;
642 cursor
->key_beg
= cursor
->key_end
;
643 error
= hammer_btree_lookup(cursor
);
644 cursor
->key_beg
= save
;
645 if (error
== ENOENT
||
646 (cursor
->flags
& HAMMER_CURSOR_END_INCLUSIVE
) == 0) {
647 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
648 error
= hammer_btree_iterate_reverse(cursor
);
650 cursor
->flags
|= HAMMER_CURSOR_ATEDISK
;
655 * Extract the record and/or data associated with the cursor's current
656 * position. Any prior record or data stored in the cursor is replaced.
657 * The cursor must be positioned at a leaf node.
659 * NOTE: All extractions occur at the leaf of the B-Tree.
662 hammer_btree_extract(hammer_cursor_t cursor
, int flags
)
665 hammer_node_ondisk_t node
;
666 hammer_btree_elm_t elm
;
667 hammer_off_t data_off
;
672 * The case where the data reference resolves to the same buffer
673 * as the record reference must be handled.
675 node
= cursor
->node
->ondisk
;
676 elm
= &node
->elms
[cursor
->index
];
678 hmp
= cursor
->node
->hmp
;
681 * There is nothing to extract for an internal element.
683 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
)
687 * Only record types have data.
689 KKASSERT(node
->type
== HAMMER_BTREE_TYPE_LEAF
);
690 cursor
->leaf
= &elm
->leaf
;
692 if ((flags
& HAMMER_CURSOR_GET_DATA
) == 0)
694 if (elm
->leaf
.base
.btype
!= HAMMER_BTREE_TYPE_RECORD
)
696 data_off
= elm
->leaf
.data_offset
;
697 data_len
= elm
->leaf
.data_len
;
704 KKASSERT(data_len
>= 0 && data_len
<= HAMMER_XBUFSIZE
);
705 cursor
->data
= hammer_bread_ext(hmp
, data_off
, data_len
,
706 &error
, &cursor
->data_buffer
);
707 if (hammer_crc_test_leaf(cursor
->data
, &elm
->leaf
) == 0) {
708 kprintf("CRC DATA @ %016llx/%d FAILED\n",
709 elm
->leaf
.data_offset
, elm
->leaf
.data_len
);
710 Debugger("CRC FAILED: DATA");
717 * Insert a leaf element into the B-Tree at the current cursor position.
718 * The cursor is positioned such that the element at and beyond the cursor
719 * are shifted to make room for the new record.
721 * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT
722 * flag set and that call must return ENOENT before this function can be
725 * The caller may depend on the cursor's exclusive lock after return to
726 * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE).
728 * ENOSPC is returned if there is no room to insert a new record.
731 hammer_btree_insert(hammer_cursor_t cursor
, hammer_btree_leaf_elm_t elm
,
734 hammer_node_ondisk_t node
;
739 if ((error
= hammer_cursor_upgrade_node(cursor
)) != 0)
741 ++hammer_stats_btree_inserts
;
744 * Insert the element at the leaf node and update the count in the
745 * parent. It is possible for parent to be NULL, indicating that
746 * the filesystem's ROOT B-Tree node is a leaf itself, which is
747 * possible. The root inode can never be deleted so the leaf should
750 * Remember that the right-hand boundary is not included in the
753 hammer_modify_node_all(cursor
->trans
, cursor
->node
);
754 node
= cursor
->node
->ondisk
;
756 KKASSERT(elm
->base
.btype
!= 0);
757 KKASSERT(node
->type
== HAMMER_BTREE_TYPE_LEAF
);
758 KKASSERT(node
->count
< HAMMER_BTREE_LEAF_ELMS
);
759 if (i
!= node
->count
) {
760 bcopy(&node
->elms
[i
], &node
->elms
[i
+1],
761 (node
->count
- i
) * sizeof(*elm
));
763 node
->elms
[i
].leaf
= *elm
;
765 hammer_cursor_inserted_element(cursor
->node
, i
);
768 * Update the leaf node's aggregate mirror_tid for mirroring
771 if (node
->mirror_tid
< elm
->base
.delete_tid
) {
772 node
->mirror_tid
= elm
->base
.delete_tid
;
775 if (node
->mirror_tid
< elm
->base
.create_tid
) {
776 node
->mirror_tid
= elm
->base
.create_tid
;
779 hammer_modify_node_done(cursor
->node
);
782 * Debugging sanity checks.
784 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
->base
) <= 0);
785 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
->base
) > 0);
787 KKASSERT(hammer_btree_cmp(&node
->elms
[i
-1].leaf
.base
, &elm
->base
) < 0);
789 if (i
!= node
->count
- 1)
790 KKASSERT(hammer_btree_cmp(&node
->elms
[i
+1].leaf
.base
, &elm
->base
) > 0);
796 * Delete a record from the B-Tree at the current cursor position.
797 * The cursor is positioned such that the current element is the one
800 * On return the cursor will be positioned after the deleted element and
801 * MAY point to an internal node. It will be suitable for the continuation
802 * of an iteration but not for an insertion or deletion.
804 * Deletions will attempt to partially rebalance the B-Tree in an upward
805 * direction, but will terminate rather then deadlock. Empty internal nodes
806 * are never allowed by a deletion which deadlocks may end up giving us an
807 * empty leaf. The pruner will clean up and rebalance the tree.
809 * This function can return EDEADLK, requiring the caller to retry the
810 * operation after clearing the deadlock.
813 hammer_btree_delete(hammer_cursor_t cursor
)
815 hammer_node_ondisk_t ondisk
;
817 hammer_node_t parent
;
821 KKASSERT (cursor
->trans
->sync_lock_refs
> 0);
822 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
824 ++hammer_stats_btree_deletes
;
827 * Delete the element from the leaf node.
829 * Remember that leaf nodes do not have boundaries.
832 ondisk
= node
->ondisk
;
835 KKASSERT(ondisk
->type
== HAMMER_BTREE_TYPE_LEAF
);
836 KKASSERT(i
>= 0 && i
< ondisk
->count
);
837 hammer_modify_node_all(cursor
->trans
, node
);
838 if (i
+ 1 != ondisk
->count
) {
839 bcopy(&ondisk
->elms
[i
+1], &ondisk
->elms
[i
],
840 (ondisk
->count
- i
- 1) * sizeof(ondisk
->elms
[0]));
843 hammer_modify_node_done(node
);
844 hammer_cursor_deleted_element(node
, i
);
847 * Validate local parent
849 if (ondisk
->parent
) {
850 parent
= cursor
->parent
;
852 KKASSERT(parent
!= NULL
);
853 KKASSERT(parent
->node_offset
== ondisk
->parent
);
857 * If the leaf becomes empty it must be detached from the parent,
858 * potentially recursing through to the filesystem root.
860 * This may reposition the cursor at one of the parent's of the
863 * Ignore deadlock errors, that simply means that btree_remove
864 * was unable to recurse and had to leave us with an empty leaf.
866 KKASSERT(cursor
->index
<= ondisk
->count
);
867 if (ondisk
->count
== 0) {
868 error
= btree_remove(cursor
);
869 if (error
== EDEADLK
)
874 KKASSERT(cursor
->parent
== NULL
||
875 cursor
->parent_index
< cursor
->parent
->ondisk
->count
);
880 * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE
882 * Search the filesystem B-Tree for cursor->key_beg, return the matching node.
884 * The search can begin ANYWHERE in the B-Tree. As a first step the search
885 * iterates up the tree as necessary to properly position itself prior to
886 * actually doing the sarch.
888 * INSERTIONS: The search will split full nodes and leaves on its way down
889 * and guarentee that the leaf it ends up on is not full. If we run out
890 * of space the search continues to the leaf (to position the cursor for
891 * the spike), but ENOSPC is returned.
893 * The search is only guarenteed to end up on a leaf if an error code of 0
894 * is returned, or if inserting and an error code of ENOENT is returned.
895 * Otherwise it can stop at an internal node. On success a search returns
898 * COMPLEXITY WARNING! This is the core B-Tree search code for the entire
899 * filesystem, and it is not simple code. Please note the following facts:
901 * - Internal node recursions have a boundary on the left AND right. The
902 * right boundary is non-inclusive. The create_tid is a generic part
903 * of the key for internal nodes.
905 * - Leaf nodes contain terminal elements only now.
907 * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a
908 * historical search. ASOF and INSERT are mutually exclusive. When
909 * doing an as-of lookup btree_search() checks for a right-edge boundary
910 * case. If while recursing down the left-edge differs from the key
911 * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along
912 * with cursor->create_check. This is used by btree_lookup() to iterate.
913 * The iteration backwards because as-of searches can wind up going
914 * down the wrong branch of the B-Tree.
918 btree_search(hammer_cursor_t cursor
, int flags
)
920 hammer_node_ondisk_t node
;
921 hammer_btree_elm_t elm
;
928 flags
|= cursor
->flags
;
929 ++hammer_stats_btree_searches
;
931 if (hammer_debug_btree
) {
932 kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx lo=%02x (td = %p)\n",
933 cursor
->node
->node_offset
,
935 cursor
->key_beg
.obj_id
,
936 cursor
->key_beg
.rec_type
,
938 cursor
->key_beg
.create_tid
,
939 cursor
->key_beg
.localization
,
943 kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n",
944 cursor
->parent
->node_offset
, cursor
->parent_index
,
945 cursor
->left_bound
->obj_id
,
946 cursor
->parent
->ondisk
->elms
[cursor
->parent_index
].internal
.base
.obj_id
,
947 cursor
->right_bound
->obj_id
,
948 cursor
->parent
->ondisk
->elms
[cursor
->parent_index
+1].internal
.base
.obj_id
,
950 &cursor
->parent
->ondisk
->elms
[cursor
->parent_index
],
952 &cursor
->parent
->ondisk
->elms
[cursor
->parent_index
+1]
957 * Move our cursor up the tree until we find a node whos range covers
958 * the key we are trying to locate.
960 * The left bound is inclusive, the right bound is non-inclusive.
961 * It is ok to cursor up too far.
964 r
= hammer_btree_cmp(&cursor
->key_beg
, cursor
->left_bound
);
965 s
= hammer_btree_cmp(&cursor
->key_beg
, cursor
->right_bound
);
968 KKASSERT(cursor
->parent
);
969 ++hammer_stats_btree_iterations
;
970 error
= hammer_cursor_up(cursor
);
976 * The delete-checks below are based on node, not parent. Set the
977 * initial delete-check based on the parent.
980 KKASSERT(cursor
->left_bound
->create_tid
!= 1);
981 cursor
->create_check
= cursor
->left_bound
->create_tid
- 1;
982 cursor
->flags
|= HAMMER_CURSOR_CREATE_CHECK
;
986 * We better have ended up with a node somewhere.
988 KKASSERT(cursor
->node
!= NULL
);
991 * If we are inserting we can't start at a full node if the parent
992 * is also full (because there is no way to split the node),
993 * continue running up the tree until the requirement is satisfied
994 * or we hit the root of the filesystem.
996 * (If inserting we aren't doing an as-of search so we don't have
997 * to worry about create_check).
999 while ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0) {
1000 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
1001 if (btree_node_is_full(cursor
->node
->ondisk
) == 0)
1004 if (btree_node_is_full(cursor
->node
->ondisk
) ==0)
1007 if (cursor
->node
->ondisk
->parent
== 0 ||
1008 cursor
->parent
->ondisk
->count
!= HAMMER_BTREE_INT_ELMS
) {
1011 ++hammer_stats_btree_iterations
;
1012 error
= hammer_cursor_up(cursor
);
1013 /* node may have become stale */
1019 * Push down through internal nodes to locate the requested key.
1021 node
= cursor
->node
->ondisk
;
1022 while (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
1024 * Scan the node to find the subtree index to push down into.
1025 * We go one-past, then back-up.
1027 * We must proactively remove deleted elements which may
1028 * have been left over from a deadlocked btree_remove().
1030 * The left and right boundaries are included in the loop
1031 * in order to detect edge cases.
1033 * If the separator only differs by create_tid (r == 1)
1034 * and we are doing an as-of search, we may end up going
1035 * down a branch to the left of the one containing the
1036 * desired key. This requires numerous special cases.
1038 ++hammer_stats_btree_iterations
;
1039 if (hammer_debug_btree
) {
1040 kprintf("SEARCH-I %016llx count=%d\n",
1041 cursor
->node
->node_offset
,
1046 * Try to shortcut the search before dropping into the
1047 * linear loop. Locate the first node where r <= 1.
1049 i
= hammer_btree_search_node(&cursor
->key_beg
, node
);
1050 while (i
<= node
->count
) {
1051 ++hammer_stats_btree_elements
;
1052 elm
= &node
->elms
[i
];
1053 r
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->base
);
1054 if (hammer_debug_btree
> 2) {
1055 kprintf(" IELM %p %d r=%d\n",
1056 &node
->elms
[i
], i
, r
);
1061 KKASSERT(elm
->base
.create_tid
!= 1);
1062 cursor
->create_check
= elm
->base
.create_tid
- 1;
1063 cursor
->flags
|= HAMMER_CURSOR_CREATE_CHECK
;
1067 if (hammer_debug_btree
) {
1068 kprintf("SEARCH-I preI=%d/%d r=%d\n",
1073 * These cases occur when the parent's idea of the boundary
1074 * is wider then the child's idea of the boundary, and
1075 * require special handling. If not inserting we can
1076 * terminate the search early for these cases but the
1077 * child's boundaries cannot be unconditionally modified.
1081 * If i == 0 the search terminated to the LEFT of the
1082 * left_boundary but to the RIGHT of the parent's left
1087 elm
= &node
->elms
[0];
1090 * If we aren't inserting we can stop here.
1092 if ((flags
& (HAMMER_CURSOR_INSERT
|
1093 HAMMER_CURSOR_PRUNING
)) == 0) {
1099 * Correct a left-hand boundary mismatch.
1101 * We can only do this if we can upgrade the lock,
1102 * and synchronized as a background cursor (i.e.
1103 * inserting or pruning).
1105 * WARNING: We can only do this if inserting, i.e.
1106 * we are running on the backend.
1108 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1110 KKASSERT(cursor
->flags
& HAMMER_CURSOR_BACKEND
);
1111 hammer_modify_node_field(cursor
->trans
, cursor
->node
,
1113 save
= node
->elms
[0].base
.btype
;
1114 node
->elms
[0].base
= *cursor
->left_bound
;
1115 node
->elms
[0].base
.btype
= save
;
1116 hammer_modify_node_done(cursor
->node
);
1117 } else if (i
== node
->count
+ 1) {
1119 * If i == node->count + 1 the search terminated to
1120 * the RIGHT of the right boundary but to the LEFT
1121 * of the parent's right boundary. If we aren't
1122 * inserting we can stop here.
1124 * Note that the last element in this case is
1125 * elms[i-2] prior to adjustments to 'i'.
1128 if ((flags
& (HAMMER_CURSOR_INSERT
|
1129 HAMMER_CURSOR_PRUNING
)) == 0) {
1135 * Correct a right-hand boundary mismatch.
1136 * (actual push-down record is i-2 prior to
1137 * adjustments to i).
1139 * We can only do this if we can upgrade the lock,
1140 * and synchronized as a background cursor (i.e.
1141 * inserting or pruning).
1143 * WARNING: We can only do this if inserting, i.e.
1144 * we are running on the backend.
1146 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1148 elm
= &node
->elms
[i
];
1149 KKASSERT(cursor
->flags
& HAMMER_CURSOR_BACKEND
);
1150 hammer_modify_node(cursor
->trans
, cursor
->node
,
1151 &elm
->base
, sizeof(elm
->base
));
1152 elm
->base
= *cursor
->right_bound
;
1153 hammer_modify_node_done(cursor
->node
);
1157 * The push-down index is now i - 1. If we had
1158 * terminated on the right boundary this will point
1159 * us at the last element.
1164 elm
= &node
->elms
[i
];
1166 if (hammer_debug_btree
) {
1167 kprintf("RESULT-I %016llx[%d] %016llx %02x "
1168 "key=%016llx cre=%016llx lo=%02x\n",
1169 cursor
->node
->node_offset
,
1171 elm
->internal
.base
.obj_id
,
1172 elm
->internal
.base
.rec_type
,
1173 elm
->internal
.base
.key
,
1174 elm
->internal
.base
.create_tid
,
1175 elm
->internal
.base
.localization
1180 * We better have a valid subtree offset.
1182 KKASSERT(elm
->internal
.subtree_offset
!= 0);
1185 * Handle insertion and deletion requirements.
1187 * If inserting split full nodes. The split code will
1188 * adjust cursor->node and cursor->index if the current
1189 * index winds up in the new node.
1191 * If inserting and a left or right edge case was detected,
1192 * we cannot correct the left or right boundary and must
1193 * prepend and append an empty leaf node in order to make
1194 * the boundary correction.
1196 * If we run out of space we set enospc and continue on
1197 * to a leaf to provide the spike code with a good point
1200 if ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0) {
1201 if (btree_node_is_full(node
)) {
1202 error
= btree_split_internal(cursor
);
1204 if (error
!= ENOSPC
)
1209 * reload stale pointers
1212 node
= cursor
->node
->ondisk
;
1217 * Push down (push into new node, existing node becomes
1218 * the parent) and continue the search.
1220 error
= hammer_cursor_down(cursor
);
1221 /* node may have become stale */
1224 node
= cursor
->node
->ondisk
;
1228 * We are at a leaf, do a linear search of the key array.
1230 * On success the index is set to the matching element and 0
1233 * On failure the index is set to the insertion point and ENOENT
1236 * Boundaries are not stored in leaf nodes, so the index can wind
1237 * up to the left of element 0 (index == 0) or past the end of
1238 * the array (index == node->count). It is also possible that the
1239 * leaf might be empty.
1241 ++hammer_stats_btree_iterations
;
1242 KKASSERT (node
->type
== HAMMER_BTREE_TYPE_LEAF
);
1243 KKASSERT(node
->count
<= HAMMER_BTREE_LEAF_ELMS
);
1244 if (hammer_debug_btree
) {
1245 kprintf("SEARCH-L %016llx count=%d\n",
1246 cursor
->node
->node_offset
,
1251 * Try to shortcut the search before dropping into the
1252 * linear loop. Locate the first node where r <= 1.
1254 i
= hammer_btree_search_node(&cursor
->key_beg
, node
);
1255 while (i
< node
->count
) {
1256 ++hammer_stats_btree_elements
;
1257 elm
= &node
->elms
[i
];
1259 r
= hammer_btree_cmp(&cursor
->key_beg
, &elm
->leaf
.base
);
1261 if (hammer_debug_btree
> 1)
1262 kprintf(" ELM %p %d r=%d\n", &node
->elms
[i
], i
, r
);
1265 * We are at a record element. Stop if we've flipped past
1266 * key_beg, not counting the create_tid test. Allow the
1267 * r == 1 case (key_beg > element but differs only by its
1268 * create_tid) to fall through to the AS-OF check.
1270 KKASSERT (elm
->leaf
.base
.btype
== HAMMER_BTREE_TYPE_RECORD
);
1280 * Check our as-of timestamp against the element.
1282 if (flags
& HAMMER_CURSOR_ASOF
) {
1283 if (hammer_btree_chkts(cursor
->asof
,
1284 &node
->elms
[i
].base
) != 0) {
1290 if (r
> 0) { /* can only be +1 */
1298 if (hammer_debug_btree
) {
1299 kprintf("RESULT-L %016llx[%d] (SUCCESS)\n",
1300 cursor
->node
->node_offset
, i
);
1306 * The search of the leaf node failed. i is the insertion point.
1309 if (hammer_debug_btree
) {
1310 kprintf("RESULT-L %016llx[%d] (FAILED)\n",
1311 cursor
->node
->node_offset
, i
);
1315 * No exact match was found, i is now at the insertion point.
1317 * If inserting split a full leaf before returning. This
1318 * may have the side effect of adjusting cursor->node and
1322 if ((flags
& HAMMER_CURSOR_INSERT
) && enospc
== 0 &&
1323 btree_node_is_full(node
)) {
1324 error
= btree_split_leaf(cursor
);
1326 if (error
!= ENOSPC
)
1331 * reload stale pointers
1335 node = &cursor->node->internal;
1340 * We reached a leaf but did not find the key we were looking for.
1341 * If this is an insert we will be properly positioned for an insert
1342 * (ENOENT) or spike (ENOSPC) operation.
1344 error
= enospc
? ENOSPC
: ENOENT
;
1350 * Heuristical search for the first element whos comparison is <= 1. May
1351 * return an index whos compare result is > 1 but may only return an index
1352 * whos compare result is <= 1 if it is the first element with that result.
1355 hammer_btree_search_node(hammer_base_elm_t elm
, hammer_node_ondisk_t node
)
1363 * Don't bother if the node does not have very many elements
1368 i
= b
+ (s
- b
) / 2;
1369 ++hammer_stats_btree_elements
;
1370 r
= hammer_btree_cmp(elm
, &node
->elms
[i
].leaf
.base
);
1381 /************************************************************************
1382 * SPLITTING AND MERGING *
1383 ************************************************************************
1385 * These routines do all the dirty work required to split and merge nodes.
1389 * Split an internal node into two nodes and move the separator at the split
1390 * point to the parent.
1392 * (cursor->node, cursor->index) indicates the element the caller intends
1393 * to push into. We will adjust node and index if that element winds
1394 * up in the split node.
1396 * If we are at the root of the filesystem a new root must be created with
1397 * two elements, one pointing to the original root and one pointing to the
1398 * newly allocated split node.
1402 btree_split_internal(hammer_cursor_t cursor
)
1404 hammer_node_ondisk_t ondisk
;
1406 hammer_node_t parent
;
1407 hammer_node_t new_node
;
1408 hammer_btree_elm_t elm
;
1409 hammer_btree_elm_t parent_elm
;
1410 hammer_node_locklist_t locklist
= NULL
;
1411 hammer_mount_t hmp
= cursor
->trans
->hmp
;
1417 const int esize
= sizeof(*elm
);
1419 error
= hammer_btree_lock_children(cursor
, &locklist
);
1422 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1424 ++hammer_stats_btree_splits
;
1427 * We are splitting but elms[split] will be promoted to the parent,
1428 * leaving the right hand node with one less element. If the
1429 * insertion point will be on the left-hand side adjust the split
1430 * point to give the right hand side one additional node.
1432 node
= cursor
->node
;
1433 ondisk
= node
->ondisk
;
1434 split
= (ondisk
->count
+ 1) / 2;
1435 if (cursor
->index
<= split
)
1439 * If we are at the root of the filesystem, create a new root node
1440 * with 1 element and split normally. Avoid making major
1441 * modifications until we know the whole operation will work.
1443 if (ondisk
->parent
== 0) {
1444 parent
= hammer_alloc_btree(cursor
->trans
, &error
);
1447 hammer_lock_ex(&parent
->lock
);
1448 hammer_modify_node_noundo(cursor
->trans
, parent
);
1449 ondisk
= parent
->ondisk
;
1452 ondisk
->mirror_tid
= node
->ondisk
->mirror_tid
;
1453 ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1454 ondisk
->elms
[0].base
= hmp
->root_btree_beg
;
1455 ondisk
->elms
[0].base
.btype
= node
->ondisk
->type
;
1456 ondisk
->elms
[0].internal
.subtree_offset
= node
->node_offset
;
1457 ondisk
->elms
[1].base
= hmp
->root_btree_end
;
1458 hammer_modify_node_done(parent
);
1459 /* ondisk->elms[1].base.btype - not used */
1461 parent_index
= 0; /* index of current node in parent */
1464 parent
= cursor
->parent
;
1465 parent_index
= cursor
->parent_index
;
1469 * Split node into new_node at the split point.
1471 * B O O O P N N B <-- P = node->elms[split]
1472 * 0 1 2 3 4 5 6 <-- subtree indices
1477 * B O O O B B N N B <--- inner boundary points are 'P'
1481 new_node
= hammer_alloc_btree(cursor
->trans
, &error
);
1482 if (new_node
== NULL
) {
1484 hammer_unlock(&parent
->lock
);
1485 hammer_delete_node(cursor
->trans
, parent
);
1486 hammer_rel_node(parent
);
1490 hammer_lock_ex(&new_node
->lock
);
1493 * Create the new node. P becomes the left-hand boundary in the
1494 * new node. Copy the right-hand boundary as well.
1496 * elm is the new separator.
1498 hammer_modify_node_noundo(cursor
->trans
, new_node
);
1499 hammer_modify_node_all(cursor
->trans
, node
);
1500 ondisk
= node
->ondisk
;
1501 elm
= &ondisk
->elms
[split
];
1502 bcopy(elm
, &new_node
->ondisk
->elms
[0],
1503 (ondisk
->count
- split
+ 1) * esize
);
1504 new_node
->ondisk
->count
= ondisk
->count
- split
;
1505 new_node
->ondisk
->parent
= parent
->node_offset
;
1506 new_node
->ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1507 new_node
->ondisk
->mirror_tid
= ondisk
->mirror_tid
;
1508 KKASSERT(ondisk
->type
== new_node
->ondisk
->type
);
1509 hammer_cursor_split_node(node
, new_node
, split
);
1512 * Cleanup the original node. Elm (P) becomes the new boundary,
1513 * its subtree_offset was moved to the new node. If we had created
1514 * a new root its parent pointer may have changed.
1516 elm
->internal
.subtree_offset
= 0;
1517 ondisk
->count
= split
;
1520 * Insert the separator into the parent, fixup the parent's
1521 * reference to the original node, and reference the new node.
1522 * The separator is P.
1524 * Remember that base.count does not include the right-hand boundary.
1526 hammer_modify_node_all(cursor
->trans
, parent
);
1527 ondisk
= parent
->ondisk
;
1528 KKASSERT(ondisk
->count
!= HAMMER_BTREE_INT_ELMS
);
1529 parent_elm
= &ondisk
->elms
[parent_index
+1];
1530 bcopy(parent_elm
, parent_elm
+ 1,
1531 (ondisk
->count
- parent_index
) * esize
);
1532 parent_elm
->internal
.base
= elm
->base
; /* separator P */
1533 parent_elm
->internal
.base
.btype
= new_node
->ondisk
->type
;
1534 parent_elm
->internal
.subtree_offset
= new_node
->node_offset
;
1535 parent_elm
->internal
.mirror_tid
= new_node
->ondisk
->mirror_tid
;
1537 hammer_modify_node_done(parent
);
1538 hammer_cursor_inserted_element(parent
, parent_index
+ 1);
1541 * The children of new_node need their parent pointer set to new_node.
1542 * The children have already been locked by
1543 * hammer_btree_lock_children().
1545 for (i
= 0; i
< new_node
->ondisk
->count
; ++i
) {
1546 elm
= &new_node
->ondisk
->elms
[i
];
1547 error
= btree_set_parent(cursor
->trans
, new_node
, elm
);
1549 panic("btree_split_internal: btree-fixup problem");
1552 hammer_modify_node_done(new_node
);
1555 * The filesystem's root B-Tree pointer may have to be updated.
1558 hammer_volume_t volume
;
1560 volume
= hammer_get_root_volume(hmp
, &error
);
1561 KKASSERT(error
== 0);
1563 hammer_modify_volume_field(cursor
->trans
, volume
,
1565 volume
->ondisk
->vol0_btree_root
= parent
->node_offset
;
1566 hammer_modify_volume_done(volume
);
1567 node
->ondisk
->parent
= parent
->node_offset
;
1568 if (cursor
->parent
) {
1569 hammer_unlock(&cursor
->parent
->lock
);
1570 hammer_rel_node(cursor
->parent
);
1572 cursor
->parent
= parent
; /* lock'd and ref'd */
1573 hammer_rel_volume(volume
, 0);
1575 hammer_modify_node_done(node
);
1578 * Ok, now adjust the cursor depending on which element the original
1579 * index was pointing at. If we are >= the split point the push node
1580 * is now in the new node.
1582 * NOTE: If we are at the split point itself we cannot stay with the
1583 * original node because the push index will point at the right-hand
1584 * boundary, which is illegal.
1586 * NOTE: The cursor's parent or parent_index must be adjusted for
1587 * the case where a new parent (new root) was created, and the case
1588 * where the cursor is now pointing at the split node.
1590 if (cursor
->index
>= split
) {
1591 cursor
->parent_index
= parent_index
+ 1;
1592 cursor
->index
-= split
;
1593 hammer_unlock(&cursor
->node
->lock
);
1594 hammer_rel_node(cursor
->node
);
1595 cursor
->node
= new_node
; /* locked and ref'd */
1597 cursor
->parent_index
= parent_index
;
1598 hammer_unlock(&new_node
->lock
);
1599 hammer_rel_node(new_node
);
1603 * Fixup left and right bounds
1605 parent_elm
= &parent
->ondisk
->elms
[cursor
->parent_index
];
1606 cursor
->left_bound
= &parent_elm
[0].internal
.base
;
1607 cursor
->right_bound
= &parent_elm
[1].internal
.base
;
1608 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1609 &cursor
->node
->ondisk
->elms
[0].internal
.base
) <= 0);
1610 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1611 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
].internal
.base
) >= 0);
1614 hammer_btree_unlock_children(&locklist
);
1615 hammer_cursor_downgrade(cursor
);
1620 * Same as the above, but splits a full leaf node.
1626 btree_split_leaf(hammer_cursor_t cursor
)
1628 hammer_node_ondisk_t ondisk
;
1629 hammer_node_t parent
;
1632 hammer_node_t new_leaf
;
1633 hammer_btree_elm_t elm
;
1634 hammer_btree_elm_t parent_elm
;
1635 hammer_base_elm_t mid_boundary
;
1640 const size_t esize
= sizeof(*elm
);
1642 if ((error
= hammer_cursor_upgrade(cursor
)) != 0)
1644 ++hammer_stats_btree_splits
;
1646 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1647 &cursor
->node
->ondisk
->elms
[0].leaf
.base
) <= 0);
1648 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1649 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
-1].leaf
.base
) > 0);
1652 * Calculate the split point. If the insertion point will be on
1653 * the left-hand side adjust the split point to give the right
1654 * hand side one additional node.
1656 * Spikes are made up of two leaf elements which cannot be
1659 leaf
= cursor
->node
;
1660 ondisk
= leaf
->ondisk
;
1661 split
= (ondisk
->count
+ 1) / 2;
1662 if (cursor
->index
<= split
)
1667 elm
= &ondisk
->elms
[split
];
1669 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
[-1].leaf
.base
) <= 0);
1670 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &elm
->leaf
.base
) <= 0);
1671 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
->leaf
.base
) > 0);
1672 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &elm
[1].leaf
.base
) > 0);
1675 * If we are at the root of the tree, create a new root node with
1676 * 1 element and split normally. Avoid making major modifications
1677 * until we know the whole operation will work.
1679 if (ondisk
->parent
== 0) {
1680 parent
= hammer_alloc_btree(cursor
->trans
, &error
);
1683 hammer_lock_ex(&parent
->lock
);
1684 hammer_modify_node_noundo(cursor
->trans
, parent
);
1685 ondisk
= parent
->ondisk
;
1688 ondisk
->mirror_tid
= leaf
->ondisk
->mirror_tid
;
1689 ondisk
->type
= HAMMER_BTREE_TYPE_INTERNAL
;
1690 ondisk
->elms
[0].base
= hmp
->root_btree_beg
;
1691 ondisk
->elms
[0].base
.btype
= leaf
->ondisk
->type
;
1692 ondisk
->elms
[0].internal
.subtree_offset
= leaf
->node_offset
;
1693 ondisk
->elms
[1].base
= hmp
->root_btree_end
;
1694 /* ondisk->elms[1].base.btype = not used */
1695 hammer_modify_node_done(parent
);
1697 parent_index
= 0; /* insertion point in parent */
1700 parent
= cursor
->parent
;
1701 parent_index
= cursor
->parent_index
;
1705 * Split leaf into new_leaf at the split point. Select a separator
1706 * value in-between the two leafs but with a bent towards the right
1707 * leaf since comparisons use an 'elm >= separator' inequality.
1716 new_leaf
= hammer_alloc_btree(cursor
->trans
, &error
);
1717 if (new_leaf
== NULL
) {
1719 hammer_unlock(&parent
->lock
);
1720 hammer_delete_node(cursor
->trans
, parent
);
1721 hammer_rel_node(parent
);
1725 hammer_lock_ex(&new_leaf
->lock
);
1728 * Create the new node and copy the leaf elements from the split
1729 * point on to the new node.
1731 hammer_modify_node_all(cursor
->trans
, leaf
);
1732 hammer_modify_node_noundo(cursor
->trans
, new_leaf
);
1733 ondisk
= leaf
->ondisk
;
1734 elm
= &ondisk
->elms
[split
];
1735 bcopy(elm
, &new_leaf
->ondisk
->elms
[0], (ondisk
->count
- split
) * esize
);
1736 new_leaf
->ondisk
->count
= ondisk
->count
- split
;
1737 new_leaf
->ondisk
->parent
= parent
->node_offset
;
1738 new_leaf
->ondisk
->type
= HAMMER_BTREE_TYPE_LEAF
;
1739 new_leaf
->ondisk
->mirror_tid
= ondisk
->mirror_tid
;
1740 KKASSERT(ondisk
->type
== new_leaf
->ondisk
->type
);
1741 hammer_modify_node_done(new_leaf
);
1742 hammer_cursor_split_node(leaf
, new_leaf
, split
);
1745 * Cleanup the original node. Because this is a leaf node and
1746 * leaf nodes do not have a right-hand boundary, there
1747 * aren't any special edge cases to clean up. We just fixup the
1750 ondisk
->count
= split
;
1753 * Insert the separator into the parent, fixup the parent's
1754 * reference to the original node, and reference the new node.
1755 * The separator is P.
1757 * Remember that base.count does not include the right-hand boundary.
1758 * We are copying parent_index+1 to parent_index+2, not +0 to +1.
1760 hammer_modify_node_all(cursor
->trans
, parent
);
1761 ondisk
= parent
->ondisk
;
1762 KKASSERT(split
!= 0);
1763 KKASSERT(ondisk
->count
!= HAMMER_BTREE_INT_ELMS
);
1764 parent_elm
= &ondisk
->elms
[parent_index
+1];
1765 bcopy(parent_elm
, parent_elm
+ 1,
1766 (ondisk
->count
- parent_index
) * esize
);
1768 hammer_make_separator(&elm
[-1].base
, &elm
[0].base
, &parent_elm
->base
);
1769 parent_elm
->internal
.base
.btype
= new_leaf
->ondisk
->type
;
1770 parent_elm
->internal
.subtree_offset
= new_leaf
->node_offset
;
1771 parent_elm
->internal
.mirror_tid
= new_leaf
->ondisk
->mirror_tid
;
1772 mid_boundary
= &parent_elm
->base
;
1774 hammer_modify_node_done(parent
);
1775 hammer_cursor_inserted_element(parent
, parent_index
+ 1);
1778 * The filesystem's root B-Tree pointer may have to be updated.
1781 hammer_volume_t volume
;
1783 volume
= hammer_get_root_volume(hmp
, &error
);
1784 KKASSERT(error
== 0);
1786 hammer_modify_volume_field(cursor
->trans
, volume
,
1788 volume
->ondisk
->vol0_btree_root
= parent
->node_offset
;
1789 hammer_modify_volume_done(volume
);
1790 leaf
->ondisk
->parent
= parent
->node_offset
;
1791 if (cursor
->parent
) {
1792 hammer_unlock(&cursor
->parent
->lock
);
1793 hammer_rel_node(cursor
->parent
);
1795 cursor
->parent
= parent
; /* lock'd and ref'd */
1796 hammer_rel_volume(volume
, 0);
1798 hammer_modify_node_done(leaf
);
1801 * Ok, now adjust the cursor depending on which element the original
1802 * index was pointing at. If we are >= the split point the push node
1803 * is now in the new node.
1805 * NOTE: If we are at the split point itself we need to select the
1806 * old or new node based on where key_beg's insertion point will be.
1807 * If we pick the wrong side the inserted element will wind up in
1808 * the wrong leaf node and outside that node's bounds.
1810 if (cursor
->index
> split
||
1811 (cursor
->index
== split
&&
1812 hammer_btree_cmp(&cursor
->key_beg
, mid_boundary
) >= 0)) {
1813 cursor
->parent_index
= parent_index
+ 1;
1814 cursor
->index
-= split
;
1815 hammer_unlock(&cursor
->node
->lock
);
1816 hammer_rel_node(cursor
->node
);
1817 cursor
->node
= new_leaf
;
1819 cursor
->parent_index
= parent_index
;
1820 hammer_unlock(&new_leaf
->lock
);
1821 hammer_rel_node(new_leaf
);
1825 * Fixup left and right bounds
1827 parent_elm
= &parent
->ondisk
->elms
[cursor
->parent_index
];
1828 cursor
->left_bound
= &parent_elm
[0].internal
.base
;
1829 cursor
->right_bound
= &parent_elm
[1].internal
.base
;
1832 * Assert that the bounds are correct.
1834 KKASSERT(hammer_btree_cmp(cursor
->left_bound
,
1835 &cursor
->node
->ondisk
->elms
[0].leaf
.base
) <= 0);
1836 KKASSERT(hammer_btree_cmp(cursor
->right_bound
,
1837 &cursor
->node
->ondisk
->elms
[cursor
->node
->ondisk
->count
-1].leaf
.base
) > 0);
1838 KKASSERT(hammer_btree_cmp(cursor
->left_bound
, &cursor
->key_beg
) <= 0);
1839 KKASSERT(hammer_btree_cmp(cursor
->right_bound
, &cursor
->key_beg
) > 0);
1842 hammer_cursor_downgrade(cursor
);
1849 * Recursively correct the right-hand boundary's create_tid to (tid) as
1850 * long as the rest of the key matches. We have to recurse upward in
1851 * the tree as well as down the left side of each parent's right node.
1853 * Return EDEADLK if we were only partially successful, forcing the caller
1854 * to try again. The original cursor is not modified. This routine can
1855 * also fail with EDEADLK if it is forced to throw away a portion of its
1858 * The caller must pass a downgraded cursor to us (otherwise we can't dup it).
1861 TAILQ_ENTRY(hammer_rhb
) entry
;
1866 TAILQ_HEAD(hammer_rhb_list
, hammer_rhb
);
1869 hammer_btree_correct_rhb(hammer_cursor_t cursor
, hammer_tid_t tid
)
1871 struct hammer_rhb_list rhb_list
;
1872 hammer_base_elm_t elm
;
1873 hammer_node_t orig_node
;
1874 struct hammer_rhb
*rhb
;
1878 TAILQ_INIT(&rhb_list
);
1881 * Save our position so we can restore it on return. This also
1882 * gives us a stable 'elm'.
1884 orig_node
= cursor
->node
;
1885 hammer_ref_node(orig_node
);
1886 hammer_lock_sh(&orig_node
->lock
);
1887 orig_index
= cursor
->index
;
1888 elm
= &orig_node
->ondisk
->elms
[orig_index
].base
;
1891 * Now build a list of parents going up, allocating a rhb
1892 * structure for each one.
1894 while (cursor
->parent
) {
1896 * Stop if we no longer have any right-bounds to fix up
1898 if (elm
->obj_id
!= cursor
->right_bound
->obj_id
||
1899 elm
->rec_type
!= cursor
->right_bound
->rec_type
||
1900 elm
->key
!= cursor
->right_bound
->key
) {
1905 * Stop if the right-hand bound's create_tid does not
1906 * need to be corrected.
1908 if (cursor
->right_bound
->create_tid
>= tid
)
1911 rhb
= kmalloc(sizeof(*rhb
), M_HAMMER
, M_WAITOK
|M_ZERO
);
1912 rhb
->node
= cursor
->parent
;
1913 rhb
->index
= cursor
->parent_index
;
1914 hammer_ref_node(rhb
->node
);
1915 hammer_lock_sh(&rhb
->node
->lock
);
1916 TAILQ_INSERT_HEAD(&rhb_list
, rhb
, entry
);
1918 hammer_cursor_up(cursor
);
1922 * now safely adjust the right hand bound for each rhb. This may
1923 * also require taking the right side of the tree and iterating down
1927 while (error
== 0 && (rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1928 error
= hammer_cursor_seek(cursor
, rhb
->node
, rhb
->index
);
1931 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1932 hammer_unlock(&rhb
->node
->lock
);
1933 hammer_rel_node(rhb
->node
);
1934 kfree(rhb
, M_HAMMER
);
1936 switch (cursor
->node
->ondisk
->type
) {
1937 case HAMMER_BTREE_TYPE_INTERNAL
:
1939 * Right-boundary for parent at internal node
1940 * is one element to the right of the element whos
1941 * right boundary needs adjusting. We must then
1942 * traverse down the left side correcting any left
1943 * bounds (which may now be too far to the left).
1946 error
= hammer_btree_correct_lhb(cursor
, tid
);
1949 panic("hammer_btree_correct_rhb(): Bad node type");
1958 while ((rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
1959 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
1960 hammer_unlock(&rhb
->node
->lock
);
1961 hammer_rel_node(rhb
->node
);
1962 kfree(rhb
, M_HAMMER
);
1964 error
= hammer_cursor_seek(cursor
, orig_node
, orig_index
);
1965 hammer_unlock(&orig_node
->lock
);
1966 hammer_rel_node(orig_node
);
1971 * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand
1972 * bound going downward starting at the current cursor position.
1974 * This function does not restore the cursor after use.
1977 hammer_btree_correct_lhb(hammer_cursor_t cursor
, hammer_tid_t tid
)
1979 struct hammer_rhb_list rhb_list
;
1980 hammer_base_elm_t elm
;
1981 hammer_base_elm_t cmp
;
1982 struct hammer_rhb
*rhb
;
1985 TAILQ_INIT(&rhb_list
);
1987 cmp
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
1990 * Record the node and traverse down the left-hand side for all
1991 * matching records needing a boundary correction.
1995 rhb
= kmalloc(sizeof(*rhb
), M_HAMMER
, M_WAITOK
|M_ZERO
);
1996 rhb
->node
= cursor
->node
;
1997 rhb
->index
= cursor
->index
;
1998 hammer_ref_node(rhb
->node
);
1999 hammer_lock_sh(&rhb
->node
->lock
);
2000 TAILQ_INSERT_HEAD(&rhb_list
, rhb
, entry
);
2002 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
2004 * Nothing to traverse down if we are at the right
2005 * boundary of an internal node.
2007 if (cursor
->index
== cursor
->node
->ondisk
->count
)
2010 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
2011 if (elm
->btype
== HAMMER_BTREE_TYPE_RECORD
)
2013 panic("Illegal leaf record type %02x", elm
->btype
);
2015 error
= hammer_cursor_down(cursor
);
2019 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
2020 if (elm
->obj_id
!= cmp
->obj_id
||
2021 elm
->rec_type
!= cmp
->rec_type
||
2022 elm
->key
!= cmp
->key
) {
2025 if (elm
->create_tid
>= tid
)
2031 * Now we can safely adjust the left-hand boundary from the bottom-up.
2032 * The last element we remove from the list is the caller's right hand
2033 * boundary, which must also be adjusted.
2035 while (error
== 0 && (rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
2036 error
= hammer_cursor_seek(cursor
, rhb
->node
, rhb
->index
);
2039 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
2040 hammer_unlock(&rhb
->node
->lock
);
2041 hammer_rel_node(rhb
->node
);
2042 kfree(rhb
, M_HAMMER
);
2044 elm
= &cursor
->node
->ondisk
->elms
[cursor
->index
].base
;
2045 if (cursor
->node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
2046 hammer_modify_node(cursor
->trans
, cursor
->node
,
2048 sizeof(elm
->create_tid
));
2049 elm
->create_tid
= tid
;
2050 hammer_modify_node_done(cursor
->node
);
2052 panic("hammer_btree_correct_lhb(): Bad element type");
2059 while ((rhb
= TAILQ_FIRST(&rhb_list
)) != NULL
) {
2060 TAILQ_REMOVE(&rhb_list
, rhb
, entry
);
2061 hammer_unlock(&rhb
->node
->lock
);
2062 hammer_rel_node(rhb
->node
);
2063 kfree(rhb
, M_HAMMER
);
2071 * Attempt to remove the locked, empty or want-to-be-empty B-Tree node at
2072 * (cursor->node). Returns 0 on success, EDEADLK if we could not complete
2073 * the operation due to a deadlock, or some other error.
2075 * This routine is initially called with an empty leaf and may be
2076 * recursively called with single-element internal nodes.
2078 * It should also be noted that when removing empty leaves we must be sure
2079 * to test and update mirror_tid because another thread may have deadlocked
2080 * against us (or someone) trying to propagate it up and cannot retry once
2081 * the node has been deleted.
2083 * On return the cursor may end up pointing to an internal node, suitable
2084 * for further iteration but not for an immediate insertion or deletion.
2087 btree_remove(hammer_cursor_t cursor
)
2089 hammer_node_ondisk_t ondisk
;
2090 hammer_btree_elm_t elm
;
2092 hammer_node_t parent
;
2093 const int esize
= sizeof(*elm
);
2096 node
= cursor
->node
;
2099 * When deleting the root of the filesystem convert it to
2100 * an empty leaf node. Internal nodes cannot be empty.
2102 ondisk
= node
->ondisk
;
2103 if (ondisk
->parent
== 0) {
2104 KKASSERT(cursor
->parent
== NULL
);
2105 hammer_modify_node_all(cursor
->trans
, node
);
2106 KKASSERT(ondisk
== node
->ondisk
);
2107 ondisk
->type
= HAMMER_BTREE_TYPE_LEAF
;
2109 hammer_modify_node_done(node
);
2114 parent
= cursor
->parent
;
2115 hammer_cursor_removed_node(node
, parent
, cursor
->parent_index
);
2118 * Attempt to remove the parent's reference to the child. If the
2119 * parent would become empty we have to recurse. If we fail we
2120 * leave the parent pointing to an empty leaf node.
2122 * We have to recurse successfully before we can delete the internal
2123 * node as it is illegal to have empty internal nodes. Even though
2124 * the operation may be aborted we must still fixup any unlocked
2125 * cursors as if we had deleted the element prior to recursing
2126 * (by calling hammer_cursor_deleted_element()) so those cursors
2127 * are properly forced up the chain by the recursion.
2129 if (parent
->ondisk
->count
== 1) {
2131 * This special cursor_up_locked() call leaves the original
2132 * node exclusively locked and referenced, leaves the
2133 * original parent locked (as the new node), and locks the
2134 * new parent. It can return EDEADLK.
2136 error
= hammer_cursor_up_locked(cursor
);
2138 hammer_cursor_deleted_element(cursor
->node
, 0);
2139 error
= btree_remove(cursor
);
2141 hammer_modify_node_all(cursor
->trans
, node
);
2142 ondisk
= node
->ondisk
;
2143 ondisk
->type
= HAMMER_BTREE_TYPE_DELETED
;
2145 hammer_modify_node_done(node
);
2146 hammer_flush_node(node
);
2147 hammer_delete_node(cursor
->trans
, node
);
2149 kprintf("Warning: BTREE_REMOVE: Defering "
2150 "parent removal1 @ %016llx, skipping\n",
2153 hammer_unlock(&node
->lock
);
2154 hammer_rel_node(node
);
2156 kprintf("Warning: BTREE_REMOVE: Defering parent "
2157 "removal2 @ %016llx, skipping\n",
2161 KKASSERT(parent
->ondisk
->count
> 1);
2163 hammer_modify_node_all(cursor
->trans
, parent
);
2164 ondisk
= parent
->ondisk
;
2165 KKASSERT(ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
2167 elm
= &ondisk
->elms
[cursor
->parent_index
];
2168 KKASSERT(elm
->internal
.subtree_offset
== node
->node_offset
);
2169 KKASSERT(ondisk
->count
> 0);
2172 * We must retain the highest mirror_tid. The deleted
2173 * range is now encompassed by the element to the left.
2174 * If we are already at the left edge the new left edge
2175 * inherits mirror_tid.
2177 * Note that bounds of the parent to our parent may create
2178 * a gap to the left of our left-most node or to the right
2179 * of our right-most node. The gap is silently included
2180 * in the mirror_tid's area of effect from the point of view
2183 if (cursor
->parent_index
) {
2184 if (elm
[-1].internal
.mirror_tid
<
2185 elm
[0].internal
.mirror_tid
) {
2186 elm
[-1].internal
.mirror_tid
=
2187 elm
[0].internal
.mirror_tid
;
2190 if (elm
[1].internal
.mirror_tid
<
2191 elm
[0].internal
.mirror_tid
) {
2192 elm
[1].internal
.mirror_tid
=
2193 elm
[0].internal
.mirror_tid
;
2198 * Delete the subtree reference in the parent
2200 bcopy(&elm
[1], &elm
[0],
2201 (ondisk
->count
- cursor
->parent_index
) * esize
);
2203 hammer_modify_node_done(parent
);
2204 hammer_cursor_deleted_element(parent
, cursor
->parent_index
);
2205 hammer_flush_node(node
);
2206 hammer_delete_node(cursor
->trans
, node
);
2209 * cursor->node is invalid, cursor up to make the cursor
2212 error
= hammer_cursor_up(cursor
);
2218 * Propagate cursor->trans->tid up the B-Tree starting at the current
2219 * cursor position using pseudofs info gleaned from the passed inode.
2221 * The passed inode has no relationship to the cursor position other
2222 * then being in the same pseudofs as the insertion or deletion we
2223 * are propagating the mirror_tid for.
2226 hammer_btree_do_propagation(hammer_cursor_t cursor
,
2227 hammer_pseudofs_inmem_t pfsm
,
2228 hammer_btree_leaf_elm_t leaf
)
2230 hammer_cursor_t ncursor
;
2231 hammer_tid_t mirror_tid
;
2235 * We do not propagate a mirror_tid if the filesystem was mounted
2236 * in no-mirror mode.
2238 if (cursor
->trans
->hmp
->master_id
< 0)
2242 * This is a bit of a hack because we cannot deadlock or return
2243 * EDEADLK here. The related operation has already completed and
2244 * we must propagate the mirror_tid now regardless.
2246 * Generate a new cursor which inherits the original's locks and
2247 * unlock the original. Use the new cursor to propagate the
2248 * mirror_tid. Then clean up the new cursor and reacquire locks
2251 * hammer_dup_cursor() cannot dup locks. The dup inherits the
2252 * original's locks and the original is tracked and must be
2255 mirror_tid
= cursor
->node
->ondisk
->mirror_tid
;
2256 KKASSERT(mirror_tid
!= 0);
2257 ncursor
= hammer_push_cursor(cursor
);
2258 error
= hammer_btree_mirror_propagate(ncursor
, mirror_tid
);
2259 KKASSERT(error
== 0);
2260 hammer_pop_cursor(cursor
, ncursor
);
2265 * Propagate a mirror TID update upwards through the B-Tree to the root.
2267 * A locked internal node must be passed in. The node will remain locked
2270 * This function syncs mirror_tid at the specified internal node's element,
2271 * adjusts the node's aggregation mirror_tid, and then recurses upwards.
2274 hammer_btree_mirror_propagate(hammer_cursor_t cursor
, hammer_tid_t mirror_tid
)
2276 hammer_btree_internal_elm_t elm
;
2281 error
= hammer_cursor_up(cursor
);
2283 error
= hammer_cursor_upgrade(cursor
);
2284 while (error
== EDEADLK
) {
2285 hammer_recover_cursor(cursor
);
2286 error
= hammer_cursor_upgrade(cursor
);
2290 node
= cursor
->node
;
2291 KKASSERT (node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
2294 * Adjust the node's element
2296 elm
= &node
->ondisk
->elms
[cursor
->index
].internal
;
2297 if (elm
->mirror_tid
>= mirror_tid
)
2299 hammer_modify_node(cursor
->trans
, node
, &elm
->mirror_tid
,
2300 sizeof(elm
->mirror_tid
));
2301 elm
->mirror_tid
= mirror_tid
;
2302 hammer_modify_node_done(node
);
2303 if (hammer_debug_general
& 0x0002) {
2304 kprintf("mirror_propagate: propagate "
2305 "%016llx @%016llx:%d\n",
2306 mirror_tid
, node
->node_offset
, cursor
->index
);
2311 * Adjust the node's mirror_tid aggregator
2313 if (node
->ondisk
->mirror_tid
>= mirror_tid
)
2315 hammer_modify_node_field(cursor
->trans
, node
, mirror_tid
);
2316 node
->ondisk
->mirror_tid
= mirror_tid
;
2317 hammer_modify_node_done(node
);
2318 if (hammer_debug_general
& 0x0002) {
2319 kprintf("mirror_propagate: propagate "
2320 "%016llx @%016llx\n",
2321 mirror_tid
, node
->node_offset
);
2324 if (error
== ENOENT
)
2330 hammer_btree_get_parent(hammer_node_t node
, int *parent_indexp
, int *errorp
,
2333 hammer_node_t parent
;
2334 hammer_btree_elm_t elm
;
2340 parent
= hammer_get_node(node
->hmp
, node
->ondisk
->parent
, 0, errorp
);
2342 KKASSERT(parent
== NULL
);
2345 KKASSERT ((parent
->flags
& HAMMER_NODE_DELETED
) == 0);
2350 if (try_exclusive
) {
2351 if (hammer_lock_ex_try(&parent
->lock
)) {
2352 hammer_rel_node(parent
);
2357 hammer_lock_sh(&parent
->lock
);
2361 * Figure out which element in the parent is pointing to the
2364 if (node
->ondisk
->count
) {
2365 i
= hammer_btree_search_node(&node
->ondisk
->elms
[0].base
,
2370 while (i
< parent
->ondisk
->count
) {
2371 elm
= &parent
->ondisk
->elms
[i
];
2372 if (elm
->internal
.subtree_offset
== node
->node_offset
)
2376 if (i
== parent
->ondisk
->count
) {
2377 hammer_unlock(&parent
->lock
);
2378 panic("Bad B-Tree link: parent %p node %p\n", parent
, node
);
2381 KKASSERT(*errorp
== 0);
2386 * The element (elm) has been moved to a new internal node (node).
2388 * If the element represents a pointer to an internal node that node's
2389 * parent must be adjusted to the element's new location.
2391 * XXX deadlock potential here with our exclusive locks
2394 btree_set_parent(hammer_transaction_t trans
, hammer_node_t node
,
2395 hammer_btree_elm_t elm
)
2397 hammer_node_t child
;
2402 switch(elm
->base
.btype
) {
2403 case HAMMER_BTREE_TYPE_INTERNAL
:
2404 case HAMMER_BTREE_TYPE_LEAF
:
2405 child
= hammer_get_node(node
->hmp
, elm
->internal
.subtree_offset
,
2408 hammer_modify_node_field(trans
, child
, parent
);
2409 child
->ondisk
->parent
= node
->node_offset
;
2410 hammer_modify_node_done(child
);
2411 hammer_rel_node(child
);
2421 * Exclusively lock all the children of node. This is used by the split
2422 * code to prevent anyone from accessing the children of a cursor node
2423 * while we fix-up its parent offset.
2425 * If we don't lock the children we can really mess up cursors which block
2426 * trying to cursor-up into our node.
2428 * On failure EDEADLK (or some other error) is returned. If a deadlock
2429 * error is returned the cursor is adjusted to block on termination.
2432 hammer_btree_lock_children(hammer_cursor_t cursor
,
2433 struct hammer_node_locklist
**locklistp
)
2436 hammer_node_locklist_t item
;
2437 hammer_node_ondisk_t ondisk
;
2438 hammer_btree_elm_t elm
;
2439 hammer_node_t child
;
2443 node
= cursor
->node
;
2444 ondisk
= node
->ondisk
;
2448 * We really do not want to block on I/O with exclusive locks held,
2449 * pre-get the children before trying to lock the mess.
2451 for (i
= 0; i
< ondisk
->count
; ++i
) {
2452 ++hammer_stats_btree_elements
;
2453 elm
= &ondisk
->elms
[i
];
2454 if (elm
->base
.btype
!= HAMMER_BTREE_TYPE_LEAF
&&
2455 elm
->base
.btype
!= HAMMER_BTREE_TYPE_INTERNAL
) {
2458 child
= hammer_get_node(node
->hmp
,
2459 elm
->internal
.subtree_offset
,
2462 hammer_rel_node(child
);
2468 for (i
= 0; error
== 0 && i
< ondisk
->count
; ++i
) {
2469 ++hammer_stats_btree_elements
;
2470 elm
= &ondisk
->elms
[i
];
2472 switch(elm
->base
.btype
) {
2473 case HAMMER_BTREE_TYPE_INTERNAL
:
2474 case HAMMER_BTREE_TYPE_LEAF
:
2475 KKASSERT(elm
->internal
.subtree_offset
!= 0);
2476 child
= hammer_get_node(node
->hmp
,
2477 elm
->internal
.subtree_offset
,
2485 if (hammer_lock_ex_try(&child
->lock
) != 0) {
2486 if (cursor
->deadlk_node
== NULL
) {
2487 cursor
->deadlk_node
= child
;
2488 hammer_ref_node(cursor
->deadlk_node
);
2491 hammer_rel_node(child
);
2493 item
= kmalloc(sizeof(*item
),
2494 M_HAMMER
, M_WAITOK
);
2495 item
->next
= *locklistp
;
2502 hammer_btree_unlock_children(locklistp
);
2508 * Release previously obtained node locks.
2511 hammer_btree_unlock_children(struct hammer_node_locklist
**locklistp
)
2513 hammer_node_locklist_t item
;
2515 while ((item
= *locklistp
) != NULL
) {
2516 *locklistp
= item
->next
;
2517 hammer_unlock(&item
->node
->lock
);
2518 hammer_rel_node(item
->node
);
2519 kfree(item
, M_HAMMER
);
2523 /************************************************************************
2524 * MISCELLANIOUS SUPPORT *
2525 ************************************************************************/
2528 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
2530 * Note that for this particular function a return value of -1, 0, or +1
2531 * can denote a match if create_tid is otherwise discounted. A create_tid
2532 * of zero is considered to be 'infinity' in comparisons.
2534 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
2537 hammer_btree_cmp(hammer_base_elm_t key1
, hammer_base_elm_t key2
)
2539 if (key1
->localization
< key2
->localization
)
2541 if (key1
->localization
> key2
->localization
)
2544 if (key1
->obj_id
< key2
->obj_id
)
2546 if (key1
->obj_id
> key2
->obj_id
)
2549 if (key1
->rec_type
< key2
->rec_type
)
2551 if (key1
->rec_type
> key2
->rec_type
)
2554 if (key1
->key
< key2
->key
)
2556 if (key1
->key
> key2
->key
)
2560 * A create_tid of zero indicates a record which is undeletable
2561 * and must be considered to have a value of positive infinity.
2563 if (key1
->create_tid
== 0) {
2564 if (key2
->create_tid
== 0)
2568 if (key2
->create_tid
== 0)
2570 if (key1
->create_tid
< key2
->create_tid
)
2572 if (key1
->create_tid
> key2
->create_tid
)
2578 * Test a timestamp against an element to determine whether the
2579 * element is visible. A timestamp of 0 means 'infinity'.
2582 hammer_btree_chkts(hammer_tid_t asof
, hammer_base_elm_t base
)
2585 if (base
->delete_tid
)
2589 if (asof
< base
->create_tid
)
2591 if (base
->delete_tid
&& asof
>= base
->delete_tid
)
2597 * Create a separator half way inbetween key1 and key2. For fields just
2598 * one unit apart, the separator will match key2. key1 is on the left-hand
2599 * side and key2 is on the right-hand side.
2601 * key2 must be >= the separator. It is ok for the separator to match key2.
2603 * NOTE: Even if key1 does not match key2, the separator may wind up matching
2606 * NOTE: It might be beneficial to just scrap this whole mess and just
2607 * set the separator to key2.
2609 #define MAKE_SEPARATOR(key1, key2, dest, field) \
2610 dest->field = key1->field + ((key2->field - key1->field + 1) >> 1);
2613 hammer_make_separator(hammer_base_elm_t key1
, hammer_base_elm_t key2
,
2614 hammer_base_elm_t dest
)
2616 bzero(dest
, sizeof(*dest
));
2618 dest
->rec_type
= key2
->rec_type
;
2619 dest
->key
= key2
->key
;
2620 dest
->obj_id
= key2
->obj_id
;
2621 dest
->create_tid
= key2
->create_tid
;
2623 MAKE_SEPARATOR(key1
, key2
, dest
, localization
);
2624 if (key1
->localization
== key2
->localization
) {
2625 MAKE_SEPARATOR(key1
, key2
, dest
, obj_id
);
2626 if (key1
->obj_id
== key2
->obj_id
) {
2627 MAKE_SEPARATOR(key1
, key2
, dest
, rec_type
);
2628 if (key1
->rec_type
== key2
->rec_type
) {
2629 MAKE_SEPARATOR(key1
, key2
, dest
, key
);
2631 * Don't bother creating a separator for
2632 * create_tid, which also conveniently avoids
2633 * having to handle the create_tid == 0
2634 * (infinity) case. Just leave create_tid
2637 * Worst case, dest matches key2 exactly,
2638 * which is acceptable.
2645 #undef MAKE_SEPARATOR
2648 * Return whether a generic internal or leaf node is full
2651 btree_node_is_full(hammer_node_ondisk_t node
)
2653 switch(node
->type
) {
2654 case HAMMER_BTREE_TYPE_INTERNAL
:
2655 if (node
->count
== HAMMER_BTREE_INT_ELMS
)
2658 case HAMMER_BTREE_TYPE_LEAF
:
2659 if (node
->count
== HAMMER_BTREE_LEAF_ELMS
)
2663 panic("illegal btree subtype");
2670 btree_max_elements(u_int8_t type
)
2672 if (type
== HAMMER_BTREE_TYPE_LEAF
)
2673 return(HAMMER_BTREE_LEAF_ELMS
);
2674 if (type
== HAMMER_BTREE_TYPE_INTERNAL
)
2675 return(HAMMER_BTREE_INT_ELMS
);
2676 panic("btree_max_elements: bad type %d\n", type
);
2681 hammer_print_btree_node(hammer_node_ondisk_t ondisk
)
2683 hammer_btree_elm_t elm
;
2686 kprintf("node %p count=%d parent=%016llx type=%c\n",
2687 ondisk
, ondisk
->count
, ondisk
->parent
, ondisk
->type
);
2690 * Dump both boundary elements if an internal node
2692 if (ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
2693 for (i
= 0; i
<= ondisk
->count
; ++i
) {
2694 elm
= &ondisk
->elms
[i
];
2695 hammer_print_btree_elm(elm
, ondisk
->type
, i
);
2698 for (i
= 0; i
< ondisk
->count
; ++i
) {
2699 elm
= &ondisk
->elms
[i
];
2700 hammer_print_btree_elm(elm
, ondisk
->type
, i
);
2706 hammer_print_btree_elm(hammer_btree_elm_t elm
, u_int8_t type
, int i
)
2709 kprintf("\tobj_id = %016llx\n", elm
->base
.obj_id
);
2710 kprintf("\tkey = %016llx\n", elm
->base
.key
);
2711 kprintf("\tcreate_tid = %016llx\n", elm
->base
.create_tid
);
2712 kprintf("\tdelete_tid = %016llx\n", elm
->base
.delete_tid
);
2713 kprintf("\trec_type = %04x\n", elm
->base
.rec_type
);
2714 kprintf("\tobj_type = %02x\n", elm
->base
.obj_type
);
2715 kprintf("\tbtype = %02x (%c)\n",
2717 (elm
->base
.btype
? elm
->base
.btype
: '?'));
2718 kprintf("\tlocalization = %02x\n", elm
->base
.localization
);
2721 case HAMMER_BTREE_TYPE_INTERNAL
:
2722 kprintf("\tsubtree_off = %016llx\n",
2723 elm
->internal
.subtree_offset
);
2725 case HAMMER_BTREE_TYPE_RECORD
:
2726 kprintf("\tdata_offset = %016llx\n", elm
->leaf
.data_offset
);
2727 kprintf("\tdata_len = %08x\n", elm
->leaf
.data_len
);
2728 kprintf("\tdata_crc = %08x\n", elm
->leaf
.data_crc
);