2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_cursor.c,v 1.24 2008/05/05 20:34:47 dillon Exp $
38 * HAMMER B-Tree index - cursor support routines
42 static int hammer_load_cursor_parent(hammer_cursor_t cursor);
45 * Initialize a fresh cursor using the B-Tree node cache. If the cache
46 * is not available initialize a fresh cursor at the root of the filesystem.
49 hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
50 struct hammer_node **cache, hammer_inode_t ip)
52 hammer_volume_t volume;
56 bzero(cursor, sizeof(*cursor));
58 cursor->trans = trans;
61 * If the cursor operation is on behalf of an inode, lock
64 if ((cursor->ip = ip) != NULL) {
66 if (trans->type == HAMMER_TRANS_FLS)
67 hammer_lock_ex(&ip->lock);
69 hammer_lock_sh(&ip->lock);
73 * Step 1 - acquire a locked node from the cache if possible
75 if (cache && *cache) {
76 node = hammer_ref_node_safe(trans->hmp, cache, &error);
78 hammer_lock_sh(&node->lock);
79 if (node->flags & HAMMER_NODE_DELETED) {
80 hammer_unlock(&node->lock);
81 hammer_rel_node(node);
90 * Step 2 - If we couldn't get a node from the cache, get
91 * the one from the root of the filesystem.
93 while (node == NULL) {
94 volume = hammer_get_root_volume(trans->hmp, &error);
97 node = hammer_get_node(trans->hmp,
98 volume->ondisk->vol0_btree_root,
100 hammer_rel_volume(volume, 0);
103 hammer_lock_sh(&node->lock);
104 if (node->flags & HAMMER_NODE_DELETED) {
105 hammer_unlock(&node->lock);
106 hammer_rel_node(node);
112 * Step 3 - finish initializing the cursor by acquiring the parent
116 error = hammer_load_cursor_parent(cursor);
117 KKASSERT(error == 0);
118 /* if (error) hammer_done_cursor(cursor); */
124 hammer_reinit_cursor(hammer_cursor_t cursor)
126 hammer_transaction_t trans;
128 struct hammer_node **cache;
130 trans = cursor->trans;
132 hammer_done_cursor(cursor);
133 cache = ip ? &ip->cache[0] : NULL;
134 error = hammer_init_cursor(trans, cursor, cache, ip);
141 * Normalize a cursor. Sometimes cursors can be left in a state
142 * where node is NULL. If the cursor is in this state, cursor up.
145 hammer_normalize_cursor(hammer_cursor_t cursor)
147 if (cursor->node == NULL) {
148 KKASSERT(cursor->parent != NULL);
149 hammer_cursor_up(cursor);
155 * We are finished with a cursor. We NULL out various fields as sanity
156 * check, in case the structure is inappropriately used afterwords.
159 hammer_done_cursor(hammer_cursor_t cursor)
163 if (cursor->parent) {
164 hammer_unlock(&cursor->parent->lock);
165 hammer_rel_node(cursor->parent);
166 cursor->parent = NULL;
169 hammer_unlock(&cursor->node->lock);
170 hammer_rel_node(cursor->node);
173 if (cursor->data_buffer) {
174 hammer_rel_buffer(cursor->data_buffer, 0);
175 cursor->data_buffer = NULL;
177 if (cursor->record_buffer) {
178 hammer_rel_buffer(cursor->record_buffer, 0);
179 cursor->record_buffer = NULL;
181 if ((ip = cursor->ip) != NULL) {
182 hammer_mem_done(cursor);
183 KKASSERT(ip->cursor_ip_refs > 0);
184 --ip->cursor_ip_refs;
185 hammer_unlock(&ip->lock);
191 * If we deadlocked this node will be referenced. Do a quick
192 * lock/unlock to wait for the deadlock condition to clear.
194 if (cursor->deadlk_node) {
195 hammer_lock_ex(&cursor->deadlk_node->lock);
196 hammer_unlock(&cursor->deadlk_node->lock);
197 hammer_rel_node(cursor->deadlk_node);
198 cursor->deadlk_node = NULL;
200 if (cursor->deadlk_rec) {
201 hammer_wait_mem_record(cursor->deadlk_rec);
202 hammer_rel_mem_record(cursor->deadlk_rec);
203 cursor->deadlk_rec = NULL;
207 cursor->record = NULL;
208 cursor->left_bound = NULL;
209 cursor->right_bound = NULL;
210 cursor->trans = NULL;
214 * Upgrade cursor->node and cursor->parent to exclusive locks. This
215 * function can return EDEADLK.
217 * The lock must already be either held shared or already held exclusively
220 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
221 * we add another reference to the node that failed and set
222 * cursor->deadlk_node so hammer_done_cursor() can block on it.
225 hammer_cursor_upgrade(hammer_cursor_t cursor)
229 error = hammer_lock_upgrade(&cursor->node->lock);
230 if (error && cursor->deadlk_node == NULL) {
231 cursor->deadlk_node = cursor->node;
232 hammer_ref_node(cursor->deadlk_node);
233 } else if (error == 0 && cursor->parent) {
234 error = hammer_lock_upgrade(&cursor->parent->lock);
235 if (error && cursor->deadlk_node == NULL) {
236 cursor->deadlk_node = cursor->parent;
237 hammer_ref_node(cursor->deadlk_node);
244 * Downgrade cursor->node and cursor->parent to shared locks. This
245 * function can return EDEADLK.
248 hammer_cursor_downgrade(hammer_cursor_t cursor)
250 if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
251 hammer_lock_downgrade(&cursor->node->lock);
252 if (cursor->parent &&
253 hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
254 hammer_lock_downgrade(&cursor->parent->lock);
259 * Seek the cursor to the specified node and index.
262 hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
266 hammer_cursor_downgrade(cursor);
269 if (cursor->node != node) {
270 hammer_unlock(&cursor->node->lock);
271 hammer_rel_node(cursor->node);
273 hammer_ref_node(node);
274 hammer_lock_sh(&node->lock);
276 if (cursor->parent) {
277 hammer_unlock(&cursor->parent->lock);
278 hammer_rel_node(cursor->parent);
279 cursor->parent = NULL;
280 cursor->parent_index = 0;
282 error = hammer_load_cursor_parent(cursor);
284 cursor->index = index;
289 * Load the parent of cursor->node into cursor->parent.
293 hammer_load_cursor_parent(hammer_cursor_t cursor)
296 hammer_node_t parent;
298 hammer_btree_elm_t elm;
302 hmp = cursor->trans->hmp;
304 if (cursor->node->ondisk->parent) {
306 parent = hammer_get_node(hmp, node->ondisk->parent, 0, &error);
309 hammer_lock_sh(&parent->lock);
311 for (i = 0; i < parent->ondisk->count; ++i) {
312 elm = &parent->ondisk->elms[i];
313 if (parent->ondisk->elms[i].internal.subtree_offset ==
318 if (i == parent->ondisk->count) {
319 hammer_unlock(&parent->lock);
320 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
322 KKASSERT(i != parent->ondisk->count);
323 cursor->parent = parent;
324 cursor->parent_index = i;
325 cursor->left_bound = &elm[0].internal.base;
326 cursor->right_bound = &elm[1].internal.base;
329 cursor->parent = NULL;
330 cursor->parent_index = 0;
331 cursor->left_bound = &hmp->root_btree_beg;
332 cursor->right_bound = &hmp->root_btree_end;
339 * Cursor up to our parent node. Return ENOENT if we are at the root of
342 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
343 * the cursor remains unchanged.
346 hammer_cursor_up(hammer_cursor_t cursor)
350 hammer_cursor_downgrade(cursor);
353 * If the parent is NULL we are at the root of the B-Tree and
356 if (cursor->parent == NULL)
360 * Set the node to its parent.
362 hammer_unlock(&cursor->node->lock);
363 hammer_rel_node(cursor->node);
364 cursor->node = cursor->parent;
365 cursor->index = cursor->parent_index;
366 cursor->parent = NULL;
367 cursor->parent_index = 0;
369 error = hammer_load_cursor_parent(cursor);
374 * Cursor down through the current node, which must be an internal node.
376 * This routine adjusts the cursor and sets index to 0.
379 hammer_cursor_down(hammer_cursor_t cursor)
382 hammer_btree_elm_t elm;
386 * The current node becomes the current parent
388 hammer_cursor_downgrade(cursor);
390 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
391 if (cursor->parent) {
392 hammer_unlock(&cursor->parent->lock);
393 hammer_rel_node(cursor->parent);
395 cursor->parent = node;
396 cursor->parent_index = cursor->index;
401 * Extract element to push into at (node,index), set bounds.
403 elm = &node->ondisk->elms[cursor->parent_index];
406 * Ok, push down into elm. If elm specifies an internal or leaf
407 * node the current node must be an internal node. If elm specifies
408 * a spike then the current node must be a leaf node.
410 switch(elm->base.btype) {
411 case HAMMER_BTREE_TYPE_INTERNAL:
412 case HAMMER_BTREE_TYPE_LEAF:
413 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
414 KKASSERT(elm->internal.subtree_offset != 0);
415 cursor->left_bound = &elm[0].internal.base;
416 cursor->right_bound = &elm[1].internal.base;
417 node = hammer_get_node(cursor->trans->hmp,
418 elm->internal.subtree_offset, 0, &error);
420 KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p\n", elm->base.btype, node->ondisk->type, node));
421 if (node->ondisk->parent != cursor->parent->node_offset)
422 panic("node %p %016llx vs %016llx\n", node, node->ondisk->parent, cursor->parent->node_offset);
423 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
427 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
429 (elm->base.btype ? elm->base.btype : '?'));
433 hammer_lock_sh(&node->lock);