HAMMER 40D/Many: Inode/link-count sequencer cleanup pass.
[dragonfly.git] / sys / vfs / hammer / hammer_cursor.c
blob0edcca81e974002f690e38374f4840bf07b4c7ba
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
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
40 #include "hammer.h"
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.
48 int
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;
53 hammer_node_t node;
54 int error;
56 bzero(cursor, sizeof(*cursor));
58 cursor->trans = trans;
61 * If the cursor operation is on behalf of an inode, lock
62 * the inode.
64 if ((cursor->ip = ip) != NULL) {
65 ++ip->cursor_ip_refs;
66 if (trans->type == HAMMER_TRANS_FLS)
67 hammer_lock_ex(&ip->lock);
68 else
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);
77 if (error == 0) {
78 hammer_lock_sh(&node->lock);
79 if (node->flags & HAMMER_NODE_DELETED) {
80 hammer_unlock(&node->lock);
81 hammer_rel_node(node);
82 node = NULL;
85 } else {
86 node = NULL;
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);
95 if (error)
96 break;
97 node = hammer_get_node(trans->hmp,
98 volume->ondisk->vol0_btree_root, &error);
99 hammer_rel_volume(volume, 0);
100 if (error)
101 break;
102 hammer_lock_sh(&node->lock);
103 if (node->flags & HAMMER_NODE_DELETED) {
104 hammer_unlock(&node->lock);
105 hammer_rel_node(node);
106 node = NULL;
111 * Step 3 - finish initializing the cursor by acquiring the parent
113 cursor->node = node;
114 if (error == 0)
115 error = hammer_load_cursor_parent(cursor);
116 KKASSERT(error == 0);
117 /* if (error) hammer_done_cursor(cursor); */
118 return(error);
121 #if 0
123 hammer_reinit_cursor(hammer_cursor_t cursor)
125 hammer_transaction_t trans;
126 hammer_inode_t ip;
127 struct hammer_node **cache;
129 trans = cursor->trans;
130 ip = cursor->ip;
131 hammer_done_cursor(cursor);
132 cache = ip ? &ip->cache[0] : NULL;
133 error = hammer_init_cursor(trans, cursor, cache, ip);
134 return (error);
137 #endif
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.
143 void
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.
157 void
158 hammer_done_cursor(hammer_cursor_t cursor)
160 hammer_inode_t ip;
162 if (cursor->parent) {
163 hammer_unlock(&cursor->parent->lock);
164 hammer_rel_node(cursor->parent);
165 cursor->parent = NULL;
167 if (cursor->node) {
168 hammer_unlock(&cursor->node->lock);
169 hammer_rel_node(cursor->node);
170 cursor->node = NULL;
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);
185 cursor->ip = NULL;
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;
205 cursor->data = 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
217 * by us.
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)
226 int error;
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);
239 return(error);
243 * Downgrade cursor->node and cursor->parent to shared locks. This
244 * function can return EDEADLK.
246 void
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)
263 int error;
265 hammer_cursor_downgrade(cursor);
266 error = 0;
268 if (cursor->node != node) {
269 hammer_unlock(&cursor->node->lock);
270 hammer_rel_node(cursor->node);
271 cursor->node = 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;
284 return (error);
288 * Load the parent of cursor->node into cursor->parent.
290 static
292 hammer_load_cursor_parent(hammer_cursor_t cursor)
294 hammer_mount_t hmp;
295 hammer_node_t parent;
296 hammer_node_t node;
297 hammer_btree_elm_t elm;
298 int error;
299 int i;
301 hmp = cursor->trans->hmp;
303 if (cursor->node->ondisk->parent) {
304 node = cursor->node;
305 parent = hammer_get_node(hmp, node->ondisk->parent, &error);
306 if (error)
307 return(error);
308 hammer_lock_sh(&parent->lock);
309 elm = NULL;
310 for (i = 0; i < parent->ondisk->count; ++i) {
311 elm = &parent->ondisk->elms[i];
312 if (parent->ondisk->elms[i].internal.subtree_offset ==
313 node->node_offset) {
314 break;
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;
326 return(error);
327 } else {
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;
332 error = 0;
334 return(error);
338 * Cursor up to our parent node. Return ENOENT if we are at the root of
339 * the filesystem.
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)
347 int error;
349 hammer_cursor_downgrade(cursor);
352 * If the parent is NULL we are at the root of the B-Tree and
353 * return ENOENT.
355 if (cursor->parent == NULL)
356 return (ENOENT);
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);
369 return(error);
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)
380 hammer_node_t node;
381 hammer_btree_elm_t elm;
382 int error;
385 * The current node becomes the current parent
387 hammer_cursor_downgrade(cursor);
388 node = cursor->node;
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;
396 cursor->node = NULL;
397 cursor->index = 0;
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);
418 if (error == 0) {
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);
424 break;
425 default:
426 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
427 elm->base.btype,
428 (elm->base.btype ? elm->base.btype : '?'));
429 break;
431 if (error == 0) {
432 hammer_lock_sh(&node->lock);
433 cursor->node = node;
434 cursor->index = 0;
436 return(error);