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.8 2007/12/30 08:49:20 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);
75 * Initialize a fresh cursor at the root of the specified cluster and
76 * limit operations to within the cluster.
79 hammer_init_cursor_cluster(hammer_cursor_t cursor, hammer_cluster_t cluster)
83 bzero(cursor, sizeof(*cursor));
84 cursor->flags |= HAMMER_CURSOR_INCLUSTER;
85 cursor->node = hammer_get_node(cluster,
86 cluster->ondisk->clu_btree_root,
89 hammer_lock_ex(&cursor->node->lock);
90 error = hammer_load_cursor_parent(cursor);
97 * Initialize a fresh cursor using the B-Tree node cached by the
101 hammer_init_cursor_ip(hammer_cursor_t cursor, hammer_inode_t ip)
107 bzero(cursor, sizeof(*cursor));
109 error = hammer_ref_node(node);
111 hammer_lock_ex(&node->lock);
113 error = hammer_load_cursor_parent(cursor);
119 error = hammer_init_cursor_hmp(cursor, ip->hmp);
121 KKASSERT(error == 0);
126 * We are finished with a cursor. We NULL out various fields as sanity
127 * check, in case the structure is inappropriately used afterwords.
130 hammer_done_cursor(hammer_cursor_t cursor)
132 if (cursor->parent) {
133 hammer_unlock(&cursor->parent->lock);
134 hammer_rel_node(cursor->parent);
135 cursor->parent = NULL;
138 hammer_unlock(&cursor->node->lock);
139 hammer_rel_node(cursor->node);
142 if (cursor->data_buffer) {
143 hammer_rel_buffer(cursor->data_buffer, 0);
144 cursor->data_buffer = NULL;
146 if (cursor->record_buffer) {
147 hammer_rel_buffer(cursor->record_buffer, 0);
148 cursor->record_buffer = NULL;
151 hammer_mem_done(cursor);
154 cursor->record = NULL;
155 cursor->left_bound = NULL;
156 cursor->right_bound = NULL;
160 * Acquire the parent B-Tree node of the specified node, returning a
161 * referenced but unlocked node. NULL can be returned with *errorp == 0
162 * if node is the root node of the root cluster.
166 hammer_get_parent_node(hammer_node_t node, int *errorp)
168 hammer_cluster_t cluster;
169 hammer_node_t parent;
171 cluster = node->cluster;
172 if (node->ondisk->parent) {
176 parent = hammer_get_node(cluster, node->ondisk->parent, errorp);
177 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0) {
179 * At cluster root, locate node in parent cluster
181 hammer_cluster_ondisk_t ondisk;
182 hammer_cluster_t pcluster;
183 hammer_volume_t pvolume;
187 ondisk = cluster->ondisk;
188 vol_no = ondisk->clu_btree_parent_vol_no;
189 clu_no = ondisk->clu_btree_parent_clu_no;
192 * Acquire the node from (volume, cluster, offset)
194 pvolume = hammer_get_volume(cluster->volume->hmp, vol_no,
198 pcluster = hammer_get_cluster(pvolume, clu_no, errorp, 0);
199 hammer_rel_volume(pvolume, 0);
202 parent = hammer_get_node(pcluster,
203 ondisk->clu_btree_parent_offset,
205 hammer_rel_cluster(pcluster, 0);
208 * At root of root cluster, there is no parent.
210 KKASSERT(cluster->ondisk->clu_btree_parent_vol_no == -1);
218 * Load the parent of cursor->node into cursor->parent. There are several
219 * cases. (1) The parent is in the current cluster. (2) We are at the
220 * root of the cluster and the parent is in another cluster. (3) We are at
221 * the root of the root cluster.
223 * If HAMMER_CURSOR_INCLUSTER is set and we are at the root of the cluster,
224 * we do not access the parent cluster at all and make the cursor look like
229 hammer_load_cursor_parent(hammer_cursor_t cursor)
231 hammer_cluster_t cluster;
234 cluster = cursor->node->cluster;
236 if (cursor->node->ondisk->parent) {
237 error = hammer_load_cursor_parent_local(cursor);
238 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0 &&
239 ((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0)
241 error = hammer_load_cursor_parent_cluster(cursor);
243 cursor->parent = NULL;
244 cursor->parent_index = 0;
245 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
246 cursor->right_bound = &cluster->ondisk->clu_btree_end;
254 hammer_load_cursor_parent_local(hammer_cursor_t cursor)
257 hammer_node_t parent;
258 hammer_btree_elm_t elm;
263 parent = hammer_get_node(node->cluster, node->ondisk->parent, &error);
267 for (i = 0; i < parent->ondisk->count; ++i) {
268 elm = &parent->ondisk->elms[i];
269 if (parent->ondisk->elms[i].internal.subtree_offset ==
274 if (i == parent->ondisk->count)
275 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
276 KKASSERT(i != parent->ondisk->count);
277 KKASSERT(parent->ondisk->elms[i].internal.rec_offset == 0);
278 cursor->parent = parent;
279 cursor->parent_index = i;
280 cursor->left_bound = &elm[0].internal.base;
281 cursor->right_bound = &elm[1].internal.base;
283 if (hammer_lock_ex_try(&parent->lock) != 0) {
284 hammer_unlock(&cursor->node->lock);
285 hammer_lock_ex(&parent->lock);
286 hammer_lock_ex(&cursor->node->lock);
287 /* XXX check node generation count */
294 hammer_load_cursor_parent_cluster(hammer_cursor_t cursor)
296 hammer_cluster_ondisk_t ondisk;
297 hammer_cluster_t pcluster;
298 hammer_cluster_t ccluster;
299 hammer_volume_t volume;
301 hammer_node_t parent;
302 hammer_btree_elm_t elm;
309 ccluster = node->cluster;
310 ondisk = ccluster->ondisk;
311 vol_no = ondisk->clu_btree_parent_vol_no;
312 clu_no = ondisk->clu_btree_parent_clu_no;
315 * Acquire the node from (volume, cluster, offset)
317 volume = hammer_get_volume(ccluster->volume->hmp, vol_no, &error);
320 pcluster = hammer_get_cluster(volume, clu_no, &error, 0);
321 hammer_rel_volume(volume, 0);
324 parent = hammer_get_node(pcluster, ondisk->clu_btree_parent_offset,
326 hammer_rel_cluster(pcluster, 0);
331 * Ok, we have the node. Locate the inter-cluster element
334 for (i = 0; i < parent->ondisk->count; ++i) {
335 elm = &parent->ondisk->elms[i];
336 if (elm->internal.rec_offset != 0 &&
337 elm->internal.subtree_type == HAMMER_BTREE_TYPE_CLUSTER &&
338 elm->internal.subtree_clu_no == cursor->node->cluster->clu_no) {
342 KKASSERT(i != parent->ondisk->count);
343 cursor->parent = parent;
344 cursor->parent_index = i;
345 cursor->left_bound = &elm[0].internal.base;
346 cursor->right_bound = &elm[1].internal.base;
348 KKASSERT(hammer_btree_cmp(cursor->left_bound,
349 &ccluster->ondisk->clu_btree_beg) == 0);
350 KKASSERT(hammer_btree_cmp(cursor->right_bound,
351 &ccluster->ondisk->clu_btree_end) == 0);
353 if (hammer_lock_ex_try(&parent->lock) != 0) {
354 hammer_unlock(&cursor->node->lock);
355 hammer_lock_ex(&parent->lock);
356 hammer_lock_ex(&cursor->node->lock);
357 /* XXX check node generation count */
363 * Cursor up to our parent node. Return ENOENT if we are at the root of
366 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
367 * the cursor remains unchanged.
370 hammer_cursor_up(hammer_cursor_t cursor, int nonblock)
376 * If asked to do this non-blocking acquire a lock on the parent
377 * now, before we mess with the cursor.
380 save = hammer_get_parent_node(cursor->parent, &error);
384 if (hammer_lock_ex_try(&save->lock) != 0) {
385 hammer_rel_node(save);
394 * Set the node to its parent. If the parent is NULL we are at
395 * the root of the root cluster and return ENOENT.
397 hammer_unlock(&cursor->node->lock);
398 hammer_rel_node(cursor->node);
399 cursor->node = cursor->parent;
400 cursor->index = cursor->parent_index;
401 cursor->parent = NULL;
402 cursor->parent_index = 0;
404 if (cursor->node == NULL) {
406 } else if ((cursor->flags & HAMMER_CURSOR_INCLUSTER) &&
407 cursor->node->ondisk->parent == 0
411 error = hammer_load_cursor_parent(cursor);
414 hammer_unlock(&save->lock);
415 hammer_rel_node(save);
421 * Set the cursor to the root of the current cursor's cluster.
424 hammer_cursor_toroot(hammer_cursor_t cursor)
426 hammer_cluster_t cluster;
430 * Already at root of cluster?
432 if (cursor->node->ondisk->parent == 0)
436 * Parent is root of cluster?
438 if (cursor->parent->ondisk->parent == 0)
439 return (hammer_cursor_up(cursor, 0));
442 * Ok, reload the cursor with the root of the cluster, then
445 cluster = cursor->node->cluster;
446 error = hammer_ref_cluster(cluster);
450 hammer_unlock(&cursor->parent->lock);
451 hammer_rel_node(cursor->parent);
452 hammer_unlock(&cursor->node->lock);
453 hammer_rel_node(cursor->node);
454 cursor->parent = NULL;
455 cursor->parent_index = 0;
457 cursor->node = hammer_get_node(cluster, cluster->ondisk->clu_btree_root,
460 hammer_lock_ex(&cursor->node->lock);
461 hammer_rel_cluster(cluster, 0);
463 error = hammer_load_cursor_parent(cursor);
468 * Cursor down through the current node, which must be an internal node.
470 * This routine adjusts the cursor and sets index to 0.
473 hammer_cursor_down(hammer_cursor_t cursor)
476 hammer_btree_elm_t elm;
477 hammer_volume_t volume;
478 hammer_cluster_t cluster;
484 * The current node becomes the current parent
487 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
488 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
489 if (cursor->parent) {
490 hammer_unlock(&cursor->parent->lock);
491 hammer_rel_node(cursor->parent);
493 cursor->parent = node;
494 cursor->parent_index = cursor->index;
499 * Extract element to push into at (node,index), set bounds.
501 elm = &node->ondisk->elms[cursor->parent_index];
502 cursor->left_bound = &elm[0].internal.base;
503 cursor->right_bound = &elm[1].internal.base;
506 * Ok, push down into elm. If rec_offset is non-zero this is
507 * an inter-cluster push, otherwise it is a intra-cluster push.
509 * Cursoring down through a cluster transition when the INCLUSTER
510 * flag is set is not legal.
512 if (elm->internal.rec_offset) {
513 KKASSERT((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0);
514 vol_no = elm->internal.subtree_vol_no;
515 clu_no = elm->internal.subtree_clu_no;
516 volume = hammer_get_volume(node->cluster->volume->hmp,
518 KKASSERT(error != EINVAL);
521 cluster = hammer_get_cluster(volume, clu_no, &error, 0);
522 KKASSERT(error != EINVAL);
523 hammer_rel_volume(volume, 0);
526 node = hammer_get_node(cluster,
527 cluster->ondisk->clu_btree_root,
529 hammer_rel_cluster(cluster, 0);
531 KKASSERT(elm->internal.subtree_offset != 0);
532 node = hammer_get_node(node->cluster,
533 elm->internal.subtree_offset,
536 KKASSERT(elm->internal.subtree_type == node->ondisk->type);
537 if(node->ondisk->parent != cursor->parent->node_offset)
538 kprintf("node %p %d vs %d\n", node, node->ondisk->parent, cursor->parent->node_offset);
539 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
543 hammer_lock_ex(&node->lock);