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.5 2007/11/30 00:16:56 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 at the root of the filesystem hierarchy.
49 * cursor->parent will be NULL on return since we are loading the root
50 * node of the root cluster.
53 hammer_init_cursor_hmp(hammer_cursor_t cursor, hammer_mount_t hmp)
55 hammer_cluster_t cluster;
58 bzero(cursor, sizeof(*cursor));
59 cluster = hammer_get_root_cluster(hmp, &error);
61 cursor->node = hammer_get_node(cluster,
62 cluster->ondisk->clu_btree_root,
64 hammer_rel_cluster(cluster, 0);
66 hammer_lock_ex(&cursor->node->lock);
69 error = hammer_load_cursor_parent(cursor);
74 * Initialize a fresh cursor using the B-Tree node cached by the
78 hammer_init_cursor_ip(hammer_cursor_t cursor, hammer_inode_t ip)
84 bzero(cursor, sizeof(*cursor));
86 error = hammer_ref_node(node);
88 hammer_lock_ex(&node->lock);
90 error = hammer_load_cursor_parent(cursor);
96 error = hammer_init_cursor_hmp(cursor, ip->hmp);
102 * We are finished with a cursor. We NULL out various fields as sanity
103 * check, in case the structure is inappropriately used afterwords.
106 hammer_done_cursor(hammer_cursor_t cursor)
108 if (cursor->parent) {
109 hammer_unlock(&cursor->parent->lock);
110 hammer_rel_node(cursor->parent);
111 cursor->parent = NULL;
114 hammer_unlock(&cursor->node->lock);
115 hammer_rel_node(cursor->node);
118 if (cursor->data_buffer) {
119 hammer_rel_buffer(cursor->data_buffer, 0);
120 cursor->data_buffer = NULL;
122 if (cursor->record_buffer) {
123 hammer_rel_buffer(cursor->record_buffer, 0);
124 cursor->record_buffer = NULL;
127 hammer_mem_done(cursor);
130 cursor->record = NULL;
131 cursor->left_bound = NULL;
132 cursor->right_bound = NULL;
136 * Acquire the parent B-Tree node of the specified node, returning a
137 * referenced but unlocked node. NULL can be returned with *errorp == 0
138 * if node is the root node of the root cluster.
142 hammer_get_parent_node(hammer_node_t node, int *errorp)
144 hammer_cluster_t cluster;
145 hammer_node_t parent;
147 cluster = node->cluster;
148 if (node->ondisk->parent) {
152 parent = hammer_get_node(cluster, node->ondisk->parent, errorp);
153 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0) {
155 * At cluster root, locate node in parent cluster
157 hammer_cluster_ondisk_t ondisk;
158 hammer_cluster_t pcluster;
159 hammer_volume_t pvolume;
163 ondisk = cluster->ondisk;
164 vol_no = ondisk->clu_btree_parent_vol_no;
165 clu_no = ondisk->clu_btree_parent_clu_no;
168 * Acquire the node from (volume, cluster, offset)
170 pvolume = hammer_get_volume(cluster->volume->hmp, vol_no,
174 pcluster = hammer_get_cluster(pvolume, clu_no, errorp, 0);
175 hammer_rel_volume(pvolume, 0);
178 parent = hammer_get_node(pcluster,
179 ondisk->clu_btree_parent_offset,
181 hammer_rel_cluster(pcluster, 0);
184 * At root of root cluster, there is no parent.
193 * Load the parent of cursor->node into cursor->parent. There are several
194 * cases. (1) The parent is in the current cluster. (2) We are at the
195 * root of the cluster and the parent is in another cluster. (3) We are at
196 * the root of the root cluster.
200 hammer_load_cursor_parent(hammer_cursor_t cursor)
202 hammer_cluster_t cluster;
205 cluster = cursor->node->cluster;
207 if (cursor->node->ondisk->parent) {
208 error = hammer_load_cursor_parent_local(cursor);
209 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0) {
210 error = hammer_load_cursor_parent_cluster(cursor);
212 cursor->parent = NULL;
213 cursor->parent_index = 0;
214 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
215 cursor->right_bound = &cluster->ondisk->clu_btree_end;
223 hammer_load_cursor_parent_local(hammer_cursor_t cursor)
226 hammer_node_t parent;
227 hammer_btree_elm_t elm;
232 parent = hammer_get_node(node->cluster, node->ondisk->parent, &error);
236 for (i = 0; i < parent->ondisk->count; ++i) {
237 elm = &parent->ondisk->elms[i];
238 if (parent->ondisk->elms[i].internal.subtree_offset ==
243 KKASSERT(i != parent->ondisk->count);
244 KKASSERT(parent->ondisk->elms[i].internal.rec_offset == 0);
245 cursor->parent = parent;
246 cursor->parent_index = i;
247 cursor->left_bound = &elm[0].internal.base;
248 cursor->right_bound = &elm[1].internal.base;
250 if (hammer_lock_ex_try(&parent->lock) != 0) {
251 hammer_unlock(&cursor->node->lock);
252 hammer_lock_ex(&parent->lock);
253 hammer_lock_ex(&cursor->node->lock);
254 /* XXX check node generation count */
261 hammer_load_cursor_parent_cluster(hammer_cursor_t cursor)
263 hammer_cluster_ondisk_t ondisk;
264 hammer_cluster_t cluster;
265 hammer_volume_t volume;
267 hammer_node_t parent;
268 hammer_btree_elm_t elm;
275 ondisk = node->cluster->ondisk;
276 vol_no = ondisk->clu_btree_parent_vol_no;
277 clu_no = ondisk->clu_btree_parent_clu_no;
280 * Acquire the node from (volume, cluster, offset)
282 volume = hammer_get_volume(node->cluster->volume->hmp, vol_no, &error);
285 cluster = hammer_get_cluster(volume, clu_no, &error, 0);
286 hammer_rel_volume(volume, 0);
289 parent = hammer_get_node(cluster, ondisk->clu_btree_parent_offset,
291 hammer_rel_cluster(cluster, 0);
296 * Ok, we have the node. Locate the inter-cluster element
299 for (i = 0; i < parent->ondisk->count; ++i) {
300 elm = &parent->ondisk->elms[i];
301 if (elm->internal.rec_offset != 0 &&
302 elm->internal.subtree_cluid == clu_no) {
306 KKASSERT(i != parent->ondisk->count);
307 cursor->parent = parent;
308 cursor->parent_index = i;
309 cursor->left_bound = &elm[0].internal.base;
310 cursor->right_bound = &elm[1].internal.base;
312 KKASSERT(hammer_btree_cmp(cursor->left_bound,
313 &parent->cluster->ondisk->clu_btree_beg) == 0);
314 KKASSERT(hammer_btree_cmp(cursor->right_bound,
315 &parent->cluster->ondisk->clu_btree_end) == 0);
317 if (hammer_lock_ex_try(&parent->lock) != 0) {
318 hammer_unlock(&cursor->node->lock);
319 hammer_lock_ex(&parent->lock);
320 hammer_lock_ex(&cursor->node->lock);
321 /* XXX check node generation count */
327 * Cursor up to our parent node. Return ENOENT if we are at the root of
330 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
331 * the cursor remains unchanged.
334 hammer_cursor_up(hammer_cursor_t cursor, int nonblock)
340 * If asked to do this non-blocking acquire a lock on the parent
341 * now, before we mess with the cursor.
344 save = hammer_get_parent_node(cursor->parent, &error);
348 if (hammer_lock_ex_try(&save->lock) != 0) {
349 hammer_rel_node(save);
358 * Set the node to its parent. If the parent is NULL we are at
359 * the root of the root cluster and return ENOENT.
361 hammer_unlock(&cursor->node->lock);
362 hammer_rel_node(cursor->node);
363 cursor->node = cursor->parent;
364 cursor->index = cursor->parent_index;
365 cursor->parent = NULL;
366 cursor->parent_index = 0;
368 if (cursor->node == NULL) {
371 error = hammer_load_cursor_parent(cursor);
374 hammer_unlock(&save->lock);
375 hammer_rel_node(save);
381 * Set the cursor to the root of the current cursor's cluster.
384 hammer_cursor_toroot(hammer_cursor_t cursor)
386 hammer_cluster_t cluster;
390 * Already at root of cluster?
392 if (cursor->node->ondisk->parent == 0)
396 * Parent is root of cluster?
398 if (cursor->parent->ondisk->parent == 0)
399 return (hammer_cursor_up(cursor, 0));
402 * Ok, reload the cursor with the root of the cluster, then
405 cluster = cursor->node->cluster;
406 error = hammer_ref_cluster(cluster);
410 hammer_unlock(&cursor->parent->lock);
411 hammer_rel_node(cursor->parent);
412 hammer_unlock(&cursor->node->lock);
413 hammer_rel_node(cursor->node);
414 cursor->parent = NULL;
415 cursor->parent_index = 0;
417 cursor->node = hammer_get_node(cluster, cluster->ondisk->clu_btree_root,
420 hammer_rel_cluster(cluster, 0);
422 error = hammer_load_cursor_parent(cursor);
427 * Cursor down through the current node, which must be an internal node.
429 * This routine adjusts the cursor and sets index to 0.
432 hammer_cursor_down(hammer_cursor_t cursor)
435 hammer_btree_elm_t elm;
436 hammer_volume_t volume;
437 hammer_cluster_t cluster;
443 * The current node becomes the current parent
446 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
447 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
448 if (cursor->parent) {
449 hammer_unlock(&cursor->parent->lock);
450 hammer_rel_node(cursor->parent);
452 cursor->parent = node;
453 cursor->parent_index = cursor->index;
458 * Extract element to push into at (node,index), set bounds.
460 elm = &node->ondisk->elms[cursor->parent_index];
461 cursor->left_bound = &elm[0].internal.base;
462 cursor->right_bound = &elm[1].internal.base;
465 * Ok, push down into elm. If rec_offset is non-zero this is
466 * an inter-cluster push, otherwise it is a intra-cluster push.
468 if (elm->internal.rec_offset) {
469 vol_no = elm->internal.subtree_volno;
470 clu_no = elm->internal.subtree_cluid;
471 volume = hammer_get_volume(node->cluster->volume->hmp,
475 cluster = hammer_get_cluster(volume, clu_no, &error, 0);
476 hammer_rel_volume(volume, 0);
479 node = hammer_get_node(cluster,
480 cluster->ondisk->clu_btree_root,
482 hammer_rel_cluster(cluster, 0);
484 node = hammer_get_node(node->cluster,
485 elm->internal.subtree_offset,
489 hammer_lock_ex(&node->lock);