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
36 * HAMMER B-Tree index - cursor support routines
40 static int hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive);
43 * Initialize a fresh cursor using the B-Tree node cache. If the cache
44 * is not available initialize a fresh cursor at the root of the filesystem.
47 hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
48 hammer_node_cache_t cache, hammer_inode_t ip)
50 hammer_volume_t volume;
56 bzero(cursor, sizeof(*cursor));
58 cursor->trans = trans;
62 * As the number of inodes queued to the flusher increases we use
63 * time-domain multiplexing to control read vs flush performance.
64 * We have to do it here, before acquiring any ip or node locks,
65 * to avoid deadlocking or excessively delaying the flusher.
67 * The full time period is hammer_tdmux_ticks, typically 1/5 of
70 * inode allocation begins to get restrained at 2/4 the limit
71 * via the "hmrrcm" mechanism in hammer_inode. We want to begin
72 * limiting read activity before that to try to avoid processes
73 * stalling out in "hmrrcm".
75 tticks = hammer_tdmux_ticks;
76 if (trans->type != HAMMER_TRANS_FLS && tticks &&
77 hmp->count_reclaims > hammer_limit_reclaims / tticks &&
78 hmp->count_reclaims > hammer_autoflush * 2 &&
79 hammer_flusher_running(hmp)) {
85 * 0 ... xticks ... tticks
87 * rticks is the calculated position, xticks is the demarc
88 * where values below xticks are reserved for the flusher
89 * and values >= to xticks may be used by the frontend.
91 * At least one tick is always made available for the
94 rticks = (u_int)ticks % tticks;
95 xticks = hmp->count_reclaims * tticks / hammer_limit_reclaims;
98 * Ensure rticks and xticks are stable
101 if (rticks < xticks) {
102 if (hammer_debug_general & 0x0004)
103 kprintf("rt %3u, xt %3u, tt %3u\n",
104 rticks, xticks, tticks);
105 tsleep(&dummy, 0, "htdmux", xticks - rticks);
110 * If the cursor operation is on behalf of an inode, lock
113 * When acquiring a shared lock on an inode on which the backend
114 * flusher deadlocked, wait up to hammer_tdmux_ticks (1 second)
115 * for the deadlock to clear.
117 if ((cursor->ip = ip) != NULL) {
118 ++ip->cursor_ip_refs;
119 if (trans->type == HAMMER_TRANS_FLS) {
120 hammer_lock_ex(&ip->lock);
123 if (ip->cursor_exclreq_count) {
124 tsleep(&ip->cursor_exclreq_count, 0,
125 "hstag1", hammer_tdmux_ticks);
128 hammer_lock_sh(&ip->lock);
133 * Step 1 - acquire a locked node from the cache if possible
135 if (cache && cache->node) {
136 node = hammer_ref_node_safe(trans, cache, &error);
138 hammer_lock_sh(&node->lock);
139 if (node->flags & HAMMER_NODE_DELETED) {
140 hammer_unlock(&node->lock);
141 hammer_rel_node(node);
146 ++hammer_stats_btree_root_iterations;
149 ++hammer_stats_btree_root_iterations;
153 * Step 2 - If we couldn't get a node from the cache, get
154 * the one from the root of the filesystem.
156 while (node == NULL) {
157 volume = hammer_get_root_volume(hmp, &error);
160 node = hammer_get_node(trans, volume->ondisk->vol0_btree_root,
162 hammer_rel_volume(volume, 0);
166 * When the frontend acquires the root b-tree node while the
167 * backend is deadlocked on it, wait up to hammer_tdmux_ticks
168 * (1 second) for the deadlock to clear.
171 if (node->cursor_exclreq_count &&
172 cursor->trans->type != HAMMER_TRANS_FLS) {
173 tsleep(&node->cursor_exclreq_count, 0,
174 "hstag3", hammer_tdmux_ticks);
177 hammer_lock_sh(&node->lock);
180 * If someone got in before we could lock the node, retry.
182 if (node->flags & HAMMER_NODE_DELETED) {
183 hammer_unlock(&node->lock);
184 hammer_rel_node(node);
188 if (volume->ondisk->vol0_btree_root != node->node_offset) {
189 hammer_unlock(&node->lock);
190 hammer_rel_node(node);
197 * Step 3 - finish initializing the cursor by acquiring the parent
201 error = hammer_load_cursor_parent(cursor, 0);
202 KKASSERT(error == 0);
203 /* if (error) hammer_done_cursor(cursor); */
208 * Normalize a cursor. Sometimes cursors can be left in a state
209 * where node is NULL. If the cursor is in this state, cursor up.
212 hammer_normalize_cursor(hammer_cursor_t cursor)
214 if (cursor->node == NULL) {
215 KKASSERT(cursor->parent != NULL);
216 hammer_cursor_up(cursor);
222 * We are finished with a cursor. We NULL out various fields as sanity
223 * check, in case the structure is inappropriately used afterwords.
226 hammer_done_cursor(hammer_cursor_t cursor)
230 KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
231 if (cursor->parent) {
232 hammer_unlock(&cursor->parent->lock);
233 hammer_rel_node(cursor->parent);
234 cursor->parent = NULL;
237 hammer_unlock(&cursor->node->lock);
238 hammer_rel_node(cursor->node);
241 if (cursor->data_buffer) {
242 hammer_rel_buffer(cursor->data_buffer, 0);
243 cursor->data_buffer = NULL;
245 if ((ip = cursor->ip) != NULL) {
246 KKASSERT(ip->cursor_ip_refs > 0);
247 --ip->cursor_ip_refs;
248 hammer_unlock(&ip->lock);
252 hammer_rel_mem_record(cursor->iprec);
253 cursor->iprec = NULL;
257 * If we deadlocked this node will be referenced. Do a quick
258 * lock/unlock to wait for the deadlock condition to clear.
260 * Maintain exclreq_count / wakeup as necessary to notify new
261 * entrants into ip. We continue to hold the fs_token so our
262 * EDEADLK retry loop should get its chance before another thread
265 if (cursor->deadlk_node) {
267 if (ip && cursor->trans->type == HAMMER_TRANS_FLS)
268 ++ip->cursor_exclreq_count;
269 ++cursor->deadlk_node->cursor_exclreq_count;
271 hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
272 hammer_unlock(&cursor->deadlk_node->lock);
274 if (--cursor->deadlk_node->cursor_exclreq_count == 0)
275 wakeup(&cursor->deadlk_node->cursor_exclreq_count);
276 if (ip && cursor->trans->type == HAMMER_TRANS_FLS) {
277 if (--ip->cursor_exclreq_count == 0)
278 wakeup(&ip->cursor_exclreq_count);
281 hammer_rel_node(cursor->deadlk_node);
282 cursor->deadlk_node = NULL;
284 if (cursor->deadlk_rec) {
285 hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
286 hammer_rel_mem_record(cursor->deadlk_rec);
287 cursor->deadlk_rec = NULL;
292 cursor->left_bound = NULL;
293 cursor->right_bound = NULL;
294 cursor->trans = NULL;
298 * Upgrade cursor->node and cursor->parent to exclusive locks. This
299 * function can return EDEADLK.
301 * The lock must already be either held shared or already held exclusively
304 * We upgrade the parent first as it is the most likely to collide first
305 * with the downward traversal that the frontend typically does.
307 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
308 * we add another reference to the node that failed and set
309 * cursor->deadlk_node so hammer_done_cursor() can block on it.
312 hammer_cursor_upgrade(hammer_cursor_t cursor)
316 if (cursor->parent) {
317 error = hammer_lock_upgrade(&cursor->parent->lock, 1);
318 if (error && cursor->deadlk_node == NULL) {
319 cursor->deadlk_node = cursor->parent;
320 hammer_ref_node(cursor->deadlk_node);
326 error = hammer_lock_upgrade(&cursor->node->lock, 1);
327 if (error && cursor->deadlk_node == NULL) {
328 cursor->deadlk_node = cursor->node;
329 hammer_ref_node(cursor->deadlk_node);
333 error = hammer_lock_upgrade(&cursor->node->lock, 1);
334 if (error && cursor->deadlk_node == NULL) {
335 cursor->deadlk_node = cursor->node;
336 hammer_ref_node(cursor->deadlk_node);
337 } else if (error == 0 && cursor->parent) {
338 error = hammer_lock_upgrade(&cursor->parent->lock, 1);
339 if (error && cursor->deadlk_node == NULL) {
340 cursor->deadlk_node = cursor->parent;
341 hammer_ref_node(cursor->deadlk_node);
349 hammer_cursor_upgrade_node(hammer_cursor_t cursor)
353 error = hammer_lock_upgrade(&cursor->node->lock, 1);
354 if (error && cursor->deadlk_node == NULL) {
355 cursor->deadlk_node = cursor->node;
356 hammer_ref_node(cursor->deadlk_node);
362 * Downgrade cursor->node and cursor->parent to shared locks. This
363 * function can return EDEADLK.
366 hammer_cursor_downgrade(hammer_cursor_t cursor)
368 if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
369 hammer_lock_downgrade(&cursor->node->lock, 1);
370 if (cursor->parent &&
371 hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
372 hammer_lock_downgrade(&cursor->parent->lock, 1);
377 * Upgrade and downgrade pairs of cursors. This is used by the dedup
378 * code which must deal with two cursors. A special function is needed
379 * because some of the nodes may be shared between the two cursors,
380 * resulting in share counts > 1 which will normally cause an upgrade
385 collect_node(hammer_node_t *array, int *counts, int n, hammer_node_t node)
389 for (i = 0; i < n; ++i) {
390 if (array[i] == node)
404 hammer_cursor_upgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
406 hammer_node_t nodes[4];
412 n = collect_node(nodes, counts, 0, cursor1->node);
414 n = collect_node(nodes, counts, n, cursor1->parent);
415 n = collect_node(nodes, counts, n, cursor2->node);
417 n = collect_node(nodes, counts, n, cursor2->parent);
420 for (i = 0; i < n; ++i) {
421 error = hammer_lock_upgrade(&nodes[i]->lock, counts[i]);
427 hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
433 hammer_cursor_downgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
435 hammer_node_t nodes[4];
440 n = collect_node(nodes, counts, 0, cursor1->node);
442 n = collect_node(nodes, counts, n, cursor1->parent);
443 n = collect_node(nodes, counts, n, cursor2->node);
445 n = collect_node(nodes, counts, n, cursor2->parent);
447 for (i = 0; i < n; ++i)
448 hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
452 * Seek the cursor to the specified node and index.
454 * The caller must ref the node prior to calling this routine and release
455 * it after it returns. If the seek succeeds the cursor will gain its own
459 hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
463 hammer_cursor_downgrade(cursor);
466 if (cursor->node != node) {
467 hammer_unlock(&cursor->node->lock);
468 hammer_rel_node(cursor->node);
470 hammer_ref_node(node);
471 hammer_lock_sh(&node->lock);
472 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
474 if (cursor->parent) {
475 hammer_unlock(&cursor->parent->lock);
476 hammer_rel_node(cursor->parent);
477 cursor->parent = NULL;
478 cursor->parent_index = 0;
480 error = hammer_load_cursor_parent(cursor, 0);
482 cursor->index = index;
487 * Load the parent of cursor->node into cursor->parent.
491 hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive)
494 hammer_node_t parent;
496 hammer_btree_elm_t elm;
500 hmp = cursor->trans->hmp;
502 if (cursor->node->ondisk->parent) {
504 parent = hammer_btree_get_parent(cursor->trans, node,
506 &error, try_exclusive);
508 elm = &parent->ondisk->elms[parent_index];
509 cursor->parent = parent;
510 cursor->parent_index = parent_index;
511 cursor->left_bound = &elm[0].internal.base;
512 cursor->right_bound = &elm[1].internal.base;
515 cursor->parent = NULL;
516 cursor->parent_index = 0;
517 cursor->left_bound = &hmp->root_btree_beg;
518 cursor->right_bound = &hmp->root_btree_end;
525 * Cursor up to our parent node. Return ENOENT if we are at the root of
529 hammer_cursor_up(hammer_cursor_t cursor)
533 hammer_cursor_downgrade(cursor);
536 * If the parent is NULL we are at the root of the B-Tree and
539 if (cursor->parent == NULL)
543 * Set the node to its parent.
545 hammer_unlock(&cursor->node->lock);
546 hammer_rel_node(cursor->node);
547 cursor->node = cursor->parent;
548 cursor->index = cursor->parent_index;
549 cursor->parent = NULL;
550 cursor->parent_index = 0;
552 error = hammer_load_cursor_parent(cursor, 0);
557 * Special cursor up given a locked cursor. The orignal node is not
558 * unlocked or released and the cursor is not downgraded.
560 * This function can fail with EDEADLK.
562 * This function is only run when recursively deleting parent nodes
563 * to get rid of an empty leaf.
566 hammer_cursor_up_locked(hammer_cursor_t cursor)
573 * If the parent is NULL we are at the root of the B-Tree and
576 if (cursor->parent == NULL)
580 save_index = cursor->index;
583 * Set the node to its parent.
585 cursor->node = cursor->parent;
586 cursor->index = cursor->parent_index;
587 cursor->parent = NULL;
588 cursor->parent_index = 0;
591 * load the new parent, attempt to exclusively lock it. Note that
592 * we are still holding the old parent (now cursor->node) exclusively
595 * This can return EDEADLK. Undo the operation on any error. These
596 * up sequences can occur during iterations so be sure to restore
599 error = hammer_load_cursor_parent(cursor, 1);
601 cursor->parent = cursor->node;
602 cursor->parent_index = cursor->index;
604 cursor->index = save_index;
611 * Cursor down through the current node, which must be an internal node.
613 * This routine adjusts the cursor and sets index to 0.
616 hammer_cursor_down(hammer_cursor_t cursor)
619 hammer_btree_elm_t elm;
623 * The current node becomes the current parent
625 hammer_cursor_downgrade(cursor);
627 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
628 if (cursor->parent) {
629 hammer_unlock(&cursor->parent->lock);
630 hammer_rel_node(cursor->parent);
632 cursor->parent = node;
633 cursor->parent_index = cursor->index;
638 * Extract element to push into at (node,index), set bounds.
640 elm = &node->ondisk->elms[cursor->parent_index];
643 * Ok, push down into elm. If elm specifies an internal or leaf
644 * node the current node must be an internal node. If elm specifies
645 * a spike then the current node must be a leaf node.
647 switch(elm->base.btype) {
648 case HAMMER_BTREE_TYPE_INTERNAL:
649 case HAMMER_BTREE_TYPE_LEAF:
650 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
651 KKASSERT(elm->internal.subtree_offset != 0);
652 cursor->left_bound = &elm[0].internal.base;
653 cursor->right_bound = &elm[1].internal.base;
654 node = hammer_get_node(cursor->trans,
655 elm->internal.subtree_offset, 0, &error);
657 KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p", elm->base.btype, node->ondisk->type, node));
658 if (node->ondisk->parent != cursor->parent->node_offset)
659 panic("node %p %016llx vs %016llx", node, (long long)node->ondisk->parent, (long long)cursor->parent->node_offset);
660 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
664 panic("hammer_cursor_down: illegal btype %02x (%c)",
666 (elm->base.btype ? elm->base.btype : '?'));
671 * If no error occured we can lock the new child node. If the
672 * node is deadlock flagged wait up to hammer_tdmux_ticks (1 second)
673 * for the deadlock to clear. Otherwise a large number of concurrent
674 * readers can continuously stall the flusher.
676 * We specifically do this in the cursor_down() code in order to
677 * deal with frontend top-down searches smashing against bottom-up
678 * flusher-based mirror updates. These collisions typically occur
679 * above the inode in the B-Tree and are not covered by the
680 * ip->cursor_exclreq_count logic.
684 if (node->cursor_exclreq_count &&
685 cursor->trans->type != HAMMER_TRANS_FLS) {
686 tsleep(&node->cursor_exclreq_count, 0,
687 "hstag2", hammer_tdmux_ticks);
690 hammer_lock_sh(&node->lock);
691 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
698 /************************************************************************
699 * DEADLOCK RECOVERY *
700 ************************************************************************
702 * These are the new deadlock recovery functions. Currently they are only
703 * used for the mirror propagation and physical node removal cases but
704 * ultimately the intention is to use them for all deadlock recovery
707 * WARNING! The contents of the cursor may be modified while unlocked.
708 * passive modifications including adjusting the node, parent,
709 * indexes, and leaf pointer.
711 * An outright removal of the element the cursor was pointing at
712 * will cause the HAMMER_CURSOR_TRACKED_RIPOUT flag to be set,
713 * which chains to causing the HAMMER_CURSOR_RETEST to be set
714 * when the cursor is locked again.
717 hammer_unlock_cursor(hammer_cursor_t cursor)
721 KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
722 KKASSERT(cursor->node);
725 * Release the cursor's locks and track B-Tree operations on node.
726 * While being tracked our cursor can be modified by other threads
727 * and the node may be replaced.
729 if (cursor->parent) {
730 hammer_unlock(&cursor->parent->lock);
731 hammer_rel_node(cursor->parent);
732 cursor->parent = NULL;
735 cursor->flags |= HAMMER_CURSOR_TRACKED;
736 TAILQ_INSERT_TAIL(&node->cursor_list, cursor, deadlk_entry);
737 hammer_unlock(&node->lock);
741 * Get the cursor heated up again. The cursor's node may have
742 * changed and we might have to locate the new parent.
744 * If the exact element we were on got deleted RIPOUT will be
745 * set and we must clear ATEDISK so an iteration does not skip
746 * the element after it.
749 hammer_lock_cursor(hammer_cursor_t cursor)
754 KKASSERT(cursor->flags & HAMMER_CURSOR_TRACKED);
761 hammer_ref_node(node);
762 hammer_lock_sh(&node->lock);
763 if (cursor->node == node) {
764 hammer_rel_node(node);
767 hammer_unlock(&node->lock);
768 hammer_rel_node(node);
772 * Untrack the cursor, clean up, and re-establish the parent node.
774 TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
775 cursor->flags &= ~HAMMER_CURSOR_TRACKED;
778 * If a ripout has occured iterations must re-test the (new)
779 * current element. Clearing ATEDISK prevents the element from
780 * being skipped and RETEST causes it to be re-tested.
782 if (cursor->flags & HAMMER_CURSOR_TRACKED_RIPOUT) {
783 cursor->flags &= ~HAMMER_CURSOR_TRACKED_RIPOUT;
784 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
785 cursor->flags |= HAMMER_CURSOR_RETEST;
787 error = hammer_load_cursor_parent(cursor, 0);
792 * Recover from a deadlocked cursor, tracking any node removals or
793 * replacements. If the cursor's current node is removed by another
794 * thread (via btree_remove()) the cursor will be seeked upwards.
796 * The caller is working a modifying operation and must be holding the
797 * sync lock (shared). We do not release the sync lock because this
798 * would break atomicy.
801 hammer_recover_cursor(hammer_cursor_t cursor)
803 hammer_transaction_t trans;
807 hammer_unlock_cursor(cursor);
810 trans = cursor->trans;
811 KKASSERT(trans->sync_lock_refs > 0);
814 * Wait for the deadlock to clear.
816 * Maintain exclreq_count / wakeup as necessary to notify new
817 * entrants into ip. We continue to hold the fs_token so our
818 * EDEADLK retry loop should get its chance before another thread
821 if (cursor->deadlk_node) {
823 if (ip && trans->type == HAMMER_TRANS_FLS)
824 ++ip->cursor_exclreq_count;
825 ++cursor->deadlk_node->cursor_exclreq_count;
827 hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
828 hammer_unlock(&cursor->deadlk_node->lock);
830 if (--cursor->deadlk_node->cursor_exclreq_count == 0)
831 wakeup(&cursor->deadlk_node->cursor_exclreq_count);
832 if (ip && trans->type == HAMMER_TRANS_FLS) {
833 if (--ip->cursor_exclreq_count == 0)
834 wakeup(&ip->cursor_exclreq_count);
837 hammer_rel_node(cursor->deadlk_node);
838 cursor->deadlk_node = NULL;
840 if (cursor->deadlk_rec) {
841 hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
842 hammer_rel_mem_record(cursor->deadlk_rec);
843 cursor->deadlk_rec = NULL;
845 error = hammer_lock_cursor(cursor);
850 * Dup ocursor to ncursor. ncursor inherits ocursor's locks and ocursor
851 * is effectively unlocked and becomes tracked. If ocursor was not locked
852 * then ncursor also inherits the tracking.
854 * After the caller finishes working with ncursor it must be cleaned up
855 * with hammer_done_cursor(), and the caller must re-lock ocursor.
858 hammer_push_cursor(hammer_cursor_t ocursor)
860 hammer_cursor_t ncursor;
865 hmp = ocursor->trans->hmp;
866 ncursor = kmalloc(sizeof(*ncursor), hmp->m_misc, M_WAITOK | M_ZERO);
867 bcopy(ocursor, ncursor, sizeof(*ocursor));
869 node = ocursor->node;
870 hammer_ref_node(node);
871 if ((ocursor->flags & HAMMER_CURSOR_TRACKED) == 0) {
872 ocursor->flags |= HAMMER_CURSOR_TRACKED;
873 TAILQ_INSERT_TAIL(&node->cursor_list, ocursor, deadlk_entry);
876 ocursor->parent = NULL;
877 ocursor->data_buffer = NULL;
878 ocursor->leaf = NULL;
879 ocursor->data = NULL;
880 if (ncursor->flags & HAMMER_CURSOR_TRACKED)
881 TAILQ_INSERT_TAIL(&node->cursor_list, ncursor, deadlk_entry);
882 if ((ip = ncursor->ip) != NULL) {
883 ++ip->cursor_ip_refs;
886 hammer_ref(&ncursor->iprec->lock);
891 * Destroy ncursor and restore ocursor
893 * This is a temporary hack for the release. We can't afford to lose
894 * the IP lock until the IP object scan code is able to deal with it,
895 * so have ocursor inherit it back.
898 hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor)
903 hmp = ncursor->trans->hmp;
907 --ip->cursor_ip_refs;
908 hammer_done_cursor(ncursor);
909 kfree(ncursor, hmp->m_misc);
910 KKASSERT(ocursor->ip == ip);
911 hammer_lock_cursor(ocursor);
915 * onode is being replaced by nnode by the reblocking code.
918 hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode)
920 hammer_cursor_t cursor;
921 hammer_node_ondisk_t ondisk;
922 hammer_node_ondisk_t nndisk;
924 ondisk = onode->ondisk;
925 nndisk = nnode->ondisk;
927 while ((cursor = TAILQ_FIRST(&onode->cursor_list)) != NULL) {
928 TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
929 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
930 KKASSERT(cursor->node == onode);
931 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
932 cursor->leaf = &nndisk->elms[cursor->index].leaf;
933 cursor->node = nnode;
934 hammer_ref_node(nnode);
935 hammer_rel_node(onode);
940 * We have removed <node> from the parent and collapsed the parent.
942 * Cursors in deadlock recovery are seeked upward to the parent so the
943 * btree_remove() recursion works properly even though we have marked
944 * the cursor as requiring a reseek.
946 * This is the only cursor function which sets HAMMER_CURSOR_ITERATE_CHECK,
947 * meaning the cursor is no longer definitively pointing at an element
948 * within its iteration (if the cursor is being used to iterate). The
949 * iteration code will take this into account instead of asserting if the
950 * cursor is outside the iteration range.
953 hammer_cursor_removed_node(hammer_node_t node, hammer_node_t parent, int index)
955 hammer_cursor_t cursor;
956 hammer_node_ondisk_t ondisk;
958 KKASSERT(parent != NULL);
959 ondisk = node->ondisk;
961 while ((cursor = TAILQ_FIRST(&node->cursor_list)) != NULL) {
962 KKASSERT(cursor->node == node);
963 KKASSERT(cursor->index == 0);
964 TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
965 TAILQ_INSERT_TAIL(&parent->cursor_list, cursor, deadlk_entry);
966 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
968 cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
969 cursor->flags |= HAMMER_CURSOR_ITERATE_CHECK;
970 cursor->node = parent;
971 cursor->index = index;
972 hammer_ref_node(parent);
973 hammer_rel_node(node);
978 * node was split at (onode, index) with elements >= index moved to nnode.
981 hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode, int index)
983 hammer_cursor_t cursor;
984 hammer_node_ondisk_t ondisk;
985 hammer_node_ondisk_t nndisk;
987 ondisk = onode->ondisk;
988 nndisk = nnode->ondisk;
991 TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
992 KKASSERT(cursor->node == onode);
993 if (cursor->index < index)
995 TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
996 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
997 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
998 cursor->leaf = &nndisk->elms[cursor->index - index].leaf;
999 cursor->node = nnode;
1000 cursor->index -= index;
1001 hammer_ref_node(nnode);
1002 hammer_rel_node(onode);
1008 * An element was moved from one node to another or within a node. The
1009 * index may also represent the end of the node (index == numelements).
1011 * {oparent,pindex} is the parent node's pointer to onode/oindex.
1013 * This is used by the rebalancing code. This is not an insertion or
1014 * deletion and any additional elements, including the degenerate case at
1015 * the end of the node, will be dealt with by additional distinct calls.
1018 hammer_cursor_moved_element(hammer_node_t oparent, int pindex,
1019 hammer_node_t onode, int oindex,
1020 hammer_node_t nnode, int nindex)
1022 hammer_cursor_t cursor;
1023 hammer_node_ondisk_t ondisk;
1024 hammer_node_ondisk_t nndisk;
1027 * Adjust any cursors pointing at the element
1029 ondisk = onode->ondisk;
1030 nndisk = nnode->ondisk;
1032 TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
1033 KKASSERT(cursor->node == onode);
1034 if (cursor->index != oindex)
1036 TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
1037 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
1038 if (cursor->leaf == &ondisk->elms[oindex].leaf)
1039 cursor->leaf = &nndisk->elms[nindex].leaf;
1040 cursor->node = nnode;
1041 cursor->index = nindex;
1042 hammer_ref_node(nnode);
1043 hammer_rel_node(onode);
1048 * When moving the first element of onode to a different node any
1049 * cursor which is pointing at (oparent,pindex) must be repointed
1050 * to nnode and ATEDISK must be cleared.
1052 * This prevents cursors from losing track due to insertions.
1053 * Insertions temporarily release the cursor in order to update
1054 * the mirror_tids. It primarily effects the mirror_write code.
1055 * The other code paths generally only do a single insertion and
1056 * then relookup or drop the cursor.
1058 if (onode == nnode || oindex)
1060 ondisk = oparent->ondisk;
1062 TAILQ_FOREACH(cursor, &oparent->cursor_list, deadlk_entry) {
1063 KKASSERT(cursor->node == oparent);
1064 if (cursor->index != pindex)
1066 kprintf("HAMMER debug: shifted cursor pointing at parent\n"
1067 "parent %016jx:%d onode %016jx:%d nnode %016jx:%d\n",
1068 (intmax_t)oparent->node_offset, pindex,
1069 (intmax_t)onode->node_offset, oindex,
1070 (intmax_t)nnode->node_offset, nindex);
1071 TAILQ_REMOVE(&oparent->cursor_list, cursor, deadlk_entry);
1072 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
1073 if (cursor->leaf == &ondisk->elms[oindex].leaf)
1074 cursor->leaf = &nndisk->elms[nindex].leaf;
1075 cursor->node = nnode;
1076 cursor->index = nindex;
1077 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1078 hammer_ref_node(nnode);
1079 hammer_rel_node(oparent);
1085 * The B-Tree element pointing to the specified node was moved from (oparent)
1086 * to (nparent, nindex). We must locate any tracked cursors pointing at
1087 * node and adjust their parent accordingly.
1089 * This is used by the rebalancing code when packing elements causes an
1090 * element to shift from one node to another.
1093 hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent,
1094 hammer_node_t nparent, int nindex)
1096 hammer_cursor_t cursor;
1099 TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
1100 KKASSERT(cursor->node == node);
1101 if (cursor->parent == oparent) {
1102 cursor->parent = nparent;
1103 cursor->parent_index = nindex;
1104 hammer_ref_node(nparent);
1105 hammer_rel_node(oparent);
1112 * Deleted element at (node, index)
1114 * Shift indexes >= index
1117 hammer_cursor_deleted_element(hammer_node_t node, int index)
1119 hammer_cursor_t cursor;
1120 hammer_node_ondisk_t ondisk;
1122 ondisk = node->ondisk;
1124 TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
1125 KKASSERT(cursor->node == node);
1126 if (cursor->index == index) {
1127 cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
1128 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1129 cursor->leaf = NULL;
1130 } else if (cursor->index > index) {
1131 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1132 cursor->leaf = &ondisk->elms[cursor->index - 1].leaf;
1139 * Inserted element at (node, index)
1141 * Shift indexes >= index
1144 hammer_cursor_inserted_element(hammer_node_t node, int index)
1146 hammer_cursor_t cursor;
1147 hammer_node_ondisk_t ondisk;
1149 ondisk = node->ondisk;
1151 TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
1152 KKASSERT(cursor->node == node);
1153 if (cursor->index >= index) {
1154 if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1155 cursor->leaf = &ondisk->elms[cursor->index + 1].leaf;
1162 * Invalidate the cached data buffer associated with a cursor.
1164 * This needs to be done when the underlying block is being freed or
1165 * the referenced buffer can prevent the related buffer cache buffer
1166 * from being properly invalidated.
1169 hammer_cursor_invalidate_cache(hammer_cursor_t cursor)
1171 if (cursor->data_buffer) {
1172 hammer_rel_buffer(cursor->data_buffer, 0);
1173 cursor->data_buffer = NULL;
1174 cursor->data = NULL;