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.23 2008/05/03 05:28:55 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
, &error
);
99 hammer_rel_volume(volume
, 0);
102 hammer_lock_sh(&node
->lock
);
103 if (node
->flags
& HAMMER_NODE_DELETED
) {
104 hammer_unlock(&node
->lock
);
105 hammer_rel_node(node
);
111 * Step 3 - finish initializing the cursor by acquiring the parent
115 error
= hammer_load_cursor_parent(cursor
);
116 KKASSERT(error
== 0);
117 /* if (error) hammer_done_cursor(cursor); */
123 hammer_reinit_cursor(hammer_cursor_t cursor
)
125 hammer_transaction_t trans
;
127 struct hammer_node
**cache
;
129 trans
= cursor
->trans
;
131 hammer_done_cursor(cursor
);
132 cache
= ip
? &ip
->cache
[0] : NULL
;
133 error
= hammer_init_cursor(trans
, cursor
, cache
, ip
);
140 * Normalize a cursor. Sometimes cursors can be left in a state
141 * where node is NULL. If the cursor is in this state, cursor up.
144 hammer_normalize_cursor(hammer_cursor_t cursor
)
146 if (cursor
->node
== NULL
) {
147 KKASSERT(cursor
->parent
!= NULL
);
148 hammer_cursor_up(cursor
);
154 * We are finished with a cursor. We NULL out various fields as sanity
155 * check, in case the structure is inappropriately used afterwords.
158 hammer_done_cursor(hammer_cursor_t cursor
)
162 if (cursor
->parent
) {
163 hammer_unlock(&cursor
->parent
->lock
);
164 hammer_rel_node(cursor
->parent
);
165 cursor
->parent
= NULL
;
168 hammer_unlock(&cursor
->node
->lock
);
169 hammer_rel_node(cursor
->node
);
172 if (cursor
->data_buffer
) {
173 hammer_rel_buffer(cursor
->data_buffer
, 0);
174 cursor
->data_buffer
= NULL
;
176 if (cursor
->record_buffer
) {
177 hammer_rel_buffer(cursor
->record_buffer
, 0);
178 cursor
->record_buffer
= NULL
;
180 if ((ip
= cursor
->ip
) != NULL
) {
181 hammer_mem_done(cursor
);
182 KKASSERT(ip
->cursor_ip_refs
> 0);
183 --ip
->cursor_ip_refs
;
184 hammer_unlock(&ip
->lock
);
190 * If we deadlocked this node will be referenced. Do a quick
191 * lock/unlock to wait for the deadlock condition to clear.
193 if (cursor
->deadlk_node
) {
194 hammer_lock_ex(&cursor
->deadlk_node
->lock
);
195 hammer_unlock(&cursor
->deadlk_node
->lock
);
196 hammer_rel_node(cursor
->deadlk_node
);
197 cursor
->deadlk_node
= NULL
;
199 if (cursor
->deadlk_rec
) {
200 hammer_wait_mem_record(cursor
->deadlk_rec
);
201 hammer_rel_mem_record(cursor
->deadlk_rec
);
202 cursor
->deadlk_rec
= NULL
;
206 cursor
->record
= NULL
;
207 cursor
->left_bound
= NULL
;
208 cursor
->right_bound
= NULL
;
209 cursor
->trans
= NULL
;
213 * Upgrade cursor->node and cursor->parent to exclusive locks. This
214 * function can return EDEADLK.
216 * The lock must already be either held shared or already held exclusively
219 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
220 * we add another reference to the node that failed and set
221 * cursor->deadlk_node so hammer_done_cursor() can block on it.
224 hammer_cursor_upgrade(hammer_cursor_t cursor
)
228 error
= hammer_lock_upgrade(&cursor
->node
->lock
);
229 if (error
&& cursor
->deadlk_node
== NULL
) {
230 cursor
->deadlk_node
= cursor
->node
;
231 hammer_ref_node(cursor
->deadlk_node
);
232 } else if (error
== 0 && cursor
->parent
) {
233 error
= hammer_lock_upgrade(&cursor
->parent
->lock
);
234 if (error
&& cursor
->deadlk_node
== NULL
) {
235 cursor
->deadlk_node
= cursor
->parent
;
236 hammer_ref_node(cursor
->deadlk_node
);
243 * Downgrade cursor->node and cursor->parent to shared locks. This
244 * function can return EDEADLK.
247 hammer_cursor_downgrade(hammer_cursor_t cursor
)
249 if (hammer_lock_excl_owned(&cursor
->node
->lock
, curthread
))
250 hammer_lock_downgrade(&cursor
->node
->lock
);
251 if (cursor
->parent
&&
252 hammer_lock_excl_owned(&cursor
->parent
->lock
, curthread
)) {
253 hammer_lock_downgrade(&cursor
->parent
->lock
);
258 * Seek the cursor to the specified node and index.
261 hammer_cursor_seek(hammer_cursor_t cursor
, hammer_node_t node
, int index
)
265 hammer_cursor_downgrade(cursor
);
268 if (cursor
->node
!= node
) {
269 hammer_unlock(&cursor
->node
->lock
);
270 hammer_rel_node(cursor
->node
);
272 hammer_ref_node(node
);
273 hammer_lock_sh(&node
->lock
);
275 if (cursor
->parent
) {
276 hammer_unlock(&cursor
->parent
->lock
);
277 hammer_rel_node(cursor
->parent
);
278 cursor
->parent
= NULL
;
279 cursor
->parent_index
= 0;
281 error
= hammer_load_cursor_parent(cursor
);
283 cursor
->index
= index
;
288 * Load the parent of cursor->node into cursor->parent.
292 hammer_load_cursor_parent(hammer_cursor_t cursor
)
295 hammer_node_t parent
;
297 hammer_btree_elm_t elm
;
301 hmp
= cursor
->trans
->hmp
;
303 if (cursor
->node
->ondisk
->parent
) {
305 parent
= hammer_get_node(hmp
, node
->ondisk
->parent
, &error
);
308 hammer_lock_sh(&parent
->lock
);
310 for (i
= 0; i
< parent
->ondisk
->count
; ++i
) {
311 elm
= &parent
->ondisk
->elms
[i
];
312 if (parent
->ondisk
->elms
[i
].internal
.subtree_offset
==
317 if (i
== parent
->ondisk
->count
) {
318 hammer_unlock(&parent
->lock
);
319 panic("Bad B-Tree link: parent %p node %p\n", parent
, node
);
321 KKASSERT(i
!= parent
->ondisk
->count
);
322 cursor
->parent
= parent
;
323 cursor
->parent_index
= i
;
324 cursor
->left_bound
= &elm
[0].internal
.base
;
325 cursor
->right_bound
= &elm
[1].internal
.base
;
328 cursor
->parent
= NULL
;
329 cursor
->parent_index
= 0;
330 cursor
->left_bound
= &hmp
->root_btree_beg
;
331 cursor
->right_bound
= &hmp
->root_btree_end
;
338 * Cursor up to our parent node. Return ENOENT if we are at the root of
341 * If doing a nonblocking cursor-up and we are unable to acquire the lock,
342 * the cursor remains unchanged.
345 hammer_cursor_up(hammer_cursor_t cursor
)
349 hammer_cursor_downgrade(cursor
);
352 * If the parent is NULL we are at the root of the B-Tree and
355 if (cursor
->parent
== NULL
)
359 * Set the node to its parent.
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 error
= hammer_load_cursor_parent(cursor
);
373 * Cursor down through the current node, which must be an internal node.
375 * This routine adjusts the cursor and sets index to 0.
378 hammer_cursor_down(hammer_cursor_t cursor
)
381 hammer_btree_elm_t elm
;
385 * The current node becomes the current parent
387 hammer_cursor_downgrade(cursor
);
389 KKASSERT(cursor
->index
>= 0 && cursor
->index
< node
->ondisk
->count
);
390 if (cursor
->parent
) {
391 hammer_unlock(&cursor
->parent
->lock
);
392 hammer_rel_node(cursor
->parent
);
394 cursor
->parent
= node
;
395 cursor
->parent_index
= cursor
->index
;
400 * Extract element to push into at (node,index), set bounds.
402 elm
= &node
->ondisk
->elms
[cursor
->parent_index
];
405 * Ok, push down into elm. If elm specifies an internal or leaf
406 * node the current node must be an internal node. If elm specifies
407 * a spike then the current node must be a leaf node.
409 switch(elm
->base
.btype
) {
410 case HAMMER_BTREE_TYPE_INTERNAL
:
411 case HAMMER_BTREE_TYPE_LEAF
:
412 KKASSERT(node
->ondisk
->type
== HAMMER_BTREE_TYPE_INTERNAL
);
413 KKASSERT(elm
->internal
.subtree_offset
!= 0);
414 cursor
->left_bound
= &elm
[0].internal
.base
;
415 cursor
->right_bound
= &elm
[1].internal
.base
;
416 node
= hammer_get_node(cursor
->trans
->hmp
,
417 elm
->internal
.subtree_offset
, &error
);
419 KASSERT(elm
->base
.btype
== node
->ondisk
->type
, ("BTYPE MISMATCH %c %c NODE %p\n", elm
->base
.btype
, node
->ondisk
->type
, node
));
420 if (node
->ondisk
->parent
!= cursor
->parent
->node_offset
)
421 panic("node %p %016llx vs %016llx\n", node
, node
->ondisk
->parent
, cursor
->parent
->node_offset
);
422 KKASSERT(node
->ondisk
->parent
== cursor
->parent
->node_offset
);
426 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
428 (elm
->base
.btype
? elm
->base
.btype
: '?'));
432 hammer_lock_sh(&node
->lock
);