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.7 2007/12/30 00:47:22 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 at the root of the specified cluster and
75 * limit operations to within the cluster.
78 hammer_init_cursor_cluster(hammer_cursor_t cursor, hammer_cluster_t cluster)
82 bzero(cursor, sizeof(*cursor));
83 cursor->flags |= HAMMER_CURSOR_INCLUSTER;
84 cursor->node = hammer_get_node(cluster,
85 cluster->ondisk->clu_btree_root,
88 hammer_lock_ex(&cursor->node->lock);
89 error = hammer_load_cursor_parent(cursor);
95 * Initialize a fresh cursor using the B-Tree node cached by the
99 hammer_init_cursor_ip(hammer_cursor_t cursor, hammer_inode_t ip)
105 bzero(cursor, sizeof(*cursor));
107 error = hammer_ref_node(node);
109 hammer_lock_ex(&node->lock);
111 error = hammer_load_cursor_parent(cursor);
117 error = hammer_init_cursor_hmp(cursor, ip->hmp);
123 * We are finished with a cursor. We NULL out various fields as sanity
124 * check, in case the structure is inappropriately used afterwords.
127 hammer_done_cursor(hammer_cursor_t cursor)
129 if (cursor->parent) {
130 hammer_unlock(&cursor->parent->lock);
131 hammer_rel_node(cursor->parent);
132 cursor->parent = NULL;
135 hammer_unlock(&cursor->node->lock);
136 hammer_rel_node(cursor->node);
139 if (cursor->data_buffer) {
140 hammer_rel_buffer(cursor->data_buffer, 0);
141 cursor->data_buffer = NULL;
143 if (cursor->record_buffer) {
144 hammer_rel_buffer(cursor->record_buffer, 0);
145 cursor->record_buffer = NULL;
148 hammer_mem_done(cursor);
151 cursor->record = NULL;
152 cursor->left_bound = NULL;
153 cursor->right_bound = NULL;
157 * Acquire the parent B-Tree node of the specified node, returning a
158 * referenced but unlocked node. NULL can be returned with *errorp == 0
159 * if node is the root node of the root cluster.
163 hammer_get_parent_node(hammer_node_t node, int *errorp)
165 hammer_cluster_t cluster;
166 hammer_node_t parent;
168 cluster = node->cluster;
169 if (node->ondisk->parent) {
173 parent = hammer_get_node(cluster, node->ondisk->parent, errorp);
174 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0) {
176 * At cluster root, locate node in parent cluster
178 hammer_cluster_ondisk_t ondisk;
179 hammer_cluster_t pcluster;
180 hammer_volume_t pvolume;
184 ondisk = cluster->ondisk;
185 vol_no = ondisk->clu_btree_parent_vol_no;
186 clu_no = ondisk->clu_btree_parent_clu_no;
189 * Acquire the node from (volume, cluster, offset)
191 pvolume = hammer_get_volume(cluster->volume->hmp, vol_no,
195 pcluster = hammer_get_cluster(pvolume, clu_no, errorp, 0);
196 hammer_rel_volume(pvolume, 0);
199 parent = hammer_get_node(pcluster,
200 ondisk->clu_btree_parent_offset,
202 hammer_rel_cluster(pcluster, 0);
205 * At root of root cluster, there is no parent.
207 KKASSERT(cluster->ondisk->clu_btree_parent_vol_no == -1);
215 * Load the parent of cursor->node into cursor->parent. There are several
216 * cases. (1) The parent is in the current cluster. (2) We are at the
217 * root of the cluster and the parent is in another cluster. (3) We are at
218 * the root of the root cluster.
220 * If HAMMER_CURSOR_INCLUSTER is set and we are at the root of the cluster,
221 * we do not access the parent cluster at all and make the cursor look like
226 hammer_load_cursor_parent(hammer_cursor_t cursor)
228 hammer_cluster_t cluster;
231 cluster = cursor->node->cluster;
233 if (cursor->node->ondisk->parent) {
234 error = hammer_load_cursor_parent_local(cursor);
235 } else if (cluster->ondisk->clu_btree_parent_vol_no >= 0 &&
236 ((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0)
238 error = hammer_load_cursor_parent_cluster(cursor);
240 cursor->parent = NULL;
241 cursor->parent_index = 0;
242 cursor->left_bound = &cluster->ondisk->clu_btree_beg;
243 cursor->right_bound = &cluster->ondisk->clu_btree_end;
251 hammer_load_cursor_parent_local(hammer_cursor_t cursor)
254 hammer_node_t parent;
255 hammer_btree_elm_t elm;
260 parent = hammer_get_node(node->cluster, node->ondisk->parent, &error);
264 for (i = 0; i < parent->ondisk->count; ++i) {
265 elm = &parent->ondisk->elms[i];
266 if (parent->ondisk->elms[i].internal.subtree_offset ==
271 if (i == parent->ondisk->count)
272 panic("Bad B-Tree link: parent %p node %p\n", parent, node);
273 KKASSERT(i != parent->ondisk->count);
274 KKASSERT(parent->ondisk->elms[i].internal.rec_offset == 0);
275 cursor->parent = parent;
276 cursor->parent_index = i;
277 cursor->left_bound = &elm[0].internal.base;
278 cursor->right_bound = &elm[1].internal.base;
280 if (hammer_lock_ex_try(&parent->lock) != 0) {
281 hammer_unlock(&cursor->node->lock);
282 hammer_lock_ex(&parent->lock);
283 hammer_lock_ex(&cursor->node->lock);
284 /* XXX check node generation count */
291 hammer_load_cursor_parent_cluster(hammer_cursor_t cursor)
293 hammer_cluster_ondisk_t ondisk;
294 hammer_cluster_t pcluster;
295 hammer_cluster_t ccluster;
296 hammer_volume_t volume;
298 hammer_node_t parent;
299 hammer_btree_elm_t elm;
306 ccluster = node->cluster;
307 ondisk = ccluster->ondisk;
308 vol_no = ondisk->clu_btree_parent_vol_no;
309 clu_no = ondisk->clu_btree_parent_clu_no;
312 * Acquire the node from (volume, cluster, offset)
314 volume = hammer_get_volume(ccluster->volume->hmp, vol_no, &error);
317 pcluster = hammer_get_cluster(volume, clu_no, &error, 0);
318 hammer_rel_volume(volume, 0);
321 parent = hammer_get_node(pcluster, ondisk->clu_btree_parent_offset,
323 hammer_rel_cluster(pcluster, 0);
328 * Ok, we have the node. Locate the inter-cluster element
331 for (i = 0; i < parent->ondisk->count; ++i) {
332 elm = &parent->ondisk->elms[i];
333 if (elm->internal.rec_offset != 0 &&
334 elm->internal.subtree_type == HAMMER_BTREE_TYPE_CLUSTER &&
335 elm->internal.subtree_clu_no == cursor->node->cluster->clu_no) {
339 KKASSERT(i != parent->ondisk->count);
340 cursor->parent = parent;
341 cursor->parent_index = i;
342 cursor->left_bound = &elm[0].internal.base;
343 cursor->right_bound = &elm[1].internal.base;
345 KKASSERT(hammer_btree_cmp(cursor->left_bound,
346 &ccluster->ondisk->clu_btree_beg) == 0);
347 KKASSERT(hammer_btree_cmp(cursor->right_bound,
348 &ccluster->ondisk->clu_btree_end) == 0);
350 if (hammer_lock_ex_try(&parent->lock) != 0) {
351 hammer_unlock(&cursor->node->lock);
352 hammer_lock_ex(&parent->lock);
353 hammer_lock_ex(&cursor->node->lock);
354 /* XXX check node generation count */
360 * Cursor up to our parent node. Return ENOENT if we are at the root of
363 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
364 * the cursor remains unchanged.
367 hammer_cursor_up(hammer_cursor_t cursor, int nonblock)
373 * If asked to do this non-blocking acquire a lock on the parent
374 * now, before we mess with the cursor.
377 save = hammer_get_parent_node(cursor->parent, &error);
381 if (hammer_lock_ex_try(&save->lock) != 0) {
382 hammer_rel_node(save);
391 * Set the node to its parent. If the parent is NULL we are at
392 * the root of the root cluster and return ENOENT.
394 hammer_unlock(&cursor->node->lock);
395 hammer_rel_node(cursor->node);
396 cursor->node = cursor->parent;
397 cursor->index = cursor->parent_index;
398 cursor->parent = NULL;
399 cursor->parent_index = 0;
401 if (cursor->node == NULL) {
403 } else if ((cursor->flags & HAMMER_CURSOR_INCLUSTER) &&
404 cursor->node->ondisk->parent == 0
408 error = hammer_load_cursor_parent(cursor);
411 hammer_unlock(&save->lock);
412 hammer_rel_node(save);
418 * Set the cursor to the root of the current cursor's cluster.
421 hammer_cursor_toroot(hammer_cursor_t cursor)
423 hammer_cluster_t cluster;
427 * Already at root of cluster?
429 if (cursor->node->ondisk->parent == 0)
433 * Parent is root of cluster?
435 if (cursor->parent->ondisk->parent == 0)
436 return (hammer_cursor_up(cursor, 0));
439 * Ok, reload the cursor with the root of the cluster, then
442 cluster = cursor->node->cluster;
443 error = hammer_ref_cluster(cluster);
447 hammer_unlock(&cursor->parent->lock);
448 hammer_rel_node(cursor->parent);
449 hammer_unlock(&cursor->node->lock);
450 hammer_rel_node(cursor->node);
451 cursor->parent = NULL;
452 cursor->parent_index = 0;
454 cursor->node = hammer_get_node(cluster, cluster->ondisk->clu_btree_root,
457 hammer_rel_cluster(cluster, 0);
459 error = hammer_load_cursor_parent(cursor);
464 * Cursor down through the current node, which must be an internal node.
466 * This routine adjusts the cursor and sets index to 0.
469 hammer_cursor_down(hammer_cursor_t cursor)
472 hammer_btree_elm_t elm;
473 hammer_volume_t volume;
474 hammer_cluster_t cluster;
480 * The current node becomes the current parent
483 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
484 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
485 if (cursor->parent) {
486 hammer_unlock(&cursor->parent->lock);
487 hammer_rel_node(cursor->parent);
489 cursor->parent = node;
490 cursor->parent_index = cursor->index;
495 * Extract element to push into at (node,index), set bounds.
497 elm = &node->ondisk->elms[cursor->parent_index];
498 cursor->left_bound = &elm[0].internal.base;
499 cursor->right_bound = &elm[1].internal.base;
502 * Ok, push down into elm. If rec_offset is non-zero this is
503 * an inter-cluster push, otherwise it is a intra-cluster push.
505 * Cursoring down through a cluster transition when the INCLUSTER
506 * flag is set is not legal.
508 if (elm->internal.rec_offset) {
509 KKASSERT((cursor->flags & HAMMER_CURSOR_INCLUSTER) == 0);
510 vol_no = elm->internal.subtree_vol_no;
511 clu_no = elm->internal.subtree_clu_no;
512 volume = hammer_get_volume(node->cluster->volume->hmp,
516 cluster = hammer_get_cluster(volume, clu_no, &error, 0);
517 hammer_rel_volume(volume, 0);
520 node = hammer_get_node(cluster,
521 cluster->ondisk->clu_btree_root,
523 hammer_rel_cluster(cluster, 0);
525 KKASSERT(elm->internal.subtree_offset != 0);
526 node = hammer_get_node(node->cluster,
527 elm->internal.subtree_offset,
530 KKASSERT(elm->internal.subtree_type == node->ondisk->type);
531 if(node->ondisk->parent != cursor->parent->node_offset)
532 kprintf("node %p %d vs %d\n", node, node->ondisk->parent, cursor->parent->node_offset);
533 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
537 hammer_lock_ex(&node->lock);