| Commit | Line | Data |
|---|---|---|
| 8cd0a023 | 1 | /* |
| b84de5af | 2 | * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. |
| 8cd0a023 MD |
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: | |
| 10 | * | |
| 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. | |
| 20 | * | |
| 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. | |
| 33 | * | |
| 5c8d05e2 | 34 | * $DragonFly: src/sys/vfs/hammer/hammer_cursor.c,v 1.42 2008/08/06 15:38:58 dillon Exp $ |
| 8cd0a023 MD |
35 | */ |
| 36 | ||
| 37 | /* | |
| 38 | * HAMMER B-Tree index - cursor support routines | |
| 39 | */ | |
| 40 | #include "hammer.h" | |
| 41 | ||
| f36a9737 | 42 | static int hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive); |
| 8cd0a023 MD |
43 | |
| 44 | /* | |
| 61aeeb33 | 45 | * Initialize a fresh cursor using the B-Tree node cache. If the cache |
| 47197d71 | 46 | * is not available initialize a fresh cursor at the root of the filesystem. |
| 8cd0a023 MD |
47 | */ |
| 48 | int | |
| 36f82b23 | 49 | hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor, |
| bcac4bbb | 50 | hammer_node_cache_t cache, hammer_inode_t ip) |
| 8cd0a023 | 51 | { |
| 47197d71 | 52 | hammer_volume_t volume; |
| 61aeeb33 | 53 | hammer_node_t node; |
| 8cd0a023 MD |
54 | int error; |
| 55 | ||
| 56 | bzero(cursor, sizeof(*cursor)); | |
| 61aeeb33 | 57 | |
| 36f82b23 MD |
58 | cursor->trans = trans; |
| 59 | ||
| 61aeeb33 | 60 | /* |
| 4e17f465 MD |
61 | * If the cursor operation is on behalf of an inode, lock |
| 62 | * the inode. | |
| 63 | */ | |
| 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); | |
| 70 | } | |
| 71 | ||
| 72 | /* | |
| 61aeeb33 MD |
73 | * Step 1 - acquire a locked node from the cache if possible |
| 74 | */ | |
| bcac4bbb | 75 | if (cache && cache->node) { |
| 4c286c36 | 76 | node = hammer_ref_node_safe(trans, cache, &error); |
| 61aeeb33 | 77 | if (error == 0) { |
| 98da6d8c | 78 | hammer_lock_sh(&node->lock); |
| 61aeeb33 MD |
79 | if (node->flags & HAMMER_NODE_DELETED) { |
| 80 | hammer_unlock(&node->lock); | |
| 81 | hammer_rel_node(node); | |
| 82 | node = NULL; | |
| 83 | } | |
| 61aeeb33 | 84 | } |
| 39d8fd63 MD |
85 | if (node == NULL) |
| 86 | ++hammer_stats_btree_root_iterations; | |
| 61aeeb33 MD |
87 | } else { |
| 88 | node = NULL; | |
| 39d8fd63 | 89 | ++hammer_stats_btree_root_iterations; |
| 61aeeb33 MD |
90 | } |
| 91 | ||
| 92 | /* | |
| 93 | * Step 2 - If we couldn't get a node from the cache, get | |
| 47197d71 | 94 | * the one from the root of the filesystem. |
| 61aeeb33 MD |
95 | */ |
| 96 | while (node == NULL) { | |
| 36f82b23 | 97 | volume = hammer_get_root_volume(trans->hmp, &error); |
| 61aeeb33 MD |
98 | if (error) |
| 99 | break; | |
| 82010f9f | 100 | node = hammer_get_node(trans, volume->ondisk->vol0_btree_root, |
| 19619882 | 101 | 0, &error); |
| 47197d71 | 102 | hammer_rel_volume(volume, 0); |
| 61aeeb33 MD |
103 | if (error) |
| 104 | break; | |
| 98da6d8c | 105 | hammer_lock_sh(&node->lock); |
| 11ad5ade MD |
106 | |
| 107 | /* | |
| 108 | * If someone got in before we could lock the node, retry. | |
| 109 | */ | |
| 61aeeb33 MD |
110 | if (node->flags & HAMMER_NODE_DELETED) { |
| 111 | hammer_unlock(&node->lock); | |
| 112 | hammer_rel_node(node); | |
| 113 | node = NULL; | |
| 11ad5ade MD |
114 | continue; |
| 115 | } | |
| 116 | if (volume->ondisk->vol0_btree_root != node->node_offset) { | |
| 117 | hammer_unlock(&node->lock); | |
| 118 | hammer_rel_node(node); | |
| 119 | node = NULL; | |
| 120 | continue; | |
| 61aeeb33 | 121 | } |
| 8cd0a023 | 122 | } |
| 61aeeb33 MD |
123 | |
| 124 | /* | |
| 125 | * Step 3 - finish initializing the cursor by acquiring the parent | |
| 126 | */ | |
| 127 | cursor->node = node; | |
| 8cd0a023 | 128 | if (error == 0) |
| f36a9737 | 129 | error = hammer_load_cursor_parent(cursor, 0); |
| 0b075555 | 130 | KKASSERT(error == 0); |
| a84a197d | 131 | /* if (error) hammer_done_cursor(cursor); */ |
| 8cd0a023 MD |
132 | return(error); |
| 133 | } | |
| 134 | ||
| 4e17f465 MD |
135 | /* |
| 136 | * Normalize a cursor. Sometimes cursors can be left in a state | |
| 137 | * where node is NULL. If the cursor is in this state, cursor up. | |
| 138 | */ | |
| 139 | void | |
| 140 | hammer_normalize_cursor(hammer_cursor_t cursor) | |
| 141 | { | |
| 142 | if (cursor->node == NULL) { | |
| 143 | KKASSERT(cursor->parent != NULL); | |
| 144 | hammer_cursor_up(cursor); | |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | ||
| 8cd0a023 | 149 | /* |
| 8cd0a023 MD |
150 | * We are finished with a cursor. We NULL out various fields as sanity |
| 151 | * check, in case the structure is inappropriately used afterwords. | |
| 152 | */ | |
| 153 | void | |
| 154 | hammer_done_cursor(hammer_cursor_t cursor) | |
| 155 | { | |
| 4e17f465 MD |
156 | hammer_inode_t ip; |
| 157 | ||
| adf01747 | 158 | KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0); |
| 8cd0a023 MD |
159 | if (cursor->parent) { |
| 160 | hammer_unlock(&cursor->parent->lock); | |
| 161 | hammer_rel_node(cursor->parent); | |
| 162 | cursor->parent = NULL; | |
| 163 | } | |
| 164 | if (cursor->node) { | |
| 165 | hammer_unlock(&cursor->node->lock); | |
| 166 | hammer_rel_node(cursor->node); | |
| 167 | cursor->node = NULL; | |
| 168 | } | |
| 169 | if (cursor->data_buffer) { | |
| 170 | hammer_rel_buffer(cursor->data_buffer, 0); | |
| 171 | cursor->data_buffer = NULL; | |
| 172 | } | |
| 4e17f465 | 173 | if ((ip = cursor->ip) != NULL) { |
| 4e17f465 MD |
174 | KKASSERT(ip->cursor_ip_refs > 0); |
| 175 | --ip->cursor_ip_refs; | |
| 176 | hammer_unlock(&ip->lock); | |
| 177 | cursor->ip = NULL; | |
| 178 | } | |
| 3f43fb33 MD |
179 | if (cursor->iprec) { |
| 180 | hammer_rel_mem_record(cursor->iprec); | |
| 181 | cursor->iprec = NULL; | |
| 182 | } | |
| 4e17f465 | 183 | |
| 6a37e7e4 MD |
184 | /* |
| 185 | * If we deadlocked this node will be referenced. Do a quick | |
| 186 | * lock/unlock to wait for the deadlock condition to clear. | |
| 187 | */ | |
| 188 | if (cursor->deadlk_node) { | |
| af209b0f | 189 | hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk"); |
| 6a37e7e4 MD |
190 | hammer_unlock(&cursor->deadlk_node->lock); |
| 191 | hammer_rel_node(cursor->deadlk_node); | |
| 192 | cursor->deadlk_node = NULL; | |
| 193 | } | |
| b84de5af | 194 | if (cursor->deadlk_rec) { |
| a99b9ea2 | 195 | hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr"); |
| b84de5af MD |
196 | hammer_rel_mem_record(cursor->deadlk_rec); |
| 197 | cursor->deadlk_rec = NULL; | |
| 198 | } | |
| 6a37e7e4 | 199 | |
| 40043e7f | 200 | cursor->data = NULL; |
| 11ad5ade | 201 | cursor->leaf = NULL; |
| 8cd0a023 MD |
202 | cursor->left_bound = NULL; |
| 203 | cursor->right_bound = NULL; | |
| 36f82b23 | 204 | cursor->trans = NULL; |
| 8cd0a023 MD |
205 | } |
| 206 | ||
| 207 | /* | |
| 6a37e7e4 MD |
208 | * Upgrade cursor->node and cursor->parent to exclusive locks. This |
| 209 | * function can return EDEADLK. | |
| 210 | * | |
| 7aa3b8a6 MD |
211 | * The lock must already be either held shared or already held exclusively |
| 212 | * by us. | |
| 213 | * | |
| 6a37e7e4 MD |
214 | * If we fail to upgrade the lock and cursor->deadlk_node is NULL, |
| 215 | * we add another reference to the node that failed and set | |
| 216 | * cursor->deadlk_node so hammer_done_cursor() can block on it. | |
| 217 | */ | |
| 218 | int | |
| 219 | hammer_cursor_upgrade(hammer_cursor_t cursor) | |
| 220 | { | |
| 221 | int error; | |
| 222 | ||
| 7aa3b8a6 MD |
223 | error = hammer_lock_upgrade(&cursor->node->lock); |
| 224 | if (error && cursor->deadlk_node == NULL) { | |
| 225 | cursor->deadlk_node = cursor->node; | |
| 226 | hammer_ref_node(cursor->deadlk_node); | |
| 227 | } else if (error == 0 && cursor->parent) { | |
| 6a37e7e4 MD |
228 | error = hammer_lock_upgrade(&cursor->parent->lock); |
| 229 | if (error && cursor->deadlk_node == NULL) { | |
| 230 | cursor->deadlk_node = cursor->parent; | |
| 231 | hammer_ref_node(cursor->deadlk_node); | |
| 232 | } | |
| 6a37e7e4 MD |
233 | } |
| 234 | return(error); | |
| 235 | } | |
| 236 | ||
| 7bc5b8c2 MD |
237 | int |
| 238 | hammer_cursor_upgrade_node(hammer_cursor_t cursor) | |
| 239 | { | |
| 240 | int error; | |
| 241 | ||
| 242 | error = hammer_lock_upgrade(&cursor->node->lock); | |
| 243 | if (error && cursor->deadlk_node == NULL) { | |
| 244 | cursor->deadlk_node = cursor->node; | |
| 245 | hammer_ref_node(cursor->deadlk_node); | |
| 246 | } | |
| 247 | return(error); | |
| 248 | } | |
| 249 | ||
| 6a37e7e4 MD |
250 | /* |
| 251 | * Downgrade cursor->node and cursor->parent to shared locks. This | |
| 252 | * function can return EDEADLK. | |
| 253 | */ | |
| 254 | void | |
| 255 | hammer_cursor_downgrade(hammer_cursor_t cursor) | |
| 256 | { | |
| 7aa3b8a6 | 257 | if (hammer_lock_excl_owned(&cursor->node->lock, curthread)) |
| 6a37e7e4 | 258 | hammer_lock_downgrade(&cursor->node->lock); |
| 7aa3b8a6 MD |
259 | if (cursor->parent && |
| 260 | hammer_lock_excl_owned(&cursor->parent->lock, curthread)) { | |
| 6a37e7e4 | 261 | hammer_lock_downgrade(&cursor->parent->lock); |
| 7aa3b8a6 | 262 | } |
| 6a37e7e4 MD |
263 | } |
| 264 | ||
| 32c90105 MD |
265 | /* |
| 266 | * Seek the cursor to the specified node and index. | |
| cb51be26 MD |
267 | * |
| 268 | * The caller must ref the node prior to calling this routine and release | |
| 269 | * it after it returns. If the seek succeeds the cursor will gain its own | |
| 270 | * ref on the node. | |
| 32c90105 MD |
271 | */ |
| 272 | int | |
| 273 | hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index) | |
| 274 | { | |
| 275 | int error; | |
| 276 | ||
| 277 | hammer_cursor_downgrade(cursor); | |
| 278 | error = 0; | |
| 279 | ||
| 280 | if (cursor->node != node) { | |
| 281 | hammer_unlock(&cursor->node->lock); | |
| 282 | hammer_rel_node(cursor->node); | |
| 283 | cursor->node = node; | |
| 32c90105 MD |
284 | hammer_ref_node(node); |
| 285 | hammer_lock_sh(&node->lock); | |
| f36a9737 | 286 | KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0); |
| 32c90105 MD |
287 | |
| 288 | if (cursor->parent) { | |
| 289 | hammer_unlock(&cursor->parent->lock); | |
| 290 | hammer_rel_node(cursor->parent); | |
| 291 | cursor->parent = NULL; | |
| 292 | cursor->parent_index = 0; | |
| 293 | } | |
| f36a9737 | 294 | error = hammer_load_cursor_parent(cursor, 0); |
| 32c90105 | 295 | } |
| a84a197d | 296 | cursor->index = index; |
| 32c90105 MD |
297 | return (error); |
| 298 | } | |
| 6a37e7e4 | 299 | |
| 6a37e7e4 | 300 | /* |
| 47197d71 | 301 | * Load the parent of cursor->node into cursor->parent. |
| 8cd0a023 MD |
302 | */ |
| 303 | static | |
| 304 | int | |
| f36a9737 | 305 | hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive) |
| 8cd0a023 | 306 | { |
| 47197d71 MD |
307 | hammer_mount_t hmp; |
| 308 | hammer_node_t parent; | |
| 309 | hammer_node_t node; | |
| 310 | hammer_btree_elm_t elm; | |
| 8cd0a023 | 311 | int error; |
| c82af904 | 312 | int parent_index; |
| 8cd0a023 | 313 | |
| 36f82b23 | 314 | hmp = cursor->trans->hmp; |
| 8cd0a023 MD |
315 | |
| 316 | if (cursor->node->ondisk->parent) { | |
| 47197d71 | 317 | node = cursor->node; |
| 82010f9f MD |
318 | parent = hammer_btree_get_parent(cursor->trans, node, |
| 319 | &parent_index, | |
| c82af904 MD |
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; | |
| a84a197d | 327 | } |
| 8cd0a023 MD |
328 | } else { |
| 329 | cursor->parent = NULL; | |
| 330 | cursor->parent_index = 0; | |
| 47197d71 MD |
331 | cursor->left_bound = &hmp->root_btree_beg; |
| 332 | cursor->right_bound = &hmp->root_btree_end; | |
| 8cd0a023 MD |
333 | error = 0; |
| 334 | } | |
| 335 | return(error); | |
| 336 | } | |
| 337 | ||
| 8cd0a023 MD |
338 | /* |
| 339 | * Cursor up to our parent node. Return ENOENT if we are at the root of | |
| 47197d71 | 340 | * the filesystem. |
| 8cd0a023 MD |
341 | */ |
| 342 | int | |
| 6a37e7e4 | 343 | hammer_cursor_up(hammer_cursor_t cursor) |
| 8cd0a023 MD |
344 | { |
| 345 | int error; | |
| 346 | ||
| 6a37e7e4 | 347 | hammer_cursor_downgrade(cursor); |
| 195c19a1 MD |
348 | |
| 349 | /* | |
| 4e17f465 MD |
350 | * If the parent is NULL we are at the root of the B-Tree and |
| 351 | * return ENOENT. | |
| 352 | */ | |
| 353 | if (cursor->parent == NULL) | |
| 354 | return (ENOENT); | |
| 355 | ||
| 356 | /* | |
| 357 | * Set the node to its parent. | |
| 8cd0a023 MD |
358 | */ |
| 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; | |
| 365 | ||
| f36a9737 | 366 | error = hammer_load_cursor_parent(cursor, 0); |
| 8cd0a023 MD |
367 | return(error); |
| 368 | } | |
| 369 | ||
| 370 | /* | |
| f36a9737 | 371 | * Special cursor up given a locked cursor. The orignal node is not |
| b3bad96f MD |
372 | * unlocked or released and the cursor is not downgraded. |
| 373 | * | |
| 374 | * This function will recover from deadlocks. EDEADLK cannot be returned. | |
| f36a9737 MD |
375 | */ |
| 376 | int | |
| 377 | hammer_cursor_up_locked(hammer_cursor_t cursor) | |
| 378 | { | |
| 379 | hammer_node_t save; | |
| 380 | int error; | |
| 381 | ||
| 382 | /* | |
| 383 | * If the parent is NULL we are at the root of the B-Tree and | |
| 384 | * return ENOENT. | |
| 385 | */ | |
| 386 | if (cursor->parent == NULL) | |
| 387 | return (ENOENT); | |
| 388 | ||
| 389 | save = cursor->node; | |
| 390 | ||
| 391 | /* | |
| 392 | * Set the node to its parent. | |
| 393 | */ | |
| 394 | cursor->node = cursor->parent; | |
| 395 | cursor->index = cursor->parent_index; | |
| 396 | cursor->parent = NULL; | |
| 397 | cursor->parent_index = 0; | |
| 398 | ||
| 399 | /* | |
| 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. | |
| 403 | */ | |
| 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; | |
| 410 | } | |
| 411 | return(error); | |
| 412 | } | |
| 413 | ||
| 414 | ||
| 415 | /* | |
| 8cd0a023 MD |
416 | * Cursor down through the current node, which must be an internal node. |
| 417 | * | |
| 418 | * This routine adjusts the cursor and sets index to 0. | |
| 419 | */ | |
| 420 | int | |
| 421 | hammer_cursor_down(hammer_cursor_t cursor) | |
| 422 | { | |
| 423 | hammer_node_t node; | |
| 424 | hammer_btree_elm_t elm; | |
| 8cd0a023 MD |
425 | int error; |
| 426 | ||
| 427 | /* | |
| 428 | * The current node becomes the current parent | |
| 429 | */ | |
| 6a37e7e4 | 430 | hammer_cursor_downgrade(cursor); |
| 8cd0a023 | 431 | node = cursor->node; |
| 8cd0a023 MD |
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); | |
| 436 | } | |
| 437 | cursor->parent = node; | |
| 438 | cursor->parent_index = cursor->index; | |
| 439 | cursor->node = NULL; | |
| 440 | cursor->index = 0; | |
| 441 | ||
| 442 | /* | |
| 76376933 | 443 | * Extract element to push into at (node,index), set bounds. |
| 8cd0a023 MD |
444 | */ |
| 445 | elm = &node->ondisk->elms[cursor->parent_index]; | |
| 446 | ||
| 447 | /* | |
| fe7678ee MD |
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. | |
| 8cd0a023 | 451 | */ |
| fe7678ee MD |
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; | |
| 82010f9f | 459 | node = hammer_get_node(cursor->trans, |
| 19619882 | 460 | elm->internal.subtree_offset, 0, &error); |
| fe7678ee | 461 | if (error == 0) { |
| 40043e7f | 462 | KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p\n", elm->base.btype, node->ondisk->type, node)); |
| b33e2cc0 | 463 | if (node->ondisk->parent != cursor->parent->node_offset) |
| 973c11b9 | 464 | panic("node %p %016llx vs %016llx\n", node, (long long)node->ondisk->parent, (long long)cursor->parent->node_offset); |
| fe7678ee MD |
465 | KKASSERT(node->ondisk->parent == cursor->parent->node_offset); |
| 466 | } | |
| 467 | break; | |
| fe7678ee MD |
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; | |
| 8cd0a023 MD |
473 | } |
| 474 | if (error == 0) { | |
| 6a37e7e4 | 475 | hammer_lock_sh(&node->lock); |
| f36a9737 | 476 | KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0); |
| 8cd0a023 MD |
477 | cursor->node = node; |
| 478 | cursor->index = 0; | |
| 479 | } | |
| 480 | return(error); | |
| 481 | } | |
| 482 | ||
| b3bad96f MD |
483 | /************************************************************************ |
| 484 | * DEADLOCK RECOVERY * | |
| 485 | ************************************************************************ | |
| 486 | * | |
| 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. | |
| 491 | */ | |
| adf01747 | 492 | void |
| 982be4bf | 493 | hammer_unlock_cursor(hammer_cursor_t cursor) |
| b3bad96f | 494 | { |
| b3bad96f | 495 | hammer_node_t node; |
| b3bad96f | 496 | |
| adf01747 | 497 | KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0); |
| b3bad96f | 498 | KKASSERT(cursor->node); |
| 19168d41 MD |
499 | |
| 500 | /* | |
| b3bad96f MD |
501 | * Release the cursor's locks and track B-Tree operations on node. |
| 502 | * While being tracked our cursor can be modified by other threads | |
| 5c8d05e2 | 503 | * and the node may be replaced. |
| b3bad96f MD |
504 | */ |
| 505 | if (cursor->parent) { | |
| 506 | hammer_unlock(&cursor->parent->lock); | |
| 507 | hammer_rel_node(cursor->parent); | |
| 508 | cursor->parent = NULL; | |
| 509 | } | |
| 510 | node = cursor->node; | |
| adf01747 | 511 | cursor->flags |= HAMMER_CURSOR_TRACKED; |
| b3bad96f | 512 | TAILQ_INSERT_TAIL(&node->cursor_list, cursor, deadlk_entry); |
| b3bad96f | 513 | hammer_unlock(&node->lock); |
| adf01747 MD |
514 | } |
| 515 | ||
| 516 | /* | |
| 517 | * Get the cursor heated up again. The cursor's node may have | |
| 518 | * changed and we might have to locate the new parent. | |
| 519 | * | |
| 520 | * If the exact element we were on got deleted RIPOUT will be | |
| 521 | * set and we must clear ATEDISK so an iteration does not skip | |
| 522 | * the element after it. | |
| 523 | */ | |
| 524 | int | |
| 982be4bf | 525 | hammer_lock_cursor(hammer_cursor_t cursor) |
| adf01747 | 526 | { |
| adf01747 MD |
527 | hammer_node_t node; |
| 528 | int error; | |
| 529 | ||
| 530 | KKASSERT(cursor->flags & HAMMER_CURSOR_TRACKED); | |
| 531 | ||
| 532 | /* | |
| adf01747 MD |
533 | * Relock the node |
| 534 | */ | |
| 535 | for (;;) { | |
| 536 | node = cursor->node; | |
| 537 | hammer_ref_node(node); | |
| 538 | hammer_lock_sh(&node->lock); | |
| 539 | if (cursor->node == node) { | |
| 540 | hammer_rel_node(node); | |
| 541 | break; | |
| 542 | } | |
| 543 | hammer_unlock(&node->lock); | |
| 544 | hammer_rel_node(node); | |
| 545 | } | |
| 546 | ||
| 547 | /* | |
| 548 | * Untrack the cursor, clean up, and re-establish the parent node. | |
| 549 | */ | |
| 550 | TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry); | |
| 551 | cursor->flags &= ~HAMMER_CURSOR_TRACKED; | |
| 552 | ||
| 5c8d05e2 MD |
553 | /* |
| 554 | * If a ripout has occured iterations must re-test the (new) | |
| 555 | * current element. Clearing ATEDISK prevents the element from | |
| 556 | * being skipped and RETEST causes it to be re-tested. | |
| 557 | */ | |
| adf01747 MD |
558 | if (cursor->flags & HAMMER_CURSOR_TRACKED_RIPOUT) { |
| 559 | cursor->flags &= ~HAMMER_CURSOR_TRACKED_RIPOUT; | |
| 560 | cursor->flags &= ~HAMMER_CURSOR_ATEDISK; | |
| 5c8d05e2 | 561 | cursor->flags |= HAMMER_CURSOR_RETEST; |
| adf01747 MD |
562 | } |
| 563 | error = hammer_load_cursor_parent(cursor, 0); | |
| 564 | return(error); | |
| 565 | } | |
| 566 | ||
| 567 | /* | |
| 568 | * Recover from a deadlocked cursor, tracking any node removals or | |
| 569 | * replacements. If the cursor's current node is removed by another | |
| 570 | * thread (via btree_remove()) the cursor will be seeked upwards. | |
| 98da6d8c MD |
571 | * |
| 572 | * The caller is working a modifying operation and must be holding the | |
| 573 | * sync lock (shared). We do not release the sync lock because this | |
| 574 | * would break atomicy. | |
| adf01747 MD |
575 | */ |
| 576 | int | |
| 577 | hammer_recover_cursor(hammer_cursor_t cursor) | |
| 578 | { | |
| 579 | int error; | |
| 580 | ||
| 982be4bf | 581 | hammer_unlock_cursor(cursor); |
| 98da6d8c | 582 | KKASSERT(cursor->trans->sync_lock_refs > 0); |
| b3bad96f MD |
583 | |
| 584 | /* | |
| 585 | * Wait for the deadlock to clear | |
| 586 | */ | |
| 587 | if (cursor->deadlk_node) { | |
| 588 | hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk"); | |
| 589 | hammer_unlock(&cursor->deadlk_node->lock); | |
| 590 | hammer_rel_node(cursor->deadlk_node); | |
| 591 | cursor->deadlk_node = NULL; | |
| 592 | } | |
| 593 | if (cursor->deadlk_rec) { | |
| 594 | hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr"); | |
| 595 | hammer_rel_mem_record(cursor->deadlk_rec); | |
| 596 | cursor->deadlk_rec = NULL; | |
| 597 | } | |
| 982be4bf | 598 | error = hammer_lock_cursor(cursor); |
| adf01747 MD |
599 | return(error); |
| 600 | } | |
| 601 | ||
| 602 | /* | |
| 603 | * Dup ocursor to ncursor. ncursor inherits ocursor's locks and ocursor | |
| 604 | * is effectively unlocked and becomes tracked. If ocursor was not locked | |
| 605 | * then ncursor also inherits the tracking. | |
| 606 | * | |
| 607 | * After the caller finishes working with ncursor it must be cleaned up | |
| 608 | * with hammer_done_cursor(), and the caller must re-lock ocursor. | |
| 609 | */ | |
| 3f43fb33 MD |
610 | hammer_cursor_t |
| 611 | hammer_push_cursor(hammer_cursor_t ocursor) | |
| adf01747 | 612 | { |
| 3f43fb33 | 613 | hammer_cursor_t ncursor; |
| adf01747 MD |
614 | hammer_inode_t ip; |
| 615 | hammer_node_t node; | |
| bac808fe | 616 | hammer_mount_t hmp; |
| adf01747 | 617 | |
| bac808fe MD |
618 | hmp = ocursor->trans->hmp; |
| 619 | ncursor = kmalloc(sizeof(*ncursor), hmp->m_misc, M_WAITOK | M_ZERO); | |
| adf01747 MD |
620 | bcopy(ocursor, ncursor, sizeof(*ocursor)); |
| 621 | ||
| 622 | node = ocursor->node; | |
| 623 | hammer_ref_node(node); | |
| 624 | if ((ocursor->flags & HAMMER_CURSOR_TRACKED) == 0) { | |
| 625 | ocursor->flags |= HAMMER_CURSOR_TRACKED; | |
| 626 | TAILQ_INSERT_TAIL(&node->cursor_list, ocursor, deadlk_entry); | |
| b3bad96f | 627 | } |
| adf01747 MD |
628 | if (ncursor->parent) |
| 629 | ocursor->parent = NULL; | |
| 630 | ocursor->data_buffer = NULL; | |
| 631 | ocursor->leaf = NULL; | |
| 632 | ocursor->data = NULL; | |
| 633 | if (ncursor->flags & HAMMER_CURSOR_TRACKED) | |
| 634 | TAILQ_INSERT_TAIL(&node->cursor_list, ncursor, deadlk_entry); | |
| 635 | if ((ip = ncursor->ip) != NULL) { | |
| 636 | ++ip->cursor_ip_refs; | |
| b3bad96f | 637 | } |
| adf01747 MD |
638 | if (ncursor->iprec) |
| 639 | hammer_ref(&ncursor->iprec->lock); | |
| 3f43fb33 MD |
640 | return(ncursor); |
| 641 | } | |
| 642 | ||
| 643 | /* | |
| 644 | * Destroy ncursor and restore ocursor | |
| 645 | * | |
| 646 | * This is a temporary hack for the release. We can't afford to lose | |
| 647 | * the IP lock until the IP object scan code is able to deal with it, | |
| 648 | * so have ocursor inherit it back. | |
| 649 | */ | |
| 650 | void | |
| 651 | hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor) | |
| 652 | { | |
| bac808fe | 653 | hammer_mount_t hmp; |
| 3f43fb33 MD |
654 | hammer_inode_t ip; |
| 655 | ||
| bac808fe | 656 | hmp = ncursor->trans->hmp; |
| 3f43fb33 MD |
657 | ip = ncursor->ip; |
| 658 | ncursor->ip = NULL; | |
| 659 | if (ip) | |
| 660 | --ip->cursor_ip_refs; | |
| 661 | hammer_done_cursor(ncursor); | |
| bac808fe | 662 | kfree(ncursor, hmp->m_misc); |
| 3f43fb33 | 663 | KKASSERT(ocursor->ip == ip); |
| 982be4bf | 664 | hammer_lock_cursor(ocursor); |
| b3bad96f MD |
665 | } |
| 666 | ||
| 667 | /* | |
| 668 | * onode is being replaced by nnode by the reblocking code. | |
| 669 | */ | |
| 670 | void | |
| 671 | hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode) | |
| 672 | { | |
| 673 | hammer_cursor_t cursor; | |
| 674 | ||
| 675 | while ((cursor = TAILQ_FIRST(&onode->cursor_list)) != NULL) { | |
| 676 | TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry); | |
| 677 | TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry); | |
| 678 | KKASSERT(cursor->node == onode); | |
| 679 | cursor->node = nnode; | |
| 680 | hammer_ref_node(nnode); | |
| 681 | hammer_rel_node(onode); | |
| 682 | } | |
| 683 | } | |
| 684 | ||
| 685 | /* | |
| 686 | * node is being removed, cursors in deadlock recovery are seeked upward | |
| 687 | * to the parent. | |
| 688 | */ | |
| 689 | void | |
| 690 | hammer_cursor_removed_node(hammer_node_t node, hammer_node_t parent, int index) | |
| 691 | { | |
| 692 | hammer_cursor_t cursor; | |
| 693 | ||
| 694 | KKASSERT(parent != NULL); | |
| 695 | while ((cursor = TAILQ_FIRST(&node->cursor_list)) != NULL) { | |
| 696 | KKASSERT(cursor->node == node); | |
| 697 | KKASSERT(cursor->index == 0); | |
| 698 | TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry); | |
| 699 | TAILQ_INSERT_TAIL(&parent->cursor_list, cursor, deadlk_entry); | |
| adf01747 | 700 | cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT; |
| b3bad96f MD |
701 | cursor->node = parent; |
| 702 | cursor->index = index; | |
| 703 | hammer_ref_node(parent); | |
| 704 | hammer_rel_node(node); | |
| 705 | } | |
| 706 | } | |
| 707 | ||
| 708 | /* | |
| 709 | * node was split at (onode, index) with elements >= index moved to nnode. | |
| 710 | */ | |
| 711 | void | |
| 712 | hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode, int index) | |
| 713 | { | |
| 714 | hammer_cursor_t cursor; | |
| 715 | ||
| 716 | again: | |
| 717 | TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) { | |
| 718 | KKASSERT(cursor->node == onode); | |
| 719 | if (cursor->index < index) | |
| 720 | continue; | |
| 721 | TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry); | |
| 722 | TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry); | |
| 723 | cursor->node = nnode; | |
| 724 | cursor->index -= index; | |
| 725 | hammer_ref_node(nnode); | |
| 726 | hammer_rel_node(onode); | |
| 727 | goto again; | |
| 728 | } | |
| 729 | } | |
| 730 | ||
| 731 | /* | |
| 1775b6a0 MD |
732 | * An element was moved from one node to another or within a node. The |
| 733 | * index may also represent the end of the node (index == numelements). | |
| 734 | * | |
| 735 | * This is used by the rebalancing code. This is not an insertion or | |
| 736 | * deletion and any additional elements, including the degenerate case at | |
| 737 | * the end of the node, will be dealt with by additional distinct calls. | |
| 738 | */ | |
| 739 | void | |
| 740 | hammer_cursor_moved_element(hammer_node_t onode, hammer_node_t nnode, | |
| 741 | int oindex, int nindex) | |
| 742 | { | |
| 743 | hammer_cursor_t cursor; | |
| 744 | ||
| 745 | again: | |
| 746 | TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) { | |
| 747 | KKASSERT(cursor->node == onode); | |
| 748 | if (cursor->index != oindex) | |
| 749 | continue; | |
| 750 | TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry); | |
| 751 | TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry); | |
| 752 | cursor->node = nnode; | |
| 753 | cursor->index = nindex; | |
| 754 | hammer_ref_node(nnode); | |
| 755 | hammer_rel_node(onode); | |
| 756 | goto again; | |
| 757 | } | |
| 758 | } | |
| 759 | ||
| 760 | /* | |
| 761 | * The B-Tree element pointing to the specified node was moved from (oparent) | |
| 762 | * to (nparent, nindex). We must locate any tracked cursors pointing at | |
| 763 | * node and adjust their parent accordingly. | |
| 764 | * | |
| 765 | * This is used by the rebalancing code when packing elements causes an | |
| 766 | * element to shift from one node to another. | |
| 767 | */ | |
| 768 | void | |
| 769 | hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent, | |
| 770 | hammer_node_t nparent, int nindex) | |
| 771 | { | |
| 772 | hammer_cursor_t cursor; | |
| 773 | ||
| 774 | again: | |
| 775 | TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) { | |
| 776 | KKASSERT(cursor->node == node); | |
| 777 | if (cursor->parent == oparent) { | |
| 778 | cursor->parent = nparent; | |
| 779 | cursor->parent_index = nindex; | |
| 780 | hammer_ref_node(nparent); | |
| 781 | hammer_rel_node(oparent); | |
| 782 | goto again; | |
| 783 | } | |
| 784 | } | |
| 785 | } | |
| 786 | ||
| 787 | /* | |
| e4a5ff06 | 788 | * Deleted element at (node, index) |
| b3bad96f MD |
789 | * |
| 790 | * Shift indexes >= index | |
| 791 | */ | |
| 792 | void | |
| 793 | hammer_cursor_deleted_element(hammer_node_t node, int index) | |
| 794 | { | |
| 795 | hammer_cursor_t cursor; | |
| 796 | ||
| 797 | TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) { | |
| 798 | KKASSERT(cursor->node == node); | |
| 799 | if (cursor->index == index) { | |
| adf01747 | 800 | cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT; |
| b3bad96f MD |
801 | } else if (cursor->index > index) { |
| 802 | --cursor->index; | |
| 803 | } | |
| 804 | } | |
| 805 | } | |
| 806 | ||
| 807 | /* | |
| e4a5ff06 | 808 | * Inserted element at (node, index) |
| b3bad96f MD |
809 | * |
| 810 | * Shift indexes >= index | |
| 811 | */ | |
| 812 | void | |
| 813 | hammer_cursor_inserted_element(hammer_node_t node, int index) | |
| 814 | { | |
| 815 | hammer_cursor_t cursor; | |
| 816 | ||
| 817 | TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) { | |
| 818 | KKASSERT(cursor->node == node); | |
| 819 | if (cursor->index >= index) | |
| 820 | ++cursor->index; | |
| 821 | } | |
| 822 | } | |
| 823 |