HAMMER 60D/Many: Mirroring, bug fixes
[dragonfly.git] / sys / vfs / hammer / hammer_cursor.c
blobb7f795d57f07495fce36fa6cf9619e503b486803
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.37 2008/07/05 18:59:27 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, int try_exclusive);
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 hammer_node_cache_t 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->node) {
76 node = hammer_ref_node_safe(trans->hmp, cache, &error);
77 if (error == 0) {
78 hammer_lock_sh_lowpri(&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,
99 0, &error);
100 hammer_rel_volume(volume, 0);
101 if (error)
102 break;
103 hammer_lock_sh_lowpri(&node->lock);
106 * If someone got in before we could lock the node, retry.
108 if (node->flags & HAMMER_NODE_DELETED) {
109 hammer_unlock(&node->lock);
110 hammer_rel_node(node);
111 node = NULL;
112 continue;
114 if (volume->ondisk->vol0_btree_root != node->node_offset) {
115 hammer_unlock(&node->lock);
116 hammer_rel_node(node);
117 node = NULL;
118 continue;
123 * Step 3 - finish initializing the cursor by acquiring the parent
125 cursor->node = node;
126 if (error == 0)
127 error = hammer_load_cursor_parent(cursor, 0);
128 KKASSERT(error == 0);
129 /* if (error) hammer_done_cursor(cursor); */
130 return(error);
134 * Normalize a cursor. Sometimes cursors can be left in a state
135 * where node is NULL. If the cursor is in this state, cursor up.
137 void
138 hammer_normalize_cursor(hammer_cursor_t cursor)
140 if (cursor->node == NULL) {
141 KKASSERT(cursor->parent != NULL);
142 hammer_cursor_up(cursor);
148 * We are finished with a cursor. We NULL out various fields as sanity
149 * check, in case the structure is inappropriately used afterwords.
151 void
152 hammer_done_cursor(hammer_cursor_t cursor)
154 hammer_inode_t ip;
156 if (cursor->parent) {
157 hammer_unlock(&cursor->parent->lock);
158 hammer_rel_node(cursor->parent);
159 cursor->parent = NULL;
161 if (cursor->node) {
162 hammer_unlock(&cursor->node->lock);
163 hammer_rel_node(cursor->node);
164 cursor->node = NULL;
166 if (cursor->data_buffer) {
167 hammer_rel_buffer(cursor->data_buffer, 0);
168 cursor->data_buffer = NULL;
170 if (cursor->record_buffer) {
171 hammer_rel_buffer(cursor->record_buffer, 0);
172 cursor->record_buffer = NULL;
174 if ((ip = cursor->ip) != NULL) {
175 if (cursor->iprec) {
176 hammer_rel_mem_record(cursor->iprec);
177 cursor->iprec = NULL;
179 KKASSERT(ip->cursor_ip_refs > 0);
180 --ip->cursor_ip_refs;
181 hammer_unlock(&ip->lock);
182 cursor->ip = NULL;
186 * If we deadlocked this node will be referenced. Do a quick
187 * lock/unlock to wait for the deadlock condition to clear.
189 if (cursor->deadlk_node) {
190 hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
191 hammer_unlock(&cursor->deadlk_node->lock);
192 hammer_rel_node(cursor->deadlk_node);
193 cursor->deadlk_node = NULL;
195 if (cursor->deadlk_rec) {
196 hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
197 hammer_rel_mem_record(cursor->deadlk_rec);
198 cursor->deadlk_rec = NULL;
201 cursor->data = NULL;
202 cursor->leaf = NULL;
203 cursor->left_bound = NULL;
204 cursor->right_bound = NULL;
205 cursor->trans = NULL;
209 * Upgrade cursor->node and cursor->parent to exclusive locks. This
210 * function can return EDEADLK.
212 * The lock must already be either held shared or already held exclusively
213 * by us.
215 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
216 * we add another reference to the node that failed and set
217 * cursor->deadlk_node so hammer_done_cursor() can block on it.
220 hammer_cursor_upgrade(hammer_cursor_t cursor)
222 int error;
224 error = hammer_lock_upgrade(&cursor->node->lock);
225 if (error && cursor->deadlk_node == NULL) {
226 cursor->deadlk_node = cursor->node;
227 hammer_ref_node(cursor->deadlk_node);
228 } else if (error == 0 && cursor->parent) {
229 error = hammer_lock_upgrade(&cursor->parent->lock);
230 if (error && cursor->deadlk_node == NULL) {
231 cursor->deadlk_node = cursor->parent;
232 hammer_ref_node(cursor->deadlk_node);
235 return(error);
239 hammer_cursor_upgrade_node(hammer_cursor_t cursor)
241 int error;
243 error = hammer_lock_upgrade(&cursor->node->lock);
244 if (error && cursor->deadlk_node == NULL) {
245 cursor->deadlk_node = cursor->node;
246 hammer_ref_node(cursor->deadlk_node);
248 return(error);
252 * Downgrade cursor->node and cursor->parent to shared locks. This
253 * function can return EDEADLK.
255 void
256 hammer_cursor_downgrade(hammer_cursor_t cursor)
258 if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
259 hammer_lock_downgrade(&cursor->node->lock);
260 if (cursor->parent &&
261 hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
262 hammer_lock_downgrade(&cursor->parent->lock);
267 * Seek the cursor to the specified node and index.
269 * The caller must ref the node prior to calling this routine and release
270 * it after it returns. If the seek succeeds the cursor will gain its own
271 * ref on the node.
274 hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
276 int error;
278 hammer_cursor_downgrade(cursor);
279 error = 0;
281 if (cursor->node != node) {
282 hammer_unlock(&cursor->node->lock);
283 hammer_rel_node(cursor->node);
284 cursor->node = node;
285 hammer_ref_node(node);
286 hammer_lock_sh(&node->lock);
287 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
289 if (cursor->parent) {
290 hammer_unlock(&cursor->parent->lock);
291 hammer_rel_node(cursor->parent);
292 cursor->parent = NULL;
293 cursor->parent_index = 0;
295 error = hammer_load_cursor_parent(cursor, 0);
297 cursor->index = index;
298 return (error);
302 * Load the parent of cursor->node into cursor->parent.
304 static
306 hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive)
308 hammer_mount_t hmp;
309 hammer_node_t parent;
310 hammer_node_t node;
311 hammer_btree_elm_t elm;
312 int error;
313 int parent_index;
315 hmp = cursor->trans->hmp;
317 if (cursor->node->ondisk->parent) {
318 node = cursor->node;
319 parent = hammer_btree_get_parent(node, &parent_index,
320 &error, try_exclusive);
321 if (error == 0) {
322 elm = &parent->ondisk->elms[parent_index];
323 cursor->parent = parent;
324 cursor->parent_index = parent_index;
325 cursor->left_bound = &elm[0].internal.base;
326 cursor->right_bound = &elm[1].internal.base;
328 } else {
329 cursor->parent = NULL;
330 cursor->parent_index = 0;
331 cursor->left_bound = &hmp->root_btree_beg;
332 cursor->right_bound = &hmp->root_btree_end;
333 error = 0;
335 return(error);
339 * Cursor up to our parent node. Return ENOENT if we are at the root of
340 * the filesystem.
343 hammer_cursor_up(hammer_cursor_t cursor)
345 int error;
347 hammer_cursor_downgrade(cursor);
350 * If the parent is NULL we are at the root of the B-Tree and
351 * return ENOENT.
353 if (cursor->parent == NULL)
354 return (ENOENT);
357 * Set the node to its parent.
359 hammer_unlock(&cursor->node->lock);
360 hammer_rel_node(cursor->node);
361 cursor->node = cursor->parent;
362 cursor->index = cursor->parent_index;
363 cursor->parent = NULL;
364 cursor->parent_index = 0;
366 error = hammer_load_cursor_parent(cursor, 0);
367 return(error);
371 * Special cursor up given a locked cursor. The orignal node is not
372 * unlocked or released and the cursor is not downgraded.
374 * This function will recover from deadlocks. EDEADLK cannot be returned.
377 hammer_cursor_up_locked(hammer_cursor_t cursor)
379 hammer_node_t save;
380 int error;
383 * If the parent is NULL we are at the root of the B-Tree and
384 * return ENOENT.
386 if (cursor->parent == NULL)
387 return (ENOENT);
389 save = cursor->node;
392 * Set the node to its parent.
394 cursor->node = cursor->parent;
395 cursor->index = cursor->parent_index;
396 cursor->parent = NULL;
397 cursor->parent_index = 0;
400 * load the new parent, attempt to exclusively lock it. Note that
401 * we are still holding the old parent (now cursor->node) exclusively
402 * locked. This can return EDEADLK.
404 error = hammer_load_cursor_parent(cursor, 1);
405 if (error) {
406 cursor->parent = cursor->node;
407 cursor->parent_index = cursor->index;
408 cursor->node = save;
409 cursor->index = 0;
411 return(error);
416 * Cursor down through the current node, which must be an internal node.
418 * This routine adjusts the cursor and sets index to 0.
421 hammer_cursor_down(hammer_cursor_t cursor)
423 hammer_node_t node;
424 hammer_btree_elm_t elm;
425 int error;
428 * The current node becomes the current parent
430 hammer_cursor_downgrade(cursor);
431 node = cursor->node;
432 KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
433 if (cursor->parent) {
434 hammer_unlock(&cursor->parent->lock);
435 hammer_rel_node(cursor->parent);
437 cursor->parent = node;
438 cursor->parent_index = cursor->index;
439 cursor->node = NULL;
440 cursor->index = 0;
443 * Extract element to push into at (node,index), set bounds.
445 elm = &node->ondisk->elms[cursor->parent_index];
448 * Ok, push down into elm. If elm specifies an internal or leaf
449 * node the current node must be an internal node. If elm specifies
450 * a spike then the current node must be a leaf node.
452 switch(elm->base.btype) {
453 case HAMMER_BTREE_TYPE_INTERNAL:
454 case HAMMER_BTREE_TYPE_LEAF:
455 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
456 KKASSERT(elm->internal.subtree_offset != 0);
457 cursor->left_bound = &elm[0].internal.base;
458 cursor->right_bound = &elm[1].internal.base;
459 node = hammer_get_node(cursor->trans->hmp,
460 elm->internal.subtree_offset, 0, &error);
461 if (error == 0) {
462 KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p\n", elm->base.btype, node->ondisk->type, node));
463 if (node->ondisk->parent != cursor->parent->node_offset)
464 panic("node %p %016llx vs %016llx\n", node, node->ondisk->parent, cursor->parent->node_offset);
465 KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
467 break;
468 default:
469 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
470 elm->base.btype,
471 (elm->base.btype ? elm->base.btype : '?'));
472 break;
474 if (error == 0) {
475 hammer_lock_sh(&node->lock);
476 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
477 cursor->node = node;
478 cursor->index = 0;
480 return(error);
483 /************************************************************************
484 * DEADLOCK RECOVERY *
485 ************************************************************************
487 * These are the new deadlock recovery functions. Currently they are only
488 * used for the mirror propagation and physical node removal cases but
489 * ultimately the intention is to use them for all deadlock recovery
490 * operations.
494 * Recover from a deadlocked cursor, tracking any node removals or
495 * replacements. If the cursor's current node is removed by another
496 * thread (via btree_remove()) the cursor will be seeked upwards.
499 hammer_recover_cursor(hammer_cursor_t cursor)
501 hammer_inode_t ip;
502 hammer_node_t node;
503 int status;
504 int error;
506 again:
507 KKASSERT((cursor->flags & HAMMER_CURSOR_DEADLK_RECOVER) == 0);
508 KKASSERT(cursor->node);
510 * Release the cursor's locks and track B-Tree operations on node.
511 * While being tracked our cursor can be modified by other threads
512 * and node may be replaced.
514 if (cursor->parent) {
515 hammer_unlock(&cursor->parent->lock);
516 hammer_rel_node(cursor->parent);
517 cursor->parent = NULL;
519 node = cursor->node;
520 cursor->flags |= HAMMER_CURSOR_DEADLK_RECOVER;
521 TAILQ_INSERT_TAIL(&node->cursor_list, cursor, deadlk_entry);
522 status = hammer_lock_status(&node->lock);
523 hammer_unlock(&node->lock);
525 if ((ip = cursor->ip) != NULL)
526 hammer_unlock(&ip->lock);
529 * Wait for the deadlock to clear
531 if (cursor->deadlk_node) {
532 hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
533 hammer_unlock(&cursor->deadlk_node->lock);
534 hammer_rel_node(cursor->deadlk_node);
535 cursor->deadlk_node = NULL;
537 if (cursor->deadlk_rec) {
538 hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
539 hammer_rel_mem_record(cursor->deadlk_rec);
540 cursor->deadlk_rec = NULL;
544 * Get the cursor heated up again. The cursor's node may have
545 * changed and we might have to locate the new parent.
547 * If the exact element we were on got deleted RIPOUT will be
548 * set and we must clear ATEDISK so an iteration does not skip
549 * the element after it.
551 KKASSERT(cursor->flags & HAMMER_CURSOR_DEADLK_RECOVER);
552 node = cursor->node;
553 TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
554 cursor->flags &= ~HAMMER_CURSOR_DEADLK_RECOVER;
555 if (cursor->flags & HAMMER_CURSOR_DEADLK_RIPOUT) {
556 cursor->flags &= ~HAMMER_CURSOR_DEADLK_RIPOUT;
557 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
559 hammer_lock_sh(&node->lock);
560 error = hammer_load_cursor_parent(cursor, 0);
561 if (error == 0) {
562 if (status > 0) {
563 error = hammer_cursor_upgrade(cursor);
564 if (error == EDEADLK) {
565 kprintf("r");
566 goto again;
570 return(error);
574 * onode is being replaced by nnode by the reblocking code.
576 void
577 hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode)
579 hammer_cursor_t cursor;
581 while ((cursor = TAILQ_FIRST(&onode->cursor_list)) != NULL) {
582 TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
583 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
584 KKASSERT(cursor->node == onode);
585 cursor->node = nnode;
586 hammer_ref_node(nnode);
587 hammer_rel_node(onode);
592 * node is being removed, cursors in deadlock recovery are seeked upward
593 * to the parent.
595 void
596 hammer_cursor_removed_node(hammer_node_t node, hammer_node_t parent, int index)
598 hammer_cursor_t cursor;
600 KKASSERT(parent != NULL);
601 while ((cursor = TAILQ_FIRST(&node->cursor_list)) != NULL) {
602 KKASSERT(cursor->node == node);
603 KKASSERT(cursor->index == 0);
604 TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
605 TAILQ_INSERT_TAIL(&parent->cursor_list, cursor, deadlk_entry);
606 cursor->flags |= HAMMER_CURSOR_DEADLK_RIPOUT;
607 cursor->node = parent;
608 cursor->index = index;
609 hammer_ref_node(parent);
610 hammer_rel_node(node);
615 * node was split at (onode, index) with elements >= index moved to nnode.
617 void
618 hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode, int index)
620 hammer_cursor_t cursor;
622 again:
623 TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
624 KKASSERT(cursor->node == onode);
625 if (cursor->index < index)
626 continue;
627 TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
628 TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
629 cursor->node = nnode;
630 cursor->index -= index;
631 hammer_ref_node(nnode);
632 hammer_rel_node(onode);
633 goto again;
638 * Inserted element at (node, index)
640 * Shift indexes >= index
642 void
643 hammer_cursor_deleted_element(hammer_node_t node, int index)
645 hammer_cursor_t cursor;
647 TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
648 KKASSERT(cursor->node == node);
649 if (cursor->index == index) {
650 cursor->flags |= HAMMER_CURSOR_DEADLK_RIPOUT;
651 } else if (cursor->index > index) {
652 --cursor->index;
658 * Deleted element at (node, index)
660 * Shift indexes >= index
662 void
663 hammer_cursor_inserted_element(hammer_node_t node, int index)
665 hammer_cursor_t cursor;
667 TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
668 KKASSERT(cursor->node == node);
669 if (cursor->index >= index)
670 ++cursor->index;