2 * Copyright (c) 2007 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.15 2008/01/24 02:14:45 dillon Exp $
38 * HAMMER B-Tree index - cursor support routines
42 static int hammer_load_cursor_parent(hammer_cursor_t cursor);
43 static int hammer_load_cursor_parent_local(hammer_cursor_t cursor);
44 static int hammer_load_cursor_parent_cluster(hammer_cursor_t cursor);
47 * Initialize a fresh cursor using the B-Tree node cache. If the cache
48 * is not available initialize a fresh cursor at the root of the root
52 hammer_init_cursor_hmp(hammer_cursor_t cursor, struct hammer_node **cache,
55 hammer_cluster_t cluster;
59 bzero(cursor, sizeof(*cursor));
62 * Step 1 - acquire a locked node from the cache if possible
64 if (cache && *cache) {
65 node = hammer_ref_node_safe(hmp, cache, &error);
67 hammer_lock_sh(&node->lock);
68 if (node->flags & HAMMER_NODE_DELETED) {
69 hammer_unlock(&node->lock);
70 hammer_rel_node(node);
79 * Step 2 - If we couldn't get a node from the cache, get
80 * the one from the root of the root cluster.
82 while (node == NULL) {
83 cluster = hammer_get_root_cluster(hmp, &error);
86 node = hammer_get_node(cluster,
87 cluster->ondisk->clu_btree_root,
89 hammer_rel_cluster(cluster, 0);
92 hammer_lock_sh(&node->lock);
93 if (node->flags & HAMMER_NODE_DELETED) {
94 hammer_unlock(&node->lock);
95 hammer_rel_node(node);
101 * Step 3 - finish initializing the cursor by acquiring the parent
105 error = hammer_load_cursor_parent(cursor);
106 KKASSERT(error == 0);
111 * Initialize a fresh cursor at the root of the specified cluster and
112 * limit operations to within the cluster.
114 * NOTE: cursor->parent will be set to NULL to avoid referencing B-Tree
115 * nodes in other clusters.
118 hammer_init_cursor_cluster(hammer_cursor_t cursor, hammer_cluster_t cluster)
122 bzero(cursor, sizeof(*cursor));
123 cursor->flags |= HAMMER_CURSOR_INCLUSTER;
124 cursor->node = hammer_get_node(cluster,
125 cluster->ondisk->clu_btree_root,
128 hammer_lock_sh(&cursor->node->lock);
129 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
130 cursor->right_bound = &cluster->ondisk->clu_btree_end;
131 KKASSERT(error == 0);
136 * We are finished with a cursor. We NULL out various fields as sanity
137 * check, in case the structure is inappropriately used afterwords.
140 hammer_done_cursor(hammer_cursor_t cursor)
142 if (cursor->parent) {
143 hammer_unlock(&cursor->parent->lock);
144 hammer_rel_node(cursor->parent);
145 cursor->parent = NULL;
148 hammer_unlock(&cursor->node->lock);
149 hammer_rel_node(cursor->node);
152 if (cursor->data_buffer) {
153 hammer_rel_buffer(cursor->data_buffer, 0);
154 cursor->data_buffer = NULL;
156 if (cursor->record_buffer) {
157 hammer_rel_buffer(cursor->record_buffer, 0);
158 cursor->record_buffer = NULL;
161 hammer_mem_done(cursor);
164 * If we deadlocked this node will be referenced. Do a quick
165 * lock/unlock to wait for the deadlock condition to clear.
167 if (cursor->deadlk_node) {
168 hammer_lock_ex(&cursor->deadlk_node->lock);
169 hammer_unlock(&cursor->deadlk_node->lock);
170 hammer_rel_node(cursor->deadlk_node);
171 cursor->deadlk_node = NULL;
175 cursor->record = NULL;
176 cursor->left_bound = NULL;
177 cursor->right_bound = NULL;
181 * Upgrade cursor->node and cursor->parent to exclusive locks. This
182 * function can return EDEADLK.
184 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
185 * we add another reference to the node that failed and set
186 * cursor->deadlk_node so hammer_done_cursor() can block on it.
189 hammer_cursor_upgrade(hammer_cursor_t cursor)
193 if (hammer_lock_held(&cursor->node->lock) < 0) {
194 error = hammer_lock_upgrade(&cursor->node->lock);
195 if (error && cursor->deadlk_node == NULL) {
196 cursor->deadlk_node = cursor->node;
197 hammer_ref_node(cursor->deadlk_node);
202 if (cursor->parent && hammer_lock_held(&cursor->parent->lock) < 0) {
203 error = hammer_lock_upgrade(&cursor->parent->lock);
204 if (error && cursor->deadlk_node == NULL) {
205 cursor->deadlk_node = cursor->parent;
206 hammer_ref_node(cursor->deadlk_node);
215 * Downgrade cursor->node and cursor->parent to shared locks. This
216 * function can return EDEADLK.
219 hammer_cursor_downgrade(hammer_cursor_t cursor)
221 if (hammer_lock_held(&cursor->node->lock) > 0)
222 hammer_lock_downgrade(&cursor->node->lock);
223 if (cursor->parent && hammer_lock_held(&cursor->parent->lock) > 0)
224 hammer_lock_downgrade(&cursor->parent->lock);
231 * Acquire the parent B-Tree node of the specified node, returning a
232 * referenced but unlocked node. NULL can be returned with *errorp == 0
233 * if node is the root node of the root cluster.
237 hammer_get_parent_node(hammer_node_t node, int *errorp)
239 hammer_cluster_t cluster;
240 hammer_node_t parent;
242 cluster = node->cluster;
243 if (node->ondisk->parent) {
247 parent = hammer_get_node(cluster, node->ondisk->parent, errorp);
248 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0) {
250 * At cluster root, locate node in parent cluster
252 hammer_cluster_ondisk_t ondisk;
253 hammer_cluster_t pcluster;
254 hammer_volume_t pvolume;
258 ondisk = cluster->ondisk;
259 vol_no = ondisk->clu_btree_parent_vol_no;
260 clu_no = ondisk->clu_btree_parent_clu_no;
263 * Acquire the node from (volume, cluster, offset)
265 pvolume = hammer_get_volume(cluster->volume->hmp, vol_no,
269 pcluster = hammer_get_cluster(pvolume, clu_no, errorp, 0);
270 hammer_rel_volume(pvolume, 0);
273 parent = hammer_get_node(pcluster,
274 ondisk->clu_btree_parent_offset,
276 hammer_rel_cluster(pcluster, 0);
279 * At root of root cluster, there is no parent.
281 KKASSERT(cluster->ondisk->clu_btree_parent_vol_no == -1);
291 * Load the parent of cursor->node into cursor->parent. There are several
292 * cases. (1) The parent is in the current cluster. (2) We are at the
293 * root of the cluster and the parent is in another cluster. (3) We are at
294 * the root of the root cluster.
296 * If HAMMER_CURSOR_INCLUSTER is set and we are at the root of the cluster,
297 * we do not access the parent cluster at all and make the cursor look like
302 hammer_load_cursor_parent(hammer_cursor_t cursor)
304 hammer_cluster_t cluster;
307 cluster = cursor->node->cluster;
309 if (cursor->node->ondisk->parent) {
310 error = hammer_load_cursor_parent_local(cursor);
311 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0 &&
312 ((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0)
314 error = hammer_load_cursor_parent_cluster(cursor);
316 cursor->parent = NULL;
317 cursor->parent_index = 0;
318 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
319 cursor->right_bound = &cluster->ondisk->clu_btree_end;
327 hammer_load_cursor_parent_local(hammer_cursor_t cursor)
330 hammer_node_t parent;
331 hammer_btree_elm_t elm;
336 parent = hammer_get_node(node->cluster, node->ondisk->parent, &error);
340 for (i = 0; i < parent->ondisk->count; ++i) {
341 elm = &parent->ondisk->elms[i];
342 if (parent->ondisk->elms[i].internal.subtree_offset ==
347 if (i == parent->ondisk->count)
348 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
349 KKASSERT(i != parent->ondisk->count);
350 cursor->parent = parent;
351 cursor->parent_index = i;
352 cursor->left_bound = &elm[0].internal.base;
353 cursor->right_bound = &elm[1].internal.base;
355 hammer_lock_sh(&parent->lock);
361 hammer_load_cursor_parent_cluster(hammer_cursor_t cursor)
363 hammer_cluster_ondisk_t ondisk;
364 hammer_cluster_t pcluster;
365 hammer_cluster_t ccluster;
366 hammer_volume_t volume;
368 hammer_node_t parent;
369 hammer_btree_elm_t elm;
376 ccluster = node->cluster;
377 ondisk = ccluster->ondisk;
378 vol_no = ondisk->clu_btree_parent_vol_no;
379 clu_no = ondisk->clu_btree_parent_clu_no;
382 * Acquire the node from (volume, cluster, offset). This should
383 * be a leaf node containing the HAMMER_BTREE_TYPE_SPIKE_END element.
385 volume = hammer_get_volume(ccluster->volume->hmp, vol_no, &error);
388 pcluster = hammer_get_cluster(volume, clu_no, &error, 0);
389 hammer_rel_volume(volume, 0);
392 parent = hammer_get_node(pcluster, ondisk->clu_btree_parent_offset,
394 hammer_rel_cluster(pcluster, 0);
397 KKASSERT(parent->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
400 * Ok, we have the node. Locate the inter-cluster element
403 for (i = 0; i < parent->ondisk->count; ++i) {
404 elm = &parent->ondisk->elms[i];
406 if (elm->leaf.base.btype == HAMMER_BTREE_TYPE_SPIKE_END &&
407 elm->leaf.spike_clu_no == cursor->node->cluster->clu_no) {
411 KKASSERT(i != parent->ondisk->count);
412 KKASSERT(i && elm[-1].leaf.base.btype == HAMMER_BTREE_TYPE_SPIKE_BEG);
413 cursor->parent = parent;
414 cursor->parent_index = i;
415 cursor->left_bound = &ccluster->ondisk->clu_btree_beg;
416 cursor->right_bound = &ccluster->ondisk->clu_btree_end;
418 KKASSERT(hammer_btree_cmp(&elm[-1].leaf.base,
419 &ccluster->ondisk->clu_btree_beg) == 0);
421 * spike_end is an inclusive boundary and will != clu_btree_end
422 KKASSERT(hammer_btree_cmp(cursor->right_bound,
423 &ccluster->ondisk->clu_btree_end) >= 0);
426 hammer_lock_sh(&parent->lock);
431 * Cursor up to our parent node. Return ENOENT if we are at the root of
434 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
435 * the cursor remains unchanged.
438 hammer_cursor_up(hammer_cursor_t cursor)
442 hammer_cursor_downgrade(cursor);
445 * Set the node to its parent. If the parent is NULL we are at
446 * the root of the root cluster and return ENOENT.
448 hammer_unlock(&cursor->node->lock);
449 hammer_rel_node(cursor->node);
450 cursor->node = cursor->parent;
451 cursor->index = cursor->parent_index;
452 cursor->parent = NULL;
453 cursor->parent_index = 0;
455 if (cursor->node == NULL) {
457 } else if ((cursor->flags & HAMMER_CURSOR_INCLUSTER) &&
458 cursor->node->ondisk->parent == 0
462 error = hammer_load_cursor_parent(cursor);
468 * Set the cursor to the root of the current cursor's cluster.
471 hammer_cursor_toroot(hammer_cursor_t cursor)
473 hammer_cluster_t cluster;
477 * Already at root of cluster?
479 if (cursor->node->ondisk->parent == 0)
482 hammer_cursor_downgrade(cursor);
485 * Parent is root of cluster?
487 if (cursor->parent->ondisk->parent == 0)
488 return (hammer_cursor_up(cursor));
491 * Ok, reload the cursor with the root of the cluster, then
494 cluster = cursor->node->cluster;
495 error = hammer_ref_cluster(cluster);
499 hammer_unlock(&cursor->parent->lock);
500 hammer_rel_node(cursor->parent);
501 hammer_unlock(&cursor->node->lock);
502 hammer_rel_node(cursor->node);
503 cursor->parent = NULL;
504 cursor->parent_index = 0;
506 cursor->node = hammer_get_node(cluster, cluster->ondisk->clu_btree_root,
509 hammer_lock_sh(&cursor->node->lock);
510 hammer_rel_cluster(cluster, 0);
512 error = hammer_load_cursor_parent(cursor);
517 * Cursor down through the current node, which must be an internal node.
519 * This routine adjusts the cursor and sets index to 0.
522 hammer_cursor_down(hammer_cursor_t cursor)
525 hammer_btree_elm_t elm;
526 hammer_volume_t volume;
527 hammer_cluster_t cluster;
533 * The current node becomes the current parent
535 hammer_cursor_downgrade(cursor);
537 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
538 if (cursor->parent) {
539 hammer_unlock(&cursor->parent->lock);
540 hammer_rel_node(cursor->parent);
542 cursor->parent = node;
543 cursor->parent_index = cursor->index;
548 * Extract element to push into at (node,index), set bounds.
550 elm = &node->ondisk->elms[cursor->parent_index];
553 * Ok, push down into elm. If elm specifies an internal or leaf
554 * node the current node must be an internal node. If elm specifies
555 * a spike then the current node must be a leaf node.
557 * Cursoring down through a cluster transition when the INCLUSTER
558 * flag is set is not legal.
560 switch(elm->base.btype) {
561 case HAMMER_BTREE_TYPE_INTERNAL:
562 case HAMMER_BTREE_TYPE_LEAF:
563 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
564 KKASSERT(elm->internal.subtree_offset != 0);
565 cursor->left_bound = &elm[0].internal.base;
566 cursor->right_bound = &elm[1].internal.base;
567 node = hammer_get_node(node->cluster,
568 elm->internal.subtree_offset,
571 KKASSERT(elm->base.btype == node->ondisk->type);
572 if (node->ondisk->parent != cursor->parent->node_offset)
573 panic("node %p %d vs %d\n", node, node->ondisk->parent, cursor->parent->node_offset);
574 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
577 case HAMMER_BTREE_TYPE_SPIKE_BEG:
578 /* case not supported yet */
580 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
581 KKASSERT(cursor->parent_index < node->ondisk->count - 1);
582 KKASSERT(elm[1].leaf.base.btype == HAMMER_BTREE_TYPE_SPIKE_END);
584 ++cursor->parent_index;
586 case HAMMER_BTREE_TYPE_SPIKE_END:
587 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
588 KKASSERT((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0);
589 vol_no = elm->leaf.spike_vol_no;
590 clu_no = elm->leaf.spike_clu_no;
591 volume = hammer_get_volume(node->cluster->volume->hmp,
593 KKASSERT(error != EINVAL);
596 cluster = hammer_get_cluster(volume, clu_no, &error, 0);
597 KKASSERT(error != EINVAL);
598 hammer_rel_volume(volume, 0);
601 KKASSERT(cluster->ondisk->clu_btree_parent_offset ==
602 cursor->parent->node_offset);
603 KKASSERT(cluster->ondisk->clu_btree_parent_clu_no ==
604 cursor->parent->cluster->clu_no);
606 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
607 cursor->right_bound = &cluster->ondisk->clu_btree_end;
608 node = hammer_get_node(cluster,
609 cluster->ondisk->clu_btree_root,
611 hammer_rel_cluster(cluster, 0);
614 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
616 (elm->base.btype ? elm->base.btype : '?'));
620 hammer_lock_sh(&node->lock);