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_cursor.c,v 1.42 2008/08/06 15:38:58 dillon Exp $
38 * HAMMER B-Tree index - cursor support routines
42 static int hammer_load_cursor_parent(hammer_cursor_t cursor
, int try_exclusive
);
45 * Initialize a fresh cursor using the B-Tree node cache. If the cache
46 * is not available initialize a fresh cursor at the root of the filesystem.
49 hammer_init_cursor(hammer_transaction_t trans
, hammer_cursor_t cursor
,
50 hammer_node_cache_t cache
, hammer_inode_t ip
)
52 hammer_volume_t volume
;
56 bzero(cursor
, sizeof(*cursor
));
58 cursor
->trans
= trans
;
61 * If the cursor operation is on behalf of an inode, lock
64 if ((cursor
->ip
= ip
) != NULL
) {
66 if (trans
->type
== HAMMER_TRANS_FLS
)
67 hammer_lock_ex(&ip
->lock
);
69 hammer_lock_sh(&ip
->lock
);
73 * Step 1 - acquire a locked node from the cache if possible
75 if (cache
&& cache
->node
) {
76 node
= hammer_ref_node_safe(trans
, cache
, &error
);
78 hammer_lock_sh(&node
->lock
);
79 if (node
->flags
& HAMMER_NODE_DELETED
) {
80 hammer_unlock(&node
->lock
);
81 hammer_rel_node(node
);
86 ++hammer_stats_btree_root_iterations
;
89 ++hammer_stats_btree_root_iterations
;
93 * Step 2 - If we couldn't get a node from the cache, get
94 * the one from the root of the filesystem.
96 while (node
== NULL
) {
97 volume
= hammer_get_root_volume(trans
->hmp
, &error
);
100 node
= hammer_get_node(trans
, volume
->ondisk
->vol0_btree_root
,
102 hammer_rel_volume(volume
, 0);
105 hammer_lock_sh(&node
->lock
);
108 * If someone got in before we could lock the node, retry.
110 if (node
->flags
& HAMMER_NODE_DELETED
) {
111 hammer_unlock(&node
->lock
);
112 hammer_rel_node(node
);
116 if (volume
->ondisk
->vol0_btree_root
!= node
->node_offset
) {
117 hammer_unlock(&node
->lock
);
118 hammer_rel_node(node
);
125 * Step 3 - finish initializing the cursor by acquiring the parent
129 error
= hammer_load_cursor_parent(cursor
, 0);
130 KKASSERT(error
== 0);
131 /* if (error) hammer_done_cursor(cursor); */
136 * Normalize a cursor. Sometimes cursors can be left in a state
137 * where node is NULL. If the cursor is in this state, cursor up.
140 hammer_normalize_cursor(hammer_cursor_t cursor
)
142 if (cursor
->node
== NULL
) {
143 KKASSERT(cursor
->parent
!= NULL
);
144 hammer_cursor_up(cursor
);
150 * We are finished with a cursor. We NULL out various fields as sanity
151 * check, in case the structure is inappropriately used afterwords.
154 hammer_done_cursor(hammer_cursor_t cursor
)
158 KKASSERT((cursor
->flags
& HAMMER_CURSOR_TRACKED
) == 0);
159 if (cursor
->parent
) {
160 hammer_unlock(&cursor
->parent
->lock
);
161 hammer_rel_node(cursor
->parent
);
162 cursor
->parent
= NULL
;
165 hammer_unlock(&cursor
->node
->lock
);
166 hammer_rel_node(cursor
->node
);
169 if (cursor
->data_buffer
) {
170 hammer_rel_buffer(cursor
->data_buffer
, 0);
171 cursor
->data_buffer
= NULL
;
173 if ((ip
= cursor
->ip
) != NULL
) {
174 KKASSERT(ip
->cursor_ip_refs
> 0);
175 --ip
->cursor_ip_refs
;
176 hammer_unlock(&ip
->lock
);
180 hammer_rel_mem_record(cursor
->iprec
);
181 cursor
->iprec
= NULL
;
185 * If we deadlocked this node will be referenced. Do a quick
186 * lock/unlock to wait for the deadlock condition to clear.
188 if (cursor
->deadlk_node
) {
189 hammer_lock_ex_ident(&cursor
->deadlk_node
->lock
, "hmrdlk");
190 hammer_unlock(&cursor
->deadlk_node
->lock
);
191 hammer_rel_node(cursor
->deadlk_node
);
192 cursor
->deadlk_node
= NULL
;
194 if (cursor
->deadlk_rec
) {
195 hammer_wait_mem_record_ident(cursor
->deadlk_rec
, "hmmdlr");
196 hammer_rel_mem_record(cursor
->deadlk_rec
);
197 cursor
->deadlk_rec
= NULL
;
202 cursor
->left_bound
= NULL
;
203 cursor
->right_bound
= NULL
;
204 cursor
->trans
= NULL
;
208 * Upgrade cursor->node and cursor->parent to exclusive locks. This
209 * function can return EDEADLK.
211 * The lock must already be either held shared or already held exclusively
214 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
215 * we add another reference to the node that failed and set
216 * cursor->deadlk_node so hammer_done_cursor() can block on it.
219 hammer_cursor_upgrade(hammer_cursor_t cursor
)
223 error
= hammer_lock_upgrade(&cursor
->node
->lock
);
224 if (error
&& cursor
->deadlk_node
== NULL
) {
225 cursor
->deadlk_node
= cursor
->node
;
226 hammer_ref_node(cursor
->deadlk_node
);
227 } else if (error
== 0 && cursor
->parent
) {
228 error
= hammer_lock_upgrade(&cursor
->parent
->lock
);
229 if (error
&& cursor
->deadlk_node
== NULL
) {
230 cursor
->deadlk_node
= cursor
->parent
;
231 hammer_ref_node(cursor
->deadlk_node
);
238 hammer_cursor_upgrade_node(hammer_cursor_t cursor
)
242 error
= hammer_lock_upgrade(&cursor
->node
->lock
);
243 if (error
&& cursor
->deadlk_node
== NULL
) {
244 cursor
->deadlk_node
= cursor
->node
;
245 hammer_ref_node(cursor
->deadlk_node
);
251 * Downgrade cursor->node and cursor->parent to shared locks. This
252 * function can return EDEADLK.
255 hammer_cursor_downgrade(hammer_cursor_t cursor
)
257 if (hammer_lock_excl_owned(&cursor
->node
->lock
, curthread
))
258 hammer_lock_downgrade(&cursor
->node
->lock
);
259 if (cursor
->parent
&&
260 hammer_lock_excl_owned(&cursor
->parent
->lock
, curthread
)) {
261 hammer_lock_downgrade(&cursor
->parent
->lock
);
266 * Seek the cursor to the specified node and index.
268 * The caller must ref the node prior to calling this routine and release
269 * it after it returns. If the seek succeeds the cursor will gain its own
273 hammer_cursor_seek(hammer_cursor_t cursor
, hammer_node_t node
, int index
)
277 hammer_cursor_downgrade(cursor
);
280 if (cursor
->node
!= node
) {
281 hammer_unlock(&cursor
->node
->lock
);
282 hammer_rel_node(cursor
->node
);
284 hammer_ref_node(node
);
285 hammer_lock_sh(&node
->lock
);
286 KKASSERT ((node
->flags
& HAMMER_NODE_DELETED
) == 0);
288 if (cursor
->parent
) {
289 hammer_unlock(&cursor
->parent
->lock
);
290 hammer_rel_node(cursor
->parent
);
291 cursor
->parent
= NULL
;
292 cursor
->parent_index
= 0;
294 error
= hammer_load_cursor_parent(cursor
, 0);
296 cursor
->index
= index
;
301 * Load the parent of cursor->node into cursor->parent.
305 hammer_load_cursor_parent(hammer_cursor_t cursor
, int try_exclusive
)
308 hammer_node_t parent
;
310 hammer_btree_elm_t elm
;
314 hmp
= cursor
->trans
->hmp
;
316 if (cursor
->node
->ondisk
->parent
) {
318 parent
= hammer_btree_get_parent(cursor
->trans
, node
,
320 &error
, try_exclusive
);
322 elm
= &parent
->ondisk
->elms
[parent_index
];
323 cursor
->parent
= parent
;
324 cursor
->parent_index
= parent_index
;
325 cursor
->left_bound
= &elm
[0].internal
.base
;
326 cursor
->right_bound
= &elm
[1].internal
.base
;
329 cursor
->parent
= NULL
;
330 cursor
->parent_index
= 0;
331 cursor
->left_bound
= &hmp
->root_btree_beg
;
332 cursor
->right_bound
= &hmp
->root_btree_end
;
339 * Cursor up to our parent node. Return ENOENT if we are at the root of
343 hammer_cursor_up(hammer_cursor_t cursor
)
347 hammer_cursor_downgrade(cursor
);
350 * If the parent is NULL we are at the root of the B-Tree and
353 if (cursor
->parent
== NULL
)
357 * Set the node to its parent.
359 hammer_unlock(&cursor
->node
->lock
);
360 hammer_rel_node(cursor
->node
);
361 cursor
->node
= cursor
->parent
;
362 cursor
->index
= cursor
->parent_index
;
363 cursor
->parent
= NULL
;
364 cursor
->parent_index
= 0;
366 error
= hammer_load_cursor_parent(cursor
, 0);
371 * Special cursor up given a locked cursor. The orignal node is not
372 * unlocked or released and the cursor is not downgraded.
374 * This function will recover from deadlocks. EDEADLK cannot be returned.
377 hammer_cursor_up_locked(hammer_cursor_t cursor
)
383 * If the parent is NULL we are at the root of the B-Tree and
386 if (cursor
->parent
== NULL
)
392 * Set the node to its parent.
394 cursor
->node
= cursor
->parent
;
395 cursor
->index
= cursor
->parent_index
;
396 cursor
->parent
= NULL
;
397 cursor
->parent_index
= 0;
400 * load the new parent, attempt to exclusively lock it. Note that
401 * we are still holding the old parent (now cursor->node) exclusively
402 * locked. This can return EDEADLK.
404 error
= hammer_load_cursor_parent(cursor
, 1);
406 cursor
->parent
= cursor
->node
;
407 cursor
->parent_index
= cursor
->index
;
416 * Cursor down through the current node, which must be an internal node.
418 * This routine adjusts the cursor and sets index to 0.
421 hammer_cursor_down(hammer_cursor_t cursor
)
424 hammer_btree_elm_t elm
;
428 * The current node becomes the current parent
430 hammer_cursor_downgrade(cursor
);
432 KKASSERT(cursor
->index
>= 0 && cursor
->index
< node
->ondisk
->count
);
433 if (cursor
->parent
) {
434 hammer_unlock(&cursor
->parent
->lock
);
435 hammer_rel_node(cursor
->parent
);
437 cursor
->parent
= node
;
438 cursor
->parent_index
= cursor
->index
;
443 * Extract element to push into at (node,index), set bounds.
445 elm
= &node
->ondisk
->elms
[cursor
->parent_index
];
448 * Ok, push down into elm. If elm specifies an internal or leaf
449 * node the current node must be an internal node. If elm specifies
450 * a spike then the current node must be a leaf node.
452 switch(elm
->base
.btype
) {
453 case HAMMER_BTREE_TYPE_INTERNAL
:
454 case HAMMER_BTREE_TYPE_LEAF
:
455 KKASSERT(node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
456 KKASSERT(elm
->internal
.subtree_offset
!= 0);
457 cursor
->left_bound
= &elm
[0].internal
.base
;
458 cursor
->right_bound
= &elm
[1].internal
.base
;
459 node
= hammer_get_node(cursor
->trans
,
460 elm
->internal
.subtree_offset
, 0, &error
);
462 KASSERT(elm
->base
.btype
== node
->ondisk
->type
, ("BTYPE MISMATCH %c %c NODE %p\n", elm
->base
.btype
, node
->ondisk
->type
, node
));
463 if (node
->ondisk
->parent
!= cursor
->parent
->node_offset
)
464 panic("node %p %016llx vs %016llx\n", node
, (long long)node
->ondisk
->parent
, (long long)cursor
->parent
->node_offset
);
465 KKASSERT(node
->ondisk
->parent
== cursor
->parent
->node_offset
);
469 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
471 (elm
->base
.btype
? elm
->base
.btype
: '?'));
475 hammer_lock_sh(&node
->lock
);
476 KKASSERT ((node
->flags
& HAMMER_NODE_DELETED
) == 0);
483 /************************************************************************
484 * DEADLOCK RECOVERY *
485 ************************************************************************
487 * These are the new deadlock recovery functions. Currently they are only
488 * used for the mirror propagation and physical node removal cases but
489 * ultimately the intention is to use them for all deadlock recovery
492 * WARNING! The contents of the cursor may be modified while unlocked.
493 * passive modifications including adjusting the node, parent,
494 * indexes, and leaf pointer.
496 * An outright removal of the element the cursor was pointing at
497 * will cause the HAMMER_CURSOR_TRACKED_RIPOUT flag to be set,
498 * which chains to causing the HAMMER_CURSOR_RETEST to be set
499 * when the cursor is locked again.
502 hammer_unlock_cursor(hammer_cursor_t cursor
)
506 KKASSERT((cursor
->flags
& HAMMER_CURSOR_TRACKED
) == 0);
507 KKASSERT(cursor
->node
);
510 * Release the cursor's locks and track B-Tree operations on node.
511 * While being tracked our cursor can be modified by other threads
512 * and the node may be replaced.
514 if (cursor
->parent
) {
515 hammer_unlock(&cursor
->parent
->lock
);
516 hammer_rel_node(cursor
->parent
);
517 cursor
->parent
= NULL
;
520 cursor
->flags
|= HAMMER_CURSOR_TRACKED
;
521 TAILQ_INSERT_TAIL(&node
->cursor_list
, cursor
, deadlk_entry
);
522 hammer_unlock(&node
->lock
);
526 * Get the cursor heated up again. The cursor's node may have
527 * changed and we might have to locate the new parent.
529 * If the exact element we were on got deleted RIPOUT will be
530 * set and we must clear ATEDISK so an iteration does not skip
531 * the element after it.
534 hammer_lock_cursor(hammer_cursor_t cursor
)
539 KKASSERT(cursor
->flags
& HAMMER_CURSOR_TRACKED
);
546 hammer_ref_node(node
);
547 hammer_lock_sh(&node
->lock
);
548 if (cursor
->node
== node
) {
549 hammer_rel_node(node
);
552 hammer_unlock(&node
->lock
);
553 hammer_rel_node(node
);
557 * Untrack the cursor, clean up, and re-establish the parent node.
559 TAILQ_REMOVE(&node
->cursor_list
, cursor
, deadlk_entry
);
560 cursor
->flags
&= ~HAMMER_CURSOR_TRACKED
;
563 * If a ripout has occured iterations must re-test the (new)
564 * current element. Clearing ATEDISK prevents the element from
565 * being skipped and RETEST causes it to be re-tested.
567 if (cursor
->flags
& HAMMER_CURSOR_TRACKED_RIPOUT
) {
568 cursor
->flags
&= ~HAMMER_CURSOR_TRACKED_RIPOUT
;
569 cursor
->flags
&= ~HAMMER_CURSOR_ATEDISK
;
570 cursor
->flags
|= HAMMER_CURSOR_RETEST
;
572 error
= hammer_load_cursor_parent(cursor
, 0);
577 * Recover from a deadlocked cursor, tracking any node removals or
578 * replacements. If the cursor's current node is removed by another
579 * thread (via btree_remove()) the cursor will be seeked upwards.
581 * The caller is working a modifying operation and must be holding the
582 * sync lock (shared). We do not release the sync lock because this
583 * would break atomicy.
586 hammer_recover_cursor(hammer_cursor_t cursor
)
590 hammer_unlock_cursor(cursor
);
591 KKASSERT(cursor
->trans
->sync_lock_refs
> 0);
594 * Wait for the deadlock to clear
596 if (cursor
->deadlk_node
) {
597 hammer_lock_ex_ident(&cursor
->deadlk_node
->lock
, "hmrdlk");
598 hammer_unlock(&cursor
->deadlk_node
->lock
);
599 hammer_rel_node(cursor
->deadlk_node
);
600 cursor
->deadlk_node
= NULL
;
602 if (cursor
->deadlk_rec
) {
603 hammer_wait_mem_record_ident(cursor
->deadlk_rec
, "hmmdlr");
604 hammer_rel_mem_record(cursor
->deadlk_rec
);
605 cursor
->deadlk_rec
= NULL
;
607 error
= hammer_lock_cursor(cursor
);
612 * Dup ocursor to ncursor. ncursor inherits ocursor's locks and ocursor
613 * is effectively unlocked and becomes tracked. If ocursor was not locked
614 * then ncursor also inherits the tracking.
616 * After the caller finishes working with ncursor it must be cleaned up
617 * with hammer_done_cursor(), and the caller must re-lock ocursor.
620 hammer_push_cursor(hammer_cursor_t ocursor
)
622 hammer_cursor_t ncursor
;
627 hmp
= ocursor
->trans
->hmp
;
628 ncursor
= kmalloc(sizeof(*ncursor
), hmp
->m_misc
, M_WAITOK
| M_ZERO
);
629 bcopy(ocursor
, ncursor
, sizeof(*ocursor
));
631 node
= ocursor
->node
;
632 hammer_ref_node(node
);
633 if ((ocursor
->flags
& HAMMER_CURSOR_TRACKED
) == 0) {
634 ocursor
->flags
|= HAMMER_CURSOR_TRACKED
;
635 TAILQ_INSERT_TAIL(&node
->cursor_list
, ocursor
, deadlk_entry
);
638 ocursor
->parent
= NULL
;
639 ocursor
->data_buffer
= NULL
;
640 ocursor
->leaf
= NULL
;
641 ocursor
->data
= NULL
;
642 if (ncursor
->flags
& HAMMER_CURSOR_TRACKED
)
643 TAILQ_INSERT_TAIL(&node
->cursor_list
, ncursor
, deadlk_entry
);
644 if ((ip
= ncursor
->ip
) != NULL
) {
645 ++ip
->cursor_ip_refs
;
648 hammer_ref(&ncursor
->iprec
->lock
);
653 * Destroy ncursor and restore ocursor
655 * This is a temporary hack for the release. We can't afford to lose
656 * the IP lock until the IP object scan code is able to deal with it,
657 * so have ocursor inherit it back.
660 hammer_pop_cursor(hammer_cursor_t ocursor
, hammer_cursor_t ncursor
)
665 hmp
= ncursor
->trans
->hmp
;
669 --ip
->cursor_ip_refs
;
670 hammer_done_cursor(ncursor
);
671 kfree(ncursor
, hmp
->m_misc
);
672 KKASSERT(ocursor
->ip
== ip
);
673 hammer_lock_cursor(ocursor
);
677 * onode is being replaced by nnode by the reblocking code.
680 hammer_cursor_replaced_node(hammer_node_t onode
, hammer_node_t nnode
)
682 hammer_cursor_t cursor
;
683 hammer_node_ondisk_t ondisk
;
684 hammer_node_ondisk_t nndisk
;
686 ondisk
= onode
->ondisk
;
687 nndisk
= nnode
->ondisk
;
689 while ((cursor
= TAILQ_FIRST(&onode
->cursor_list
)) != NULL
) {
690 TAILQ_REMOVE(&onode
->cursor_list
, cursor
, deadlk_entry
);
691 TAILQ_INSERT_TAIL(&nnode
->cursor_list
, cursor
, deadlk_entry
);
692 KKASSERT(cursor
->node
== onode
);
693 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
694 cursor
->leaf
= &nndisk
->elms
[cursor
->index
].leaf
;
695 cursor
->node
= nnode
;
696 hammer_ref_node(nnode
);
697 hammer_rel_node(onode
);
702 * node is being removed, cursors in deadlock recovery are seeked upward
706 hammer_cursor_removed_node(hammer_node_t node
, hammer_node_t parent
, int index
)
708 hammer_cursor_t cursor
;
709 hammer_node_ondisk_t ondisk
;
711 KKASSERT(parent
!= NULL
);
712 ondisk
= node
->ondisk
;
714 while ((cursor
= TAILQ_FIRST(&node
->cursor_list
)) != NULL
) {
715 KKASSERT(cursor
->node
== node
);
716 KKASSERT(cursor
->index
== 0);
717 TAILQ_REMOVE(&node
->cursor_list
, cursor
, deadlk_entry
);
718 TAILQ_INSERT_TAIL(&parent
->cursor_list
, cursor
, deadlk_entry
);
719 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
721 cursor
->flags
|= HAMMER_CURSOR_TRACKED_RIPOUT
;
722 cursor
->node
= parent
;
723 cursor
->index
= index
;
724 hammer_ref_node(parent
);
725 hammer_rel_node(node
);
730 * node was split at (onode, index) with elements >= index moved to nnode.
733 hammer_cursor_split_node(hammer_node_t onode
, hammer_node_t nnode
, int index
)
735 hammer_cursor_t cursor
;
736 hammer_node_ondisk_t ondisk
;
737 hammer_node_ondisk_t nndisk
;
739 ondisk
= onode
->ondisk
;
740 nndisk
= nnode
->ondisk
;
743 TAILQ_FOREACH(cursor
, &onode
->cursor_list
, deadlk_entry
) {
744 KKASSERT(cursor
->node
== onode
);
745 if (cursor
->index
< index
)
747 TAILQ_REMOVE(&onode
->cursor_list
, cursor
, deadlk_entry
);
748 TAILQ_INSERT_TAIL(&nnode
->cursor_list
, cursor
, deadlk_entry
);
749 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
750 cursor
->leaf
= &nndisk
->elms
[cursor
->index
- index
].leaf
;
751 cursor
->node
= nnode
;
752 cursor
->index
-= index
;
753 hammer_ref_node(nnode
);
754 hammer_rel_node(onode
);
760 * An element was moved from one node to another or within a node. The
761 * index may also represent the end of the node (index == numelements).
763 * This is used by the rebalancing code. This is not an insertion or
764 * deletion and any additional elements, including the degenerate case at
765 * the end of the node, will be dealt with by additional distinct calls.
768 hammer_cursor_moved_element(hammer_node_t onode
, hammer_node_t nnode
,
769 int oindex
, int nindex
)
771 hammer_cursor_t cursor
;
772 hammer_node_ondisk_t ondisk
;
773 hammer_node_ondisk_t nndisk
;
775 ondisk
= onode
->ondisk
;
776 nndisk
= nnode
->ondisk
;
779 TAILQ_FOREACH(cursor
, &onode
->cursor_list
, deadlk_entry
) {
780 KKASSERT(cursor
->node
== onode
);
781 if (cursor
->index
!= oindex
)
783 TAILQ_REMOVE(&onode
->cursor_list
, cursor
, deadlk_entry
);
784 TAILQ_INSERT_TAIL(&nnode
->cursor_list
, cursor
, deadlk_entry
);
785 if (cursor
->leaf
== &ondisk
->elms
[oindex
].leaf
)
786 cursor
->leaf
= &nndisk
->elms
[nindex
].leaf
;
787 cursor
->node
= nnode
;
788 cursor
->index
= nindex
;
789 hammer_ref_node(nnode
);
790 hammer_rel_node(onode
);
796 * The B-Tree element pointing to the specified node was moved from (oparent)
797 * to (nparent, nindex). We must locate any tracked cursors pointing at
798 * node and adjust their parent accordingly.
800 * This is used by the rebalancing code when packing elements causes an
801 * element to shift from one node to another.
804 hammer_cursor_parent_changed(hammer_node_t node
, hammer_node_t oparent
,
805 hammer_node_t nparent
, int nindex
)
807 hammer_cursor_t cursor
;
810 TAILQ_FOREACH(cursor
, &node
->cursor_list
, deadlk_entry
) {
811 KKASSERT(cursor
->node
== node
);
812 if (cursor
->parent
== oparent
) {
813 cursor
->parent
= nparent
;
814 cursor
->parent_index
= nindex
;
815 hammer_ref_node(nparent
);
816 hammer_rel_node(oparent
);
823 * Deleted element at (node, index)
825 * Shift indexes >= index
828 hammer_cursor_deleted_element(hammer_node_t node
, int index
)
830 hammer_cursor_t cursor
;
831 hammer_node_ondisk_t ondisk
;
833 ondisk
= node
->ondisk
;
835 TAILQ_FOREACH(cursor
, &node
->cursor_list
, deadlk_entry
) {
836 KKASSERT(cursor
->node
== node
);
837 if (cursor
->index
== index
) {
838 cursor
->flags
|= HAMMER_CURSOR_TRACKED_RIPOUT
;
839 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
841 } else if (cursor
->index
> index
) {
842 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
843 cursor
->leaf
= &ondisk
->elms
[cursor
->index
- 1].leaf
;
850 * Inserted element at (node, index)
852 * Shift indexes >= index
855 hammer_cursor_inserted_element(hammer_node_t node
, int index
)
857 hammer_cursor_t cursor
;
858 hammer_node_ondisk_t ondisk
;
860 ondisk
= node
->ondisk
;
862 TAILQ_FOREACH(cursor
, &node
->cursor_list
, deadlk_entry
) {
863 KKASSERT(cursor
->node
== node
);
864 if (cursor
->index
>= index
) {
865 if (cursor
->leaf
== &ondisk
->elms
[cursor
->index
].leaf
)
866 cursor
->leaf
= &ondisk
->elms
[cursor
->index
+ 1].leaf
;