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 * If HAMMER_CURSOR_ITERATE_CHECK is set it is possible that the cursor
114 * was reverse indexed due to being moved to a parent while unlocked,
115 * and something else might have inserted an element outside the iteration
116 * range. When this case occurs the iterator just keeps iterating until
117 * it gets back into the iteration range (instead of asserting).
119 * NOTE! EDEADLK *CANNOT* be returned by this procedure.
122 hammer_btree_iterate(hammer_cursor_t cursor)
124 hammer_node_ondisk_t node;
125 hammer_btree_elm_t elm;
132 * Skip past the current record
134 hmp = cursor->trans->hmp;
135 node = cursor->node->ondisk;
138 if (cursor->index < node->count &&
139 (cursor->flags & HAMMER_CURSOR_ATEDISK)) {
144 * HAMMER can wind up being cpu-bound.
146 if (++hmp->check_yield > hammer_yield_check) {
147 hmp->check_yield = 0;
153 * Loop until an element is found or we are done.
157 * We iterate up the tree and then index over one element
158 * while we are at the last element in the current node.
160 * If we are at the root of the filesystem, cursor_up
163 * XXX this could be optimized by storing the information in
164 * the parent reference.
166 * XXX we can lose the node lock temporarily, this could mess
169 ++hammer_stats_btree_iterations;
170 hammer_flusher_clean_loose_ios(hmp);
172 if (cursor->index == node->count) {
173 if (hammer_debug_btree) {
174 kprintf("BRACKETU %016llx[%d] -> %016llx[%d] (td=%p)\n",
175 (long long)cursor->node->node_offset,
177 (long long)(cursor->parent ? cursor->parent->node_offset : -1),
178 cursor->parent_index,
181 KKASSERT(cursor->parent == NULL || cursor->parent->ondisk->elms[cursor->parent_index].internal.subtree_offset == cursor->node->node_offset);
182 error = hammer_cursor_up(cursor);
185 /* reload stale pointer */
186 node = cursor->node->ondisk;
187 KKASSERT(cursor->index != node->count);
190 * If we are reblocking we want to return internal
191 * nodes. Note that the internal node will be
192 * returned multiple times, on each upward recursion
193 * from its children. The caller selects which
194 * revisit it cares about (usually first or last only).
196 if (cursor->flags & HAMMER_CURSOR_REBLOCKING) {
197 cursor->flags |= HAMMER_CURSOR_ATEDISK;
205 * Check internal or leaf element. Determine if the record
206 * at the cursor has gone beyond the end of our range.
208 * We recurse down through internal nodes.
210 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
211 elm = &node->elms[cursor->index];
213 r = hammer_btree_cmp(&cursor->key_end, &elm[0].base);
214 s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base);
215 if (hammer_debug_btree) {
216 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d (td=%p)\n",
217 (long long)cursor->node->node_offset,
219 (long long)elm[0].internal.base.obj_id,
220 elm[0].internal.base.rec_type,
221 (long long)elm[0].internal.base.key,
222 elm[0].internal.base.localization,
226 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
227 (long long)cursor->node->node_offset,
229 (long long)elm[1].internal.base.obj_id,
230 elm[1].internal.base.rec_type,
231 (long long)elm[1].internal.base.key,
232 elm[1].internal.base.localization,
241 if (r == 0 && (cursor->flags &
242 HAMMER_CURSOR_END_INCLUSIVE) == 0) {
250 KKASSERT(elm->internal.subtree_offset != 0);
254 * If running the mirror filter see if we
255 * can skip one or more entire sub-trees.
256 * If we can we return the internal node
257 * and the caller processes the skipped
258 * range (see mirror_read).
261 HAMMER_CURSOR_MIRROR_FILTERED) {
262 if (elm->internal.mirror_tid <
263 cursor->cmirror->mirror_tid) {
264 hammer_cursor_mirror_filter(cursor);
270 * Normally it would be impossible for the
271 * cursor to have gotten back-indexed,
272 * but it can happen if a node is deleted
273 * and the cursor is moved to its parent
274 * internal node. ITERATE_CHECK will be set.
276 KKASSERT(cursor->flags &
277 HAMMER_CURSOR_ITERATE_CHECK);
278 kprintf("hammer_btree_iterate: "
279 "DEBUG: Caught parent seek "
280 "in internal iteration\n");
283 error = hammer_cursor_down(cursor);
286 KKASSERT(cursor->index == 0);
287 /* reload stale pointer */
288 node = cursor->node->ondisk;
291 elm = &node->elms[cursor->index];
292 r = hammer_btree_cmp(&cursor->key_end, &elm->base);
293 if (hammer_debug_btree) {
294 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
295 (long long)cursor->node->node_offset,
297 (elm[0].leaf.base.btype ?
298 elm[0].leaf.base.btype : '?'),
299 (long long)elm[0].leaf.base.obj_id,
300 elm[0].leaf.base.rec_type,
301 (long long)elm[0].leaf.base.key,
302 elm[0].leaf.base.localization,
312 * We support both end-inclusive and
313 * end-exclusive searches.
316 (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) {
322 * If ITERATE_CHECK is set an unlocked cursor may
323 * have been moved to a parent and the iterate can
324 * happen upon elements that are not in the requested
327 if (cursor->flags & HAMMER_CURSOR_ITERATE_CHECK) {
328 s = hammer_btree_cmp(&cursor->key_beg,
331 kprintf("hammer_btree_iterate: "
332 "DEBUG: Caught parent seek "
333 "in leaf iteration\n");
338 cursor->flags &= ~HAMMER_CURSOR_ITERATE_CHECK;
343 switch(elm->leaf.base.btype) {
344 case HAMMER_BTREE_TYPE_RECORD:
345 if ((cursor->flags & HAMMER_CURSOR_ASOF) &&
346 hammer_btree_chkts(cursor->asof, &elm->base)) {
360 * node pointer invalid after loop
366 if (hammer_debug_btree) {
367 int i = cursor->index;
368 hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i];
369 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
371 (long long)elm->internal.base.obj_id,
372 elm->internal.base.rec_type,
373 (long long)elm->internal.base.key,
374 elm->internal.base.localization
383 * We hit an internal element that we could skip as part of a mirroring
384 * scan. Calculate the entire range being skipped.
386 * It is important to include any gaps between the parent's left_bound
387 * and the node's left_bound, and same goes for the right side.
390 hammer_cursor_mirror_filter(hammer_cursor_t cursor)
392 struct hammer_cmirror *cmirror;
393 hammer_node_ondisk_t ondisk;
394 hammer_btree_elm_t elm;
396 ondisk = cursor->node->ondisk;
397 cmirror = cursor->cmirror;
400 * Calculate the skipped range
402 elm = &ondisk->elms[cursor->index];
403 if (cursor->index == 0)
404 cmirror->skip_beg = *cursor->left_bound;
406 cmirror->skip_beg = elm->internal.base;
407 while (cursor->index < ondisk->count) {
408 if (elm->internal.mirror_tid >= cmirror->mirror_tid)
413 if (cursor->index == ondisk->count)
414 cmirror->skip_end = *cursor->right_bound;
416 cmirror->skip_end = elm->internal.base;
419 * clip the returned result.
421 if (hammer_btree_cmp(&cmirror->skip_beg, &cursor->key_beg) < 0)
422 cmirror->skip_beg = cursor->key_beg;
423 if (hammer_btree_cmp(&cmirror->skip_end, &cursor->key_end) > 0)
424 cmirror->skip_end = cursor->key_end;
428 * Iterate in the reverse direction. This is used by the pruning code to
429 * avoid overlapping records.
432 hammer_btree_iterate_reverse(hammer_cursor_t cursor)
434 hammer_node_ondisk_t node;
435 hammer_btree_elm_t elm;
441 /* mirror filtering not supported for reverse iteration */
442 KKASSERT ((cursor->flags & HAMMER_CURSOR_MIRROR_FILTERED) == 0);
445 * Skip past the current record. For various reasons the cursor
446 * may end up set to -1 or set to point at the end of the current
447 * node. These cases must be addressed.
449 node = cursor->node->ondisk;
452 if (cursor->index != -1 &&
453 (cursor->flags & HAMMER_CURSOR_ATEDISK)) {
456 if (cursor->index == cursor->node->ondisk->count)
460 * HAMMER can wind up being cpu-bound.
462 hmp = cursor->trans->hmp;
463 if (++hmp->check_yield > hammer_yield_check) {
464 hmp->check_yield = 0;
469 * Loop until an element is found or we are done.
472 ++hammer_stats_btree_iterations;
473 hammer_flusher_clean_loose_ios(hmp);
476 * We iterate up the tree and then index over one element
477 * while we are at the last element in the current node.
479 if (cursor->index == -1) {
480 error = hammer_cursor_up(cursor);
482 cursor->index = 0; /* sanity */
485 /* reload stale pointer */
486 node = cursor->node->ondisk;
487 KKASSERT(cursor->index != node->count);
493 * Check internal or leaf element. Determine if the record
494 * at the cursor has gone beyond the end of our range.
496 * We recurse down through internal nodes.
498 KKASSERT(cursor->index != node->count);
499 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
500 elm = &node->elms[cursor->index];
501 r = hammer_btree_cmp(&cursor->key_end, &elm[0].base);
502 s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base);
503 if (hammer_debug_btree) {
504 kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
505 (long long)cursor->node->node_offset,
507 (long long)elm[0].internal.base.obj_id,
508 elm[0].internal.base.rec_type,
509 (long long)elm[0].internal.base.key,
510 elm[0].internal.base.localization,
513 kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n",
514 (long long)cursor->node->node_offset,
516 (long long)elm[1].internal.base.obj_id,
517 elm[1].internal.base.rec_type,
518 (long long)elm[1].internal.base.key,
519 elm[1].internal.base.localization,
530 * It shouldn't be possible to be seeked past key_end,
531 * even if the cursor got moved to a parent.
538 KKASSERT(elm->internal.subtree_offset != 0);
540 error = hammer_cursor_down(cursor);
543 KKASSERT(cursor->index == 0);
544 /* reload stale pointer */
545 node = cursor->node->ondisk;
547 /* this can assign -1 if the leaf was empty */
548 cursor->index = node->count - 1;
551 elm = &node->elms[cursor->index];
552 s = hammer_btree_cmp(&cursor->key_beg, &elm->base);
553 if (hammer_debug_btree) {
554 kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n",
555 (long long)cursor->node->node_offset,
557 (elm[0].leaf.base.btype ?
558 elm[0].leaf.base.btype : '?'),
559 (long long)elm[0].leaf.base.obj_id,
560 elm[0].leaf.base.rec_type,
561 (long long)elm[0].leaf.base.key,
562 elm[0].leaf.base.localization,
572 * It shouldn't be possible to be seeked past key_end,
573 * even if the cursor got moved to a parent.
575 cursor->flags &= ~HAMMER_CURSOR_ITERATE_CHECK;
580 switch(elm->leaf.base.btype) {
581 case HAMMER_BTREE_TYPE_RECORD:
582 if ((cursor->flags & HAMMER_CURSOR_ASOF) &&
583 hammer_btree_chkts(cursor->asof, &elm->base)) {
597 * node pointer invalid after loop
603 if (hammer_debug_btree) {
604 int i = cursor->index;
605 hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i];
606 kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n",
608 (long long)elm->internal.base.obj_id,
609 elm->internal.base.rec_type,
610 (long long)elm->internal.base.key,
611 elm->internal.base.localization
620 * Lookup cursor->key_beg. 0 is returned on success, ENOENT if the entry
621 * could not be found, EDEADLK if inserting and a retry is needed, and a
622 * fatal error otherwise. When retrying, the caller must terminate the
623 * cursor and reinitialize it. EDEADLK cannot be returned if not inserting.
625 * The cursor is suitably positioned for a deletion on success, and suitably
626 * positioned for an insertion on ENOENT if HAMMER_CURSOR_INSERT was
629 * The cursor may begin anywhere, the search will traverse the tree in
630 * either direction to locate the requested element.
632 * Most of the logic implementing historical searches is handled here. We
633 * do an initial lookup with create_tid set to the asof TID. Due to the
634 * way records are laid out, a backwards iteration may be required if
635 * ENOENT is returned to locate the historical record. Here's the
638 * create_tid: 10 15 20
642 * Lets say we want to do a lookup AS-OF timestamp 17. We will traverse
643 * LEAF2 but the only record in LEAF2 has a create_tid of 18, which is
644 * not visible and thus causes ENOENT to be returned. We really need
645 * to check record 11 in LEAF1. If it also fails then the search fails
646 * (e.g. it might represent the range 11-16 and thus still not match our
647 * AS-OF timestamp of 17). Note that LEAF1 could be empty, requiring
648 * further iterations.
650 * If this case occurs btree_search() will set HAMMER_CURSOR_CREATE_CHECK
651 * and the cursor->create_check TID if an iteration might be needed.
652 * In the above example create_check would be set to 14.
655 hammer_btree_lookup(hammer_cursor_t cursor)
659 cursor->flags &= ~HAMMER_CURSOR_ITERATE_CHECK;
660 KKASSERT ((cursor->flags & HAMMER_CURSOR_INSERT) == 0 ||
661 cursor->trans->sync_lock_refs > 0);
662 ++hammer_stats_btree_lookups;
663 if (cursor->flags & HAMMER_CURSOR_ASOF) {
664 KKASSERT((cursor->flags & HAMMER_CURSOR_INSERT) == 0);
665 cursor->key_beg.create_tid = cursor->asof;
667 cursor->flags &= ~HAMMER_CURSOR_CREATE_CHECK;
668 error = btree_search(cursor, 0);
669 if (error != ENOENT ||
670 (cursor->flags & HAMMER_CURSOR_CREATE_CHECK) == 0) {
673 * Stop if error other then ENOENT.
674 * Stop if ENOENT and not special case.
678 if (hammer_debug_btree) {
679 kprintf("CREATE_CHECK %016llx\n",
680 (long long)cursor->create_check);
682 cursor->key_beg.create_tid = cursor->create_check;
686 error = btree_search(cursor, 0);
689 error = hammer_btree_extract(cursor, cursor->flags);
694 * Execute the logic required to start an iteration. The first record
695 * located within the specified range is returned and iteration control
696 * flags are adjusted for successive hammer_btree_iterate() calls.
698 * Set ATEDISK so a low-level caller can call btree_first/btree_iterate
699 * in a loop without worrying about it. Higher-level merged searches will
700 * adjust the flag appropriately.
703 hammer_btree_first(hammer_cursor_t cursor)
707 error = hammer_btree_lookup(cursor);
708 if (error == ENOENT) {
709 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
710 error = hammer_btree_iterate(cursor);
712 cursor->flags |= HAMMER_CURSOR_ATEDISK;
717 * Similarly but for an iteration in the reverse direction.
719 * Set ATEDISK when iterating backwards to skip the current entry,
720 * which after an ENOENT lookup will be pointing beyond our end point.
722 * Set ATEDISK so a low-level caller can call btree_last/btree_iterate_reverse
723 * in a loop without worrying about it. Higher-level merged searches will
724 * adjust the flag appropriately.
727 hammer_btree_last(hammer_cursor_t cursor)
729 struct hammer_base_elm save;
732 save = cursor->key_beg;
733 cursor->key_beg = cursor->key_end;
734 error = hammer_btree_lookup(cursor);
735 cursor->key_beg = save;
736 if (error == ENOENT ||
737 (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) {
738 cursor->flags |= HAMMER_CURSOR_ATEDISK;
739 error = hammer_btree_iterate_reverse(cursor);
741 cursor->flags |= HAMMER_CURSOR_ATEDISK;
746 * Extract the record and/or data associated with the cursor's current
747 * position. Any prior record or data stored in the cursor is replaced.
748 * The cursor must be positioned at a leaf node.
750 * NOTE: All extractions occur at the leaf of the B-Tree.
753 hammer_btree_extract(hammer_cursor_t cursor, int flags)
755 hammer_node_ondisk_t node;
756 hammer_btree_elm_t elm;
757 hammer_off_t data_off;
763 * The case where the data reference resolves to the same buffer
764 * as the record reference must be handled.
766 node = cursor->node->ondisk;
767 elm = &node->elms[cursor->index];
769 hmp = cursor->node->hmp;
772 * There is nothing to extract for an internal element.
774 if (node->type == HAMMER_BTREE_TYPE_INTERNAL)
778 * Only record types have data.
780 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
781 cursor->leaf = &elm->leaf;
783 if ((flags & HAMMER_CURSOR_GET_DATA) == 0)
785 if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD)
787 data_off = elm->leaf.data_offset;
788 data_len = elm->leaf.data_len;
795 KKASSERT(data_len >= 0 && data_len <= HAMMER_XBUFSIZE);
796 cursor->data = hammer_bread_ext(hmp, data_off, data_len,
797 &error, &cursor->data_buffer);
800 * Mark the data buffer as not being meta-data if it isn't
801 * meta-data (sometimes bulk data is accessed via a volume
805 switch(elm->leaf.base.rec_type) {
806 case HAMMER_RECTYPE_DATA:
807 case HAMMER_RECTYPE_DB:
808 hammer_io_notmeta(cursor->data_buffer);
816 * Deal with CRC errors on the extracted data.
819 hammer_crc_test_leaf(cursor->data, &elm->leaf) == 0) {
820 kprintf("CRC DATA @ %016llx/%d FAILED\n",
821 (long long)elm->leaf.data_offset, elm->leaf.data_len);
822 if (hammer_debug_critical)
823 Debugger("CRC FAILED: DATA");
824 if (cursor->trans->flags & HAMMER_TRANSF_CRCDOM)
825 error = EDOM; /* less critical (mirroring) */
827 error = EIO; /* critical */
834 * Insert a leaf element into the B-Tree at the current cursor position.
835 * The cursor is positioned such that the element at and beyond the cursor
836 * are shifted to make room for the new record.
838 * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT
839 * flag set and that call must return ENOENT before this function can be
842 * The caller may depend on the cursor's exclusive lock after return to
843 * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE).
845 * ENOSPC is returned if there is no room to insert a new record.
848 hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm,
851 hammer_node_ondisk_t node;
856 if ((error = hammer_cursor_upgrade_node(cursor)) != 0)
858 ++hammer_stats_btree_inserts;
861 * Insert the element at the leaf node and update the count in the
862 * parent. It is possible for parent to be NULL, indicating that
863 * the filesystem's ROOT B-Tree node is a leaf itself, which is
864 * possible. The root inode can never be deleted so the leaf should
867 * Remember that the right-hand boundary is not included in the
870 hammer_modify_node_all(cursor->trans, cursor->node);
871 node = cursor->node->ondisk;
873 KKASSERT(elm->base.btype != 0);
874 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
875 KKASSERT(node->count < HAMMER_BTREE_LEAF_ELMS);
876 if (i != node->count) {
877 bcopy(&node->elms[i], &node->elms[i+1],
878 (node->count - i) * sizeof(*elm));
880 node->elms[i].leaf = *elm;
882 hammer_cursor_inserted_element(cursor->node, i);
885 * Update the leaf node's aggregate mirror_tid for mirroring
888 if (node->mirror_tid < elm->base.delete_tid) {
889 node->mirror_tid = elm->base.delete_tid;
892 if (node->mirror_tid < elm->base.create_tid) {
893 node->mirror_tid = elm->base.create_tid;
896 hammer_modify_node_done(cursor->node);
899 * Debugging sanity checks.
901 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->base) <= 0);
902 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->base) > 0);
904 KKASSERT(hammer_btree_cmp(&node->elms[i-1].leaf.base, &elm->base) < 0);
906 if (i != node->count - 1)
907 KKASSERT(hammer_btree_cmp(&node->elms[i+1].leaf.base, &elm->base) > 0);
913 * Delete a record from the B-Tree at the current cursor position.
914 * The cursor is positioned such that the current element is the one
917 * On return the cursor will be positioned after the deleted element and
918 * MAY point to an internal node. It will be suitable for the continuation
919 * of an iteration but not for an insertion or deletion.
921 * Deletions will attempt to partially rebalance the B-Tree in an upward
922 * direction, but will terminate rather then deadlock. Empty internal nodes
923 * are never allowed by a deletion which deadlocks may end up giving us an
924 * empty leaf. The pruner will clean up and rebalance the tree.
926 * This function can return EDEADLK, requiring the caller to retry the
927 * operation after clearing the deadlock.
930 hammer_btree_delete(hammer_cursor_t cursor)
932 hammer_node_ondisk_t ondisk;
934 hammer_node_t parent;
938 KKASSERT (cursor->trans->sync_lock_refs > 0);
939 if ((error = hammer_cursor_upgrade(cursor)) != 0)
941 ++hammer_stats_btree_deletes;
944 * Delete the element from the leaf node.
946 * Remember that leaf nodes do not have boundaries.
949 ondisk = node->ondisk;
952 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_LEAF);
953 KKASSERT(i >= 0 && i < ondisk->count);
954 hammer_modify_node_all(cursor->trans, node);
955 if (i + 1 != ondisk->count) {
956 bcopy(&ondisk->elms[i+1], &ondisk->elms[i],
957 (ondisk->count - i - 1) * sizeof(ondisk->elms[0]));
960 hammer_modify_node_done(node);
961 hammer_cursor_deleted_element(node, i);
964 * Validate local parent
966 if (ondisk->parent) {
967 parent = cursor->parent;
969 KKASSERT(parent != NULL);
970 KKASSERT(parent->node_offset == ondisk->parent);
974 * If the leaf becomes empty it must be detached from the parent,
975 * potentially recursing through to the filesystem root.
977 * This may reposition the cursor at one of the parent's of the
980 * Ignore deadlock errors, that simply means that btree_remove
981 * was unable to recurse and had to leave us with an empty leaf.
983 KKASSERT(cursor->index <= ondisk->count);
984 if (ondisk->count == 0) {
985 error = btree_remove(cursor);
986 if (error == EDEADLK)
991 KKASSERT(cursor->parent == NULL ||
992 cursor->parent_index < cursor->parent->ondisk->count);
997 * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE
999 * Search the filesystem B-Tree for cursor->key_beg, return the matching node.
1001 * The search can begin ANYWHERE in the B-Tree. As a first step the search
1002 * iterates up the tree as necessary to properly position itself prior to
1003 * actually doing the sarch.
1005 * INSERTIONS: The search will split full nodes and leaves on its way down
1006 * and guarentee that the leaf it ends up on is not full. If we run out
1007 * of space the search continues to the leaf (to position the cursor for
1008 * the spike), but ENOSPC is returned.
1010 * The search is only guarenteed to end up on a leaf if an error code of 0
1011 * is returned, or if inserting and an error code of ENOENT is returned.
1012 * Otherwise it can stop at an internal node. On success a search returns
1015 * COMPLEXITY WARNING! This is the core B-Tree search code for the entire
1016 * filesystem, and it is not simple code. Please note the following facts:
1018 * - Internal node recursions have a boundary on the left AND right. The
1019 * right boundary is non-inclusive. The create_tid is a generic part
1020 * of the key for internal nodes.
1022 * - Leaf nodes contain terminal elements only now.
1024 * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a
1025 * historical search. ASOF and INSERT are mutually exclusive. When
1026 * doing an as-of lookup btree_search() checks for a right-edge boundary
1027 * case. If while recursing down the left-edge differs from the key
1028 * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along
1029 * with cursor->create_check. This is used by btree_lookup() to iterate.
1030 * The iteration backwards because as-of searches can wind up going
1031 * down the wrong branch of the B-Tree.
1035 btree_search(hammer_cursor_t cursor, int flags)
1037 hammer_node_ondisk_t node;
1038 hammer_btree_elm_t elm;
1045 flags |= cursor->flags;
1046 ++hammer_stats_btree_searches;
1048 if (hammer_debug_btree) {
1049 kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx lo=%02x (td = %p)\n",
1050 (long long)cursor->node->node_offset,
1052 (long long)cursor->key_beg.obj_id,
1053 cursor->key_beg.rec_type,
1054 (long long)cursor->key_beg.key,
1055 (long long)cursor->key_beg.create_tid,
1056 cursor->key_beg.localization,
1060 kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n",
1061 (long long)cursor->parent->node_offset,
1062 cursor->parent_index,
1063 (long long)cursor->left_bound->obj_id,
1064 (long long)cursor->parent->ondisk->elms[cursor->parent_index].internal.base.obj_id,
1065 (long long)cursor->right_bound->obj_id,
1066 (long long)cursor->parent->ondisk->elms[cursor->parent_index+1].internal.base.obj_id,
1068 &cursor->parent->ondisk->elms[cursor->parent_index],
1069 cursor->right_bound,
1070 &cursor->parent->ondisk->elms[cursor->parent_index+1]
1075 * Move our cursor up the tree until we find a node whos range covers
1076 * the key we are trying to locate.
1078 * The left bound is inclusive, the right bound is non-inclusive.
1079 * It is ok to cursor up too far.
1082 r = hammer_btree_cmp(&cursor->key_beg, cursor->left_bound);
1083 s = hammer_btree_cmp(&cursor->key_beg, cursor->right_bound);
1084 if (r >= 0 && s < 0)
1086 KKASSERT(cursor->parent);
1087 ++hammer_stats_btree_iterations;
1088 error = hammer_cursor_up(cursor);
1094 * The delete-checks below are based on node, not parent. Set the
1095 * initial delete-check based on the parent.
1098 KKASSERT(cursor->left_bound->create_tid != 1);
1099 cursor->create_check = cursor->left_bound->create_tid - 1;
1100 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
1104 * We better have ended up with a node somewhere.
1106 KKASSERT(cursor->node != NULL);
1109 * If we are inserting we can't start at a full node if the parent
1110 * is also full (because there is no way to split the node),
1111 * continue running up the tree until the requirement is satisfied
1112 * or we hit the root of the filesystem.
1114 * (If inserting we aren't doing an as-of search so we don't have
1115 * to worry about create_check).
1117 while ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
1118 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
1119 if (btree_node_is_full(cursor->node->ondisk) == 0)
1122 if (btree_node_is_full(cursor->node->ondisk) ==0)
1125 if (cursor->node->ondisk->parent == 0 ||
1126 cursor->parent->ondisk->count != HAMMER_BTREE_INT_ELMS) {
1129 ++hammer_stats_btree_iterations;
1130 error = hammer_cursor_up(cursor);
1131 /* node may have become stale */
1137 * Push down through internal nodes to locate the requested key.
1139 node = cursor->node->ondisk;
1140 while (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
1142 * Scan the node to find the subtree index to push down into.
1143 * We go one-past, then back-up.
1145 * We must proactively remove deleted elements which may
1146 * have been left over from a deadlocked btree_remove().
1148 * The left and right boundaries are included in the loop
1149 * in order to detect edge cases.
1151 * If the separator only differs by create_tid (r == 1)
1152 * and we are doing an as-of search, we may end up going
1153 * down a branch to the left of the one containing the
1154 * desired key. This requires numerous special cases.
1156 ++hammer_stats_btree_iterations;
1157 if (hammer_debug_btree) {
1158 kprintf("SEARCH-I %016llx count=%d\n",
1159 (long long)cursor->node->node_offset,
1164 * Try to shortcut the search before dropping into the
1165 * linear loop. Locate the first node where r <= 1.
1167 i = hammer_btree_search_node(&cursor->key_beg, node);
1168 while (i <= node->count) {
1169 ++hammer_stats_btree_elements;
1170 elm = &node->elms[i];
1171 r = hammer_btree_cmp(&cursor->key_beg, &elm->base);
1172 if (hammer_debug_btree > 2) {
1173 kprintf(" IELM %p %d r=%d\n",
1174 &node->elms[i], i, r);
1179 KKASSERT(elm->base.create_tid != 1);
1180 cursor->create_check = elm->base.create_tid - 1;
1181 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
1185 if (hammer_debug_btree) {
1186 kprintf("SEARCH-I preI=%d/%d r=%d\n",
1191 * These cases occur when the parent's idea of the boundary
1192 * is wider then the child's idea of the boundary, and
1193 * require special handling. If not inserting we can
1194 * terminate the search early for these cases but the
1195 * child's boundaries cannot be unconditionally modified.
1199 * If i == 0 the search terminated to the LEFT of the
1200 * left_boundary but to the RIGHT of the parent's left
1205 elm = &node->elms[0];
1208 * If we aren't inserting we can stop here.
1210 if ((flags & (HAMMER_CURSOR_INSERT |
1211 HAMMER_CURSOR_PRUNING)) == 0) {
1217 * Correct a left-hand boundary mismatch.
1219 * We can only do this if we can upgrade the lock,
1220 * and synchronized as a background cursor (i.e.
1221 * inserting or pruning).
1223 * WARNING: We can only do this if inserting, i.e.
1224 * we are running on the backend.
1226 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1228 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1229 hammer_modify_node_field(cursor->trans, cursor->node,
1231 save = node->elms[0].base.btype;
1232 node->elms[0].base = *cursor->left_bound;
1233 node->elms[0].base.btype = save;
1234 hammer_modify_node_done(cursor->node);
1235 } else if (i == node->count + 1) {
1237 * If i == node->count + 1 the search terminated to
1238 * the RIGHT of the right boundary but to the LEFT
1239 * of the parent's right boundary. If we aren't
1240 * inserting we can stop here.
1242 * Note that the last element in this case is
1243 * elms[i-2] prior to adjustments to 'i'.
1246 if ((flags & (HAMMER_CURSOR_INSERT |
1247 HAMMER_CURSOR_PRUNING)) == 0) {
1253 * Correct a right-hand boundary mismatch.
1254 * (actual push-down record is i-2 prior to
1255 * adjustments to i).
1257 * We can only do this if we can upgrade the lock,
1258 * and synchronized as a background cursor (i.e.
1259 * inserting or pruning).
1261 * WARNING: We can only do this if inserting, i.e.
1262 * we are running on the backend.
1264 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1266 elm = &node->elms[i];
1267 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
1268 hammer_modify_node(cursor->trans, cursor->node,
1269 &elm->base, sizeof(elm->base));
1270 elm->base = *cursor->right_bound;
1271 hammer_modify_node_done(cursor->node);
1275 * The push-down index is now i - 1. If we had
1276 * terminated on the right boundary this will point
1277 * us at the last element.
1282 elm = &node->elms[i];
1284 if (hammer_debug_btree) {
1285 kprintf("RESULT-I %016llx[%d] %016llx %02x "
1286 "key=%016llx cre=%016llx lo=%02x\n",
1287 (long long)cursor->node->node_offset,
1289 (long long)elm->internal.base.obj_id,
1290 elm->internal.base.rec_type,
1291 (long long)elm->internal.base.key,
1292 (long long)elm->internal.base.create_tid,
1293 elm->internal.base.localization
1298 * We better have a valid subtree offset.
1300 KKASSERT(elm->internal.subtree_offset != 0);
1303 * Handle insertion and deletion requirements.
1305 * If inserting split full nodes. The split code will
1306 * adjust cursor->node and cursor->index if the current
1307 * index winds up in the new node.
1309 * If inserting and a left or right edge case was detected,
1310 * we cannot correct the left or right boundary and must
1311 * prepend and append an empty leaf node in order to make
1312 * the boundary correction.
1314 * If we run out of space we set enospc and continue on
1315 * to a leaf to provide the spike code with a good point
1318 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
1319 if (btree_node_is_full(node)) {
1320 error = btree_split_internal(cursor);
1322 if (error != ENOSPC)
1327 * reload stale pointers
1330 node = cursor->node->ondisk;
1335 * Push down (push into new node, existing node becomes
1336 * the parent) and continue the search.
1338 error = hammer_cursor_down(cursor);
1339 /* node may have become stale */
1342 node = cursor->node->ondisk;
1346 * We are at a leaf, do a linear search of the key array.
1348 * On success the index is set to the matching element and 0
1351 * On failure the index is set to the insertion point and ENOENT
1354 * Boundaries are not stored in leaf nodes, so the index can wind
1355 * up to the left of element 0 (index == 0) or past the end of
1356 * the array (index == node->count). It is also possible that the
1357 * leaf might be empty.
1359 ++hammer_stats_btree_iterations;
1360 KKASSERT (node->type == HAMMER_BTREE_TYPE_LEAF);
1361 KKASSERT(node->count <= HAMMER_BTREE_LEAF_ELMS);
1362 if (hammer_debug_btree) {
1363 kprintf("SEARCH-L %016llx count=%d\n",
1364 (long long)cursor->node->node_offset,
1369 * Try to shortcut the search before dropping into the
1370 * linear loop. Locate the first node where r <= 1.
1372 i = hammer_btree_search_node(&cursor->key_beg, node);
1373 while (i < node->count) {
1374 ++hammer_stats_btree_elements;
1375 elm = &node->elms[i];
1377 r = hammer_btree_cmp(&cursor->key_beg, &elm->leaf.base);
1379 if (hammer_debug_btree > 1)
1380 kprintf(" ELM %p %d r=%d\n", &node->elms[i], i, r);
1383 * We are at a record element. Stop if we've flipped past
1384 * key_beg, not counting the create_tid test. Allow the
1385 * r == 1 case (key_beg > element but differs only by its
1386 * create_tid) to fall through to the AS-OF check.
1388 KKASSERT (elm->leaf.base.btype == HAMMER_BTREE_TYPE_RECORD);
1398 * Check our as-of timestamp against the element.
1400 if (flags & HAMMER_CURSOR_ASOF) {
1401 if (hammer_btree_chkts(cursor->asof,
1402 &node->elms[i].base) != 0) {
1408 if (r > 0) { /* can only be +1 */
1416 if (hammer_debug_btree) {
1417 kprintf("RESULT-L %016llx[%d] (SUCCESS)\n",
1418 (long long)cursor->node->node_offset, i);
1424 * The search of the leaf node failed. i is the insertion point.
1427 if (hammer_debug_btree) {
1428 kprintf("RESULT-L %016llx[%d] (FAILED)\n",
1429 (long long)cursor->node->node_offset, i);
1433 * No exact match was found, i is now at the insertion point.
1435 * If inserting split a full leaf before returning. This
1436 * may have the side effect of adjusting cursor->node and
1440 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0 &&
1441 btree_node_is_full(node)) {
1442 error = btree_split_leaf(cursor);
1444 if (error != ENOSPC)
1449 * reload stale pointers
1453 node = &cursor->node->internal;
1458 * We reached a leaf but did not find the key we were looking for.
1459 * If this is an insert we will be properly positioned for an insert
1460 * (ENOENT) or spike (ENOSPC) operation.
1462 error = enospc ? ENOSPC : ENOENT;
1468 * Heuristical search for the first element whos comparison is <= 1. May
1469 * return an index whos compare result is > 1 but may only return an index
1470 * whos compare result is <= 1 if it is the first element with that result.
1473 hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node)
1481 * Don't bother if the node does not have very many elements
1486 i = b + (s - b) / 2;
1487 ++hammer_stats_btree_elements;
1488 r = hammer_btree_cmp(elm, &node->elms[i].leaf.base);
1499 /************************************************************************
1500 * SPLITTING AND MERGING *
1501 ************************************************************************
1503 * These routines do all the dirty work required to split and merge nodes.
1507 * Split an internal node into two nodes and move the separator at the split
1508 * point to the parent.
1510 * (cursor->node, cursor->index) indicates the element the caller intends
1511 * to push into. We will adjust node and index if that element winds
1512 * up in the split node.
1514 * If we are at the root of the filesystem a new root must be created with
1515 * two elements, one pointing to the original root and one pointing to the
1516 * newly allocated split node.
1520 btree_split_internal(hammer_cursor_t cursor)
1522 hammer_node_ondisk_t ondisk;
1524 hammer_node_t parent;
1525 hammer_node_t new_node;
1526 hammer_btree_elm_t elm;
1527 hammer_btree_elm_t parent_elm;
1528 struct hammer_node_lock lockroot;
1529 hammer_mount_t hmp = cursor->trans->hmp;
1536 const int esize = sizeof(*elm);
1538 hammer_node_lock_init(&lockroot, cursor->node);
1539 error = hammer_btree_lock_children(cursor, 1, &lockroot, NULL);
1542 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1544 ++hammer_stats_btree_splits;
1547 * Calculate the split point. If the insertion point is at the
1548 * end of the leaf we adjust the split point significantly to the
1549 * right to try to optimize node fill and flag it. If we hit
1550 * that same leaf again our heuristic failed and we don't try
1551 * to optimize node fill (it could lead to a degenerate case).
1553 node = cursor->node;
1554 ondisk = node->ondisk;
1555 KKASSERT(ondisk->count > 4);
1556 if (cursor->index == ondisk->count &&
1557 (node->flags & HAMMER_NODE_NONLINEAR) == 0) {
1558 split = (ondisk->count + 1) * 3 / 4;
1559 node->flags |= HAMMER_NODE_NONLINEAR;
1562 * We are splitting but elms[split] will be promoted to
1563 * the parent, leaving the right hand node with one less
1564 * element. If the insertion point will be on the
1565 * left-hand side adjust the split point to give the
1566 * right hand side one additional node.
1568 split = (ondisk->count + 1) / 2;
1569 if (cursor->index <= split)
1574 * If we are at the root of the filesystem, create a new root node
1575 * with 1 element and split normally. Avoid making major
1576 * modifications until we know the whole operation will work.
1578 if (ondisk->parent == 0) {
1579 parent = hammer_alloc_btree(cursor->trans, node->node_offset,
1583 hammer_lock_ex(&parent->lock);
1584 hammer_modify_node_noundo(cursor->trans, parent);
1585 ondisk = parent->ondisk;
1588 ondisk->mirror_tid = node->ondisk->mirror_tid;
1589 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1590 ondisk->elms[0].base = hmp->root_btree_beg;
1591 ondisk->elms[0].base.btype = node->ondisk->type;
1592 ondisk->elms[0].internal.subtree_offset = node->node_offset;
1593 ondisk->elms[1].base = hmp->root_btree_end;
1594 hammer_modify_node_done(parent);
1595 /* ondisk->elms[1].base.btype - not used */
1597 parent_index = 0; /* index of current node in parent */
1600 parent = cursor->parent;
1601 parent_index = cursor->parent_index;
1605 * Calculate a hint for the allocation of the new B-Tree node.
1606 * The most likely expansion is coming from the insertion point
1607 * at cursor->index, so try to localize the allocation of our
1608 * new node to accomodate that sub-tree.
1610 * Use the right-most sub-tree when expandinging on the right edge.
1611 * This is a very common case when copying a directory tree.
1613 if (cursor->index == ondisk->count)
1614 hint = ondisk->elms[cursor->index - 1].internal.subtree_offset;
1616 hint = ondisk->elms[cursor->index].internal.subtree_offset;
1619 * Split node into new_node at the split point.
1621 * B O O O P N N B <-- P = node->elms[split] (index 4)
1622 * 0 1 2 3 4 5 6 <-- subtree indices
1627 * B O O O B B N N B <--- inner boundary points are 'P'
1630 new_node = hammer_alloc_btree(cursor->trans, hint, &error);
1631 if (new_node == NULL) {
1633 hammer_unlock(&parent->lock);
1634 hammer_delete_node(cursor->trans, parent);
1635 hammer_rel_node(parent);
1639 hammer_lock_ex(&new_node->lock);
1642 * Create the new node. P becomes the left-hand boundary in the
1643 * new node. Copy the right-hand boundary as well.
1645 * elm is the new separator.
1647 hammer_modify_node_noundo(cursor->trans, new_node);
1648 hammer_modify_node_all(cursor->trans, node);
1649 ondisk = node->ondisk;
1650 elm = &ondisk->elms[split];
1651 bcopy(elm, &new_node->ondisk->elms[0],
1652 (ondisk->count - split + 1) * esize);
1653 new_node->ondisk->count = ondisk->count - split;
1654 new_node->ondisk->parent = parent->node_offset;
1655 new_node->ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1656 new_node->ondisk->mirror_tid = ondisk->mirror_tid;
1657 KKASSERT(ondisk->type == new_node->ondisk->type);
1658 hammer_cursor_split_node(node, new_node, split);
1661 * Cleanup the original node. Elm (P) becomes the new boundary,
1662 * its subtree_offset was moved to the new node. If we had created
1663 * a new root its parent pointer may have changed.
1665 elm->internal.subtree_offset = 0;
1666 ondisk->count = split;
1669 * Insert the separator into the parent, fixup the parent's
1670 * reference to the original node, and reference the new node.
1671 * The separator is P.
1673 * Remember that base.count does not include the right-hand boundary.
1675 hammer_modify_node_all(cursor->trans, parent);
1676 ondisk = parent->ondisk;
1677 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
1678 parent_elm = &ondisk->elms[parent_index+1];
1679 bcopy(parent_elm, parent_elm + 1,
1680 (ondisk->count - parent_index) * esize);
1681 parent_elm->internal.base = elm->base; /* separator P */
1682 parent_elm->internal.base.btype = new_node->ondisk->type;
1683 parent_elm->internal.subtree_offset = new_node->node_offset;
1684 parent_elm->internal.mirror_tid = new_node->ondisk->mirror_tid;
1686 hammer_modify_node_done(parent);
1687 hammer_cursor_inserted_element(parent, parent_index + 1);
1690 * The children of new_node need their parent pointer set to new_node.
1691 * The children have already been locked by
1692 * hammer_btree_lock_children().
1694 for (i = 0; i < new_node->ondisk->count; ++i) {
1695 elm = &new_node->ondisk->elms[i];
1696 error = btree_set_parent(cursor->trans, new_node, elm);
1698 panic("btree_split_internal: btree-fixup problem");
1701 hammer_modify_node_done(new_node);
1704 * The filesystem's root B-Tree pointer may have to be updated.
1707 hammer_volume_t volume;
1709 volume = hammer_get_root_volume(hmp, &error);
1710 KKASSERT(error == 0);
1712 hammer_modify_volume_field(cursor->trans, volume,
1714 volume->ondisk->vol0_btree_root = parent->node_offset;
1715 hammer_modify_volume_done(volume);
1716 node->ondisk->parent = parent->node_offset;
1717 if (cursor->parent) {
1718 hammer_unlock(&cursor->parent->lock);
1719 hammer_rel_node(cursor->parent);
1721 cursor->parent = parent; /* lock'd and ref'd */
1722 hammer_rel_volume(volume, 0);
1724 hammer_modify_node_done(node);
1727 * Ok, now adjust the cursor depending on which element the original
1728 * index was pointing at. If we are >= the split point the push node
1729 * is now in the new node.
1731 * NOTE: If we are at the split point itself we cannot stay with the
1732 * original node because the push index will point at the right-hand
1733 * boundary, which is illegal.
1735 * NOTE: The cursor's parent or parent_index must be adjusted for
1736 * the case where a new parent (new root) was created, and the case
1737 * where the cursor is now pointing at the split node.
1739 if (cursor->index >= split) {
1740 cursor->parent_index = parent_index + 1;
1741 cursor->index -= split;
1742 hammer_unlock(&cursor->node->lock);
1743 hammer_rel_node(cursor->node);
1744 cursor->node = new_node; /* locked and ref'd */
1746 cursor->parent_index = parent_index;
1747 hammer_unlock(&new_node->lock);
1748 hammer_rel_node(new_node);
1752 * Fixup left and right bounds
1754 parent_elm = &parent->ondisk->elms[cursor->parent_index];
1755 cursor->left_bound = &parent_elm[0].internal.base;
1756 cursor->right_bound = &parent_elm[1].internal.base;
1757 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1758 &cursor->node->ondisk->elms[0].internal.base) <= 0);
1759 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1760 &cursor->node->ondisk->elms[cursor->node->ondisk->count].internal.base) >= 0);
1763 hammer_btree_unlock_children(cursor->trans->hmp, &lockroot, NULL);
1764 hammer_cursor_downgrade(cursor);
1769 * Same as the above, but splits a full leaf node.
1775 btree_split_leaf(hammer_cursor_t cursor)
1777 hammer_node_ondisk_t ondisk;
1778 hammer_node_t parent;
1781 hammer_node_t new_leaf;
1782 hammer_btree_elm_t elm;
1783 hammer_btree_elm_t parent_elm;
1784 hammer_base_elm_t mid_boundary;
1790 const size_t esize = sizeof(*elm);
1792 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1794 ++hammer_stats_btree_splits;
1796 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1797 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
1798 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1799 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
1802 * Calculate the split point. If the insertion point is at the
1803 * end of the leaf we adjust the split point significantly to the
1804 * right to try to optimize node fill and flag it. If we hit
1805 * that same leaf again our heuristic failed and we don't try
1806 * to optimize node fill (it could lead to a degenerate case).
1808 * Spikes are made up of two leaf elements which cannot be
1811 leaf = cursor->node;
1812 ondisk = leaf->ondisk;
1813 KKASSERT(ondisk->count > 4);
1814 if (cursor->index == ondisk->count &&
1815 (leaf->flags & HAMMER_NODE_NONLINEAR) == 0) {
1816 split = (ondisk->count + 1) * 3 / 4;
1817 leaf->flags |= HAMMER_NODE_NONLINEAR;
1819 split = (ondisk->count + 1) / 2;
1824 * If the insertion point is at the split point shift the
1825 * split point left so we don't have to worry about
1827 if (cursor->index == split)
1830 KKASSERT(split > 0 && split < ondisk->count);
1835 elm = &ondisk->elms[split];
1837 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm[-1].leaf.base) <= 0);
1838 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->leaf.base) <= 0);
1839 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->leaf.base) > 0);
1840 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm[1].leaf.base) > 0);
1843 * If we are at the root of the tree, create a new root node with
1844 * 1 element and split normally. Avoid making major modifications
1845 * until we know the whole operation will work.
1847 if (ondisk->parent == 0) {
1848 parent = hammer_alloc_btree(cursor->trans, leaf->node_offset,
1852 hammer_lock_ex(&parent->lock);
1853 hammer_modify_node_noundo(cursor->trans, parent);
1854 ondisk = parent->ondisk;
1857 ondisk->mirror_tid = leaf->ondisk->mirror_tid;
1858 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1859 ondisk->elms[0].base = hmp->root_btree_beg;
1860 ondisk->elms[0].base.btype = leaf->ondisk->type;
1861 ondisk->elms[0].internal.subtree_offset = leaf->node_offset;
1862 ondisk->elms[1].base = hmp->root_btree_end;
1863 /* ondisk->elms[1].base.btype = not used */
1864 hammer_modify_node_done(parent);
1866 parent_index = 0; /* insertion point in parent */
1869 parent = cursor->parent;
1870 parent_index = cursor->parent_index;
1874 * Calculate a hint for the allocation of the new B-Tree leaf node.
1875 * For now just try to localize it within the same bigblock as
1878 * If the insertion point is at the end of the leaf we recognize a
1879 * likely append sequence of some sort (data, meta-data, inodes,
1880 * whatever). Set the hint to zero to allocate out of linear space
1881 * instead of trying to completely fill previously hinted space.
1883 * This also sets the stage for recursive splits to localize using
1886 ondisk = leaf->ondisk;
1887 if (cursor->index == ondisk->count)
1890 hint = leaf->node_offset;
1893 * Split leaf into new_leaf at the split point. Select a separator
1894 * value in-between the two leafs but with a bent towards the right
1895 * leaf since comparisons use an 'elm >= separator' inequality.
1904 new_leaf = hammer_alloc_btree(cursor->trans, hint, &error);
1905 if (new_leaf == NULL) {
1907 hammer_unlock(&parent->lock);
1908 hammer_delete_node(cursor->trans, parent);
1909 hammer_rel_node(parent);
1913 hammer_lock_ex(&new_leaf->lock);
1916 * Create the new node and copy the leaf elements from the split
1917 * point on to the new node.
1919 hammer_modify_node_all(cursor->trans, leaf);
1920 hammer_modify_node_noundo(cursor->trans, new_leaf);
1921 ondisk = leaf->ondisk;
1922 elm = &ondisk->elms[split];
1923 bcopy(elm, &new_leaf->ondisk->elms[0], (ondisk->count - split) * esize);
1924 new_leaf->ondisk->count = ondisk->count - split;
1925 new_leaf->ondisk->parent = parent->node_offset;
1926 new_leaf->ondisk->type = HAMMER_BTREE_TYPE_LEAF;
1927 new_leaf->ondisk->mirror_tid = ondisk->mirror_tid;
1928 KKASSERT(ondisk->type == new_leaf->ondisk->type);
1929 hammer_modify_node_done(new_leaf);
1930 hammer_cursor_split_node(leaf, new_leaf, split);
1933 * Cleanup the original node. Because this is a leaf node and
1934 * leaf nodes do not have a right-hand boundary, there
1935 * aren't any special edge cases to clean up. We just fixup the
1938 ondisk->count = split;
1941 * Insert the separator into the parent, fixup the parent's
1942 * reference to the original node, and reference the new node.
1943 * The separator is P.
1945 * Remember that base.count does not include the right-hand boundary.
1946 * We are copying parent_index+1 to parent_index+2, not +0 to +1.
1948 hammer_modify_node_all(cursor->trans, parent);
1949 ondisk = parent->ondisk;
1950 KKASSERT(split != 0);
1951 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
1952 parent_elm = &ondisk->elms[parent_index+1];
1953 bcopy(parent_elm, parent_elm + 1,
1954 (ondisk->count - parent_index) * esize);
1956 hammer_make_separator(&elm[-1].base, &elm[0].base, &parent_elm->base);
1957 parent_elm->internal.base.btype = new_leaf->ondisk->type;
1958 parent_elm->internal.subtree_offset = new_leaf->node_offset;
1959 parent_elm->internal.mirror_tid = new_leaf->ondisk->mirror_tid;
1960 mid_boundary = &parent_elm->base;
1962 hammer_modify_node_done(parent);
1963 hammer_cursor_inserted_element(parent, parent_index + 1);
1966 * The filesystem's root B-Tree pointer may have to be updated.
1969 hammer_volume_t volume;
1971 volume = hammer_get_root_volume(hmp, &error);
1972 KKASSERT(error == 0);
1974 hammer_modify_volume_field(cursor->trans, volume,
1976 volume->ondisk->vol0_btree_root = parent->node_offset;
1977 hammer_modify_volume_done(volume);
1978 leaf->ondisk->parent = parent->node_offset;
1979 if (cursor->parent) {
1980 hammer_unlock(&cursor->parent->lock);
1981 hammer_rel_node(cursor->parent);
1983 cursor->parent = parent; /* lock'd and ref'd */
1984 hammer_rel_volume(volume, 0);
1986 hammer_modify_node_done(leaf);
1989 * Ok, now adjust the cursor depending on which element the original
1990 * index was pointing at. If we are >= the split point the push node
1991 * is now in the new node.
1993 * NOTE: If we are at the split point itself we need to select the
1994 * old or new node based on where key_beg's insertion point will be.
1995 * If we pick the wrong side the inserted element will wind up in
1996 * the wrong leaf node and outside that node's bounds.
1998 if (cursor->index > split ||
1999 (cursor->index == split &&
2000 hammer_btree_cmp(&cursor->key_beg, mid_boundary) >= 0)) {
2001 cursor->parent_index = parent_index + 1;
2002 cursor->index -= split;
2003 hammer_unlock(&cursor->node->lock);
2004 hammer_rel_node(cursor->node);
2005 cursor->node = new_leaf;
2007 cursor->parent_index = parent_index;
2008 hammer_unlock(&new_leaf->lock);
2009 hammer_rel_node(new_leaf);
2013 * Fixup left and right bounds
2015 parent_elm = &parent->ondisk->elms[cursor->parent_index];
2016 cursor->left_bound = &parent_elm[0].internal.base;
2017 cursor->right_bound = &parent_elm[1].internal.base;
2020 * Assert that the bounds are correct.
2022 KKASSERT(hammer_btree_cmp(cursor->left_bound,
2023 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
2024 KKASSERT(hammer_btree_cmp(cursor->right_bound,
2025 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
2026 KKASSERT(hammer_btree_cmp(cursor->left_bound, &cursor->key_beg) <= 0);
2027 KKASSERT(hammer_btree_cmp(cursor->right_bound, &cursor->key_beg) > 0);
2030 hammer_cursor_downgrade(cursor);
2037 * Recursively correct the right-hand boundary's create_tid to (tid) as
2038 * long as the rest of the key matches. We have to recurse upward in
2039 * the tree as well as down the left side of each parent's right node.
2041 * Return EDEADLK if we were only partially successful, forcing the caller
2042 * to try again. The original cursor is not modified. This routine can
2043 * also fail with EDEADLK if it is forced to throw away a portion of its
2046 * The caller must pass a downgraded cursor to us (otherwise we can't dup it).
2049 TAILQ_ENTRY(hammer_rhb) entry;
2054 TAILQ_HEAD(hammer_rhb_list, hammer_rhb);
2057 hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid)
2059 struct hammer_mount *hmp;
2060 struct hammer_rhb_list rhb_list;
2061 hammer_base_elm_t elm;
2062 hammer_node_t orig_node;
2063 struct hammer_rhb *rhb;
2067 TAILQ_INIT(&rhb_list);
2068 hmp = cursor->trans->hmp;
2071 * Save our position so we can restore it on return. This also
2072 * gives us a stable 'elm'.
2074 orig_node = cursor->node;
2075 hammer_ref_node(orig_node);
2076 hammer_lock_sh(&orig_node->lock);
2077 orig_index = cursor->index;
2078 elm = &orig_node->ondisk->elms[orig_index].base;
2081 * Now build a list of parents going up, allocating a rhb
2082 * structure for each one.
2084 while (cursor->parent) {
2086 * Stop if we no longer have any right-bounds to fix up
2088 if (elm->obj_id != cursor->right_bound->obj_id ||
2089 elm->rec_type != cursor->right_bound->rec_type ||
2090 elm->key != cursor->right_bound->key) {
2095 * Stop if the right-hand bound's create_tid does not
2096 * need to be corrected.
2098 if (cursor->right_bound->create_tid >= tid)
2101 rhb = kmalloc(sizeof(*rhb), hmp->m_misc, M_WAITOK|M_ZERO);
2102 rhb->node = cursor->parent;
2103 rhb->index = cursor->parent_index;
2104 hammer_ref_node(rhb->node);
2105 hammer_lock_sh(&rhb->node->lock);
2106 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
2108 hammer_cursor_up(cursor);
2112 * now safely adjust the right hand bound for each rhb. This may
2113 * also require taking the right side of the tree and iterating down
2117 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2118 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
2121 TAILQ_REMOVE(&rhb_list, rhb, entry);
2122 hammer_unlock(&rhb->node->lock);
2123 hammer_rel_node(rhb->node);
2124 kfree(rhb, hmp->m_misc);
2126 switch (cursor->node->ondisk->type) {
2127 case HAMMER_BTREE_TYPE_INTERNAL:
2129 * Right-boundary for parent at internal node
2130 * is one element to the right of the element whos
2131 * right boundary needs adjusting. We must then
2132 * traverse down the left side correcting any left
2133 * bounds (which may now be too far to the left).
2136 error = hammer_btree_correct_lhb(cursor, tid);
2139 panic("hammer_btree_correct_rhb(): Bad node type");
2148 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2149 TAILQ_REMOVE(&rhb_list, rhb, entry);
2150 hammer_unlock(&rhb->node->lock);
2151 hammer_rel_node(rhb->node);
2152 kfree(rhb, hmp->m_misc);
2154 error = hammer_cursor_seek(cursor, orig_node, orig_index);
2155 hammer_unlock(&orig_node->lock);
2156 hammer_rel_node(orig_node);
2161 * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand
2162 * bound going downward starting at the current cursor position.
2164 * This function does not restore the cursor after use.
2167 hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid)
2169 struct hammer_rhb_list rhb_list;
2170 hammer_base_elm_t elm;
2171 hammer_base_elm_t cmp;
2172 struct hammer_rhb *rhb;
2173 struct hammer_mount *hmp;
2176 TAILQ_INIT(&rhb_list);
2177 hmp = cursor->trans->hmp;
2179 cmp = &cursor->node->ondisk->elms[cursor->index].base;
2182 * Record the node and traverse down the left-hand side for all
2183 * matching records needing a boundary correction.
2187 rhb = kmalloc(sizeof(*rhb), hmp->m_misc, M_WAITOK|M_ZERO);
2188 rhb->node = cursor->node;
2189 rhb->index = cursor->index;
2190 hammer_ref_node(rhb->node);
2191 hammer_lock_sh(&rhb->node->lock);
2192 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
2194 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
2196 * Nothing to traverse down if we are at the right
2197 * boundary of an internal node.
2199 if (cursor->index == cursor->node->ondisk->count)
2202 elm = &cursor->node->ondisk->elms[cursor->index].base;
2203 if (elm->btype == HAMMER_BTREE_TYPE_RECORD)
2205 panic("Illegal leaf record type %02x", elm->btype);
2207 error = hammer_cursor_down(cursor);
2211 elm = &cursor->node->ondisk->elms[cursor->index].base;
2212 if (elm->obj_id != cmp->obj_id ||
2213 elm->rec_type != cmp->rec_type ||
2214 elm->key != cmp->key) {
2217 if (elm->create_tid >= tid)
2223 * Now we can safely adjust the left-hand boundary from the bottom-up.
2224 * The last element we remove from the list is the caller's right hand
2225 * boundary, which must also be adjusted.
2227 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2228 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
2231 TAILQ_REMOVE(&rhb_list, rhb, entry);
2232 hammer_unlock(&rhb->node->lock);
2233 hammer_rel_node(rhb->node);
2234 kfree(rhb, hmp->m_misc);
2236 elm = &cursor->node->ondisk->elms[cursor->index].base;
2237 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
2238 hammer_modify_node(cursor->trans, cursor->node,
2240 sizeof(elm->create_tid));
2241 elm->create_tid = tid;
2242 hammer_modify_node_done(cursor->node);
2244 panic("hammer_btree_correct_lhb(): Bad element type");
2251 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
2252 TAILQ_REMOVE(&rhb_list, rhb, entry);
2253 hammer_unlock(&rhb->node->lock);
2254 hammer_rel_node(rhb->node);
2255 kfree(rhb, hmp->m_misc);
2263 * Attempt to remove the locked, empty or want-to-be-empty B-Tree node at
2264 * (cursor->node). Returns 0 on success, EDEADLK if we could not complete
2265 * the operation due to a deadlock, or some other error.
2267 * This routine is initially called with an empty leaf and may be
2268 * recursively called with single-element internal nodes.
2270 * It should also be noted that when removing empty leaves we must be sure
2271 * to test and update mirror_tid because another thread may have deadlocked
2272 * against us (or someone) trying to propagate it up and cannot retry once
2273 * the node has been deleted.
2275 * On return the cursor may end up pointing to an internal node, suitable
2276 * for further iteration but not for an immediate insertion or deletion.
2279 btree_remove(hammer_cursor_t cursor)
2281 hammer_node_ondisk_t ondisk;
2282 hammer_btree_elm_t elm;
2284 hammer_node_t parent;
2285 const int esize = sizeof(*elm);
2288 node = cursor->node;
2291 * When deleting the root of the filesystem convert it to
2292 * an empty leaf node. Internal nodes cannot be empty.
2294 ondisk = node->ondisk;
2295 if (ondisk->parent == 0) {
2296 KKASSERT(cursor->parent == NULL);
2297 hammer_modify_node_all(cursor->trans, node);
2298 KKASSERT(ondisk == node->ondisk);
2299 ondisk->type = HAMMER_BTREE_TYPE_LEAF;
2301 hammer_modify_node_done(node);
2306 parent = cursor->parent;
2309 * Attempt to remove the parent's reference to the child. If the
2310 * parent would become empty we have to recurse. If we fail we
2311 * leave the parent pointing to an empty leaf node.
2313 * We have to recurse successfully before we can delete the internal
2314 * node as it is illegal to have empty internal nodes. Even though
2315 * the operation may be aborted we must still fixup any unlocked
2316 * cursors as if we had deleted the element prior to recursing
2317 * (by calling hammer_cursor_deleted_element()) so those cursors
2318 * are properly forced up the chain by the recursion.
2320 if (parent->ondisk->count == 1) {
2322 * This special cursor_up_locked() call leaves the original
2323 * node exclusively locked and referenced, leaves the
2324 * original parent locked (as the new node), and locks the
2325 * new parent. It can return EDEADLK.
2327 * We cannot call hammer_cursor_removed_node() until we are
2328 * actually able to remove the node. If we did then tracked
2329 * cursors in the middle of iterations could be repointed
2330 * to a parent node. If this occurs they could end up
2331 * scanning newly inserted records into the node (that could
2332 * not be deleted) when they push down again.
2334 * Due to the way the recursion works the final parent is left
2335 * in cursor->parent after the recursion returns. Each
2336 * layer on the way back up is thus able to call
2337 * hammer_cursor_removed_node() and 'jump' the node up to
2338 * the (same) final parent.
2340 * NOTE! The local variable 'parent' is invalid after we
2341 * call hammer_cursor_up_locked().
2343 error = hammer_cursor_up_locked(cursor);
2347 hammer_cursor_deleted_element(cursor->node, 0);
2348 error = btree_remove(cursor);
2350 KKASSERT(node != cursor->node);
2351 hammer_cursor_removed_node(
2354 hammer_modify_node_all(cursor->trans, node);
2355 ondisk = node->ondisk;
2356 ondisk->type = HAMMER_BTREE_TYPE_DELETED;
2358 hammer_modify_node_done(node);
2359 hammer_flush_node(node, 0);
2360 hammer_delete_node(cursor->trans, node);
2363 * Defer parent removal because we could not
2364 * get the lock, just let the leaf remain
2369 hammer_unlock(&node->lock);
2370 hammer_rel_node(node);
2373 * Defer parent removal because we could not
2374 * get the lock, just let the leaf remain
2380 KKASSERT(parent->ondisk->count > 1);
2382 hammer_modify_node_all(cursor->trans, parent);
2383 ondisk = parent->ondisk;
2384 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
2386 elm = &ondisk->elms[cursor->parent_index];
2387 KKASSERT(elm->internal.subtree_offset == node->node_offset);
2388 KKASSERT(ondisk->count > 0);
2391 * We must retain the highest mirror_tid. The deleted
2392 * range is now encompassed by the element to the left.
2393 * If we are already at the left edge the new left edge
2394 * inherits mirror_tid.
2396 * Note that bounds of the parent to our parent may create
2397 * a gap to the left of our left-most node or to the right
2398 * of our right-most node. The gap is silently included
2399 * in the mirror_tid's area of effect from the point of view
2402 if (cursor->parent_index) {
2403 if (elm[-1].internal.mirror_tid <
2404 elm[0].internal.mirror_tid) {
2405 elm[-1].internal.mirror_tid =
2406 elm[0].internal.mirror_tid;
2409 if (elm[1].internal.mirror_tid <
2410 elm[0].internal.mirror_tid) {
2411 elm[1].internal.mirror_tid =
2412 elm[0].internal.mirror_tid;
2417 * Delete the subtree reference in the parent. Include
2418 * boundary element at end.
2420 bcopy(&elm[1], &elm[0],
2421 (ondisk->count - cursor->parent_index) * esize);
2423 hammer_modify_node_done(parent);
2424 hammer_cursor_removed_node(node, parent, cursor->parent_index);
2425 hammer_cursor_deleted_element(parent, cursor->parent_index);
2426 hammer_flush_node(node, 0);
2427 hammer_delete_node(cursor->trans, node);
2430 * cursor->node is invalid, cursor up to make the cursor
2431 * valid again. We have to flag the condition in case
2432 * another thread wiggles an insertion in during an
2435 cursor->flags |= HAMMER_CURSOR_ITERATE_CHECK;
2436 error = hammer_cursor_up(cursor);
2442 * Propagate cursor->trans->tid up the B-Tree starting at the current
2443 * cursor position using pseudofs info gleaned from the passed inode.
2445 * The passed inode has no relationship to the cursor position other
2446 * then being in the same pseudofs as the insertion or deletion we
2447 * are propagating the mirror_tid for.
2449 * WARNING! Because we push and pop the passed cursor, it may be
2450 * modified by other B-Tree operations while it is unlocked
2451 * and things like the node & leaf pointers, and indexes might
2455 hammer_btree_do_propagation(hammer_cursor_t cursor,
2456 hammer_pseudofs_inmem_t pfsm,
2457 hammer_btree_leaf_elm_t leaf)
2459 hammer_cursor_t ncursor;
2460 hammer_tid_t mirror_tid;
2464 * We do not propagate a mirror_tid if the filesystem was mounted
2465 * in no-mirror mode.
2467 if (cursor->trans->hmp->master_id < 0)
2471 * This is a bit of a hack because we cannot deadlock or return
2472 * EDEADLK here. The related operation has already completed and
2473 * we must propagate the mirror_tid now regardless.
2475 * Generate a new cursor which inherits the original's locks and
2476 * unlock the original. Use the new cursor to propagate the
2477 * mirror_tid. Then clean up the new cursor and reacquire locks
2480 * hammer_dup_cursor() cannot dup locks. The dup inherits the
2481 * original's locks and the original is tracked and must be
2484 mirror_tid = cursor->node->ondisk->mirror_tid;
2485 KKASSERT(mirror_tid != 0);
2486 ncursor = hammer_push_cursor(cursor);
2487 error = hammer_btree_mirror_propagate(ncursor, mirror_tid);
2488 KKASSERT(error == 0);
2489 hammer_pop_cursor(cursor, ncursor);
2490 /* WARNING: cursor's leaf pointer may change after pop */
2495 * Propagate a mirror TID update upwards through the B-Tree to the root.
2497 * A locked internal node must be passed in. The node will remain locked
2500 * This function syncs mirror_tid at the specified internal node's element,
2501 * adjusts the node's aggregation mirror_tid, and then recurses upwards.
2504 hammer_btree_mirror_propagate(hammer_cursor_t cursor, hammer_tid_t mirror_tid)
2506 hammer_btree_internal_elm_t elm;
2511 error = hammer_cursor_up(cursor);
2513 error = hammer_cursor_upgrade(cursor);
2516 * We can ignore HAMMER_CURSOR_ITERATE_CHECK, the
2517 * cursor will still be properly positioned for
2518 * mirror propagation, just not for iterations.
2520 while (error == EDEADLK) {
2521 hammer_recover_cursor(cursor);
2522 error = hammer_cursor_upgrade(cursor);
2528 * If the cursor deadlocked it could end up at a leaf
2529 * after we lost the lock.
2531 node = cursor->node;
2532 if (node->ondisk->type != HAMMER_BTREE_TYPE_INTERNAL)
2536 * Adjust the node's element
2538 elm = &node->ondisk->elms[cursor->index].internal;
2539 if (elm->mirror_tid >= mirror_tid)
2541 hammer_modify_node(cursor->trans, node, &elm->mirror_tid,
2542 sizeof(elm->mirror_tid));
2543 elm->mirror_tid = mirror_tid;
2544 hammer_modify_node_done(node);
2545 if (hammer_debug_general & 0x0002) {
2546 kprintf("mirror_propagate: propagate "
2547 "%016llx @%016llx:%d\n",
2548 (long long)mirror_tid,
2549 (long long)node->node_offset,
2555 * Adjust the node's mirror_tid aggregator
2557 if (node->ondisk->mirror_tid >= mirror_tid)
2559 hammer_modify_node_field(cursor->trans, node, mirror_tid);
2560 node->ondisk->mirror_tid = mirror_tid;
2561 hammer_modify_node_done(node);
2562 if (hammer_debug_general & 0x0002) {
2563 kprintf("mirror_propagate: propagate "
2564 "%016llx @%016llx\n",
2565 (long long)mirror_tid,
2566 (long long)node->node_offset);
2569 if (error == ENOENT)
2575 hammer_btree_get_parent(hammer_transaction_t trans, hammer_node_t node,
2576 int *parent_indexp, int *errorp, int try_exclusive)
2578 hammer_node_t parent;
2579 hammer_btree_elm_t elm;
2585 parent = hammer_get_node(trans, node->ondisk->parent, 0, errorp);
2587 KKASSERT(parent == NULL);
2590 KKASSERT ((parent->flags & HAMMER_NODE_DELETED) == 0);
2595 if (try_exclusive) {
2596 if (hammer_lock_ex_try(&parent->lock)) {
2597 hammer_rel_node(parent);
2602 hammer_lock_sh(&parent->lock);
2606 * Figure out which element in the parent is pointing to the
2609 if (node->ondisk->count) {
2610 i = hammer_btree_search_node(&node->ondisk->elms[0].base,
2615 while (i < parent->ondisk->count) {
2616 elm = &parent->ondisk->elms[i];
2617 if (elm->internal.subtree_offset == node->node_offset)
2621 if (i == parent->ondisk->count) {
2622 hammer_unlock(&parent->lock);
2623 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
2626 KKASSERT(*errorp == 0);
2631 * The element (elm) has been moved to a new internal node (node).
2633 * If the element represents a pointer to an internal node that node's
2634 * parent must be adjusted to the element's new location.
2636 * XXX deadlock potential here with our exclusive locks
2639 btree_set_parent(hammer_transaction_t trans, hammer_node_t node,
2640 hammer_btree_elm_t elm)
2642 hammer_node_t child;
2647 switch(elm->base.btype) {
2648 case HAMMER_BTREE_TYPE_INTERNAL:
2649 case HAMMER_BTREE_TYPE_LEAF:
2650 child = hammer_get_node(trans, elm->internal.subtree_offset,
2653 hammer_modify_node_field(trans, child, parent);
2654 child->ondisk->parent = node->node_offset;
2655 hammer_modify_node_done(child);
2656 hammer_rel_node(child);
2666 * Initialize the root of a recursive B-Tree node lock list structure.
2669 hammer_node_lock_init(hammer_node_lock_t parent, hammer_node_t node)
2671 TAILQ_INIT(&parent->list);
2672 parent->parent = NULL;
2673 parent->node = node;
2675 parent->count = node->ondisk->count;
2676 parent->copy = NULL;
2681 * Initialize a cache of hammer_node_lock's including space allocated
2684 * This is used by the rebalancing code to preallocate the copy space
2685 * for ~4096 B-Tree nodes (16MB of data) prior to acquiring any HAMMER
2686 * locks, otherwise we can blow out the pageout daemon's emergency
2687 * reserve and deadlock it.
2689 * NOTE: HAMMER_NODE_LOCK_LCACHE is not set on items cached in the lcache.
2690 * The flag is set when the item is pulled off the cache for use.
2693 hammer_btree_lcache_init(hammer_mount_t hmp, hammer_node_lock_t lcache,
2696 hammer_node_lock_t item;
2699 for (count = 1; depth; --depth)
2700 count *= HAMMER_BTREE_LEAF_ELMS;
2701 bzero(lcache, sizeof(*lcache));
2702 TAILQ_INIT(&lcache->list);
2704 item = kmalloc(sizeof(*item), hmp->m_misc, M_WAITOK|M_ZERO);
2705 item->copy = kmalloc(sizeof(*item->copy),
2706 hmp->m_misc, M_WAITOK);
2707 TAILQ_INIT(&item->list);
2708 TAILQ_INSERT_TAIL(&lcache->list, item, entry);
2714 hammer_btree_lcache_free(hammer_mount_t hmp, hammer_node_lock_t lcache)
2716 hammer_node_lock_t item;
2718 while ((item = TAILQ_FIRST(&lcache->list)) != NULL) {
2719 TAILQ_REMOVE(&lcache->list, item, entry);
2720 KKASSERT(item->copy);
2721 KKASSERT(TAILQ_EMPTY(&item->list));
2722 kfree(item->copy, hmp->m_misc);
2723 kfree(item, hmp->m_misc);
2725 KKASSERT(lcache->copy == NULL);
2729 * Exclusively lock all the children of node. This is used by the split
2730 * code to prevent anyone from accessing the children of a cursor node
2731 * while we fix-up its parent offset.
2733 * If we don't lock the children we can really mess up cursors which block
2734 * trying to cursor-up into our node.
2736 * On failure EDEADLK (or some other error) is returned. If a deadlock
2737 * error is returned the cursor is adjusted to block on termination.
2739 * The caller is responsible for managing parent->node, the root's node
2740 * is usually aliased from a cursor.
2743 hammer_btree_lock_children(hammer_cursor_t cursor, int depth,
2744 hammer_node_lock_t parent,
2745 hammer_node_lock_t lcache)
2748 hammer_node_lock_t item;
2749 hammer_node_ondisk_t ondisk;
2750 hammer_btree_elm_t elm;
2751 hammer_node_t child;
2752 struct hammer_mount *hmp;
2756 node = parent->node;
2757 ondisk = node->ondisk;
2759 hmp = cursor->trans->hmp;
2762 * We really do not want to block on I/O with exclusive locks held,
2763 * pre-get the children before trying to lock the mess. This is
2764 * only done one-level deep for now.
2766 for (i = 0; i < ondisk->count; ++i) {
2767 ++hammer_stats_btree_elements;
2768 elm = &ondisk->elms[i];
2769 if (elm->base.btype != HAMMER_BTREE_TYPE_LEAF &&
2770 elm->base.btype != HAMMER_BTREE_TYPE_INTERNAL) {
2773 child = hammer_get_node(cursor->trans,
2774 elm->internal.subtree_offset,
2777 hammer_rel_node(child);
2783 for (i = 0; error == 0 && i < ondisk->count; ++i) {
2784 ++hammer_stats_btree_elements;
2785 elm = &ondisk->elms[i];
2787 switch(elm->base.btype) {
2788 case HAMMER_BTREE_TYPE_INTERNAL:
2789 case HAMMER_BTREE_TYPE_LEAF:
2790 KKASSERT(elm->internal.subtree_offset != 0);
2791 child = hammer_get_node(cursor->trans,
2792 elm->internal.subtree_offset,
2800 if (hammer_lock_ex_try(&child->lock) != 0) {
2801 if (cursor->deadlk_node == NULL) {
2802 cursor->deadlk_node = child;
2803 hammer_ref_node(cursor->deadlk_node);
2806 hammer_rel_node(child);
2809 item = TAILQ_FIRST(&lcache->list);
2810 KKASSERT(item != NULL);
2811 item->flags |= HAMMER_NODE_LOCK_LCACHE;
2812 TAILQ_REMOVE(&lcache->list,
2815 item = kmalloc(sizeof(*item),
2818 TAILQ_INIT(&item->list);
2821 TAILQ_INSERT_TAIL(&parent->list, item, entry);
2822 item->parent = parent;
2825 item->count = child->ondisk->count;
2828 * Recurse (used by the rebalancing code)
2830 if (depth > 1 && elm->base.btype == HAMMER_BTREE_TYPE_INTERNAL) {
2831 error = hammer_btree_lock_children(
2841 hammer_btree_unlock_children(hmp, parent, lcache);
2846 * Create an in-memory copy of all B-Tree nodes listed, recursively,
2847 * including the parent.
2850 hammer_btree_lock_copy(hammer_cursor_t cursor, hammer_node_lock_t parent)
2852 hammer_mount_t hmp = cursor->trans->hmp;
2853 hammer_node_lock_t item;
2855 if (parent->copy == NULL) {
2856 KKASSERT((parent->flags & HAMMER_NODE_LOCK_LCACHE) == 0);
2857 parent->copy = kmalloc(sizeof(*parent->copy),
2858 hmp->m_misc, M_WAITOK);
2860 KKASSERT((parent->flags & HAMMER_NODE_LOCK_UPDATED) == 0);
2861 *parent->copy = *parent->node->ondisk;
2862 TAILQ_FOREACH(item, &parent->list, entry) {
2863 hammer_btree_lock_copy(cursor, item);
2868 * Recursively sync modified copies to the media.
2871 hammer_btree_sync_copy(hammer_cursor_t cursor, hammer_node_lock_t parent)
2873 hammer_node_lock_t item;
2876 if (parent->flags & HAMMER_NODE_LOCK_UPDATED) {
2878 hammer_modify_node_all(cursor->trans, parent->node);
2879 *parent->node->ondisk = *parent->copy;
2880 hammer_modify_node_done(parent->node);
2881 if (parent->copy->type == HAMMER_BTREE_TYPE_DELETED) {
2882 hammer_flush_node(parent->node, 0);
2883 hammer_delete_node(cursor->trans, parent->node);
2886 TAILQ_FOREACH(item, &parent->list, entry) {
2887 count += hammer_btree_sync_copy(cursor, item);
2893 * Release previously obtained node locks. The caller is responsible for
2894 * cleaning up parent->node itself (its usually just aliased from a cursor),
2895 * but this function will take care of the copies.
2897 * NOTE: The root node is not placed in the lcache and node->copy is not
2898 * deallocated when lcache != NULL.
2901 hammer_btree_unlock_children(hammer_mount_t hmp, hammer_node_lock_t parent,
2902 hammer_node_lock_t lcache)
2904 hammer_node_lock_t item;
2905 hammer_node_ondisk_t copy;
2907 while ((item = TAILQ_FIRST(&parent->list)) != NULL) {
2908 TAILQ_REMOVE(&parent->list, item, entry);
2909 hammer_btree_unlock_children(hmp, item, lcache);
2910 hammer_unlock(&item->node->lock);
2911 hammer_rel_node(item->node);
2914 * NOTE: When placing the item back in the lcache
2915 * the flag is cleared by the bzero().
2916 * Remaining fields are cleared as a safety
2919 KKASSERT(item->flags & HAMMER_NODE_LOCK_LCACHE);
2920 KKASSERT(TAILQ_EMPTY(&item->list));
2922 bzero(item, sizeof(*item));
2923 TAILQ_INIT(&item->list);
2926 bzero(copy, sizeof(*copy));
2927 TAILQ_INSERT_TAIL(&lcache->list, item, entry);
2929 kfree(item, hmp->m_misc);
2932 if (parent->copy && (parent->flags & HAMMER_NODE_LOCK_LCACHE) == 0) {
2933 kfree(parent->copy, hmp->m_misc);
2934 parent->copy = NULL; /* safety */
2938 /************************************************************************
2939 * MISCELLANIOUS SUPPORT *
2940 ************************************************************************/
2943 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
2945 * Note that for this particular function a return value of -1, 0, or +1
2946 * can denote a match if create_tid is otherwise discounted. A create_tid
2947 * of zero is considered to be 'infinity' in comparisons.
2949 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
2952 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
2954 if (key1->localization < key2->localization)
2956 if (key1->localization > key2->localization)
2959 if (key1->obj_id < key2->obj_id)
2961 if (key1->obj_id > key2->obj_id)
2964 if (key1->rec_type < key2->rec_type)
2966 if (key1->rec_type > key2->rec_type)
2969 if (key1->key < key2->key)
2971 if (key1->key > key2->key)
2975 * A create_tid of zero indicates a record which is undeletable
2976 * and must be considered to have a value of positive infinity.
2978 if (key1->create_tid == 0) {
2979 if (key2->create_tid == 0)
2983 if (key2->create_tid == 0)
2985 if (key1->create_tid < key2->create_tid)
2987 if (key1->create_tid > key2->create_tid)
2993 * Test a timestamp against an element to determine whether the
2994 * element is visible. A timestamp of 0 means 'infinity'.
2997 hammer_btree_chkts(hammer_tid_t asof, hammer_base_elm_t base)
3000 if (base->delete_tid)
3004 if (asof < base->create_tid)
3006 if (base->delete_tid && asof >= base->delete_tid)
3012 * Create a separator half way inbetween key1 and key2. For fields just
3013 * one unit apart, the separator will match key2. key1 is on the left-hand
3014 * side and key2 is on the right-hand side.
3016 * key2 must be >= the separator. It is ok for the separator to match key2.
3018 * NOTE: Even if key1 does not match key2, the separator may wind up matching
3021 * NOTE: It might be beneficial to just scrap this whole mess and just
3022 * set the separator to key2.
3024 #define MAKE_SEPARATOR(key1, key2, dest, field) \
3025 dest->field = key1->field + ((key2->field - key1->field + 1) >> 1);
3028 hammer_make_separator(hammer_base_elm_t key1, hammer_base_elm_t key2,
3029 hammer_base_elm_t dest)
3031 bzero(dest, sizeof(*dest));
3033 dest->rec_type = key2->rec_type;
3034 dest->key = key2->key;
3035 dest->obj_id = key2->obj_id;
3036 dest->create_tid = key2->create_tid;
3038 MAKE_SEPARATOR(key1, key2, dest, localization);
3039 if (key1->localization == key2->localization) {
3040 MAKE_SEPARATOR(key1, key2, dest, obj_id);
3041 if (key1->obj_id == key2->obj_id) {
3042 MAKE_SEPARATOR(key1, key2, dest, rec_type);
3043 if (key1->rec_type == key2->rec_type) {
3044 MAKE_SEPARATOR(key1, key2, dest, key);
3046 * Don't bother creating a separator for
3047 * create_tid, which also conveniently avoids
3048 * having to handle the create_tid == 0
3049 * (infinity) case. Just leave create_tid
3052 * Worst case, dest matches key2 exactly,
3053 * which is acceptable.
3060 #undef MAKE_SEPARATOR
3063 * Return whether a generic internal or leaf node is full
3066 btree_node_is_full(hammer_node_ondisk_t node)
3068 switch(node->type) {
3069 case HAMMER_BTREE_TYPE_INTERNAL:
3070 if (node->count == HAMMER_BTREE_INT_ELMS)
3073 case HAMMER_BTREE_TYPE_LEAF:
3074 if (node->count == HAMMER_BTREE_LEAF_ELMS)
3078 panic("illegal btree subtype");
3085 btree_max_elements(u_int8_t type)
3087 if (type == HAMMER_BTREE_TYPE_LEAF)
3088 return(HAMMER_BTREE_LEAF_ELMS);
3089 if (type == HAMMER_BTREE_TYPE_INTERNAL)
3090 return(HAMMER_BTREE_INT_ELMS);
3091 panic("btree_max_elements: bad type %d\n", type);
3096 hammer_print_btree_node(hammer_node_ondisk_t ondisk)
3098 hammer_btree_elm_t elm;
3101 kprintf("node %p count=%d parent=%016llx type=%c\n",
3102 ondisk, ondisk->count,
3103 (long long)ondisk->parent, ondisk->type);
3106 * Dump both boundary elements if an internal node
3108 if (ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
3109 for (i = 0; i <= ondisk->count; ++i) {
3110 elm = &ondisk->elms[i];
3111 hammer_print_btree_elm(elm, ondisk->type, i);
3114 for (i = 0; i < ondisk->count; ++i) {
3115 elm = &ondisk->elms[i];
3116 hammer_print_btree_elm(elm, ondisk->type, i);
3122 hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i)
3125 kprintf("\tobj_id = %016llx\n", (long long)elm->base.obj_id);
3126 kprintf("\tkey = %016llx\n", (long long)elm->base.key);
3127 kprintf("\tcreate_tid = %016llx\n", (long long)elm->base.create_tid);
3128 kprintf("\tdelete_tid = %016llx\n", (long long)elm->base.delete_tid);
3129 kprintf("\trec_type = %04x\n", elm->base.rec_type);
3130 kprintf("\tobj_type = %02x\n", elm->base.obj_type);
3131 kprintf("\tbtype = %02x (%c)\n",
3133 (elm->base.btype ? elm->base.btype : '?'));
3134 kprintf("\tlocalization = %02x\n", elm->base.localization);
3137 case HAMMER_BTREE_TYPE_INTERNAL:
3138 kprintf("\tsubtree_off = %016llx\n",
3139 (long long)elm->internal.subtree_offset);
3141 case HAMMER_BTREE_TYPE_RECORD:
3142 kprintf("\tdata_offset = %016llx\n",
3143 (long long)elm->leaf.data_offset);
3144 kprintf("\tdata_len = %08x\n", elm->leaf.data_len);
3145 kprintf("\tdata_crc = %08x\n", elm->leaf.data_crc);