xref: /dragonfly/sys/vfs/hammer/hammer_cursor.c (revision 35a5249b)
18cd0a023SMatthew Dillon /*
2b84de5afSMatthew Dillon  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
38cd0a023SMatthew Dillon  *
48cd0a023SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
58cd0a023SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
68cd0a023SMatthew Dillon  *
78cd0a023SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
88cd0a023SMatthew Dillon  * modification, are permitted provided that the following conditions
98cd0a023SMatthew Dillon  * are met:
108cd0a023SMatthew Dillon  *
118cd0a023SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
128cd0a023SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
138cd0a023SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
148cd0a023SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
158cd0a023SMatthew Dillon  *    the documentation and/or other materials provided with the
168cd0a023SMatthew Dillon  *    distribution.
178cd0a023SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
188cd0a023SMatthew Dillon  *    contributors may be used to endorse or promote products derived
198cd0a023SMatthew Dillon  *    from this software without specific, prior written permission.
208cd0a023SMatthew Dillon  *
218cd0a023SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
228cd0a023SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
238cd0a023SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
248cd0a023SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
258cd0a023SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
268cd0a023SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
278cd0a023SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
288cd0a023SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
298cd0a023SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
308cd0a023SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
318cd0a023SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328cd0a023SMatthew Dillon  * SUCH DAMAGE.
338cd0a023SMatthew Dillon  */
348cd0a023SMatthew Dillon 
358cd0a023SMatthew Dillon /*
368cd0a023SMatthew Dillon  * HAMMER B-Tree index - cursor support routines
378cd0a023SMatthew Dillon  */
388cd0a023SMatthew Dillon #include "hammer.h"
398cd0a023SMatthew Dillon 
40f36a9737SMatthew Dillon static int hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive);
418cd0a023SMatthew Dillon 
428cd0a023SMatthew Dillon /*
4361aeeb33SMatthew Dillon  * Initialize a fresh cursor using the B-Tree node cache.  If the cache
4447197d71SMatthew Dillon  * is not available initialize a fresh cursor at the root of the filesystem.
458cd0a023SMatthew Dillon  */
468cd0a023SMatthew Dillon int
hammer_init_cursor(hammer_transaction_t trans,hammer_cursor_t cursor,hammer_node_cache_t cache,hammer_inode_t ip)4736f82b23SMatthew Dillon hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
48bcac4bbbSMatthew Dillon 		   hammer_node_cache_t cache, hammer_inode_t ip)
498cd0a023SMatthew Dillon {
5047197d71SMatthew Dillon 	hammer_volume_t volume;
5161aeeb33SMatthew Dillon 	hammer_node_t node;
52e2a02b72SMatthew Dillon 	hammer_mount_t hmp;
53e2a02b72SMatthew Dillon 	u_int tticks;
548cd0a023SMatthew Dillon 	int error;
558cd0a023SMatthew Dillon 
568cd0a023SMatthew Dillon 	bzero(cursor, sizeof(*cursor));
5761aeeb33SMatthew Dillon 
5836f82b23SMatthew Dillon 	cursor->trans = trans;
59e2a02b72SMatthew Dillon 	hmp = trans->hmp;
60e2a02b72SMatthew Dillon 
61e2a02b72SMatthew Dillon 	/*
62e2a02b72SMatthew Dillon 	 * As the number of inodes queued to the flusher increases we use
63e2a02b72SMatthew Dillon 	 * time-domain multiplexing to control read vs flush performance.
64e2a02b72SMatthew Dillon 	 * We have to do it here, before acquiring any ip or node locks,
65e2a02b72SMatthew Dillon 	 * to avoid deadlocking or excessively delaying the flusher.
66e2a02b72SMatthew Dillon 	 *
67e2a02b72SMatthew Dillon 	 * The full time period is hammer_tdmux_ticks, typically 1/5 of
68e2a02b72SMatthew Dillon 	 * a second.
69e2a02b72SMatthew Dillon 	 *
70e2a02b72SMatthew Dillon 	 * inode allocation begins to get restrained at 2/4 the limit
71e2a02b72SMatthew Dillon 	 * via the "hmrrcm" mechanism in hammer_inode.  We want to begin
72e2a02b72SMatthew Dillon 	 * limiting read activity before that to try to avoid processes
73e2a02b72SMatthew Dillon 	 * stalling out in "hmrrcm".
74e2a02b72SMatthew Dillon 	 */
75e2a02b72SMatthew Dillon 	tticks = hammer_tdmux_ticks;
76e2a02b72SMatthew Dillon 	if (trans->type != HAMMER_TRANS_FLS && tticks &&
77e2a02b72SMatthew Dillon 	    hmp->count_reclaims > hammer_limit_reclaims / tticks &&
78e2a02b72SMatthew Dillon 	    hmp->count_reclaims > hammer_autoflush * 2 &&
79e2a02b72SMatthew Dillon 	    hammer_flusher_running(hmp)) {
80e2a02b72SMatthew Dillon 		u_int rticks;
81e2a02b72SMatthew Dillon 		u_int xticks;
82e2a02b72SMatthew Dillon 		u_int dummy;
83e2a02b72SMatthew Dillon 
84e2a02b72SMatthew Dillon 		/*
85e2a02b72SMatthew Dillon 		 * 0 ... xticks ... tticks
86e2a02b72SMatthew Dillon 		 *
87e2a02b72SMatthew Dillon 		 * rticks is the calculated position, xticks is the demarc
88e2a02b72SMatthew Dillon 		 * where values below xticks are reserved for the flusher
89e2a02b72SMatthew Dillon 		 * and values >= to xticks may be used by the frontend.
90e2a02b72SMatthew Dillon 		 *
91e2a02b72SMatthew Dillon 		 * At least one tick is always made available for the
92e2a02b72SMatthew Dillon 		 * frontend.
93e2a02b72SMatthew Dillon 		 */
94e2a02b72SMatthew Dillon 		rticks = (u_int)ticks % tticks;
95e2a02b72SMatthew Dillon 		xticks = hmp->count_reclaims * tticks / hammer_limit_reclaims;
96e2a02b72SMatthew Dillon 
97e2a02b72SMatthew Dillon 		/*
98e2a02b72SMatthew Dillon 		 * Ensure rticks and xticks are stable
99e2a02b72SMatthew Dillon 		 */
100e2a02b72SMatthew Dillon 		cpu_ccfence();
101e2a02b72SMatthew Dillon 		if (rticks < xticks) {
102e2a02b72SMatthew Dillon 			if (hammer_debug_general & 0x0004)
10333234d14STomohiro Kusumi 				hdkprintf("rt %3u, xt %3u, tt %3u\n",
104e2a02b72SMatthew Dillon 					rticks, xticks, tticks);
105e2a02b72SMatthew Dillon 			tsleep(&dummy, 0, "htdmux", xticks - rticks);
106e2a02b72SMatthew Dillon 		}
107e2a02b72SMatthew Dillon 	}
10836f82b23SMatthew Dillon 
10961aeeb33SMatthew Dillon 	/*
1104e17f465SMatthew Dillon 	 * If the cursor operation is on behalf of an inode, lock
1114e17f465SMatthew Dillon 	 * the inode.
112e2a02b72SMatthew Dillon 	 *
113e2a02b72SMatthew Dillon 	 * When acquiring a shared lock on an inode on which the backend
114e2a02b72SMatthew Dillon 	 * flusher deadlocked, wait up to hammer_tdmux_ticks (1 second)
115e2a02b72SMatthew Dillon 	 * for the deadlock to clear.
1164e17f465SMatthew Dillon 	 */
1174e17f465SMatthew Dillon 	if ((cursor->ip = ip) != NULL) {
1184e17f465SMatthew Dillon 		++ip->cursor_ip_refs;
119e2a02b72SMatthew Dillon 		if (trans->type == HAMMER_TRANS_FLS) {
1204e17f465SMatthew Dillon 			hammer_lock_ex(&ip->lock);
121e2a02b72SMatthew Dillon 		} else {
122e2a02b72SMatthew Dillon #if 0
123e2a02b72SMatthew Dillon 			if (ip->cursor_exclreq_count) {
124e2a02b72SMatthew Dillon 				tsleep(&ip->cursor_exclreq_count, 0,
125e2a02b72SMatthew Dillon 				       "hstag1", hammer_tdmux_ticks);
126e2a02b72SMatthew Dillon 			}
127e2a02b72SMatthew Dillon #endif
1284e17f465SMatthew Dillon 			hammer_lock_sh(&ip->lock);
1294e17f465SMatthew Dillon 		}
130e2a02b72SMatthew Dillon 	}
1314e17f465SMatthew Dillon 
1324e17f465SMatthew Dillon 	/*
13361aeeb33SMatthew Dillon 	 * Step 1 - acquire a locked node from the cache if possible
13461aeeb33SMatthew Dillon 	 */
135bcac4bbbSMatthew Dillon 	if (cache && cache->node) {
1364c286c36SMatthew Dillon 		node = hammer_ref_node_safe(trans, cache, &error);
1378cd0a023SMatthew Dillon 		if (error == 0) {
13898da6d8cSMatthew Dillon 			hammer_lock_sh(&node->lock);
13961aeeb33SMatthew Dillon 			if (node->flags & HAMMER_NODE_DELETED) {
14061aeeb33SMatthew Dillon 				hammer_unlock(&node->lock);
14161aeeb33SMatthew Dillon 				hammer_rel_node(node);
14261aeeb33SMatthew Dillon 				node = NULL;
14361aeeb33SMatthew Dillon 			}
14461aeeb33SMatthew Dillon 		}
14539d8fd63SMatthew Dillon 		if (node == NULL)
14639d8fd63SMatthew Dillon 			++hammer_stats_btree_root_iterations;
14761aeeb33SMatthew Dillon 	} else {
14861aeeb33SMatthew Dillon 		node = NULL;
14939d8fd63SMatthew Dillon 		++hammer_stats_btree_root_iterations;
15061aeeb33SMatthew Dillon 	}
15161aeeb33SMatthew Dillon 
15261aeeb33SMatthew Dillon 	/*
15361aeeb33SMatthew Dillon 	 * Step 2 - If we couldn't get a node from the cache, get
15447197d71SMatthew Dillon 	 * the one from the root of the filesystem.
15561aeeb33SMatthew Dillon 	 */
15661aeeb33SMatthew Dillon 	while (node == NULL) {
157e2a02b72SMatthew Dillon 		volume = hammer_get_root_volume(hmp, &error);
15861aeeb33SMatthew Dillon 		if (error)
15961aeeb33SMatthew Dillon 			break;
16082010f9fSMatthew Dillon 		node = hammer_get_node(trans, volume->ondisk->vol0_btree_root,
16119619882SMatthew Dillon 				       0, &error);
16247197d71SMatthew Dillon 		hammer_rel_volume(volume, 0);
16361aeeb33SMatthew Dillon 		if (error)
16461aeeb33SMatthew Dillon 			break;
165e2a02b72SMatthew Dillon 		/*
166e2a02b72SMatthew Dillon 		 * When the frontend acquires the root b-tree node while the
167e2a02b72SMatthew Dillon 		 * backend is deadlocked on it, wait up to hammer_tdmux_ticks
168e2a02b72SMatthew Dillon 		 * (1 second) for the deadlock to clear.
169e2a02b72SMatthew Dillon 		 */
170e2a02b72SMatthew Dillon #if 0
171e2a02b72SMatthew Dillon 		if (node->cursor_exclreq_count &&
172e2a02b72SMatthew Dillon 		    cursor->trans->type != HAMMER_TRANS_FLS) {
173e2a02b72SMatthew Dillon 			tsleep(&node->cursor_exclreq_count, 0,
174e2a02b72SMatthew Dillon 			       "hstag3", hammer_tdmux_ticks);
175e2a02b72SMatthew Dillon 		}
176e2a02b72SMatthew Dillon #endif
17798da6d8cSMatthew Dillon 		hammer_lock_sh(&node->lock);
17811ad5adeSMatthew Dillon 
17911ad5adeSMatthew Dillon 		/*
18011ad5adeSMatthew Dillon 		 * If someone got in before we could lock the node, retry.
18111ad5adeSMatthew Dillon 		 */
18261aeeb33SMatthew Dillon 		if (node->flags & HAMMER_NODE_DELETED) {
18361aeeb33SMatthew Dillon 			hammer_unlock(&node->lock);
18461aeeb33SMatthew Dillon 			hammer_rel_node(node);
18561aeeb33SMatthew Dillon 			node = NULL;
18611ad5adeSMatthew Dillon 			continue;
18711ad5adeSMatthew Dillon 		}
18811ad5adeSMatthew Dillon 		if (volume->ondisk->vol0_btree_root != node->node_offset) {
18911ad5adeSMatthew Dillon 			hammer_unlock(&node->lock);
19011ad5adeSMatthew Dillon 			hammer_rel_node(node);
19111ad5adeSMatthew Dillon 			node = NULL;
19211ad5adeSMatthew Dillon 			continue;
1938cd0a023SMatthew Dillon 		}
19461aeeb33SMatthew Dillon 	}
19561aeeb33SMatthew Dillon 
19661aeeb33SMatthew Dillon 	/*
19761aeeb33SMatthew Dillon 	 * Step 3 - finish initializing the cursor by acquiring the parent
19861aeeb33SMatthew Dillon 	 */
19961aeeb33SMatthew Dillon 	cursor->node = node;
2008cd0a023SMatthew Dillon 	if (error == 0)
201f36a9737SMatthew Dillon 		error = hammer_load_cursor_parent(cursor, 0);
2020b075555SMatthew Dillon 	KKASSERT(error == 0);
203a84a197dSMatthew Dillon 	/* if (error) hammer_done_cursor(cursor); */
2048cd0a023SMatthew Dillon 	return(error);
2058cd0a023SMatthew Dillon }
2068cd0a023SMatthew Dillon 
2074e17f465SMatthew Dillon /*
2084e17f465SMatthew Dillon  * Normalize a cursor.  Sometimes cursors can be left in a state
2094e17f465SMatthew Dillon  * where node is NULL.  If the cursor is in this state, cursor up.
2104e17f465SMatthew Dillon  */
2114e17f465SMatthew Dillon void
hammer_normalize_cursor(hammer_cursor_t cursor)2124e17f465SMatthew Dillon hammer_normalize_cursor(hammer_cursor_t cursor)
2134e17f465SMatthew Dillon {
2144e17f465SMatthew Dillon 	if (cursor->node == NULL) {
2154e17f465SMatthew Dillon 		KKASSERT(cursor->parent != NULL);
2164e17f465SMatthew Dillon 		hammer_cursor_up(cursor);
2174e17f465SMatthew Dillon 	}
2184e17f465SMatthew Dillon }
2194e17f465SMatthew Dillon 
2204e17f465SMatthew Dillon 
2218cd0a023SMatthew Dillon /*
2228cd0a023SMatthew Dillon  * We are finished with a cursor.  We NULL out various fields as sanity
2238cd0a023SMatthew Dillon  * check, in case the structure is inappropriately used afterwords.
2248cd0a023SMatthew Dillon  */
2258cd0a023SMatthew Dillon void
hammer_done_cursor(hammer_cursor_t cursor)2268cd0a023SMatthew Dillon hammer_done_cursor(hammer_cursor_t cursor)
2278cd0a023SMatthew Dillon {
2284e17f465SMatthew Dillon 	hammer_inode_t ip;
2294e17f465SMatthew Dillon 
230adf01747SMatthew Dillon 	KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
2318cd0a023SMatthew Dillon 	if (cursor->parent) {
2328cd0a023SMatthew Dillon 		hammer_unlock(&cursor->parent->lock);
2338cd0a023SMatthew Dillon 		hammer_rel_node(cursor->parent);
2348cd0a023SMatthew Dillon 		cursor->parent = NULL;
2358cd0a023SMatthew Dillon 	}
2368cd0a023SMatthew Dillon 	if (cursor->node) {
2378cd0a023SMatthew Dillon 		hammer_unlock(&cursor->node->lock);
2388cd0a023SMatthew Dillon 		hammer_rel_node(cursor->node);
2398cd0a023SMatthew Dillon 		cursor->node = NULL;
2408cd0a023SMatthew Dillon 	}
2418cd0a023SMatthew Dillon         if (cursor->data_buffer) {
2428cd0a023SMatthew Dillon                 hammer_rel_buffer(cursor->data_buffer, 0);
2438cd0a023SMatthew Dillon                 cursor->data_buffer = NULL;
2448cd0a023SMatthew Dillon         }
2454e17f465SMatthew Dillon 	if ((ip = cursor->ip) != NULL) {
2464e17f465SMatthew Dillon                 KKASSERT(ip->cursor_ip_refs > 0);
2474e17f465SMatthew Dillon                 --ip->cursor_ip_refs;
2484e17f465SMatthew Dillon 		hammer_unlock(&ip->lock);
2494e17f465SMatthew Dillon                 cursor->ip = NULL;
2504e17f465SMatthew Dillon         }
2513f43fb33SMatthew Dillon 	if (cursor->iprec) {
2523f43fb33SMatthew Dillon 		hammer_rel_mem_record(cursor->iprec);
2533f43fb33SMatthew Dillon 		cursor->iprec = NULL;
2543f43fb33SMatthew Dillon 	}
2554e17f465SMatthew Dillon 
2566a37e7e4SMatthew Dillon 	/*
2576a37e7e4SMatthew Dillon 	 * If we deadlocked this node will be referenced.  Do a quick
2586a37e7e4SMatthew Dillon 	 * lock/unlock to wait for the deadlock condition to clear.
259e2a02b72SMatthew Dillon 	 *
260e2a02b72SMatthew Dillon 	 * Maintain exclreq_count / wakeup as necessary to notify new
261e2a02b72SMatthew Dillon 	 * entrants into ip.  We continue to hold the fs_token so our
262e2a02b72SMatthew Dillon 	 * EDEADLK retry loop should get its chance before another thread
263e2a02b72SMatthew Dillon 	 * steals the lock.
2646a37e7e4SMatthew Dillon 	 */
2656a37e7e4SMatthew Dillon 	if (cursor->deadlk_node) {
266e2a02b72SMatthew Dillon #if 0
267e2a02b72SMatthew Dillon 		if (ip && cursor->trans->type == HAMMER_TRANS_FLS)
268e2a02b72SMatthew Dillon 			++ip->cursor_exclreq_count;
269e2a02b72SMatthew Dillon 		++cursor->deadlk_node->cursor_exclreq_count;
270e2a02b72SMatthew Dillon #endif
271af209b0fSMatthew Dillon 		hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
2726a37e7e4SMatthew Dillon 		hammer_unlock(&cursor->deadlk_node->lock);
273e2a02b72SMatthew Dillon #if 0
274e2a02b72SMatthew Dillon 		if (--cursor->deadlk_node->cursor_exclreq_count == 0)
275e2a02b72SMatthew Dillon 			wakeup(&cursor->deadlk_node->cursor_exclreq_count);
276e2a02b72SMatthew Dillon 		if (ip && cursor->trans->type == HAMMER_TRANS_FLS) {
277e2a02b72SMatthew Dillon 			if (--ip->cursor_exclreq_count == 0)
278e2a02b72SMatthew Dillon 				wakeup(&ip->cursor_exclreq_count);
279e2a02b72SMatthew Dillon 		}
280e2a02b72SMatthew Dillon #endif
2816a37e7e4SMatthew Dillon 		hammer_rel_node(cursor->deadlk_node);
2826a37e7e4SMatthew Dillon 		cursor->deadlk_node = NULL;
2836a37e7e4SMatthew Dillon 	}
284b84de5afSMatthew Dillon 	if (cursor->deadlk_rec) {
285a99b9ea2SMatthew Dillon 		hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
286b84de5afSMatthew Dillon 		hammer_rel_mem_record(cursor->deadlk_rec);
287b84de5afSMatthew Dillon 		cursor->deadlk_rec = NULL;
288b84de5afSMatthew Dillon 	}
2896a37e7e4SMatthew Dillon 
29040043e7fSMatthew Dillon 	cursor->data = NULL;
29111ad5adeSMatthew Dillon 	cursor->leaf = NULL;
2928cd0a023SMatthew Dillon 	cursor->left_bound = NULL;
2938cd0a023SMatthew Dillon 	cursor->right_bound = NULL;
29436f82b23SMatthew Dillon 	cursor->trans = NULL;
2958cd0a023SMatthew Dillon }
2968cd0a023SMatthew Dillon 
2978cd0a023SMatthew Dillon /*
2986a37e7e4SMatthew Dillon  * Upgrade cursor->node and cursor->parent to exclusive locks.  This
2996a37e7e4SMatthew Dillon  * function can return EDEADLK.
3006a37e7e4SMatthew Dillon  *
3017aa3b8a6SMatthew Dillon  * The lock must already be either held shared or already held exclusively
3027aa3b8a6SMatthew Dillon  * by us.
3037aa3b8a6SMatthew Dillon  *
304e2a02b72SMatthew Dillon  * We upgrade the parent first as it is the most likely to collide first
305e2a02b72SMatthew Dillon  * with the downward traversal that the frontend typically does.
306e2a02b72SMatthew Dillon  *
3076a37e7e4SMatthew Dillon  * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
3086a37e7e4SMatthew Dillon  * we add another reference to the node that failed and set
3096a37e7e4SMatthew Dillon  * cursor->deadlk_node so hammer_done_cursor() can block on it.
3106a37e7e4SMatthew Dillon  */
3116a37e7e4SMatthew Dillon int
hammer_cursor_upgrade(hammer_cursor_t cursor)3126a37e7e4SMatthew Dillon hammer_cursor_upgrade(hammer_cursor_t cursor)
3136a37e7e4SMatthew Dillon {
3146a37e7e4SMatthew Dillon 	int error;
3156a37e7e4SMatthew Dillon 
316e2a02b72SMatthew Dillon 	if (cursor->parent) {
317e2a02b72SMatthew Dillon 		error = hammer_lock_upgrade(&cursor->parent->lock, 1);
318e2a02b72SMatthew Dillon 		if (error && cursor->deadlk_node == NULL) {
319e2a02b72SMatthew Dillon 			cursor->deadlk_node = cursor->parent;
320e2a02b72SMatthew Dillon 			hammer_ref_node(cursor->deadlk_node);
321e2a02b72SMatthew Dillon 		}
322e2a02b72SMatthew Dillon 	} else {
323e2a02b72SMatthew Dillon 		error = 0;
324e2a02b72SMatthew Dillon 	}
325e2a02b72SMatthew Dillon 	if (error == 0) {
326e2a02b72SMatthew Dillon 		error = hammer_lock_upgrade(&cursor->node->lock, 1);
327e2a02b72SMatthew Dillon 		if (error && cursor->deadlk_node == NULL) {
328e2a02b72SMatthew Dillon 			cursor->deadlk_node = cursor->node;
329e2a02b72SMatthew Dillon 			hammer_ref_node(cursor->deadlk_node);
330e2a02b72SMatthew Dillon 		}
331e2a02b72SMatthew Dillon 	}
332e2a02b72SMatthew Dillon #if 0
333bb29b5d8SMatthew Dillon 	error = hammer_lock_upgrade(&cursor->node->lock, 1);
3346a37e7e4SMatthew Dillon 	if (error && cursor->deadlk_node == NULL) {
3356a37e7e4SMatthew Dillon 		cursor->deadlk_node = cursor->node;
3366a37e7e4SMatthew Dillon 		hammer_ref_node(cursor->deadlk_node);
3377aa3b8a6SMatthew Dillon 	} else if (error == 0 && cursor->parent) {
338bb29b5d8SMatthew Dillon 		error = hammer_lock_upgrade(&cursor->parent->lock, 1);
3396a37e7e4SMatthew Dillon 		if (error && cursor->deadlk_node == NULL) {
3406a37e7e4SMatthew Dillon 			cursor->deadlk_node = cursor->parent;
3416a37e7e4SMatthew Dillon 			hammer_ref_node(cursor->deadlk_node);
3426a37e7e4SMatthew Dillon 		}
3436a37e7e4SMatthew Dillon 	}
344e2a02b72SMatthew Dillon #endif
3456a37e7e4SMatthew Dillon 	return(error);
3466a37e7e4SMatthew Dillon }
3476a37e7e4SMatthew Dillon 
3487bc5b8c2SMatthew Dillon int
hammer_cursor_upgrade_node(hammer_cursor_t cursor)3497bc5b8c2SMatthew Dillon hammer_cursor_upgrade_node(hammer_cursor_t cursor)
3507bc5b8c2SMatthew Dillon {
3517bc5b8c2SMatthew Dillon 	int error;
3527bc5b8c2SMatthew Dillon 
353bb29b5d8SMatthew Dillon 	error = hammer_lock_upgrade(&cursor->node->lock, 1);
3547bc5b8c2SMatthew Dillon 	if (error && cursor->deadlk_node == NULL) {
3557bc5b8c2SMatthew Dillon 		cursor->deadlk_node = cursor->node;
3567bc5b8c2SMatthew Dillon 		hammer_ref_node(cursor->deadlk_node);
3577bc5b8c2SMatthew Dillon 	}
3587bc5b8c2SMatthew Dillon 	return(error);
3597bc5b8c2SMatthew Dillon }
3607bc5b8c2SMatthew Dillon 
3616a37e7e4SMatthew Dillon /*
362d34bdd31STomohiro Kusumi  * Downgrade cursor->node and cursor->parent to shared locks.
3636a37e7e4SMatthew Dillon  */
3646a37e7e4SMatthew Dillon void
hammer_cursor_downgrade(hammer_cursor_t cursor)3656a37e7e4SMatthew Dillon hammer_cursor_downgrade(hammer_cursor_t cursor)
3666a37e7e4SMatthew Dillon {
3677aa3b8a6SMatthew Dillon 	if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
368bb29b5d8SMatthew Dillon 		hammer_lock_downgrade(&cursor->node->lock, 1);
3697aa3b8a6SMatthew Dillon 	if (cursor->parent &&
3707aa3b8a6SMatthew Dillon 	    hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
371bb29b5d8SMatthew Dillon 		hammer_lock_downgrade(&cursor->parent->lock, 1);
3726a37e7e4SMatthew Dillon 	}
3737aa3b8a6SMatthew Dillon }
3746a37e7e4SMatthew Dillon 
37532c90105SMatthew Dillon /*
376bb29b5d8SMatthew Dillon  * Upgrade and downgrade pairs of cursors.  This is used by the dedup
377bb29b5d8SMatthew Dillon  * code which must deal with two cursors.  A special function is needed
378bb29b5d8SMatthew Dillon  * because some of the nodes may be shared between the two cursors,
379bb29b5d8SMatthew Dillon  * resulting in share counts > 1 which will normally cause an upgrade
380bb29b5d8SMatthew Dillon  * to fail.
381bb29b5d8SMatthew Dillon  */
382bb29b5d8SMatthew Dillon static __noinline
383bb29b5d8SMatthew Dillon int
collect_node(hammer_node_t * array,int * counts,int n,hammer_node_t node)384bb29b5d8SMatthew Dillon collect_node(hammer_node_t *array, int *counts, int n, hammer_node_t node)
385bb29b5d8SMatthew Dillon {
386bb29b5d8SMatthew Dillon 	int i;
387bb29b5d8SMatthew Dillon 
388bb29b5d8SMatthew Dillon 	for (i = 0; i < n; ++i) {
389bb29b5d8SMatthew Dillon 		if (array[i] == node)
390bb29b5d8SMatthew Dillon 			break;
391bb29b5d8SMatthew Dillon 	}
392bb29b5d8SMatthew Dillon 	if (i == n) {
393bb29b5d8SMatthew Dillon 		array[i] = node;
394bb29b5d8SMatthew Dillon 		counts[i] = 1;
395bb29b5d8SMatthew Dillon 		++i;
396bb29b5d8SMatthew Dillon 	} else {
397bb29b5d8SMatthew Dillon 		++counts[i];
398bb29b5d8SMatthew Dillon 	}
399bb29b5d8SMatthew Dillon 	return(i);
400bb29b5d8SMatthew Dillon }
401bb29b5d8SMatthew Dillon 
402bb29b5d8SMatthew Dillon int
hammer_cursor_upgrade2(hammer_cursor_t cursor1,hammer_cursor_t cursor2)403bb29b5d8SMatthew Dillon hammer_cursor_upgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
404bb29b5d8SMatthew Dillon {
405bb29b5d8SMatthew Dillon 	hammer_node_t nodes[4];
406bb29b5d8SMatthew Dillon 	int counts[4];
407bb29b5d8SMatthew Dillon 	int error;
408bb29b5d8SMatthew Dillon 	int i;
409bb29b5d8SMatthew Dillon 	int n;
410bb29b5d8SMatthew Dillon 
411bb29b5d8SMatthew Dillon 	n = collect_node(nodes, counts, 0, cursor1->node);
412bb29b5d8SMatthew Dillon 	if (cursor1->parent)
413bb29b5d8SMatthew Dillon 		n = collect_node(nodes, counts, n, cursor1->parent);
414bb29b5d8SMatthew Dillon 	n = collect_node(nodes, counts, n, cursor2->node);
415bb29b5d8SMatthew Dillon 	if (cursor2->parent)
416bb29b5d8SMatthew Dillon 		n = collect_node(nodes, counts, n, cursor2->parent);
417bb29b5d8SMatthew Dillon 
418bb29b5d8SMatthew Dillon 	error = 0;
419bb29b5d8SMatthew Dillon 	for (i = 0; i < n; ++i) {
420bb29b5d8SMatthew Dillon 		error = hammer_lock_upgrade(&nodes[i]->lock, counts[i]);
421bb29b5d8SMatthew Dillon 		if (error)
422bb29b5d8SMatthew Dillon 			break;
423bb29b5d8SMatthew Dillon 	}
424bb29b5d8SMatthew Dillon 	if (error) {
425bb29b5d8SMatthew Dillon 		while (--i >= 0)
426bb29b5d8SMatthew Dillon 			hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
427bb29b5d8SMatthew Dillon 	}
428bb29b5d8SMatthew Dillon 	return (error);
429bb29b5d8SMatthew Dillon }
430bb29b5d8SMatthew Dillon 
431bb29b5d8SMatthew Dillon void
hammer_cursor_downgrade2(hammer_cursor_t cursor1,hammer_cursor_t cursor2)432bb29b5d8SMatthew Dillon hammer_cursor_downgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
433bb29b5d8SMatthew Dillon {
434bb29b5d8SMatthew Dillon 	hammer_node_t nodes[4];
435bb29b5d8SMatthew Dillon 	int counts[4];
436bb29b5d8SMatthew Dillon 	int i;
437bb29b5d8SMatthew Dillon 	int n;
438bb29b5d8SMatthew Dillon 
439bb29b5d8SMatthew Dillon 	n = collect_node(nodes, counts, 0, cursor1->node);
440bb29b5d8SMatthew Dillon 	if (cursor1->parent)
441bb29b5d8SMatthew Dillon 		n = collect_node(nodes, counts, n, cursor1->parent);
442bb29b5d8SMatthew Dillon 	n = collect_node(nodes, counts, n, cursor2->node);
443bb29b5d8SMatthew Dillon 	if (cursor2->parent)
444bb29b5d8SMatthew Dillon 		n = collect_node(nodes, counts, n, cursor2->parent);
445bb29b5d8SMatthew Dillon 
446bb29b5d8SMatthew Dillon 	for (i = 0; i < n; ++i)
447bb29b5d8SMatthew Dillon 		hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
448bb29b5d8SMatthew Dillon }
449bb29b5d8SMatthew Dillon 
450bb29b5d8SMatthew Dillon /*
45132c90105SMatthew Dillon  * Seek the cursor to the specified node and index.
452cb51be26SMatthew Dillon  *
453cb51be26SMatthew Dillon  * The caller must ref the node prior to calling this routine and release
454cb51be26SMatthew Dillon  * it after it returns.  If the seek succeeds the cursor will gain its own
455cb51be26SMatthew Dillon  * ref on the node.
45632c90105SMatthew Dillon  */
45732c90105SMatthew Dillon int
hammer_cursor_seek(hammer_cursor_t cursor,hammer_node_t node,int index)45832c90105SMatthew Dillon hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
45932c90105SMatthew Dillon {
46032c90105SMatthew Dillon 	int error;
46132c90105SMatthew Dillon 
46232c90105SMatthew Dillon 	hammer_cursor_downgrade(cursor);
46332c90105SMatthew Dillon 	error = 0;
46432c90105SMatthew Dillon 
46532c90105SMatthew Dillon 	if (cursor->node != node) {
46632c90105SMatthew Dillon 		hammer_unlock(&cursor->node->lock);
46732c90105SMatthew Dillon 		hammer_rel_node(cursor->node);
46832c90105SMatthew Dillon 		cursor->node = node;
46932c90105SMatthew Dillon 		hammer_ref_node(node);
47032c90105SMatthew Dillon 		hammer_lock_sh(&node->lock);
471f36a9737SMatthew Dillon 		KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
47232c90105SMatthew Dillon 
47332c90105SMatthew Dillon 		if (cursor->parent) {
47432c90105SMatthew Dillon 			hammer_unlock(&cursor->parent->lock);
47532c90105SMatthew Dillon 			hammer_rel_node(cursor->parent);
47632c90105SMatthew Dillon 			cursor->parent = NULL;
47732c90105SMatthew Dillon 			cursor->parent_index = 0;
47832c90105SMatthew Dillon 		}
479f36a9737SMatthew Dillon 		error = hammer_load_cursor_parent(cursor, 0);
48032c90105SMatthew Dillon 	}
481a84a197dSMatthew Dillon 	cursor->index = index;
48232c90105SMatthew Dillon 	return (error);
48332c90105SMatthew Dillon }
4846a37e7e4SMatthew Dillon 
4856a37e7e4SMatthew Dillon /*
48647197d71SMatthew Dillon  * Load the parent of cursor->node into cursor->parent.
4878cd0a023SMatthew Dillon  */
4888cd0a023SMatthew Dillon static
4898cd0a023SMatthew Dillon int
hammer_load_cursor_parent(hammer_cursor_t cursor,int try_exclusive)490f36a9737SMatthew Dillon hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive)
4918cd0a023SMatthew Dillon {
49247197d71SMatthew Dillon 	hammer_mount_t hmp;
4938cd0a023SMatthew Dillon 	hammer_node_t parent;
49447197d71SMatthew Dillon 	hammer_node_t node;
4958cd0a023SMatthew Dillon 	hammer_btree_elm_t elm;
4968cd0a023SMatthew Dillon 	int error;
497c82af904SMatthew Dillon 	int parent_index;
4988cd0a023SMatthew Dillon 
49936f82b23SMatthew Dillon 	hmp = cursor->trans->hmp;
50047197d71SMatthew Dillon 
50147197d71SMatthew Dillon 	if (cursor->node->ondisk->parent) {
5028cd0a023SMatthew Dillon 		node = cursor->node;
50382010f9fSMatthew Dillon 		parent = hammer_btree_get_parent(cursor->trans, node,
50482010f9fSMatthew Dillon 						 &parent_index,
505c82af904SMatthew Dillon 						 &error, try_exclusive);
506c82af904SMatthew Dillon 		if (error == 0) {
507c82af904SMatthew Dillon 			elm = &parent->ondisk->elms[parent_index];
5088cd0a023SMatthew Dillon 			cursor->parent = parent;
509c82af904SMatthew Dillon 			cursor->parent_index = parent_index;
5108cd0a023SMatthew Dillon 			cursor->left_bound = &elm[0].internal.base;
5118cd0a023SMatthew Dillon 			cursor->right_bound = &elm[1].internal.base;
512c82af904SMatthew Dillon 		}
51347197d71SMatthew Dillon 	} else {
51447197d71SMatthew Dillon 		cursor->parent = NULL;
51547197d71SMatthew Dillon 		cursor->parent_index = 0;
51647197d71SMatthew Dillon 		cursor->left_bound = &hmp->root_btree_beg;
51747197d71SMatthew Dillon 		cursor->right_bound = &hmp->root_btree_end;
51847197d71SMatthew Dillon 		error = 0;
5198cd0a023SMatthew Dillon 	}
5208cd0a023SMatthew Dillon 	return(error);
5218cd0a023SMatthew Dillon }
5228cd0a023SMatthew Dillon 
5238cd0a023SMatthew Dillon /*
5248cd0a023SMatthew Dillon  * Cursor up to our parent node.  Return ENOENT if we are at the root of
52547197d71SMatthew Dillon  * the filesystem.
5268cd0a023SMatthew Dillon  */
5278cd0a023SMatthew Dillon int
hammer_cursor_up(hammer_cursor_t cursor)5286a37e7e4SMatthew Dillon hammer_cursor_up(hammer_cursor_t cursor)
5298cd0a023SMatthew Dillon {
5308cd0a023SMatthew Dillon 	int error;
5318cd0a023SMatthew Dillon 
5326a37e7e4SMatthew Dillon 	hammer_cursor_downgrade(cursor);
533195c19a1SMatthew Dillon 
534195c19a1SMatthew Dillon 	/*
5354e17f465SMatthew Dillon 	 * If the parent is NULL we are at the root of the B-Tree and
5364e17f465SMatthew Dillon 	 * return ENOENT.
5374e17f465SMatthew Dillon 	 */
5384e17f465SMatthew Dillon 	if (cursor->parent == NULL)
5394e17f465SMatthew Dillon 		return (ENOENT);
5404e17f465SMatthew Dillon 
5414e17f465SMatthew Dillon 	/*
5424e17f465SMatthew Dillon 	 * Set the node to its parent.
5438cd0a023SMatthew Dillon 	 */
5448cd0a023SMatthew Dillon 	hammer_unlock(&cursor->node->lock);
5458cd0a023SMatthew Dillon 	hammer_rel_node(cursor->node);
5468cd0a023SMatthew Dillon 	cursor->node = cursor->parent;
5478cd0a023SMatthew Dillon 	cursor->index = cursor->parent_index;
5488cd0a023SMatthew Dillon 	cursor->parent = NULL;
5498cd0a023SMatthew Dillon 	cursor->parent_index = 0;
5508cd0a023SMatthew Dillon 
551f36a9737SMatthew Dillon 	error = hammer_load_cursor_parent(cursor, 0);
5528cd0a023SMatthew Dillon 	return(error);
5538cd0a023SMatthew Dillon }
5548cd0a023SMatthew Dillon 
5558cd0a023SMatthew Dillon /*
556f36a9737SMatthew Dillon  * Special cursor up given a locked cursor.  The orignal node is not
557b3bad96fSMatthew Dillon  * unlocked or released and the cursor is not downgraded.
558b3bad96fSMatthew Dillon  *
559f3a4893bSMatthew Dillon  * This function can fail with EDEADLK.
560f3a4893bSMatthew Dillon  *
561f3a4893bSMatthew Dillon  * This function is only run when recursively deleting parent nodes
562f3a4893bSMatthew Dillon  * to get rid of an empty leaf.
563f36a9737SMatthew Dillon  */
564f36a9737SMatthew Dillon int
hammer_cursor_up_locked(hammer_cursor_t cursor)565f36a9737SMatthew Dillon hammer_cursor_up_locked(hammer_cursor_t cursor)
566f36a9737SMatthew Dillon {
567f36a9737SMatthew Dillon 	hammer_node_t save;
568f36a9737SMatthew Dillon 	int error;
569f3a4893bSMatthew Dillon 	int save_index;
570f36a9737SMatthew Dillon 
571f36a9737SMatthew Dillon 	/*
572f36a9737SMatthew Dillon 	 * If the parent is NULL we are at the root of the B-Tree and
573f36a9737SMatthew Dillon 	 * return ENOENT.
574f36a9737SMatthew Dillon 	 */
575f36a9737SMatthew Dillon 	if (cursor->parent == NULL)
576f36a9737SMatthew Dillon 		return (ENOENT);
577f36a9737SMatthew Dillon 
578f36a9737SMatthew Dillon 	save = cursor->node;
579f3a4893bSMatthew Dillon 	save_index = cursor->index;
580f36a9737SMatthew Dillon 
581f36a9737SMatthew Dillon 	/*
582f36a9737SMatthew Dillon 	 * Set the node to its parent.
583f36a9737SMatthew Dillon 	 */
584f36a9737SMatthew Dillon 	cursor->node = cursor->parent;
585f36a9737SMatthew Dillon 	cursor->index = cursor->parent_index;
586f36a9737SMatthew Dillon 	cursor->parent = NULL;
587f36a9737SMatthew Dillon 	cursor->parent_index = 0;
588f36a9737SMatthew Dillon 
589f36a9737SMatthew Dillon 	/*
590f36a9737SMatthew Dillon 	 * load the new parent, attempt to exclusively lock it.  Note that
591f36a9737SMatthew Dillon 	 * we are still holding the old parent (now cursor->node) exclusively
592f3a4893bSMatthew Dillon 	 * locked.
593f3a4893bSMatthew Dillon 	 *
594f3a4893bSMatthew Dillon 	 * This can return EDEADLK.  Undo the operation on any error.  These
595f3a4893bSMatthew Dillon 	 * up sequences can occur during iterations so be sure to restore
596f3a4893bSMatthew Dillon 	 * the index.
597f36a9737SMatthew Dillon 	 */
598f36a9737SMatthew Dillon 	error = hammer_load_cursor_parent(cursor, 1);
599f36a9737SMatthew Dillon 	if (error) {
600f36a9737SMatthew Dillon 		cursor->parent = cursor->node;
601f36a9737SMatthew Dillon 		cursor->parent_index = cursor->index;
602f36a9737SMatthew Dillon 		cursor->node = save;
603f3a4893bSMatthew Dillon 		cursor->index = save_index;
604f36a9737SMatthew Dillon 	}
605f36a9737SMatthew Dillon 	return(error);
606f36a9737SMatthew Dillon }
607f36a9737SMatthew Dillon 
608f36a9737SMatthew Dillon 
609f36a9737SMatthew Dillon /*
6108cd0a023SMatthew Dillon  * Cursor down through the current node, which must be an internal node.
6118cd0a023SMatthew Dillon  *
6128cd0a023SMatthew Dillon  * This routine adjusts the cursor and sets index to 0.
6138cd0a023SMatthew Dillon  */
6148cd0a023SMatthew Dillon int
hammer_cursor_down(hammer_cursor_t cursor)6158cd0a023SMatthew Dillon hammer_cursor_down(hammer_cursor_t cursor)
6168cd0a023SMatthew Dillon {
6178cd0a023SMatthew Dillon 	hammer_node_t node;
6188cd0a023SMatthew Dillon 	hammer_btree_elm_t elm;
6198cd0a023SMatthew Dillon 	int error;
6208cd0a023SMatthew Dillon 
6218cd0a023SMatthew Dillon 	/*
6228cd0a023SMatthew Dillon 	 * The current node becomes the current parent
6238cd0a023SMatthew Dillon 	 */
6246a37e7e4SMatthew Dillon 	hammer_cursor_downgrade(cursor);
6258cd0a023SMatthew Dillon 	node = cursor->node;
6268cd0a023SMatthew Dillon 	KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
6278cd0a023SMatthew Dillon 	if (cursor->parent) {
6288cd0a023SMatthew Dillon 		hammer_unlock(&cursor->parent->lock);
6298cd0a023SMatthew Dillon 		hammer_rel_node(cursor->parent);
6308cd0a023SMatthew Dillon 	}
6318cd0a023SMatthew Dillon 	cursor->parent = node;
6328cd0a023SMatthew Dillon 	cursor->parent_index = cursor->index;
6338cd0a023SMatthew Dillon 	cursor->node = NULL;
6348cd0a023SMatthew Dillon 	cursor->index = 0;
6358cd0a023SMatthew Dillon 
6368cd0a023SMatthew Dillon 	/*
63776376933SMatthew Dillon 	 * Extract element to push into at (node,index), set bounds.
6388cd0a023SMatthew Dillon 	 */
6398cd0a023SMatthew Dillon 	elm = &node->ondisk->elms[cursor->parent_index];
6408cd0a023SMatthew Dillon 
6418cd0a023SMatthew Dillon 	/*
6421424c922STomohiro Kusumi 	 * Ok, push down into elm of an internal node.
6438cd0a023SMatthew Dillon 	 */
644fe7678eeSMatthew Dillon 	KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
645fe7678eeSMatthew Dillon 	KKASSERT(elm->internal.subtree_offset != 0);
646fe7678eeSMatthew Dillon 	cursor->left_bound = &elm[0].internal.base;
647fe7678eeSMatthew Dillon 	cursor->right_bound = &elm[1].internal.base;
64882010f9fSMatthew Dillon 	node = hammer_get_node(cursor->trans,
64919619882SMatthew Dillon 			       elm->internal.subtree_offset, 0, &error);
650fe7678eeSMatthew Dillon 	if (error == 0) {
65160b4e24fSTomohiro Kusumi 		KASSERT(elm->base.btype == node->ondisk->type,
65260b4e24fSTomohiro Kusumi 			("BTYPE MISMATCH %c %c NODE %p",
65360b4e24fSTomohiro Kusumi 			 elm->base.btype, node->ondisk->type, node));
654fe7678eeSMatthew Dillon 		if (node->ondisk->parent != cursor->parent->node_offset)
655*35a5249bSTomohiro Kusumi 			hpanic("node %p %016jx vs %016jx",
65660b4e24fSTomohiro Kusumi 				node,
657*35a5249bSTomohiro Kusumi 				(intmax_t)node->ondisk->parent,
658*35a5249bSTomohiro Kusumi 				(intmax_t)cursor->parent->node_offset);
659fe7678eeSMatthew Dillon 		KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
660fe7678eeSMatthew Dillon 	}
661e2a02b72SMatthew Dillon 
662e2a02b72SMatthew Dillon 	/*
663e2a02b72SMatthew Dillon 	 * If no error occured we can lock the new child node.  If the
664e2a02b72SMatthew Dillon 	 * node is deadlock flagged wait up to hammer_tdmux_ticks (1 second)
665e2a02b72SMatthew Dillon 	 * for the deadlock to clear.  Otherwise a large number of concurrent
666e2a02b72SMatthew Dillon 	 * readers can continuously stall the flusher.
667e2a02b72SMatthew Dillon 	 *
668e2a02b72SMatthew Dillon 	 * We specifically do this in the cursor_down() code in order to
669e2a02b72SMatthew Dillon 	 * deal with frontend top-down searches smashing against bottom-up
670e2a02b72SMatthew Dillon 	 * flusher-based mirror updates.  These collisions typically occur
671e2a02b72SMatthew Dillon 	 * above the inode in the B-Tree and are not covered by the
672e2a02b72SMatthew Dillon 	 * ip->cursor_exclreq_count logic.
673e2a02b72SMatthew Dillon 	 */
6748cd0a023SMatthew Dillon 	if (error == 0) {
675e2a02b72SMatthew Dillon #if 0
676e2a02b72SMatthew Dillon 		if (node->cursor_exclreq_count &&
677e2a02b72SMatthew Dillon 		    cursor->trans->type != HAMMER_TRANS_FLS) {
678e2a02b72SMatthew Dillon 			tsleep(&node->cursor_exclreq_count, 0,
679e2a02b72SMatthew Dillon 			       "hstag2", hammer_tdmux_ticks);
680e2a02b72SMatthew Dillon 		}
681e2a02b72SMatthew Dillon #endif
6826a37e7e4SMatthew Dillon 		hammer_lock_sh(&node->lock);
683f36a9737SMatthew Dillon 		KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
6848cd0a023SMatthew Dillon 		cursor->node = node;
6858cd0a023SMatthew Dillon 		cursor->index = 0;
6868cd0a023SMatthew Dillon 	}
6878cd0a023SMatthew Dillon 	return(error);
6888cd0a023SMatthew Dillon }
6898cd0a023SMatthew Dillon 
690b3bad96fSMatthew Dillon /************************************************************************
691b3bad96fSMatthew Dillon  *				DEADLOCK RECOVERY			*
692b3bad96fSMatthew Dillon  ************************************************************************
693b3bad96fSMatthew Dillon  *
694b3bad96fSMatthew Dillon  * These are the new deadlock recovery functions.  Currently they are only
695b3bad96fSMatthew Dillon  * used for the mirror propagation and physical node removal cases but
696b3bad96fSMatthew Dillon  * ultimately the intention is to use them for all deadlock recovery
697b3bad96fSMatthew Dillon  * operations.
698c9ce54d6SMatthew Dillon  *
699c9ce54d6SMatthew Dillon  * WARNING!  The contents of the cursor may be modified while unlocked.
700c9ce54d6SMatthew Dillon  *	     passive modifications including adjusting the node, parent,
701c9ce54d6SMatthew Dillon  *	     indexes, and leaf pointer.
702c9ce54d6SMatthew Dillon  *
703c9ce54d6SMatthew Dillon  *	     An outright removal of the element the cursor was pointing at
704c9ce54d6SMatthew Dillon  *	     will cause the HAMMER_CURSOR_TRACKED_RIPOUT flag to be set,
705c9ce54d6SMatthew Dillon  *	     which chains to causing the HAMMER_CURSOR_RETEST to be set
706c9ce54d6SMatthew Dillon  *	     when the cursor is locked again.
707b3bad96fSMatthew Dillon  */
708adf01747SMatthew Dillon void
hammer_unlock_cursor(hammer_cursor_t cursor)709982be4bfSMatthew Dillon hammer_unlock_cursor(hammer_cursor_t cursor)
710b3bad96fSMatthew Dillon {
711b3bad96fSMatthew Dillon 	hammer_node_t node;
712b3bad96fSMatthew Dillon 
713adf01747SMatthew Dillon 	KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
714b3bad96fSMatthew Dillon 	KKASSERT(cursor->node);
71519168d41SMatthew Dillon 
71619168d41SMatthew Dillon 	/*
717b3bad96fSMatthew Dillon 	 * Release the cursor's locks and track B-Tree operations on node.
718b3bad96fSMatthew Dillon 	 * While being tracked our cursor can be modified by other threads
7195c8d05e2SMatthew Dillon 	 * and the node may be replaced.
720b3bad96fSMatthew Dillon 	 */
721b3bad96fSMatthew Dillon 	if (cursor->parent) {
722b3bad96fSMatthew Dillon 		hammer_unlock(&cursor->parent->lock);
723b3bad96fSMatthew Dillon 		hammer_rel_node(cursor->parent);
724b3bad96fSMatthew Dillon 		cursor->parent = NULL;
725b3bad96fSMatthew Dillon 	}
726b3bad96fSMatthew Dillon 	node = cursor->node;
727adf01747SMatthew Dillon 	cursor->flags |= HAMMER_CURSOR_TRACKED;
728b3bad96fSMatthew Dillon 	TAILQ_INSERT_TAIL(&node->cursor_list, cursor, deadlk_entry);
729b3bad96fSMatthew Dillon 	hammer_unlock(&node->lock);
730adf01747SMatthew Dillon }
731adf01747SMatthew Dillon 
732adf01747SMatthew Dillon /*
733adf01747SMatthew Dillon  * Get the cursor heated up again.  The cursor's node may have
734adf01747SMatthew Dillon  * changed and we might have to locate the new parent.
735adf01747SMatthew Dillon  *
736adf01747SMatthew Dillon  * If the exact element we were on got deleted RIPOUT will be
737adf01747SMatthew Dillon  * set and we must clear ATEDISK so an iteration does not skip
738adf01747SMatthew Dillon  * the element after it.
739adf01747SMatthew Dillon  */
740adf01747SMatthew Dillon int
hammer_lock_cursor(hammer_cursor_t cursor)741982be4bfSMatthew Dillon hammer_lock_cursor(hammer_cursor_t cursor)
742adf01747SMatthew Dillon {
743adf01747SMatthew Dillon 	hammer_node_t node;
744adf01747SMatthew Dillon 	int error;
745adf01747SMatthew Dillon 
746adf01747SMatthew Dillon 	KKASSERT(cursor->flags & HAMMER_CURSOR_TRACKED);
747adf01747SMatthew Dillon 
748adf01747SMatthew Dillon 	/*
749adf01747SMatthew Dillon 	 * Relock the node
750adf01747SMatthew Dillon 	 */
751adf01747SMatthew Dillon 	for (;;) {
752adf01747SMatthew Dillon 		node = cursor->node;
753adf01747SMatthew Dillon 		hammer_ref_node(node);
754adf01747SMatthew Dillon 		hammer_lock_sh(&node->lock);
755adf01747SMatthew Dillon 		if (cursor->node == node) {
756adf01747SMatthew Dillon 			hammer_rel_node(node);
757adf01747SMatthew Dillon 			break;
758adf01747SMatthew Dillon 		}
759adf01747SMatthew Dillon 		hammer_unlock(&node->lock);
760adf01747SMatthew Dillon 		hammer_rel_node(node);
761adf01747SMatthew Dillon 	}
762adf01747SMatthew Dillon 
763adf01747SMatthew Dillon 	/*
764adf01747SMatthew Dillon 	 * Untrack the cursor, clean up, and re-establish the parent node.
765adf01747SMatthew Dillon 	 */
766adf01747SMatthew Dillon 	TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
767adf01747SMatthew Dillon 	cursor->flags &= ~HAMMER_CURSOR_TRACKED;
768adf01747SMatthew Dillon 
7695c8d05e2SMatthew Dillon 	/*
7705c8d05e2SMatthew Dillon 	 * If a ripout has occured iterations must re-test the (new)
7715c8d05e2SMatthew Dillon 	 * current element.  Clearing ATEDISK prevents the element from
7725c8d05e2SMatthew Dillon 	 * being skipped and RETEST causes it to be re-tested.
7735c8d05e2SMatthew Dillon 	 */
774adf01747SMatthew Dillon 	if (cursor->flags & HAMMER_CURSOR_TRACKED_RIPOUT) {
775adf01747SMatthew Dillon 		cursor->flags &= ~HAMMER_CURSOR_TRACKED_RIPOUT;
776adf01747SMatthew Dillon 		cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
7775c8d05e2SMatthew Dillon 		cursor->flags |= HAMMER_CURSOR_RETEST;
778adf01747SMatthew Dillon 	}
779adf01747SMatthew Dillon 	error = hammer_load_cursor_parent(cursor, 0);
780adf01747SMatthew Dillon 	return(error);
781adf01747SMatthew Dillon }
782adf01747SMatthew Dillon 
783adf01747SMatthew Dillon /*
784adf01747SMatthew Dillon  * Recover from a deadlocked cursor, tracking any node removals or
785adf01747SMatthew Dillon  * replacements.  If the cursor's current node is removed by another
786adf01747SMatthew Dillon  * thread (via btree_remove()) the cursor will be seeked upwards.
78798da6d8cSMatthew Dillon  *
78898da6d8cSMatthew Dillon  * The caller is working a modifying operation and must be holding the
78998da6d8cSMatthew Dillon  * sync lock (shared).  We do not release the sync lock because this
79098da6d8cSMatthew Dillon  * would break atomicy.
791adf01747SMatthew Dillon  */
792adf01747SMatthew Dillon int
hammer_recover_cursor(hammer_cursor_t cursor)793adf01747SMatthew Dillon hammer_recover_cursor(hammer_cursor_t cursor)
794adf01747SMatthew Dillon {
795f31f6d84SSascha Wildner 	hammer_transaction_t trans __debugvar;
796f31f6d84SSascha Wildner #if 0
797e2a02b72SMatthew Dillon 	hammer_inode_t ip;
798f31f6d84SSascha Wildner #endif
799adf01747SMatthew Dillon 	int error;
800adf01747SMatthew Dillon 
801982be4bfSMatthew Dillon 	hammer_unlock_cursor(cursor);
802e2a02b72SMatthew Dillon 
803f31f6d84SSascha Wildner #if 0
804e2a02b72SMatthew Dillon 	ip = cursor->ip;
805f31f6d84SSascha Wildner #endif
806e2a02b72SMatthew Dillon 	trans = cursor->trans;
807e2a02b72SMatthew Dillon 	KKASSERT(trans->sync_lock_refs > 0);
808b3bad96fSMatthew Dillon 
809b3bad96fSMatthew Dillon 	/*
810e2a02b72SMatthew Dillon 	 * Wait for the deadlock to clear.
811e2a02b72SMatthew Dillon 	 *
812e2a02b72SMatthew Dillon 	 * Maintain exclreq_count / wakeup as necessary to notify new
813e2a02b72SMatthew Dillon 	 * entrants into ip.  We continue to hold the fs_token so our
814e2a02b72SMatthew Dillon 	 * EDEADLK retry loop should get its chance before another thread
815e2a02b72SMatthew Dillon 	 * steals the lock.
816b3bad96fSMatthew Dillon 	 */
817b3bad96fSMatthew Dillon 	if (cursor->deadlk_node) {
818e2a02b72SMatthew Dillon #if 0
819e2a02b72SMatthew Dillon 		if (ip && trans->type == HAMMER_TRANS_FLS)
820e2a02b72SMatthew Dillon 			++ip->cursor_exclreq_count;
821e2a02b72SMatthew Dillon 		++cursor->deadlk_node->cursor_exclreq_count;
822e2a02b72SMatthew Dillon #endif
823b3bad96fSMatthew Dillon 		hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
824b3bad96fSMatthew Dillon 		hammer_unlock(&cursor->deadlk_node->lock);
825e2a02b72SMatthew Dillon #if 0
826e2a02b72SMatthew Dillon 		if (--cursor->deadlk_node->cursor_exclreq_count == 0)
827e2a02b72SMatthew Dillon 			wakeup(&cursor->deadlk_node->cursor_exclreq_count);
828e2a02b72SMatthew Dillon 		if (ip && trans->type == HAMMER_TRANS_FLS) {
829e2a02b72SMatthew Dillon 			if (--ip->cursor_exclreq_count == 0)
830e2a02b72SMatthew Dillon 				wakeup(&ip->cursor_exclreq_count);
831e2a02b72SMatthew Dillon 		}
832e2a02b72SMatthew Dillon #endif
833b3bad96fSMatthew Dillon 		hammer_rel_node(cursor->deadlk_node);
834b3bad96fSMatthew Dillon 		cursor->deadlk_node = NULL;
835b3bad96fSMatthew Dillon 	}
836b3bad96fSMatthew Dillon 	if (cursor->deadlk_rec) {
837b3bad96fSMatthew Dillon 		hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
838b3bad96fSMatthew Dillon 		hammer_rel_mem_record(cursor->deadlk_rec);
839b3bad96fSMatthew Dillon 		cursor->deadlk_rec = NULL;
840b3bad96fSMatthew Dillon 	}
841982be4bfSMatthew Dillon 	error = hammer_lock_cursor(cursor);
842b3bad96fSMatthew Dillon 	return(error);
843b3bad96fSMatthew Dillon }
844b3bad96fSMatthew Dillon 
845b3bad96fSMatthew Dillon /*
846adf01747SMatthew Dillon  * Dup ocursor to ncursor.  ncursor inherits ocursor's locks and ocursor
847adf01747SMatthew Dillon  * is effectively unlocked and becomes tracked.  If ocursor was not locked
848adf01747SMatthew Dillon  * then ncursor also inherits the tracking.
849adf01747SMatthew Dillon  *
850adf01747SMatthew Dillon  * After the caller finishes working with ncursor it must be cleaned up
851adf01747SMatthew Dillon  * with hammer_done_cursor(), and the caller must re-lock ocursor.
852adf01747SMatthew Dillon  */
8533f43fb33SMatthew Dillon hammer_cursor_t
hammer_push_cursor(hammer_cursor_t ocursor)8543f43fb33SMatthew Dillon hammer_push_cursor(hammer_cursor_t ocursor)
855adf01747SMatthew Dillon {
8563f43fb33SMatthew Dillon 	hammer_cursor_t ncursor;
857adf01747SMatthew Dillon 	hammer_inode_t ip;
858adf01747SMatthew Dillon 	hammer_node_t node;
859bac808feSMatthew Dillon 	hammer_mount_t hmp;
860adf01747SMatthew Dillon 
861bac808feSMatthew Dillon 	hmp = ocursor->trans->hmp;
862bac808feSMatthew Dillon 	ncursor = kmalloc(sizeof(*ncursor), hmp->m_misc, M_WAITOK | M_ZERO);
863adf01747SMatthew Dillon 	bcopy(ocursor, ncursor, sizeof(*ocursor));
864adf01747SMatthew Dillon 
865adf01747SMatthew Dillon 	node = ocursor->node;
866adf01747SMatthew Dillon 	hammer_ref_node(node);
867adf01747SMatthew Dillon 	if ((ocursor->flags & HAMMER_CURSOR_TRACKED) == 0) {
868adf01747SMatthew Dillon 		ocursor->flags |= HAMMER_CURSOR_TRACKED;
869adf01747SMatthew Dillon 		TAILQ_INSERT_TAIL(&node->cursor_list, ocursor, deadlk_entry);
870adf01747SMatthew Dillon 	}
871adf01747SMatthew Dillon 	if (ncursor->parent)
872adf01747SMatthew Dillon 		ocursor->parent = NULL;
873adf01747SMatthew Dillon 	ocursor->data_buffer = NULL;
874adf01747SMatthew Dillon 	ocursor->leaf = NULL;
875adf01747SMatthew Dillon 	ocursor->data = NULL;
876adf01747SMatthew Dillon 	if (ncursor->flags & HAMMER_CURSOR_TRACKED)
877adf01747SMatthew Dillon 		TAILQ_INSERT_TAIL(&node->cursor_list, ncursor, deadlk_entry);
878adf01747SMatthew Dillon 	if ((ip = ncursor->ip) != NULL) {
879adf01747SMatthew Dillon                 ++ip->cursor_ip_refs;
880adf01747SMatthew Dillon 	}
881adf01747SMatthew Dillon 	if (ncursor->iprec)
882adf01747SMatthew Dillon 		hammer_ref(&ncursor->iprec->lock);
8833f43fb33SMatthew Dillon 	return(ncursor);
8843f43fb33SMatthew Dillon }
8853f43fb33SMatthew Dillon 
8863f43fb33SMatthew Dillon /*
8873f43fb33SMatthew Dillon  * Destroy ncursor and restore ocursor
8883f43fb33SMatthew Dillon  *
8893f43fb33SMatthew Dillon  * This is a temporary hack for the release.  We can't afford to lose
8903f43fb33SMatthew Dillon  * the IP lock until the IP object scan code is able to deal with it,
8913f43fb33SMatthew Dillon  * so have ocursor inherit it back.
8923f43fb33SMatthew Dillon  */
8933f43fb33SMatthew Dillon void
hammer_pop_cursor(hammer_cursor_t ocursor,hammer_cursor_t ncursor)8943f43fb33SMatthew Dillon hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor)
8953f43fb33SMatthew Dillon {
896bac808feSMatthew Dillon 	hammer_mount_t hmp;
8973f43fb33SMatthew Dillon 	hammer_inode_t ip;
8983f43fb33SMatthew Dillon 
899bac808feSMatthew Dillon 	hmp = ncursor->trans->hmp;
9003f43fb33SMatthew Dillon 	ip = ncursor->ip;
9013f43fb33SMatthew Dillon 	ncursor->ip = NULL;
9023f43fb33SMatthew Dillon 	if (ip)
9033f43fb33SMatthew Dillon                 --ip->cursor_ip_refs;
9043f43fb33SMatthew Dillon 	hammer_done_cursor(ncursor);
905bac808feSMatthew Dillon 	kfree(ncursor, hmp->m_misc);
9063f43fb33SMatthew Dillon 	KKASSERT(ocursor->ip == ip);
907982be4bfSMatthew Dillon 	hammer_lock_cursor(ocursor);
908adf01747SMatthew Dillon }
909adf01747SMatthew Dillon 
910adf01747SMatthew Dillon /*
911b3bad96fSMatthew Dillon  * onode is being replaced by nnode by the reblocking code.
912b3bad96fSMatthew Dillon  */
913b3bad96fSMatthew Dillon void
hammer_cursor_replaced_node(hammer_node_t onode,hammer_node_t nnode)914b3bad96fSMatthew Dillon hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode)
915b3bad96fSMatthew Dillon {
916b3bad96fSMatthew Dillon 	hammer_cursor_t cursor;
917c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
918c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t nndisk;
919c9ce54d6SMatthew Dillon 
920c9ce54d6SMatthew Dillon 	ondisk = onode->ondisk;
921c9ce54d6SMatthew Dillon 	nndisk = nnode->ondisk;
922b3bad96fSMatthew Dillon 
923b3bad96fSMatthew Dillon 	while ((cursor = TAILQ_FIRST(&onode->cursor_list)) != NULL) {
924b3bad96fSMatthew Dillon 		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
925b3bad96fSMatthew Dillon 		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
926b3bad96fSMatthew Dillon 		KKASSERT(cursor->node == onode);
927c9ce54d6SMatthew Dillon 		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
928c9ce54d6SMatthew Dillon 			cursor->leaf = &nndisk->elms[cursor->index].leaf;
929b3bad96fSMatthew Dillon 		cursor->node = nnode;
930b3bad96fSMatthew Dillon 		hammer_ref_node(nnode);
931b3bad96fSMatthew Dillon 		hammer_rel_node(onode);
932b3bad96fSMatthew Dillon 	}
933b3bad96fSMatthew Dillon }
934b3bad96fSMatthew Dillon 
935b3bad96fSMatthew Dillon /*
936f3a4893bSMatthew Dillon  * We have removed <node> from the parent and collapsed the parent.
937f3a4893bSMatthew Dillon  *
938f3a4893bSMatthew Dillon  * Cursors in deadlock recovery are seeked upward to the parent so the
9396dc17446SMatthew Dillon  * btree_remove() recursion works properly even though we have marked
9406dc17446SMatthew Dillon  * the cursor as requiring a reseek.
9416dc17446SMatthew Dillon  *
9426dc17446SMatthew Dillon  * This is the only cursor function which sets HAMMER_CURSOR_ITERATE_CHECK,
9436dc17446SMatthew Dillon  * meaning the cursor is no longer definitively pointing at an element
9446dc17446SMatthew Dillon  * within its iteration (if the cursor is being used to iterate).  The
9456dc17446SMatthew Dillon  * iteration code will take this into account instead of asserting if the
9466dc17446SMatthew Dillon  * cursor is outside the iteration range.
947b3bad96fSMatthew Dillon  */
948b3bad96fSMatthew Dillon void
hammer_cursor_removed_node(hammer_node_t node,hammer_node_t parent,int index)949b3bad96fSMatthew Dillon hammer_cursor_removed_node(hammer_node_t node, hammer_node_t parent, int index)
950b3bad96fSMatthew Dillon {
951b3bad96fSMatthew Dillon 	hammer_cursor_t cursor;
952c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
953b3bad96fSMatthew Dillon 
954b3bad96fSMatthew Dillon 	KKASSERT(parent != NULL);
955c9ce54d6SMatthew Dillon 	ondisk = node->ondisk;
956c9ce54d6SMatthew Dillon 
957b3bad96fSMatthew Dillon 	while ((cursor = TAILQ_FIRST(&node->cursor_list)) != NULL) {
958b3bad96fSMatthew Dillon 		KKASSERT(cursor->node == node);
959b3bad96fSMatthew Dillon 		KKASSERT(cursor->index == 0);
960b3bad96fSMatthew Dillon 		TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
961b3bad96fSMatthew Dillon 		TAILQ_INSERT_TAIL(&parent->cursor_list, cursor, deadlk_entry);
962c9ce54d6SMatthew Dillon 		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
963c9ce54d6SMatthew Dillon 			cursor->leaf = NULL;
964adf01747SMatthew Dillon 		cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
9656dc17446SMatthew Dillon 		cursor->flags |= HAMMER_CURSOR_ITERATE_CHECK;
966b3bad96fSMatthew Dillon 		cursor->node = parent;
967b3bad96fSMatthew Dillon 		cursor->index = index;
968b3bad96fSMatthew Dillon 		hammer_ref_node(parent);
969b3bad96fSMatthew Dillon 		hammer_rel_node(node);
970b3bad96fSMatthew Dillon 	}
971b3bad96fSMatthew Dillon }
972b3bad96fSMatthew Dillon 
973b3bad96fSMatthew Dillon /*
974b3bad96fSMatthew Dillon  * node was split at (onode, index) with elements >= index moved to nnode.
975b3bad96fSMatthew Dillon  */
976b3bad96fSMatthew Dillon void
hammer_cursor_split_node(hammer_node_t onode,hammer_node_t nnode,int index)977b3bad96fSMatthew Dillon hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode, int index)
978b3bad96fSMatthew Dillon {
979b3bad96fSMatthew Dillon 	hammer_cursor_t cursor;
980c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
981c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t nndisk;
982c9ce54d6SMatthew Dillon 
983c9ce54d6SMatthew Dillon 	ondisk = onode->ondisk;
984c9ce54d6SMatthew Dillon 	nndisk = nnode->ondisk;
985b3bad96fSMatthew Dillon 
986b3bad96fSMatthew Dillon again:
987b3bad96fSMatthew Dillon 	TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
988b3bad96fSMatthew Dillon 		KKASSERT(cursor->node == onode);
989b3bad96fSMatthew Dillon 		if (cursor->index < index)
990b3bad96fSMatthew Dillon 			continue;
991b3bad96fSMatthew Dillon 		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
992b3bad96fSMatthew Dillon 		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
993c9ce54d6SMatthew Dillon 		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
994c9ce54d6SMatthew Dillon 			cursor->leaf = &nndisk->elms[cursor->index - index].leaf;
995b3bad96fSMatthew Dillon 		cursor->node = nnode;
996b3bad96fSMatthew Dillon 		cursor->index -= index;
997b3bad96fSMatthew Dillon 		hammer_ref_node(nnode);
998b3bad96fSMatthew Dillon 		hammer_rel_node(onode);
999b3bad96fSMatthew Dillon 		goto again;
1000b3bad96fSMatthew Dillon 	}
1001b3bad96fSMatthew Dillon }
1002b3bad96fSMatthew Dillon 
1003b3bad96fSMatthew Dillon /*
10041775b6a0SMatthew Dillon  * An element was moved from one node to another or within a node.  The
10051775b6a0SMatthew Dillon  * index may also represent the end of the node (index == numelements).
10061775b6a0SMatthew Dillon  *
1007bbb01e14SMatthew Dillon  * {oparent,pindex} is the parent node's pointer to onode/oindex.
1008bbb01e14SMatthew Dillon  *
10091775b6a0SMatthew Dillon  * This is used by the rebalancing code.  This is not an insertion or
10101775b6a0SMatthew Dillon  * deletion and any additional elements, including the degenerate case at
10111775b6a0SMatthew Dillon  * the end of the node, will be dealt with by additional distinct calls.
10121775b6a0SMatthew Dillon  */
10131775b6a0SMatthew Dillon void
hammer_cursor_moved_element(hammer_node_t oparent,int pindex,hammer_node_t onode,int oindex,hammer_node_t nnode,int nindex)1014bbb01e14SMatthew Dillon hammer_cursor_moved_element(hammer_node_t oparent, int pindex,
1015bbb01e14SMatthew Dillon 			    hammer_node_t onode, int oindex,
1016bbb01e14SMatthew Dillon 			    hammer_node_t nnode, int nindex)
10171775b6a0SMatthew Dillon {
10181775b6a0SMatthew Dillon 	hammer_cursor_t cursor;
1019c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
1020c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t nndisk;
1021c9ce54d6SMatthew Dillon 
1022bbb01e14SMatthew Dillon 	/*
1023bbb01e14SMatthew Dillon 	 * Adjust any cursors pointing at the element
1024bbb01e14SMatthew Dillon 	 */
1025c9ce54d6SMatthew Dillon 	ondisk = onode->ondisk;
1026c9ce54d6SMatthew Dillon 	nndisk = nnode->ondisk;
1027bbb01e14SMatthew Dillon again1:
10281775b6a0SMatthew Dillon 	TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
10291775b6a0SMatthew Dillon 		KKASSERT(cursor->node == onode);
10301775b6a0SMatthew Dillon 		if (cursor->index != oindex)
10311775b6a0SMatthew Dillon 			continue;
10321775b6a0SMatthew Dillon 		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
10331775b6a0SMatthew Dillon 		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
1034c9ce54d6SMatthew Dillon 		if (cursor->leaf == &ondisk->elms[oindex].leaf)
1035c9ce54d6SMatthew Dillon 			cursor->leaf = &nndisk->elms[nindex].leaf;
10361775b6a0SMatthew Dillon 		cursor->node = nnode;
10371775b6a0SMatthew Dillon 		cursor->index = nindex;
10381775b6a0SMatthew Dillon 		hammer_ref_node(nnode);
10391775b6a0SMatthew Dillon 		hammer_rel_node(onode);
1040bbb01e14SMatthew Dillon 		goto again1;
1041bbb01e14SMatthew Dillon 	}
1042bbb01e14SMatthew Dillon 
1043bbb01e14SMatthew Dillon 	/*
1044bbb01e14SMatthew Dillon 	 * When moving the first element of onode to a different node any
1045bbb01e14SMatthew Dillon 	 * cursor which is pointing at (oparent,pindex) must be repointed
1046bbb01e14SMatthew Dillon 	 * to nnode and ATEDISK must be cleared.
1047bbb01e14SMatthew Dillon 	 *
1048bbb01e14SMatthew Dillon 	 * This prevents cursors from losing track due to insertions.
1049bbb01e14SMatthew Dillon 	 * Insertions temporarily release the cursor in order to update
1050bbb01e14SMatthew Dillon 	 * the mirror_tids.  It primarily effects the mirror_write code.
1051bbb01e14SMatthew Dillon 	 * The other code paths generally only do a single insertion and
1052bbb01e14SMatthew Dillon 	 * then relookup or drop the cursor.
1053bbb01e14SMatthew Dillon 	 */
1054bbb01e14SMatthew Dillon 	if (onode == nnode || oindex)
1055bbb01e14SMatthew Dillon 		return;
1056bbb01e14SMatthew Dillon 	ondisk = oparent->ondisk;
1057bbb01e14SMatthew Dillon again2:
1058bbb01e14SMatthew Dillon 	TAILQ_FOREACH(cursor, &oparent->cursor_list, deadlk_entry) {
1059bbb01e14SMatthew Dillon 		KKASSERT(cursor->node == oparent);
1060bbb01e14SMatthew Dillon 		if (cursor->index != pindex)
1061bbb01e14SMatthew Dillon 			continue;
1062d053aa8aSTomohiro Kusumi 		hkprintf("debug: shifted cursor pointing at parent\n"
1063bbb01e14SMatthew Dillon 			"parent %016jx:%d onode %016jx:%d nnode %016jx:%d\n",
1064bbb01e14SMatthew Dillon 			(intmax_t)oparent->node_offset, pindex,
1065bbb01e14SMatthew Dillon 			(intmax_t)onode->node_offset, oindex,
1066bbb01e14SMatthew Dillon 			(intmax_t)nnode->node_offset, nindex);
1067bbb01e14SMatthew Dillon 		TAILQ_REMOVE(&oparent->cursor_list, cursor, deadlk_entry);
1068bbb01e14SMatthew Dillon 		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
1069bbb01e14SMatthew Dillon 		if (cursor->leaf == &ondisk->elms[oindex].leaf)
1070bbb01e14SMatthew Dillon 			cursor->leaf = &nndisk->elms[nindex].leaf;
1071bbb01e14SMatthew Dillon 		cursor->node = nnode;
1072bbb01e14SMatthew Dillon 		cursor->index = nindex;
1073bbb01e14SMatthew Dillon 		cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
1074bbb01e14SMatthew Dillon 		hammer_ref_node(nnode);
1075bbb01e14SMatthew Dillon 		hammer_rel_node(oparent);
1076bbb01e14SMatthew Dillon 		goto again2;
10771775b6a0SMatthew Dillon 	}
10781775b6a0SMatthew Dillon }
10791775b6a0SMatthew Dillon 
10801775b6a0SMatthew Dillon /*
10811775b6a0SMatthew Dillon  * The B-Tree element pointing to the specified node was moved from (oparent)
10821775b6a0SMatthew Dillon  * to (nparent, nindex).  We must locate any tracked cursors pointing at
10831775b6a0SMatthew Dillon  * node and adjust their parent accordingly.
10841775b6a0SMatthew Dillon  *
10851775b6a0SMatthew Dillon  * This is used by the rebalancing code when packing elements causes an
10861775b6a0SMatthew Dillon  * element to shift from one node to another.
10871775b6a0SMatthew Dillon  */
10881775b6a0SMatthew Dillon void
hammer_cursor_parent_changed(hammer_node_t node,hammer_node_t oparent,hammer_node_t nparent,int nindex)10891775b6a0SMatthew Dillon hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent,
10901775b6a0SMatthew Dillon 			     hammer_node_t nparent, int nindex)
10911775b6a0SMatthew Dillon {
10921775b6a0SMatthew Dillon 	hammer_cursor_t cursor;
10931775b6a0SMatthew Dillon 
10941775b6a0SMatthew Dillon again:
10951775b6a0SMatthew Dillon 	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
10961775b6a0SMatthew Dillon 		KKASSERT(cursor->node == node);
10971775b6a0SMatthew Dillon 		if (cursor->parent == oparent) {
10981775b6a0SMatthew Dillon 			cursor->parent = nparent;
10991775b6a0SMatthew Dillon 			cursor->parent_index = nindex;
11001775b6a0SMatthew Dillon 			hammer_ref_node(nparent);
11011775b6a0SMatthew Dillon 			hammer_rel_node(oparent);
11021775b6a0SMatthew Dillon 			goto again;
11031775b6a0SMatthew Dillon 		}
11041775b6a0SMatthew Dillon 	}
11051775b6a0SMatthew Dillon }
11061775b6a0SMatthew Dillon 
11071775b6a0SMatthew Dillon /*
1108e4a5ff06SMatthew Dillon  * Deleted element at (node, index)
1109b3bad96fSMatthew Dillon  *
1110b3bad96fSMatthew Dillon  * Shift indexes >= index
1111b3bad96fSMatthew Dillon  */
1112b3bad96fSMatthew Dillon void
hammer_cursor_deleted_element(hammer_node_t node,int index)1113b3bad96fSMatthew Dillon hammer_cursor_deleted_element(hammer_node_t node, int index)
1114b3bad96fSMatthew Dillon {
1115b3bad96fSMatthew Dillon 	hammer_cursor_t cursor;
1116c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
1117c9ce54d6SMatthew Dillon 
1118c9ce54d6SMatthew Dillon 	ondisk = node->ondisk;
1119b3bad96fSMatthew Dillon 
1120b3bad96fSMatthew Dillon 	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
1121b3bad96fSMatthew Dillon 		KKASSERT(cursor->node == node);
1122b3bad96fSMatthew Dillon 		if (cursor->index == index) {
1123adf01747SMatthew Dillon 			cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
1124c9ce54d6SMatthew Dillon 			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1125c9ce54d6SMatthew Dillon 				cursor->leaf = NULL;
1126b3bad96fSMatthew Dillon 		} else if (cursor->index > index) {
1127c9ce54d6SMatthew Dillon 			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1128c9ce54d6SMatthew Dillon 				cursor->leaf = &ondisk->elms[cursor->index - 1].leaf;
1129b3bad96fSMatthew Dillon 			--cursor->index;
1130b3bad96fSMatthew Dillon 		}
1131b3bad96fSMatthew Dillon 	}
1132b3bad96fSMatthew Dillon }
1133b3bad96fSMatthew Dillon 
1134b3bad96fSMatthew Dillon /*
1135e4a5ff06SMatthew Dillon  * Inserted element at (node, index)
1136b3bad96fSMatthew Dillon  *
1137b3bad96fSMatthew Dillon  * Shift indexes >= index
1138b3bad96fSMatthew Dillon  */
1139b3bad96fSMatthew Dillon void
hammer_cursor_inserted_element(hammer_node_t node,int index)1140b3bad96fSMatthew Dillon hammer_cursor_inserted_element(hammer_node_t node, int index)
1141b3bad96fSMatthew Dillon {
1142b3bad96fSMatthew Dillon 	hammer_cursor_t cursor;
1143c9ce54d6SMatthew Dillon 	hammer_node_ondisk_t ondisk;
1144c9ce54d6SMatthew Dillon 
1145c9ce54d6SMatthew Dillon 	ondisk = node->ondisk;
1146b3bad96fSMatthew Dillon 
1147b3bad96fSMatthew Dillon 	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
1148b3bad96fSMatthew Dillon 		KKASSERT(cursor->node == node);
1149c9ce54d6SMatthew Dillon 		if (cursor->index >= index) {
1150c9ce54d6SMatthew Dillon 			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
1151c9ce54d6SMatthew Dillon 				cursor->leaf = &ondisk->elms[cursor->index + 1].leaf;
1152b3bad96fSMatthew Dillon 			++cursor->index;
1153b3bad96fSMatthew Dillon 		}
1154b3bad96fSMatthew Dillon 	}
1155c9ce54d6SMatthew Dillon }
1156b3bad96fSMatthew Dillon 
1157b9107f58SMatthew Dillon /*
1158b9107f58SMatthew Dillon  * Invalidate the cached data buffer associated with a cursor.
1159b9107f58SMatthew Dillon  *
1160b9107f58SMatthew Dillon  * This needs to be done when the underlying block is being freed or
1161b9107f58SMatthew Dillon  * the referenced buffer can prevent the related buffer cache buffer
1162b9107f58SMatthew Dillon  * from being properly invalidated.
1163b9107f58SMatthew Dillon  */
1164b9107f58SMatthew Dillon void
hammer_cursor_invalidate_cache(hammer_cursor_t cursor)1165b9107f58SMatthew Dillon hammer_cursor_invalidate_cache(hammer_cursor_t cursor)
1166b9107f58SMatthew Dillon {
1167b9107f58SMatthew Dillon         if (cursor->data_buffer) {
1168b9107f58SMatthew Dillon                 hammer_rel_buffer(cursor->data_buffer, 0);
1169b9107f58SMatthew Dillon                 cursor->data_buffer = NULL;
1170b9107f58SMatthew Dillon 		cursor->data = NULL;
1171b9107f58SMatthew Dillon         }
1172b9107f58SMatthew Dillon }
1173b9107f58SMatthew Dillon 
1174