| 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: |
| 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 | * |
| 34 | * $DragonFly: src/sys/vfs/hammer/hammer_btree.c,v 1.61 2008/07/02 21:57:54 dillon Exp $ |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * HAMMER B-Tree index |
| 39 | * |
| 40 | * HAMMER implements a modified B+Tree. In documentation this will |
| 41 | * simply be refered to as the HAMMER B-Tree. Basically a HAMMER B-Tree |
| 42 | * looks like a B+Tree (A B-Tree which stores its records only at the leafs |
| 43 | * of the tree), but adds two additional boundary elements which describe |
| 44 | * the left-most and right-most element a node is able to represent. In |
| 45 | * otherwords, we have boundary elements at the two ends of a B-Tree node |
| 46 | * instead of sub-tree pointers. |
| 47 | * |
| 48 | * A B-Tree internal node looks like this: |
| 49 | * |
| 50 | * B N N N N N N B <-- boundary and internal elements |
| 51 | * S S S S S S S <-- subtree pointers |
| 52 | * |
| 53 | * A B-Tree leaf node basically looks like this: |
| 54 | * |
| 55 | * L L L L L L L L <-- leaf elemenets |
| 56 | * |
| 57 | * The radix for an internal node is 1 less then a leaf but we get a |
| 58 | * number of significant benefits for our troubles. |
| 59 | * |
| 60 | * The big benefit to using a B-Tree containing boundary information |
| 61 | * is that it is possible to cache pointers into the middle of the tree |
| 62 | * and not have to start searches, insertions, OR deletions at the root |
| 63 | * node. In particular, searches are able to progress in a definitive |
| 64 | * direction from any point in the tree without revisting nodes. This |
| 65 | * greatly improves the efficiency of many operations, most especially |
| 66 | * record appends. |
| 67 | * |
| 68 | * B-Trees also make the stacking of trees fairly straightforward. |
| 69 | * |
| 70 | * INSERTIONS: A search performed with the intention of doing |
| 71 | * an insert will guarantee that the terminal leaf node is not full by |
| 72 | * splitting full nodes. Splits occur top-down during the dive down the |
| 73 | * B-Tree. |
| 74 | * |
| 75 | * DELETIONS: A deletion makes no attempt to proactively balance the |
| 76 | * tree and will recursively remove nodes that become empty. If a |
| 77 | * deadlock occurs a deletion may not be able to remove an empty leaf. |
| 78 | * Deletions never allow internal nodes to become empty (that would blow |
| 79 | * up the boundaries). |
| 80 | */ |
| 81 | #include "hammer.h" |
| 82 | #include <sys/buf.h> |
| 83 | #include <sys/buf2.h> |
| 84 | |
| 85 | static int btree_search(hammer_cursor_t cursor, int flags); |
| 86 | static int btree_split_internal(hammer_cursor_t cursor); |
| 87 | static int btree_split_leaf(hammer_cursor_t cursor); |
| 88 | static int btree_remove(hammer_cursor_t cursor); |
| 89 | static int btree_node_is_full(hammer_node_ondisk_t node); |
| 90 | static void hammer_make_separator(hammer_base_elm_t key1, |
| 91 | hammer_base_elm_t key2, hammer_base_elm_t dest); |
| 92 | |
| 93 | static __inline |
| 94 | int |
| 95 | hammer_do_mirror_propagate(hammer_cursor_t cursor) |
| 96 | { |
| 97 | return(1); |
| 98 | /* |
| 99 | (cursor->trans->hmp->hflags & (HMNT_MASTERID|HMNT_SLAVE))) |
| 100 | */ |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Iterate records after a search. The cursor is iterated forwards past |
| 105 | * the current record until a record matching the key-range requirements |
| 106 | * is found. ENOENT is returned if the iteration goes past the ending |
| 107 | * key. |
| 108 | * |
| 109 | * The iteration is inclusive of key_beg and can be inclusive or exclusive |
| 110 | * of key_end depending on whether HAMMER_CURSOR_END_INCLUSIVE is set. |
| 111 | * |
| 112 | * When doing an as-of search (cursor->asof != 0), key_beg.create_tid |
| 113 | * may be modified by B-Tree functions. |
| 114 | * |
| 115 | * cursor->key_beg may or may not be modified by this function during |
| 116 | * the iteration. XXX future - in case of an inverted lock we may have |
| 117 | * to reinitiate the lookup and set key_beg to properly pick up where we |
| 118 | * left off. |
| 119 | * |
| 120 | * NOTE! EDEADLK *CANNOT* be returned by this procedure. |
| 121 | */ |
| 122 | int |
| 123 | hammer_btree_iterate(hammer_cursor_t cursor) |
| 124 | { |
| 125 | hammer_node_ondisk_t node; |
| 126 | hammer_btree_elm_t elm; |
| 127 | int error; |
| 128 | int r; |
| 129 | int s; |
| 130 | |
| 131 | /* |
| 132 | * Skip past the current record |
| 133 | */ |
| 134 | node = cursor->node->ondisk; |
| 135 | if (node == NULL) |
| 136 | return(ENOENT); |
| 137 | if (cursor->index < node->count && |
| 138 | (cursor->flags & HAMMER_CURSOR_ATEDISK)) { |
| 139 | ++cursor->index; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Loop until an element is found or we are done. |
| 144 | */ |
| 145 | for (;;) { |
| 146 | /* |
| 147 | * We iterate up the tree and then index over one element |
| 148 | * while we are at the last element in the current node. |
| 149 | * |
| 150 | * If we are at the root of the filesystem, cursor_up |
| 151 | * returns ENOENT. |
| 152 | * |
| 153 | * XXX this could be optimized by storing the information in |
| 154 | * the parent reference. |
| 155 | * |
| 156 | * XXX we can lose the node lock temporarily, this could mess |
| 157 | * up our scan. |
| 158 | */ |
| 159 | ++hammer_stats_btree_iterations; |
| 160 | hammer_flusher_clean_loose_ios(cursor->trans->hmp); |
| 161 | |
| 162 | if (cursor->index == node->count) { |
| 163 | if (hammer_debug_btree) { |
| 164 | kprintf("BRACKETU %016llx[%d] -> %016llx[%d] (td=%p)\n", |
| 165 | cursor->node->node_offset, |
| 166 | cursor->index, |
| 167 | (cursor->parent ? cursor->parent->node_offset : -1), |
| 168 | cursor->parent_index, |
| 169 | curthread); |
| 170 | } |
| 171 | KKASSERT(cursor->parent == NULL || cursor->parent->ondisk->elms[cursor->parent_index].internal.subtree_offset == cursor->node->node_offset); |
| 172 | error = hammer_cursor_up(cursor); |
| 173 | if (error) |
| 174 | break; |
| 175 | /* reload stale pointer */ |
| 176 | node = cursor->node->ondisk; |
| 177 | KKASSERT(cursor->index != node->count); |
| 178 | |
| 179 | /* |
| 180 | * If we are reblocking we want to return internal |
| 181 | * nodes. |
| 182 | */ |
| 183 | if (cursor->flags & HAMMER_CURSOR_REBLOCKING) { |
| 184 | cursor->flags |= HAMMER_CURSOR_ATEDISK; |
| 185 | return(0); |
| 186 | } |
| 187 | ++cursor->index; |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Check internal or leaf element. Determine if the record |
| 193 | * at the cursor has gone beyond the end of our range. |
| 194 | * |
| 195 | * We recurse down through internal nodes. |
| 196 | */ |
| 197 | if (node->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 198 | elm = &node->elms[cursor->index]; |
| 199 | |
| 200 | r = hammer_btree_cmp(&cursor->key_end, &elm[0].base); |
| 201 | s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base); |
| 202 | if (hammer_debug_btree) { |
| 203 | kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d (td=%p)\n", |
| 204 | cursor->node->node_offset, |
| 205 | cursor->index, |
| 206 | elm[0].internal.base.obj_id, |
| 207 | elm[0].internal.base.rec_type, |
| 208 | elm[0].internal.base.key, |
| 209 | elm[0].internal.base.localization, |
| 210 | r, |
| 211 | curthread |
| 212 | ); |
| 213 | kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n", |
| 214 | cursor->node->node_offset, |
| 215 | cursor->index + 1, |
| 216 | elm[1].internal.base.obj_id, |
| 217 | elm[1].internal.base.rec_type, |
| 218 | elm[1].internal.base.key, |
| 219 | elm[1].internal.base.localization, |
| 220 | s |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | if (r < 0) { |
| 225 | error = ENOENT; |
| 226 | break; |
| 227 | } |
| 228 | if (r == 0 && (cursor->flags & |
| 229 | HAMMER_CURSOR_END_INCLUSIVE) == 0) { |
| 230 | error = ENOENT; |
| 231 | break; |
| 232 | } |
| 233 | KKASSERT(s <= 0); |
| 234 | |
| 235 | /* |
| 236 | * Better not be zero |
| 237 | */ |
| 238 | KKASSERT(elm->internal.subtree_offset != 0); |
| 239 | |
| 240 | /* |
| 241 | * If running the mirror filter see if we can skip |
| 242 | * the entire sub-tree. |
| 243 | */ |
| 244 | if (cursor->flags & HAMMER_CURSOR_MIRROR_FILTERED) { |
| 245 | if (elm->internal.mirror_tid < |
| 246 | cursor->mirror_tid) { |
| 247 | ++cursor->index; |
| 248 | continue; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | error = hammer_cursor_down(cursor); |
| 253 | if (error) |
| 254 | break; |
| 255 | KKASSERT(cursor->index == 0); |
| 256 | /* reload stale pointer */ |
| 257 | node = cursor->node->ondisk; |
| 258 | continue; |
| 259 | } else { |
| 260 | elm = &node->elms[cursor->index]; |
| 261 | r = hammer_btree_cmp(&cursor->key_end, &elm->base); |
| 262 | if (hammer_debug_btree) { |
| 263 | kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n", |
| 264 | cursor->node->node_offset, |
| 265 | cursor->index, |
| 266 | (elm[0].leaf.base.btype ? |
| 267 | elm[0].leaf.base.btype : '?'), |
| 268 | elm[0].leaf.base.obj_id, |
| 269 | elm[0].leaf.base.rec_type, |
| 270 | elm[0].leaf.base.key, |
| 271 | elm[0].leaf.base.localization, |
| 272 | r |
| 273 | ); |
| 274 | } |
| 275 | if (r < 0) { |
| 276 | error = ENOENT; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * We support both end-inclusive and |
| 282 | * end-exclusive searches. |
| 283 | */ |
| 284 | if (r == 0 && |
| 285 | (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) { |
| 286 | error = ENOENT; |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | switch(elm->leaf.base.btype) { |
| 291 | case HAMMER_BTREE_TYPE_RECORD: |
| 292 | if ((cursor->flags & HAMMER_CURSOR_ASOF) && |
| 293 | hammer_btree_chkts(cursor->asof, &elm->base)) { |
| 294 | ++cursor->index; |
| 295 | continue; |
| 296 | } |
| 297 | break; |
| 298 | default: |
| 299 | error = EINVAL; |
| 300 | break; |
| 301 | } |
| 302 | if (error) |
| 303 | break; |
| 304 | } |
| 305 | /* |
| 306 | * node pointer invalid after loop |
| 307 | */ |
| 308 | |
| 309 | /* |
| 310 | * Return entry |
| 311 | */ |
| 312 | if (hammer_debug_btree) { |
| 313 | int i = cursor->index; |
| 314 | hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i]; |
| 315 | kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n", |
| 316 | cursor->node, i, |
| 317 | elm->internal.base.obj_id, |
| 318 | elm->internal.base.rec_type, |
| 319 | elm->internal.base.key, |
| 320 | elm->internal.base.localization |
| 321 | ); |
| 322 | } |
| 323 | return(0); |
| 324 | } |
| 325 | return(error); |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Iterate in the reverse direction. This is used by the pruning code to |
| 330 | * avoid overlapping records. |
| 331 | */ |
| 332 | int |
| 333 | hammer_btree_iterate_reverse(hammer_cursor_t cursor) |
| 334 | { |
| 335 | hammer_node_ondisk_t node; |
| 336 | hammer_btree_elm_t elm; |
| 337 | int error; |
| 338 | int r; |
| 339 | int s; |
| 340 | |
| 341 | /* |
| 342 | * Skip past the current record. For various reasons the cursor |
| 343 | * may end up set to -1 or set to point at the end of the current |
| 344 | * node. These cases must be addressed. |
| 345 | */ |
| 346 | node = cursor->node->ondisk; |
| 347 | if (node == NULL) |
| 348 | return(ENOENT); |
| 349 | if (cursor->index != -1 && |
| 350 | (cursor->flags & HAMMER_CURSOR_ATEDISK)) { |
| 351 | --cursor->index; |
| 352 | } |
| 353 | if (cursor->index == cursor->node->ondisk->count) |
| 354 | --cursor->index; |
| 355 | |
| 356 | /* |
| 357 | * Loop until an element is found or we are done. |
| 358 | */ |
| 359 | for (;;) { |
| 360 | ++hammer_stats_btree_iterations; |
| 361 | hammer_flusher_clean_loose_ios(cursor->trans->hmp); |
| 362 | |
| 363 | /* |
| 364 | * We iterate up the tree and then index over one element |
| 365 | * while we are at the last element in the current node. |
| 366 | */ |
| 367 | if (cursor->index == -1) { |
| 368 | error = hammer_cursor_up(cursor); |
| 369 | if (error) { |
| 370 | cursor->index = 0; /* sanity */ |
| 371 | break; |
| 372 | } |
| 373 | /* reload stale pointer */ |
| 374 | node = cursor->node->ondisk; |
| 375 | KKASSERT(cursor->index != node->count); |
| 376 | --cursor->index; |
| 377 | continue; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Check internal or leaf element. Determine if the record |
| 382 | * at the cursor has gone beyond the end of our range. |
| 383 | * |
| 384 | * We recurse down through internal nodes. |
| 385 | */ |
| 386 | KKASSERT(cursor->index != node->count); |
| 387 | if (node->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 388 | elm = &node->elms[cursor->index]; |
| 389 | r = hammer_btree_cmp(&cursor->key_end, &elm[0].base); |
| 390 | s = hammer_btree_cmp(&cursor->key_beg, &elm[1].base); |
| 391 | if (hammer_debug_btree) { |
| 392 | kprintf("BRACKETL %016llx[%d] %016llx %02x %016llx lo=%02x %d\n", |
| 393 | cursor->node->node_offset, |
| 394 | cursor->index, |
| 395 | elm[0].internal.base.obj_id, |
| 396 | elm[0].internal.base.rec_type, |
| 397 | elm[0].internal.base.key, |
| 398 | elm[0].internal.base.localization, |
| 399 | r |
| 400 | ); |
| 401 | kprintf("BRACKETR %016llx[%d] %016llx %02x %016llx lo=%02x %d\n", |
| 402 | cursor->node->node_offset, |
| 403 | cursor->index + 1, |
| 404 | elm[1].internal.base.obj_id, |
| 405 | elm[1].internal.base.rec_type, |
| 406 | elm[1].internal.base.key, |
| 407 | elm[1].internal.base.localization, |
| 408 | s |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | if (s >= 0) { |
| 413 | error = ENOENT; |
| 414 | break; |
| 415 | } |
| 416 | KKASSERT(r >= 0); |
| 417 | |
| 418 | /* |
| 419 | * Better not be zero |
| 420 | */ |
| 421 | KKASSERT(elm->internal.subtree_offset != 0); |
| 422 | |
| 423 | error = hammer_cursor_down(cursor); |
| 424 | if (error) |
| 425 | break; |
| 426 | KKASSERT(cursor->index == 0); |
| 427 | /* reload stale pointer */ |
| 428 | node = cursor->node->ondisk; |
| 429 | |
| 430 | /* this can assign -1 if the leaf was empty */ |
| 431 | cursor->index = node->count - 1; |
| 432 | continue; |
| 433 | } else { |
| 434 | elm = &node->elms[cursor->index]; |
| 435 | s = hammer_btree_cmp(&cursor->key_beg, &elm->base); |
| 436 | if (hammer_debug_btree) { |
| 437 | kprintf("ELEMENT %016llx:%d %c %016llx %02x %016llx lo=%02x %d\n", |
| 438 | cursor->node->node_offset, |
| 439 | cursor->index, |
| 440 | (elm[0].leaf.base.btype ? |
| 441 | elm[0].leaf.base.btype : '?'), |
| 442 | elm[0].leaf.base.obj_id, |
| 443 | elm[0].leaf.base.rec_type, |
| 444 | elm[0].leaf.base.key, |
| 445 | elm[0].leaf.base.localization, |
| 446 | s |
| 447 | ); |
| 448 | } |
| 449 | if (s > 0) { |
| 450 | error = ENOENT; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | switch(elm->leaf.base.btype) { |
| 455 | case HAMMER_BTREE_TYPE_RECORD: |
| 456 | if ((cursor->flags & HAMMER_CURSOR_ASOF) && |
| 457 | hammer_btree_chkts(cursor->asof, &elm->base)) { |
| 458 | --cursor->index; |
| 459 | continue; |
| 460 | } |
| 461 | break; |
| 462 | default: |
| 463 | error = EINVAL; |
| 464 | break; |
| 465 | } |
| 466 | if (error) |
| 467 | break; |
| 468 | } |
| 469 | /* |
| 470 | * node pointer invalid after loop |
| 471 | */ |
| 472 | |
| 473 | /* |
| 474 | * Return entry |
| 475 | */ |
| 476 | if (hammer_debug_btree) { |
| 477 | int i = cursor->index; |
| 478 | hammer_btree_elm_t elm = &cursor->node->ondisk->elms[i]; |
| 479 | kprintf("ITERATE %p:%d %016llx %02x %016llx lo=%02x\n", |
| 480 | cursor->node, i, |
| 481 | elm->internal.base.obj_id, |
| 482 | elm->internal.base.rec_type, |
| 483 | elm->internal.base.key, |
| 484 | elm->internal.base.localization |
| 485 | ); |
| 486 | } |
| 487 | return(0); |
| 488 | } |
| 489 | return(error); |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Lookup cursor->key_beg. 0 is returned on success, ENOENT if the entry |
| 494 | * could not be found, EDEADLK if inserting and a retry is needed, and a |
| 495 | * fatal error otherwise. When retrying, the caller must terminate the |
| 496 | * cursor and reinitialize it. EDEADLK cannot be returned if not inserting. |
| 497 | * |
| 498 | * The cursor is suitably positioned for a deletion on success, and suitably |
| 499 | * positioned for an insertion on ENOENT if HAMMER_CURSOR_INSERT was |
| 500 | * specified. |
| 501 | * |
| 502 | * The cursor may begin anywhere, the search will traverse the tree in |
| 503 | * either direction to locate the requested element. |
| 504 | * |
| 505 | * Most of the logic implementing historical searches is handled here. We |
| 506 | * do an initial lookup with create_tid set to the asof TID. Due to the |
| 507 | * way records are laid out, a backwards iteration may be required if |
| 508 | * ENOENT is returned to locate the historical record. Here's the |
| 509 | * problem: |
| 510 | * |
| 511 | * create_tid: 10 15 20 |
| 512 | * LEAF1 LEAF2 |
| 513 | * records: (11) (18) |
| 514 | * |
| 515 | * Lets say we want to do a lookup AS-OF timestamp 17. We will traverse |
| 516 | * LEAF2 but the only record in LEAF2 has a create_tid of 18, which is |
| 517 | * not visible and thus causes ENOENT to be returned. We really need |
| 518 | * to check record 11 in LEAF1. If it also fails then the search fails |
| 519 | * (e.g. it might represent the range 11-16 and thus still not match our |
| 520 | * AS-OF timestamp of 17). Note that LEAF1 could be empty, requiring |
| 521 | * further iterations. |
| 522 | * |
| 523 | * If this case occurs btree_search() will set HAMMER_CURSOR_CREATE_CHECK |
| 524 | * and the cursor->create_check TID if an iteration might be needed. |
| 525 | * In the above example create_check would be set to 14. |
| 526 | */ |
| 527 | int |
| 528 | hammer_btree_lookup(hammer_cursor_t cursor) |
| 529 | { |
| 530 | int error; |
| 531 | |
| 532 | ++hammer_stats_btree_lookups; |
| 533 | if (cursor->flags & HAMMER_CURSOR_ASOF) { |
| 534 | KKASSERT((cursor->flags & HAMMER_CURSOR_INSERT) == 0); |
| 535 | cursor->key_beg.create_tid = cursor->asof; |
| 536 | for (;;) { |
| 537 | cursor->flags &= ~HAMMER_CURSOR_CREATE_CHECK; |
| 538 | error = btree_search(cursor, 0); |
| 539 | if (error != ENOENT || |
| 540 | (cursor->flags & HAMMER_CURSOR_CREATE_CHECK) == 0) { |
| 541 | /* |
| 542 | * Stop if no error. |
| 543 | * Stop if error other then ENOENT. |
| 544 | * Stop if ENOENT and not special case. |
| 545 | */ |
| 546 | break; |
| 547 | } |
| 548 | if (hammer_debug_btree) { |
| 549 | kprintf("CREATE_CHECK %016llx\n", |
| 550 | cursor->create_check); |
| 551 | } |
| 552 | cursor->key_beg.create_tid = cursor->create_check; |
| 553 | /* loop */ |
| 554 | } |
| 555 | } else { |
| 556 | error = btree_search(cursor, 0); |
| 557 | } |
| 558 | if (error == 0) |
| 559 | error = hammer_btree_extract(cursor, cursor->flags); |
| 560 | return(error); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Execute the logic required to start an iteration. The first record |
| 565 | * located within the specified range is returned and iteration control |
| 566 | * flags are adjusted for successive hammer_btree_iterate() calls. |
| 567 | */ |
| 568 | int |
| 569 | hammer_btree_first(hammer_cursor_t cursor) |
| 570 | { |
| 571 | int error; |
| 572 | |
| 573 | error = hammer_btree_lookup(cursor); |
| 574 | if (error == ENOENT) { |
| 575 | cursor->flags &= ~HAMMER_CURSOR_ATEDISK; |
| 576 | error = hammer_btree_iterate(cursor); |
| 577 | } |
| 578 | cursor->flags |= HAMMER_CURSOR_ATEDISK; |
| 579 | return(error); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Similarly but for an iteration in the reverse direction. |
| 584 | * |
| 585 | * Set ATEDISK when iterating backwards to skip the current entry, |
| 586 | * which after an ENOENT lookup will be pointing beyond our end point. |
| 587 | */ |
| 588 | int |
| 589 | hammer_btree_last(hammer_cursor_t cursor) |
| 590 | { |
| 591 | struct hammer_base_elm save; |
| 592 | int error; |
| 593 | |
| 594 | save = cursor->key_beg; |
| 595 | cursor->key_beg = cursor->key_end; |
| 596 | error = hammer_btree_lookup(cursor); |
| 597 | cursor->key_beg = save; |
| 598 | if (error == ENOENT || |
| 599 | (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) { |
| 600 | cursor->flags |= HAMMER_CURSOR_ATEDISK; |
| 601 | error = hammer_btree_iterate_reverse(cursor); |
| 602 | } |
| 603 | cursor->flags |= HAMMER_CURSOR_ATEDISK; |
| 604 | return(error); |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Extract the record and/or data associated with the cursor's current |
| 609 | * position. Any prior record or data stored in the cursor is replaced. |
| 610 | * The cursor must be positioned at a leaf node. |
| 611 | * |
| 612 | * NOTE: All extractions occur at the leaf of the B-Tree. |
| 613 | */ |
| 614 | int |
| 615 | hammer_btree_extract(hammer_cursor_t cursor, int flags) |
| 616 | { |
| 617 | hammer_mount_t hmp; |
| 618 | hammer_node_ondisk_t node; |
| 619 | hammer_btree_elm_t elm; |
| 620 | hammer_off_t data_off; |
| 621 | int32_t data_len; |
| 622 | int error; |
| 623 | |
| 624 | /* |
| 625 | * The case where the data reference resolves to the same buffer |
| 626 | * as the record reference must be handled. |
| 627 | */ |
| 628 | node = cursor->node->ondisk; |
| 629 | elm = &node->elms[cursor->index]; |
| 630 | cursor->data = NULL; |
| 631 | hmp = cursor->node->hmp; |
| 632 | |
| 633 | /* |
| 634 | * There is nothing to extract for an internal element. |
| 635 | */ |
| 636 | if (node->type == HAMMER_BTREE_TYPE_INTERNAL) |
| 637 | return(EINVAL); |
| 638 | |
| 639 | /* |
| 640 | * Only record types have data. |
| 641 | */ |
| 642 | KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF); |
| 643 | cursor->leaf = &elm->leaf; |
| 644 | |
| 645 | if ((flags & HAMMER_CURSOR_GET_DATA) == 0) |
| 646 | return(0); |
| 647 | if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD) |
| 648 | return(0); |
| 649 | data_off = elm->leaf.data_offset; |
| 650 | data_len = elm->leaf.data_len; |
| 651 | if (data_off == 0) |
| 652 | return(0); |
| 653 | |
| 654 | /* |
| 655 | * Load the data |
| 656 | */ |
| 657 | KKASSERT(data_len >= 0 && data_len <= HAMMER_XBUFSIZE); |
| 658 | cursor->data = hammer_bread_ext(hmp, data_off, data_len, |
| 659 | &error, &cursor->data_buffer); |
| 660 | if (hammer_crc_test_leaf(cursor->data, &elm->leaf) == 0) |
| 661 | Debugger("CRC FAILED: DATA"); |
| 662 | return(error); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | /* |
| 667 | * Insert a leaf element into the B-Tree at the current cursor position. |
| 668 | * The cursor is positioned such that the element at and beyond the cursor |
| 669 | * are shifted to make room for the new record. |
| 670 | * |
| 671 | * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT |
| 672 | * flag set and that call must return ENOENT before this function can be |
| 673 | * called. |
| 674 | * |
| 675 | * The caller may depend on the cursor's exclusive lock after return to |
| 676 | * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE). |
| 677 | * |
| 678 | * ENOSPC is returned if there is no room to insert a new record. |
| 679 | */ |
| 680 | int |
| 681 | hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm) |
| 682 | { |
| 683 | hammer_node_ondisk_t node; |
| 684 | int i; |
| 685 | int error; |
| 686 | |
| 687 | if ((error = hammer_cursor_upgrade_node(cursor)) != 0) |
| 688 | return(error); |
| 689 | ++hammer_stats_btree_inserts; |
| 690 | |
| 691 | /* |
| 692 | * Insert the element at the leaf node and update the count in the |
| 693 | * parent. It is possible for parent to be NULL, indicating that |
| 694 | * the filesystem's ROOT B-Tree node is a leaf itself, which is |
| 695 | * possible. The root inode can never be deleted so the leaf should |
| 696 | * never be empty. |
| 697 | * |
| 698 | * Remember that the right-hand boundary is not included in the |
| 699 | * count. |
| 700 | */ |
| 701 | hammer_modify_node_all(cursor->trans, cursor->node); |
| 702 | node = cursor->node->ondisk; |
| 703 | i = cursor->index; |
| 704 | KKASSERT(elm->base.btype != 0); |
| 705 | KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF); |
| 706 | KKASSERT(node->count < HAMMER_BTREE_LEAF_ELMS); |
| 707 | if (i != node->count) { |
| 708 | bcopy(&node->elms[i], &node->elms[i+1], |
| 709 | (node->count - i) * sizeof(*elm)); |
| 710 | } |
| 711 | node->elms[i].leaf = *elm; |
| 712 | ++node->count; |
| 713 | |
| 714 | /* |
| 715 | * Update the leaf node's aggregate mirror_tid for mirroring |
| 716 | * support. |
| 717 | */ |
| 718 | if (node->mirror_tid < elm->base.delete_tid) |
| 719 | node->mirror_tid = elm->base.delete_tid; |
| 720 | if (node->mirror_tid < elm->base.create_tid) |
| 721 | node->mirror_tid = elm->base.create_tid; |
| 722 | hammer_modify_node_done(cursor->node); |
| 723 | |
| 724 | /* |
| 725 | * What we really want to do is propagate mirror_tid all the way |
| 726 | * up the parent chain to the B-Tree root. That would be |
| 727 | * ultra-expensive, though. |
| 728 | */ |
| 729 | if (cursor->parent && hammer_do_mirror_propagate(cursor)) { |
| 730 | hammer_btree_mirror_propagate(cursor->trans, cursor->parent, |
| 731 | cursor->parent_index, |
| 732 | node->mirror_tid); |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Debugging sanity checks. |
| 737 | */ |
| 738 | KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->base) <= 0); |
| 739 | KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->base) > 0); |
| 740 | if (i) { |
| 741 | KKASSERT(hammer_btree_cmp(&node->elms[i-1].leaf.base, &elm->base) < 0); |
| 742 | } |
| 743 | if (i != node->count - 1) |
| 744 | KKASSERT(hammer_btree_cmp(&node->elms[i+1].leaf.base, &elm->base) > 0); |
| 745 | |
| 746 | return(0); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Delete a record from the B-Tree at the current cursor position. |
| 751 | * The cursor is positioned such that the current element is the one |
| 752 | * to be deleted. |
| 753 | * |
| 754 | * On return the cursor will be positioned after the deleted element and |
| 755 | * MAY point to an internal node. It will be suitable for the continuation |
| 756 | * of an iteration but not for an insertion or deletion. |
| 757 | * |
| 758 | * Deletions will attempt to partially rebalance the B-Tree in an upward |
| 759 | * direction, but will terminate rather then deadlock. Empty internal nodes |
| 760 | * are never allowed by a deletion which deadlocks may end up giving us an |
| 761 | * empty leaf. The pruner will clean up and rebalance the tree. |
| 762 | * |
| 763 | * This function can return EDEADLK, requiring the caller to retry the |
| 764 | * operation after clearing the deadlock. |
| 765 | */ |
| 766 | int |
| 767 | hammer_btree_delete(hammer_cursor_t cursor) |
| 768 | { |
| 769 | hammer_node_ondisk_t ondisk; |
| 770 | hammer_node_t node; |
| 771 | hammer_node_t parent; |
| 772 | int error; |
| 773 | int i; |
| 774 | |
| 775 | if ((error = hammer_cursor_upgrade(cursor)) != 0) |
| 776 | return(error); |
| 777 | ++hammer_stats_btree_deletes; |
| 778 | |
| 779 | /* |
| 780 | * Delete the element from the leaf node. |
| 781 | * |
| 782 | * Remember that leaf nodes do not have boundaries. |
| 783 | */ |
| 784 | node = cursor->node; |
| 785 | ondisk = node->ondisk; |
| 786 | i = cursor->index; |
| 787 | |
| 788 | KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_LEAF); |
| 789 | KKASSERT(i >= 0 && i < ondisk->count); |
| 790 | hammer_modify_node_all(cursor->trans, node); |
| 791 | if (i + 1 != ondisk->count) { |
| 792 | bcopy(&ondisk->elms[i+1], &ondisk->elms[i], |
| 793 | (ondisk->count - i - 1) * sizeof(ondisk->elms[0])); |
| 794 | } |
| 795 | --ondisk->count; |
| 796 | hammer_modify_node_done(node); |
| 797 | |
| 798 | /* |
| 799 | * Validate local parent |
| 800 | */ |
| 801 | if (ondisk->parent) { |
| 802 | parent = cursor->parent; |
| 803 | |
| 804 | KKASSERT(parent != NULL); |
| 805 | KKASSERT(parent->node_offset == ondisk->parent); |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * If the leaf becomes empty it must be detached from the parent, |
| 810 | * potentially recursing through to the filesystem root. |
| 811 | * |
| 812 | * This may reposition the cursor at one of the parent's of the |
| 813 | * current node. |
| 814 | * |
| 815 | * Ignore deadlock errors, that simply means that btree_remove |
| 816 | * was unable to recurse and had to leave us with an empty leaf. |
| 817 | */ |
| 818 | KKASSERT(cursor->index <= ondisk->count); |
| 819 | if (ondisk->count == 0) { |
| 820 | error = btree_remove(cursor); |
| 821 | if (error == EDEADLK) |
| 822 | error = 0; |
| 823 | } else { |
| 824 | error = 0; |
| 825 | } |
| 826 | KKASSERT(cursor->parent == NULL || |
| 827 | cursor->parent_index < cursor->parent->ondisk->count); |
| 828 | return(error); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE |
| 833 | * |
| 834 | * Search the filesystem B-Tree for cursor->key_beg, return the matching node. |
| 835 | * |
| 836 | * The search can begin ANYWHERE in the B-Tree. As a first step the search |
| 837 | * iterates up the tree as necessary to properly position itself prior to |
| 838 | * actually doing the sarch. |
| 839 | * |
| 840 | * INSERTIONS: The search will split full nodes and leaves on its way down |
| 841 | * and guarentee that the leaf it ends up on is not full. If we run out |
| 842 | * of space the search continues to the leaf (to position the cursor for |
| 843 | * the spike), but ENOSPC is returned. |
| 844 | * |
| 845 | * The search is only guarenteed to end up on a leaf if an error code of 0 |
| 846 | * is returned, or if inserting and an error code of ENOENT is returned. |
| 847 | * Otherwise it can stop at an internal node. On success a search returns |
| 848 | * a leaf node. |
| 849 | * |
| 850 | * COMPLEXITY WARNING! This is the core B-Tree search code for the entire |
| 851 | * filesystem, and it is not simple code. Please note the following facts: |
| 852 | * |
| 853 | * - Internal node recursions have a boundary on the left AND right. The |
| 854 | * right boundary is non-inclusive. The create_tid is a generic part |
| 855 | * of the key for internal nodes. |
| 856 | * |
| 857 | * - Leaf nodes contain terminal elements only now. |
| 858 | * |
| 859 | * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a |
| 860 | * historical search. ASOF and INSERT are mutually exclusive. When |
| 861 | * doing an as-of lookup btree_search() checks for a right-edge boundary |
| 862 | * case. If while recursing down the left-edge differs from the key |
| 863 | * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along |
| 864 | * with cursor->create_check. This is used by btree_lookup() to iterate. |
| 865 | * The iteration backwards because as-of searches can wind up going |
| 866 | * down the wrong branch of the B-Tree. |
| 867 | */ |
| 868 | static |
| 869 | int |
| 870 | btree_search(hammer_cursor_t cursor, int flags) |
| 871 | { |
| 872 | hammer_node_ondisk_t node; |
| 873 | hammer_btree_elm_t elm; |
| 874 | int error; |
| 875 | int enospc = 0; |
| 876 | int i; |
| 877 | int r; |
| 878 | int s; |
| 879 | |
| 880 | flags |= cursor->flags; |
| 881 | ++hammer_stats_btree_searches; |
| 882 | |
| 883 | if (hammer_debug_btree) { |
| 884 | kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx lo=%02x (td = %p)\n", |
| 885 | cursor->node->node_offset, |
| 886 | cursor->index, |
| 887 | cursor->key_beg.obj_id, |
| 888 | cursor->key_beg.rec_type, |
| 889 | cursor->key_beg.key, |
| 890 | cursor->key_beg.create_tid, |
| 891 | cursor->key_beg.localization, |
| 892 | curthread |
| 893 | ); |
| 894 | if (cursor->parent) |
| 895 | kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n", |
| 896 | cursor->parent->node_offset, cursor->parent_index, |
| 897 | cursor->left_bound->obj_id, |
| 898 | cursor->parent->ondisk->elms[cursor->parent_index].internal.base.obj_id, |
| 899 | cursor->right_bound->obj_id, |
| 900 | cursor->parent->ondisk->elms[cursor->parent_index+1].internal.base.obj_id, |
| 901 | cursor->left_bound, |
| 902 | &cursor->parent->ondisk->elms[cursor->parent_index], |
| 903 | cursor->right_bound, |
| 904 | &cursor->parent->ondisk->elms[cursor->parent_index+1] |
| 905 | ); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Move our cursor up the tree until we find a node whos range covers |
| 910 | * the key we are trying to locate. |
| 911 | * |
| 912 | * The left bound is inclusive, the right bound is non-inclusive. |
| 913 | * It is ok to cursor up too far. |
| 914 | */ |
| 915 | for (;;) { |
| 916 | r = hammer_btree_cmp(&cursor->key_beg, cursor->left_bound); |
| 917 | s = hammer_btree_cmp(&cursor->key_beg, cursor->right_bound); |
| 918 | if (r >= 0 && s < 0) |
| 919 | break; |
| 920 | KKASSERT(cursor->parent); |
| 921 | ++hammer_stats_btree_iterations; |
| 922 | error = hammer_cursor_up(cursor); |
| 923 | if (error) |
| 924 | goto done; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * The delete-checks below are based on node, not parent. Set the |
| 929 | * initial delete-check based on the parent. |
| 930 | */ |
| 931 | if (r == 1) { |
| 932 | KKASSERT(cursor->left_bound->create_tid != 1); |
| 933 | cursor->create_check = cursor->left_bound->create_tid - 1; |
| 934 | cursor->flags |= HAMMER_CURSOR_CREATE_CHECK; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * We better have ended up with a node somewhere. |
| 939 | */ |
| 940 | KKASSERT(cursor->node != NULL); |
| 941 | |
| 942 | /* |
| 943 | * If we are inserting we can't start at a full node if the parent |
| 944 | * is also full (because there is no way to split the node), |
| 945 | * continue running up the tree until the requirement is satisfied |
| 946 | * or we hit the root of the filesystem. |
| 947 | * |
| 948 | * (If inserting we aren't doing an as-of search so we don't have |
| 949 | * to worry about create_check). |
| 950 | */ |
| 951 | while ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) { |
| 952 | if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 953 | if (btree_node_is_full(cursor->node->ondisk) == 0) |
| 954 | break; |
| 955 | } else { |
| 956 | if (btree_node_is_full(cursor->node->ondisk) ==0) |
| 957 | break; |
| 958 | } |
| 959 | if (cursor->node->ondisk->parent == 0 || |
| 960 | cursor->parent->ondisk->count != HAMMER_BTREE_INT_ELMS) { |
| 961 | break; |
| 962 | } |
| 963 | ++hammer_stats_btree_iterations; |
| 964 | error = hammer_cursor_up(cursor); |
| 965 | /* node may have become stale */ |
| 966 | if (error) |
| 967 | goto done; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Push down through internal nodes to locate the requested key. |
| 972 | */ |
| 973 | node = cursor->node->ondisk; |
| 974 | while (node->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 975 | /* |
| 976 | * Scan the node to find the subtree index to push down into. |
| 977 | * We go one-past, then back-up. |
| 978 | * |
| 979 | * We must proactively remove deleted elements which may |
| 980 | * have been left over from a deadlocked btree_remove(). |
| 981 | * |
| 982 | * The left and right boundaries are included in the loop |
| 983 | * in order to detect edge cases. |
| 984 | * |
| 985 | * If the separator only differs by create_tid (r == 1) |
| 986 | * and we are doing an as-of search, we may end up going |
| 987 | * down a branch to the left of the one containing the |
| 988 | * desired key. This requires numerous special cases. |
| 989 | */ |
| 990 | ++hammer_stats_btree_iterations; |
| 991 | if (hammer_debug_btree) { |
| 992 | kprintf("SEARCH-I %016llx count=%d\n", |
| 993 | cursor->node->node_offset, |
| 994 | node->count); |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Try to shortcut the search before dropping into the |
| 999 | * linear loop. Locate the first node where r <= 1. |
| 1000 | */ |
| 1001 | i = hammer_btree_search_node(&cursor->key_beg, node); |
| 1002 | while (i <= node->count) { |
| 1003 | ++hammer_stats_btree_elements; |
| 1004 | elm = &node->elms[i]; |
| 1005 | r = hammer_btree_cmp(&cursor->key_beg, &elm->base); |
| 1006 | if (hammer_debug_btree > 2) { |
| 1007 | kprintf(" IELM %p %d r=%d\n", |
| 1008 | &node->elms[i], i, r); |
| 1009 | } |
| 1010 | if (r < 0) |
| 1011 | break; |
| 1012 | if (r == 1) { |
| 1013 | KKASSERT(elm->base.create_tid != 1); |
| 1014 | cursor->create_check = elm->base.create_tid - 1; |
| 1015 | cursor->flags |= HAMMER_CURSOR_CREATE_CHECK; |
| 1016 | } |
| 1017 | ++i; |
| 1018 | } |
| 1019 | if (hammer_debug_btree) { |
| 1020 | kprintf("SEARCH-I preI=%d/%d r=%d\n", |
| 1021 | i, node->count, r); |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * These cases occur when the parent's idea of the boundary |
| 1026 | * is wider then the child's idea of the boundary, and |
| 1027 | * require special handling. If not inserting we can |
| 1028 | * terminate the search early for these cases but the |
| 1029 | * child's boundaries cannot be unconditionally modified. |
| 1030 | */ |
| 1031 | if (i == 0) { |
| 1032 | /* |
| 1033 | * If i == 0 the search terminated to the LEFT of the |
| 1034 | * left_boundary but to the RIGHT of the parent's left |
| 1035 | * boundary. |
| 1036 | */ |
| 1037 | u_int8_t save; |
| 1038 | |
| 1039 | elm = &node->elms[0]; |
| 1040 | |
| 1041 | /* |
| 1042 | * If we aren't inserting we can stop here. |
| 1043 | */ |
| 1044 | if ((flags & (HAMMER_CURSOR_INSERT | |
| 1045 | HAMMER_CURSOR_PRUNING)) == 0) { |
| 1046 | cursor->index = 0; |
| 1047 | return(ENOENT); |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Correct a left-hand boundary mismatch. |
| 1052 | * |
| 1053 | * We can only do this if we can upgrade the lock, |
| 1054 | * and synchronized as a background cursor (i.e. |
| 1055 | * inserting or pruning). |
| 1056 | * |
| 1057 | * WARNING: We can only do this if inserting, i.e. |
| 1058 | * we are running on the backend. |
| 1059 | */ |
| 1060 | if ((error = hammer_cursor_upgrade(cursor)) != 0) |
| 1061 | return(error); |
| 1062 | KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND); |
| 1063 | hammer_modify_node_field(cursor->trans, cursor->node, |
| 1064 | elms[0]); |
| 1065 | save = node->elms[0].base.btype; |
| 1066 | node->elms[0].base = *cursor->left_bound; |
| 1067 | node->elms[0].base.btype = save; |
| 1068 | hammer_modify_node_done(cursor->node); |
| 1069 | } else if (i == node->count + 1) { |
| 1070 | /* |
| 1071 | * If i == node->count + 1 the search terminated to |
| 1072 | * the RIGHT of the right boundary but to the LEFT |
| 1073 | * of the parent's right boundary. If we aren't |
| 1074 | * inserting we can stop here. |
| 1075 | * |
| 1076 | * Note that the last element in this case is |
| 1077 | * elms[i-2] prior to adjustments to 'i'. |
| 1078 | */ |
| 1079 | --i; |
| 1080 | if ((flags & (HAMMER_CURSOR_INSERT | |
| 1081 | HAMMER_CURSOR_PRUNING)) == 0) { |
| 1082 | cursor->index = i; |
| 1083 | return (ENOENT); |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Correct a right-hand boundary mismatch. |
| 1088 | * (actual push-down record is i-2 prior to |
| 1089 | * adjustments to i). |
| 1090 | * |
| 1091 | * We can only do this if we can upgrade the lock, |
| 1092 | * and synchronized as a background cursor (i.e. |
| 1093 | * inserting or pruning). |
| 1094 | * |
| 1095 | * WARNING: We can only do this if inserting, i.e. |
| 1096 | * we are running on the backend. |
| 1097 | */ |
| 1098 | if ((error = hammer_cursor_upgrade(cursor)) != 0) |
| 1099 | return(error); |
| 1100 | elm = &node->elms[i]; |
| 1101 | KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND); |
| 1102 | hammer_modify_node(cursor->trans, cursor->node, |
| 1103 | &elm->base, sizeof(elm->base)); |
| 1104 | elm->base = *cursor->right_bound; |
| 1105 | hammer_modify_node_done(cursor->node); |
| 1106 | --i; |
| 1107 | } else { |
| 1108 | /* |
| 1109 | * The push-down index is now i - 1. If we had |
| 1110 | * terminated on the right boundary this will point |
| 1111 | * us at the last element. |
| 1112 | */ |
| 1113 | --i; |
| 1114 | } |
| 1115 | cursor->index = i; |
| 1116 | elm = &node->elms[i]; |
| 1117 | |
| 1118 | if (hammer_debug_btree) { |
| 1119 | kprintf("RESULT-I %016llx[%d] %016llx %02x " |
| 1120 | "key=%016llx cre=%016llx lo=%02x\n", |
| 1121 | cursor->node->node_offset, |
| 1122 | i, |
| 1123 | elm->internal.base.obj_id, |
| 1124 | elm->internal.base.rec_type, |
| 1125 | elm->internal.base.key, |
| 1126 | elm->internal.base.create_tid, |
| 1127 | elm->internal.base.localization |
| 1128 | ); |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * We better have a valid subtree offset. |
| 1133 | */ |
| 1134 | KKASSERT(elm->internal.subtree_offset != 0); |
| 1135 | |
| 1136 | /* |
| 1137 | * Handle insertion and deletion requirements. |
| 1138 | * |
| 1139 | * If inserting split full nodes. The split code will |
| 1140 | * adjust cursor->node and cursor->index if the current |
| 1141 | * index winds up in the new node. |
| 1142 | * |
| 1143 | * If inserting and a left or right edge case was detected, |
| 1144 | * we cannot correct the left or right boundary and must |
| 1145 | * prepend and append an empty leaf node in order to make |
| 1146 | * the boundary correction. |
| 1147 | * |
| 1148 | * If we run out of space we set enospc and continue on |
| 1149 | * to a leaf to provide the spike code with a good point |
| 1150 | * of entry. |
| 1151 | */ |
| 1152 | if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) { |
| 1153 | if (btree_node_is_full(node)) { |
| 1154 | error = btree_split_internal(cursor); |
| 1155 | if (error) { |
| 1156 | if (error != ENOSPC) |
| 1157 | goto done; |
| 1158 | enospc = 1; |
| 1159 | } |
| 1160 | /* |
| 1161 | * reload stale pointers |
| 1162 | */ |
| 1163 | i = cursor->index; |
| 1164 | node = cursor->node->ondisk; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /* |
| 1169 | * Push down (push into new node, existing node becomes |
| 1170 | * the parent) and continue the search. |
| 1171 | */ |
| 1172 | error = hammer_cursor_down(cursor); |
| 1173 | /* node may have become stale */ |
| 1174 | if (error) |
| 1175 | goto done; |
| 1176 | node = cursor->node->ondisk; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * We are at a leaf, do a linear search of the key array. |
| 1181 | * |
| 1182 | * On success the index is set to the matching element and 0 |
| 1183 | * is returned. |
| 1184 | * |
| 1185 | * On failure the index is set to the insertion point and ENOENT |
| 1186 | * is returned. |
| 1187 | * |
| 1188 | * Boundaries are not stored in leaf nodes, so the index can wind |
| 1189 | * up to the left of element 0 (index == 0) or past the end of |
| 1190 | * the array (index == node->count). It is also possible that the |
| 1191 | * leaf might be empty. |
| 1192 | */ |
| 1193 | ++hammer_stats_btree_iterations; |
| 1194 | KKASSERT (node->type == HAMMER_BTREE_TYPE_LEAF); |
| 1195 | KKASSERT(node->count <= HAMMER_BTREE_LEAF_ELMS); |
| 1196 | if (hammer_debug_btree) { |
| 1197 | kprintf("SEARCH-L %016llx count=%d\n", |
| 1198 | cursor->node->node_offset, |
| 1199 | node->count); |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Try to shortcut the search before dropping into the |
| 1204 | * linear loop. Locate the first node where r <= 1. |
| 1205 | */ |
| 1206 | i = hammer_btree_search_node(&cursor->key_beg, node); |
| 1207 | while (i < node->count) { |
| 1208 | ++hammer_stats_btree_elements; |
| 1209 | elm = &node->elms[i]; |
| 1210 | |
| 1211 | r = hammer_btree_cmp(&cursor->key_beg, &elm->leaf.base); |
| 1212 | |
| 1213 | if (hammer_debug_btree > 1) |
| 1214 | kprintf(" ELM %p %d r=%d\n", &node->elms[i], i, r); |
| 1215 | |
| 1216 | /* |
| 1217 | * We are at a record element. Stop if we've flipped past |
| 1218 | * key_beg, not counting the create_tid test. Allow the |
| 1219 | * r == 1 case (key_beg > element but differs only by its |
| 1220 | * create_tid) to fall through to the AS-OF check. |
| 1221 | */ |
| 1222 | KKASSERT (elm->leaf.base.btype == HAMMER_BTREE_TYPE_RECORD); |
| 1223 | |
| 1224 | if (r < 0) |
| 1225 | goto failed; |
| 1226 | if (r > 1) { |
| 1227 | ++i; |
| 1228 | continue; |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Check our as-of timestamp against the element. |
| 1233 | */ |
| 1234 | if (flags & HAMMER_CURSOR_ASOF) { |
| 1235 | if (hammer_btree_chkts(cursor->asof, |
| 1236 | &node->elms[i].base) != 0) { |
| 1237 | ++i; |
| 1238 | continue; |
| 1239 | } |
| 1240 | /* success */ |
| 1241 | } else { |
| 1242 | if (r > 0) { /* can only be +1 */ |
| 1243 | ++i; |
| 1244 | continue; |
| 1245 | } |
| 1246 | /* success */ |
| 1247 | } |
| 1248 | cursor->index = i; |
| 1249 | error = 0; |
| 1250 | if (hammer_debug_btree) { |
| 1251 | kprintf("RESULT-L %016llx[%d] (SUCCESS)\n", |
| 1252 | cursor->node->node_offset, i); |
| 1253 | } |
| 1254 | goto done; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * The search of the leaf node failed. i is the insertion point. |
| 1259 | */ |
| 1260 | failed: |
| 1261 | if (hammer_debug_btree) { |
| 1262 | kprintf("RESULT-L %016llx[%d] (FAILED)\n", |
| 1263 | cursor->node->node_offset, i); |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | * No exact match was found, i is now at the insertion point. |
| 1268 | * |
| 1269 | * If inserting split a full leaf before returning. This |
| 1270 | * may have the side effect of adjusting cursor->node and |
| 1271 | * cursor->index. |
| 1272 | */ |
| 1273 | cursor->index = i; |
| 1274 | if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0 && |
| 1275 | btree_node_is_full(node)) { |
| 1276 | error = btree_split_leaf(cursor); |
| 1277 | if (error) { |
| 1278 | if (error != ENOSPC) |
| 1279 | goto done; |
| 1280 | enospc = 1; |
| 1281 | } |
| 1282 | /* |
| 1283 | * reload stale pointers |
| 1284 | */ |
| 1285 | /* NOT USED |
| 1286 | i = cursor->index; |
| 1287 | node = &cursor->node->internal; |
| 1288 | */ |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * We reached a leaf but did not find the key we were looking for. |
| 1293 | * If this is an insert we will be properly positioned for an insert |
| 1294 | * (ENOENT) or spike (ENOSPC) operation. |
| 1295 | */ |
| 1296 | error = enospc ? ENOSPC : ENOENT; |
| 1297 | done: |
| 1298 | return(error); |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Heuristical search for the first element whos comparison is <= 1. May |
| 1303 | * return an index whos compare result is > 1 but may only return an index |
| 1304 | * whos compare result is <= 1 if it is the first element with that result. |
| 1305 | */ |
| 1306 | int |
| 1307 | hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node) |
| 1308 | { |
| 1309 | int b; |
| 1310 | int s; |
| 1311 | int i; |
| 1312 | int r; |
| 1313 | |
| 1314 | /* |
| 1315 | * Don't bother if the node does not have very many elements |
| 1316 | */ |
| 1317 | b = 0; |
| 1318 | s = node->count; |
| 1319 | while (s - b > 4) { |
| 1320 | i = b + (s - b) / 2; |
| 1321 | ++hammer_stats_btree_elements; |
| 1322 | r = hammer_btree_cmp(elm, &node->elms[i].leaf.base); |
| 1323 | if (r <= 1) { |
| 1324 | s = i; |
| 1325 | } else { |
| 1326 | b = i; |
| 1327 | } |
| 1328 | } |
| 1329 | return(b); |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | /************************************************************************ |
| 1334 | * SPLITTING AND MERGING * |
| 1335 | ************************************************************************ |
| 1336 | * |
| 1337 | * These routines do all the dirty work required to split and merge nodes. |
| 1338 | */ |
| 1339 | |
| 1340 | /* |
| 1341 | * Split an internal node into two nodes and move the separator at the split |
| 1342 | * point to the parent. |
| 1343 | * |
| 1344 | * (cursor->node, cursor->index) indicates the element the caller intends |
| 1345 | * to push into. We will adjust node and index if that element winds |
| 1346 | * up in the split node. |
| 1347 | * |
| 1348 | * If we are at the root of the filesystem a new root must be created with |
| 1349 | * two elements, one pointing to the original root and one pointing to the |
| 1350 | * newly allocated split node. |
| 1351 | */ |
| 1352 | static |
| 1353 | int |
| 1354 | btree_split_internal(hammer_cursor_t cursor) |
| 1355 | { |
| 1356 | hammer_node_ondisk_t ondisk; |
| 1357 | hammer_node_t node; |
| 1358 | hammer_node_t parent; |
| 1359 | hammer_node_t new_node; |
| 1360 | hammer_btree_elm_t elm; |
| 1361 | hammer_btree_elm_t parent_elm; |
| 1362 | hammer_node_locklist_t locklist = NULL; |
| 1363 | hammer_mount_t hmp = cursor->trans->hmp; |
| 1364 | int parent_index; |
| 1365 | int made_root; |
| 1366 | int split; |
| 1367 | int error; |
| 1368 | int i; |
| 1369 | const int esize = sizeof(*elm); |
| 1370 | |
| 1371 | error = hammer_btree_lock_children(cursor, &locklist); |
| 1372 | if (error) |
| 1373 | goto done; |
| 1374 | if ((error = hammer_cursor_upgrade(cursor)) != 0) |
| 1375 | goto done; |
| 1376 | ++hammer_stats_btree_splits; |
| 1377 | |
| 1378 | /* |
| 1379 | * We are splitting but elms[split] will be promoted to the parent, |
| 1380 | * leaving the right hand node with one less element. If the |
| 1381 | * insertion point will be on the left-hand side adjust the split |
| 1382 | * point to give the right hand side one additional node. |
| 1383 | */ |
| 1384 | node = cursor->node; |
| 1385 | ondisk = node->ondisk; |
| 1386 | split = (ondisk->count + 1) / 2; |
| 1387 | if (cursor->index <= split) |
| 1388 | --split; |
| 1389 | |
| 1390 | /* |
| 1391 | * If we are at the root of the filesystem, create a new root node |
| 1392 | * with 1 element and split normally. Avoid making major |
| 1393 | * modifications until we know the whole operation will work. |
| 1394 | */ |
| 1395 | if (ondisk->parent == 0) { |
| 1396 | parent = hammer_alloc_btree(cursor->trans, &error); |
| 1397 | if (parent == NULL) |
| 1398 | goto done; |
| 1399 | hammer_lock_ex(&parent->lock); |
| 1400 | hammer_modify_node_noundo(cursor->trans, parent); |
| 1401 | ondisk = parent->ondisk; |
| 1402 | ondisk->count = 1; |
| 1403 | ondisk->parent = 0; |
| 1404 | ondisk->type = HAMMER_BTREE_TYPE_INTERNAL; |
| 1405 | ondisk->elms[0].base = hmp->root_btree_beg; |
| 1406 | ondisk->elms[0].base.btype = node->ondisk->type; |
| 1407 | ondisk->elms[0].internal.subtree_offset = node->node_offset; |
| 1408 | ondisk->elms[1].base = hmp->root_btree_end; |
| 1409 | hammer_modify_node_done(parent); |
| 1410 | /* ondisk->elms[1].base.btype - not used */ |
| 1411 | made_root = 1; |
| 1412 | parent_index = 0; /* index of current node in parent */ |
| 1413 | } else { |
| 1414 | made_root = 0; |
| 1415 | parent = cursor->parent; |
| 1416 | parent_index = cursor->parent_index; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * Split node into new_node at the split point. |
| 1421 | * |
| 1422 | * B O O O P N N B <-- P = node->elms[split] |
| 1423 | * 0 1 2 3 4 5 6 <-- subtree indices |
| 1424 | * |
| 1425 | * x x P x x |
| 1426 | * s S S s |
| 1427 | * / \ |
| 1428 | * B O O O B B N N B <--- inner boundary points are 'P' |
| 1429 | * 0 1 2 3 4 5 6 |
| 1430 | * |
| 1431 | */ |
| 1432 | new_node = hammer_alloc_btree(cursor->trans, &error); |
| 1433 | if (new_node == NULL) { |
| 1434 | if (made_root) { |
| 1435 | hammer_unlock(&parent->lock); |
| 1436 | hammer_delete_node(cursor->trans, parent); |
| 1437 | hammer_rel_node(parent); |
| 1438 | } |
| 1439 | goto done; |
| 1440 | } |
| 1441 | hammer_lock_ex(&new_node->lock); |
| 1442 | |
| 1443 | /* |
| 1444 | * Create the new node. P becomes the left-hand boundary in the |
| 1445 | * new node. Copy the right-hand boundary as well. |
| 1446 | * |
| 1447 | * elm is the new separator. |
| 1448 | */ |
| 1449 | hammer_modify_node_noundo(cursor->trans, new_node); |
| 1450 | hammer_modify_node_all(cursor->trans, node); |
| 1451 | ondisk = node->ondisk; |
| 1452 | elm = &ondisk->elms[split]; |
| 1453 | bcopy(elm, &new_node->ondisk->elms[0], |
| 1454 | (ondisk->count - split + 1) * esize); |
| 1455 | new_node->ondisk->count = ondisk->count - split; |
| 1456 | new_node->ondisk->parent = parent->node_offset; |
| 1457 | new_node->ondisk->type = HAMMER_BTREE_TYPE_INTERNAL; |
| 1458 | KKASSERT(ondisk->type == new_node->ondisk->type); |
| 1459 | |
| 1460 | /* |
| 1461 | * Cleanup the original node. Elm (P) becomes the new boundary, |
| 1462 | * its subtree_offset was moved to the new node. If we had created |
| 1463 | * a new root its parent pointer may have changed. |
| 1464 | */ |
| 1465 | elm->internal.subtree_offset = 0; |
| 1466 | ondisk->count = split; |
| 1467 | |
| 1468 | /* |
| 1469 | * Insert the separator into the parent, fixup the parent's |
| 1470 | * reference to the original node, and reference the new node. |
| 1471 | * The separator is P. |
| 1472 | * |
| 1473 | * Remember that base.count does not include the right-hand boundary. |
| 1474 | */ |
| 1475 | hammer_modify_node_all(cursor->trans, parent); |
| 1476 | ondisk = parent->ondisk; |
| 1477 | KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS); |
| 1478 | parent_elm = &ondisk->elms[parent_index+1]; |
| 1479 | bcopy(parent_elm, parent_elm + 1, |
| 1480 | (ondisk->count - parent_index) * esize); |
| 1481 | parent_elm->internal.base = elm->base; /* separator P */ |
| 1482 | parent_elm->internal.base.btype = new_node->ondisk->type; |
| 1483 | parent_elm->internal.subtree_offset = new_node->node_offset; |
| 1484 | ++ondisk->count; |
| 1485 | hammer_modify_node_done(parent); |
| 1486 | |
| 1487 | /* |
| 1488 | * The children of new_node need their parent pointer set to new_node. |
| 1489 | * The children have already been locked by |
| 1490 | * hammer_btree_lock_children(). |
| 1491 | */ |
| 1492 | for (i = 0; i < new_node->ondisk->count; ++i) { |
| 1493 | elm = &new_node->ondisk->elms[i]; |
| 1494 | error = btree_set_parent(cursor->trans, new_node, elm); |
| 1495 | if (error) { |
| 1496 | panic("btree_split_internal: btree-fixup problem"); |
| 1497 | } |
| 1498 | } |
| 1499 | hammer_modify_node_done(new_node); |
| 1500 | |
| 1501 | /* |
| 1502 | * The filesystem's root B-Tree pointer may have to be updated. |
| 1503 | */ |
| 1504 | if (made_root) { |
| 1505 | hammer_volume_t volume; |
| 1506 | |
| 1507 | volume = hammer_get_root_volume(hmp, &error); |
| 1508 | KKASSERT(error == 0); |
| 1509 | |
| 1510 | hammer_modify_volume_field(cursor->trans, volume, |
| 1511 | vol0_btree_root); |
| 1512 | volume->ondisk->vol0_btree_root = parent->node_offset; |
| 1513 | hammer_modify_volume_done(volume); |
| 1514 | node->ondisk->parent = parent->node_offset; |
| 1515 | if (cursor->parent) { |
| 1516 | hammer_unlock(&cursor->parent->lock); |
| 1517 | hammer_rel_node(cursor->parent); |
| 1518 | } |
| 1519 | cursor->parent = parent; /* lock'd and ref'd */ |
| 1520 | hammer_rel_volume(volume, 0); |
| 1521 | } |
| 1522 | hammer_modify_node_done(node); |
| 1523 | |
| 1524 | |
| 1525 | /* |
| 1526 | * Ok, now adjust the cursor depending on which element the original |
| 1527 | * index was pointing at. If we are >= the split point the push node |
| 1528 | * is now in the new node. |
| 1529 | * |
| 1530 | * NOTE: If we are at the split point itself we cannot stay with the |
| 1531 | * original node because the push index will point at the right-hand |
| 1532 | * boundary, which is illegal. |
| 1533 | * |
| 1534 | * NOTE: The cursor's parent or parent_index must be adjusted for |
| 1535 | * the case where a new parent (new root) was created, and the case |
| 1536 | * where the cursor is now pointing at the split node. |
| 1537 | */ |
| 1538 | if (cursor->index >= split) { |
| 1539 | cursor->parent_index = parent_index + 1; |
| 1540 | cursor->index -= split; |
| 1541 | hammer_unlock(&cursor->node->lock); |
| 1542 | hammer_rel_node(cursor->node); |
| 1543 | cursor->node = new_node; /* locked and ref'd */ |
| 1544 | } else { |
| 1545 | cursor->parent_index = parent_index; |
| 1546 | hammer_unlock(&new_node->lock); |
| 1547 | hammer_rel_node(new_node); |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Fixup left and right bounds |
| 1552 | */ |
| 1553 | parent_elm = &parent->ondisk->elms[cursor->parent_index]; |
| 1554 | cursor->left_bound = &parent_elm[0].internal.base; |
| 1555 | cursor->right_bound = &parent_elm[1].internal.base; |
| 1556 | KKASSERT(hammer_btree_cmp(cursor->left_bound, |
| 1557 | &cursor->node->ondisk->elms[0].internal.base) <= 0); |
| 1558 | KKASSERT(hammer_btree_cmp(cursor->right_bound, |
| 1559 | &cursor->node->ondisk->elms[cursor->node->ondisk->count].internal.base) >= 0); |
| 1560 | |
| 1561 | done: |
| 1562 | hammer_btree_unlock_children(&locklist); |
| 1563 | hammer_cursor_downgrade(cursor); |
| 1564 | return (error); |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Same as the above, but splits a full leaf node. |
| 1569 | * |
| 1570 | * This function |
| 1571 | */ |
| 1572 | static |
| 1573 | int |
| 1574 | btree_split_leaf(hammer_cursor_t cursor) |
| 1575 | { |
| 1576 | hammer_node_ondisk_t ondisk; |
| 1577 | hammer_node_t parent; |
| 1578 | hammer_node_t leaf; |
| 1579 | hammer_mount_t hmp; |
| 1580 | hammer_node_t new_leaf; |
| 1581 | hammer_btree_elm_t elm; |
| 1582 | hammer_btree_elm_t parent_elm; |
| 1583 | hammer_base_elm_t mid_boundary; |
| 1584 | int parent_index; |
| 1585 | int made_root; |
| 1586 | int split; |
| 1587 | int error; |
| 1588 | const size_t esize = sizeof(*elm); |
| 1589 | |
| 1590 | if ((error = hammer_cursor_upgrade(cursor)) != 0) |
| 1591 | return(error); |
| 1592 | ++hammer_stats_btree_splits; |
| 1593 | |
| 1594 | KKASSERT(hammer_btree_cmp(cursor->left_bound, |
| 1595 | &cursor->node->ondisk->elms[0].leaf.base) <= 0); |
| 1596 | KKASSERT(hammer_btree_cmp(cursor->right_bound, |
| 1597 | &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0); |
| 1598 | |
| 1599 | /* |
| 1600 | * Calculate the split point. If the insertion point will be on |
| 1601 | * the left-hand side adjust the split point to give the right |
| 1602 | * hand side one additional node. |
| 1603 | * |
| 1604 | * Spikes are made up of two leaf elements which cannot be |
| 1605 | * safely split. |
| 1606 | */ |
| 1607 | leaf = cursor->node; |
| 1608 | ondisk = leaf->ondisk; |
| 1609 | split = (ondisk->count + 1) / 2; |
| 1610 | if (cursor->index <= split) |
| 1611 | --split; |
| 1612 | error = 0; |
| 1613 | hmp = leaf->hmp; |
| 1614 | |
| 1615 | elm = &ondisk->elms[split]; |
| 1616 | |
| 1617 | KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm[-1].leaf.base) <= 0); |
| 1618 | KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->leaf.base) <= 0); |
| 1619 | KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->leaf.base) > 0); |
| 1620 | KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm[1].leaf.base) > 0); |
| 1621 | |
| 1622 | /* |
| 1623 | * If we are at the root of the tree, create a new root node with |
| 1624 | * 1 element and split normally. Avoid making major modifications |
| 1625 | * until we know the whole operation will work. |
| 1626 | */ |
| 1627 | if (ondisk->parent == 0) { |
| 1628 | parent = hammer_alloc_btree(cursor->trans, &error); |
| 1629 | if (parent == NULL) |
| 1630 | goto done; |
| 1631 | hammer_lock_ex(&parent->lock); |
| 1632 | hammer_modify_node_noundo(cursor->trans, parent); |
| 1633 | ondisk = parent->ondisk; |
| 1634 | ondisk->count = 1; |
| 1635 | ondisk->parent = 0; |
| 1636 | ondisk->type = HAMMER_BTREE_TYPE_INTERNAL; |
| 1637 | ondisk->elms[0].base = hmp->root_btree_beg; |
| 1638 | ondisk->elms[0].base.btype = leaf->ondisk->type; |
| 1639 | ondisk->elms[0].internal.subtree_offset = leaf->node_offset; |
| 1640 | ondisk->elms[1].base = hmp->root_btree_end; |
| 1641 | /* ondisk->elms[1].base.btype = not used */ |
| 1642 | hammer_modify_node_done(parent); |
| 1643 | made_root = 1; |
| 1644 | parent_index = 0; /* insertion point in parent */ |
| 1645 | } else { |
| 1646 | made_root = 0; |
| 1647 | parent = cursor->parent; |
| 1648 | parent_index = cursor->parent_index; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Split leaf into new_leaf at the split point. Select a separator |
| 1653 | * value in-between the two leafs but with a bent towards the right |
| 1654 | * leaf since comparisons use an 'elm >= separator' inequality. |
| 1655 | * |
| 1656 | * L L L L L L L L |
| 1657 | * |
| 1658 | * x x P x x |
| 1659 | * s S S s |
| 1660 | * / \ |
| 1661 | * L L L L L L L L |
| 1662 | */ |
| 1663 | new_leaf = hammer_alloc_btree(cursor->trans, &error); |
| 1664 | if (new_leaf == NULL) { |
| 1665 | if (made_root) { |
| 1666 | hammer_unlock(&parent->lock); |
| 1667 | hammer_delete_node(cursor->trans, parent); |
| 1668 | hammer_rel_node(parent); |
| 1669 | } |
| 1670 | goto done; |
| 1671 | } |
| 1672 | hammer_lock_ex(&new_leaf->lock); |
| 1673 | |
| 1674 | /* |
| 1675 | * Create the new node and copy the leaf elements from the split |
| 1676 | * point on to the new node. |
| 1677 | */ |
| 1678 | hammer_modify_node_all(cursor->trans, leaf); |
| 1679 | hammer_modify_node_noundo(cursor->trans, new_leaf); |
| 1680 | ondisk = leaf->ondisk; |
| 1681 | elm = &ondisk->elms[split]; |
| 1682 | bcopy(elm, &new_leaf->ondisk->elms[0], (ondisk->count - split) * esize); |
| 1683 | new_leaf->ondisk->count = ondisk->count - split; |
| 1684 | new_leaf->ondisk->parent = parent->node_offset; |
| 1685 | new_leaf->ondisk->type = HAMMER_BTREE_TYPE_LEAF; |
| 1686 | KKASSERT(ondisk->type == new_leaf->ondisk->type); |
| 1687 | hammer_modify_node_done(new_leaf); |
| 1688 | |
| 1689 | /* |
| 1690 | * Cleanup the original node. Because this is a leaf node and |
| 1691 | * leaf nodes do not have a right-hand boundary, there |
| 1692 | * aren't any special edge cases to clean up. We just fixup the |
| 1693 | * count. |
| 1694 | */ |
| 1695 | ondisk->count = split; |
| 1696 | |
| 1697 | /* |
| 1698 | * Insert the separator into the parent, fixup the parent's |
| 1699 | * reference to the original node, and reference the new node. |
| 1700 | * The separator is P. |
| 1701 | * |
| 1702 | * Remember that base.count does not include the right-hand boundary. |
| 1703 | * We are copying parent_index+1 to parent_index+2, not +0 to +1. |
| 1704 | */ |
| 1705 | hammer_modify_node_all(cursor->trans, parent); |
| 1706 | ondisk = parent->ondisk; |
| 1707 | KKASSERT(split != 0); |
| 1708 | KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS); |
| 1709 | parent_elm = &ondisk->elms[parent_index+1]; |
| 1710 | bcopy(parent_elm, parent_elm + 1, |
| 1711 | (ondisk->count - parent_index) * esize); |
| 1712 | |
| 1713 | hammer_make_separator(&elm[-1].base, &elm[0].base, &parent_elm->base); |
| 1714 | parent_elm->internal.base.btype = new_leaf->ondisk->type; |
| 1715 | parent_elm->internal.subtree_offset = new_leaf->node_offset; |
| 1716 | mid_boundary = &parent_elm->base; |
| 1717 | ++ondisk->count; |
| 1718 | hammer_modify_node_done(parent); |
| 1719 | |
| 1720 | /* |
| 1721 | * The filesystem's root B-Tree pointer may have to be updated. |
| 1722 | */ |
| 1723 | if (made_root) { |
| 1724 | hammer_volume_t volume; |
| 1725 | |
| 1726 | volume = hammer_get_root_volume(hmp, &error); |
| 1727 | KKASSERT(error == 0); |
| 1728 | |
| 1729 | hammer_modify_volume_field(cursor->trans, volume, |
| 1730 | vol0_btree_root); |
| 1731 | volume->ondisk->vol0_btree_root = parent->node_offset; |
| 1732 | hammer_modify_volume_done(volume); |
| 1733 | leaf->ondisk->parent = parent->node_offset; |
| 1734 | if (cursor->parent) { |
| 1735 | hammer_unlock(&cursor->parent->lock); |
| 1736 | hammer_rel_node(cursor->parent); |
| 1737 | } |
| 1738 | cursor->parent = parent; /* lock'd and ref'd */ |
| 1739 | hammer_rel_volume(volume, 0); |
| 1740 | } |
| 1741 | hammer_modify_node_done(leaf); |
| 1742 | |
| 1743 | /* |
| 1744 | * Ok, now adjust the cursor depending on which element the original |
| 1745 | * index was pointing at. If we are >= the split point the push node |
| 1746 | * is now in the new node. |
| 1747 | * |
| 1748 | * NOTE: If we are at the split point itself we need to select the |
| 1749 | * old or new node based on where key_beg's insertion point will be. |
| 1750 | * If we pick the wrong side the inserted element will wind up in |
| 1751 | * the wrong leaf node and outside that node's bounds. |
| 1752 | */ |
| 1753 | if (cursor->index > split || |
| 1754 | (cursor->index == split && |
| 1755 | hammer_btree_cmp(&cursor->key_beg, mid_boundary) >= 0)) { |
| 1756 | cursor->parent_index = parent_index + 1; |
| 1757 | cursor->index -= split; |
| 1758 | hammer_unlock(&cursor->node->lock); |
| 1759 | hammer_rel_node(cursor->node); |
| 1760 | cursor->node = new_leaf; |
| 1761 | } else { |
| 1762 | cursor->parent_index = parent_index; |
| 1763 | hammer_unlock(&new_leaf->lock); |
| 1764 | hammer_rel_node(new_leaf); |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Fixup left and right bounds |
| 1769 | */ |
| 1770 | parent_elm = &parent->ondisk->elms[cursor->parent_index]; |
| 1771 | cursor->left_bound = &parent_elm[0].internal.base; |
| 1772 | cursor->right_bound = &parent_elm[1].internal.base; |
| 1773 | |
| 1774 | /* |
| 1775 | * Assert that the bounds are correct. |
| 1776 | */ |
| 1777 | KKASSERT(hammer_btree_cmp(cursor->left_bound, |
| 1778 | &cursor->node->ondisk->elms[0].leaf.base) <= 0); |
| 1779 | KKASSERT(hammer_btree_cmp(cursor->right_bound, |
| 1780 | &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0); |
| 1781 | KKASSERT(hammer_btree_cmp(cursor->left_bound, &cursor->key_beg) <= 0); |
| 1782 | KKASSERT(hammer_btree_cmp(cursor->right_bound, &cursor->key_beg) > 0); |
| 1783 | |
| 1784 | done: |
| 1785 | hammer_cursor_downgrade(cursor); |
| 1786 | return (error); |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Recursively correct the right-hand boundary's create_tid to (tid) as |
| 1791 | * long as the rest of the key matches. We have to recurse upward in |
| 1792 | * the tree as well as down the left side of each parent's right node. |
| 1793 | * |
| 1794 | * Return EDEADLK if we were only partially successful, forcing the caller |
| 1795 | * to try again. The original cursor is not modified. This routine can |
| 1796 | * also fail with EDEADLK if it is forced to throw away a portion of its |
| 1797 | * record history. |
| 1798 | * |
| 1799 | * The caller must pass a downgraded cursor to us (otherwise we can't dup it). |
| 1800 | */ |
| 1801 | struct hammer_rhb { |
| 1802 | TAILQ_ENTRY(hammer_rhb) entry; |
| 1803 | hammer_node_t node; |
| 1804 | int index; |
| 1805 | }; |
| 1806 | |
| 1807 | TAILQ_HEAD(hammer_rhb_list, hammer_rhb); |
| 1808 | |
| 1809 | int |
| 1810 | hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid) |
| 1811 | { |
| 1812 | struct hammer_rhb_list rhb_list; |
| 1813 | hammer_base_elm_t elm; |
| 1814 | hammer_node_t orig_node; |
| 1815 | struct hammer_rhb *rhb; |
| 1816 | int orig_index; |
| 1817 | int error; |
| 1818 | |
| 1819 | TAILQ_INIT(&rhb_list); |
| 1820 | |
| 1821 | /* |
| 1822 | * Save our position so we can restore it on return. This also |
| 1823 | * gives us a stable 'elm'. |
| 1824 | */ |
| 1825 | orig_node = cursor->node; |
| 1826 | hammer_ref_node(orig_node); |
| 1827 | hammer_lock_sh(&orig_node->lock); |
| 1828 | orig_index = cursor->index; |
| 1829 | elm = &orig_node->ondisk->elms[orig_index].base; |
| 1830 | |
| 1831 | /* |
| 1832 | * Now build a list of parents going up, allocating a rhb |
| 1833 | * structure for each one. |
| 1834 | */ |
| 1835 | while (cursor->parent) { |
| 1836 | /* |
| 1837 | * Stop if we no longer have any right-bounds to fix up |
| 1838 | */ |
| 1839 | if (elm->obj_id != cursor->right_bound->obj_id || |
| 1840 | elm->rec_type != cursor->right_bound->rec_type || |
| 1841 | elm->key != cursor->right_bound->key) { |
| 1842 | break; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * Stop if the right-hand bound's create_tid does not |
| 1847 | * need to be corrected. |
| 1848 | */ |
| 1849 | if (cursor->right_bound->create_tid >= tid) |
| 1850 | break; |
| 1851 | |
| 1852 | rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO); |
| 1853 | rhb->node = cursor->parent; |
| 1854 | rhb->index = cursor->parent_index; |
| 1855 | hammer_ref_node(rhb->node); |
| 1856 | hammer_lock_sh(&rhb->node->lock); |
| 1857 | TAILQ_INSERT_HEAD(&rhb_list, rhb, entry); |
| 1858 | |
| 1859 | hammer_cursor_up(cursor); |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * now safely adjust the right hand bound for each rhb. This may |
| 1864 | * also require taking the right side of the tree and iterating down |
| 1865 | * ITS left side. |
| 1866 | */ |
| 1867 | error = 0; |
| 1868 | while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) { |
| 1869 | error = hammer_cursor_seek(cursor, rhb->node, rhb->index); |
| 1870 | if (error) |
| 1871 | break; |
| 1872 | TAILQ_REMOVE(&rhb_list, rhb, entry); |
| 1873 | hammer_unlock(&rhb->node->lock); |
| 1874 | hammer_rel_node(rhb->node); |
| 1875 | kfree(rhb, M_HAMMER); |
| 1876 | |
| 1877 | switch (cursor->node->ondisk->type) { |
| 1878 | case HAMMER_BTREE_TYPE_INTERNAL: |
| 1879 | /* |
| 1880 | * Right-boundary for parent at internal node |
| 1881 | * is one element to the right of the element whos |
| 1882 | * right boundary needs adjusting. We must then |
| 1883 | * traverse down the left side correcting any left |
| 1884 | * bounds (which may now be too far to the left). |
| 1885 | */ |
| 1886 | ++cursor->index; |
| 1887 | error = hammer_btree_correct_lhb(cursor, tid); |
| 1888 | break; |
| 1889 | default: |
| 1890 | panic("hammer_btree_correct_rhb(): Bad node type"); |
| 1891 | error = EINVAL; |
| 1892 | break; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * Cleanup |
| 1898 | */ |
| 1899 | while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) { |
| 1900 | TAILQ_REMOVE(&rhb_list, rhb, entry); |
| 1901 | hammer_unlock(&rhb->node->lock); |
| 1902 | hammer_rel_node(rhb->node); |
| 1903 | kfree(rhb, M_HAMMER); |
| 1904 | } |
| 1905 | error = hammer_cursor_seek(cursor, orig_node, orig_index); |
| 1906 | hammer_unlock(&orig_node->lock); |
| 1907 | hammer_rel_node(orig_node); |
| 1908 | return (error); |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand |
| 1913 | * bound going downward starting at the current cursor position. |
| 1914 | * |
| 1915 | * This function does not restore the cursor after use. |
| 1916 | */ |
| 1917 | int |
| 1918 | hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid) |
| 1919 | { |
| 1920 | struct hammer_rhb_list rhb_list; |
| 1921 | hammer_base_elm_t elm; |
| 1922 | hammer_base_elm_t cmp; |
| 1923 | struct hammer_rhb *rhb; |
| 1924 | int error; |
| 1925 | |
| 1926 | TAILQ_INIT(&rhb_list); |
| 1927 | |
| 1928 | cmp = &cursor->node->ondisk->elms[cursor->index].base; |
| 1929 | |
| 1930 | /* |
| 1931 | * Record the node and traverse down the left-hand side for all |
| 1932 | * matching records needing a boundary correction. |
| 1933 | */ |
| 1934 | error = 0; |
| 1935 | for (;;) { |
| 1936 | rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO); |
| 1937 | rhb->node = cursor->node; |
| 1938 | rhb->index = cursor->index; |
| 1939 | hammer_ref_node(rhb->node); |
| 1940 | hammer_lock_sh(&rhb->node->lock); |
| 1941 | TAILQ_INSERT_HEAD(&rhb_list, rhb, entry); |
| 1942 | |
| 1943 | if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 1944 | /* |
| 1945 | * Nothing to traverse down if we are at the right |
| 1946 | * boundary of an internal node. |
| 1947 | */ |
| 1948 | if (cursor->index == cursor->node->ondisk->count) |
| 1949 | break; |
| 1950 | } else { |
| 1951 | elm = &cursor->node->ondisk->elms[cursor->index].base; |
| 1952 | if (elm->btype == HAMMER_BTREE_TYPE_RECORD) |
| 1953 | break; |
| 1954 | panic("Illegal leaf record type %02x", elm->btype); |
| 1955 | } |
| 1956 | error = hammer_cursor_down(cursor); |
| 1957 | if (error) |
| 1958 | break; |
| 1959 | |
| 1960 | elm = &cursor->node->ondisk->elms[cursor->index].base; |
| 1961 | if (elm->obj_id != cmp->obj_id || |
| 1962 | elm->rec_type != cmp->rec_type || |
| 1963 | elm->key != cmp->key) { |
| 1964 | break; |
| 1965 | } |
| 1966 | if (elm->create_tid >= tid) |
| 1967 | break; |
| 1968 | |
| 1969 | } |
| 1970 | |
| 1971 | /* |
| 1972 | * Now we can safely adjust the left-hand boundary from the bottom-up. |
| 1973 | * The last element we remove from the list is the caller's right hand |
| 1974 | * boundary, which must also be adjusted. |
| 1975 | */ |
| 1976 | while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) { |
| 1977 | error = hammer_cursor_seek(cursor, rhb->node, rhb->index); |
| 1978 | if (error) |
| 1979 | break; |
| 1980 | TAILQ_REMOVE(&rhb_list, rhb, entry); |
| 1981 | hammer_unlock(&rhb->node->lock); |
| 1982 | hammer_rel_node(rhb->node); |
| 1983 | kfree(rhb, M_HAMMER); |
| 1984 | |
| 1985 | elm = &cursor->node->ondisk->elms[cursor->index].base; |
| 1986 | if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 1987 | hammer_modify_node(cursor->trans, cursor->node, |
| 1988 | &elm->create_tid, |
| 1989 | sizeof(elm->create_tid)); |
| 1990 | elm->create_tid = tid; |
| 1991 | hammer_modify_node_done(cursor->node); |
| 1992 | } else { |
| 1993 | panic("hammer_btree_correct_lhb(): Bad element type"); |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * Cleanup |
| 1999 | */ |
| 2000 | while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) { |
| 2001 | TAILQ_REMOVE(&rhb_list, rhb, entry); |
| 2002 | hammer_unlock(&rhb->node->lock); |
| 2003 | hammer_rel_node(rhb->node); |
| 2004 | kfree(rhb, M_HAMMER); |
| 2005 | } |
| 2006 | return (error); |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Attempt to remove the locked, empty or want-to-be-empty B-Tree node at |
| 2011 | * (cursor->node). Returns 0 on success, EDEADLK if we could not complete |
| 2012 | * the operation due to a deadlock, or some other error. |
| 2013 | * |
| 2014 | * This routine is always called with an empty, locked leaf but may recurse |
| 2015 | * into want-to-be-empty parents as part of its operation. |
| 2016 | * |
| 2017 | * It should also be noted that when removing empty leaves we must be sure |
| 2018 | * to test and update mirror_tid because another thread may have deadlocked |
| 2019 | * against us (or someone) trying to propagate it up and cannot retry once |
| 2020 | * the node has been deleted. |
| 2021 | * |
| 2022 | * On return the cursor may end up pointing to an internal node, suitable |
| 2023 | * for further iteration but not for an immediate insertion or deletion. |
| 2024 | */ |
| 2025 | static int |
| 2026 | btree_remove(hammer_cursor_t cursor) |
| 2027 | { |
| 2028 | hammer_node_ondisk_t ondisk; |
| 2029 | hammer_btree_elm_t elm; |
| 2030 | hammer_node_t node; |
| 2031 | hammer_node_t parent; |
| 2032 | const int esize = sizeof(*elm); |
| 2033 | int error; |
| 2034 | |
| 2035 | node = cursor->node; |
| 2036 | |
| 2037 | /* |
| 2038 | * When deleting the root of the filesystem convert it to |
| 2039 | * an empty leaf node. Internal nodes cannot be empty. |
| 2040 | */ |
| 2041 | ondisk = node->ondisk; |
| 2042 | if (ondisk->parent == 0) { |
| 2043 | KKASSERT(cursor->parent == NULL); |
| 2044 | hammer_modify_node_all(cursor->trans, node); |
| 2045 | KKASSERT(ondisk == node->ondisk); |
| 2046 | ondisk->type = HAMMER_BTREE_TYPE_LEAF; |
| 2047 | ondisk->count = 0; |
| 2048 | hammer_modify_node_done(node); |
| 2049 | cursor->index = 0; |
| 2050 | return(0); |
| 2051 | } |
| 2052 | |
| 2053 | parent = cursor->parent; |
| 2054 | |
| 2055 | /* |
| 2056 | * If another thread deadlocked trying to propagate mirror_tid up |
| 2057 | * we have to finish the job before deleting node. XXX |
| 2058 | */ |
| 2059 | if (parent->ondisk->mirror_tid < node->ondisk->mirror_tid && |
| 2060 | hammer_do_mirror_propagate(cursor)) { |
| 2061 | hammer_btree_mirror_propagate(cursor->trans, |
| 2062 | parent, |
| 2063 | cursor->parent_index, |
| 2064 | node->ondisk->mirror_tid); |
| 2065 | |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * Attempt to remove the parent's reference to the child. If the |
| 2070 | * parent would become empty we have to recurse. If we fail we |
| 2071 | * leave the parent pointing to an empty leaf node. |
| 2072 | */ |
| 2073 | if (parent->ondisk->count == 1) { |
| 2074 | /* |
| 2075 | * This special cursor_up_locked() call leaves the original |
| 2076 | * node exclusively locked and referenced, leaves the |
| 2077 | * original parent locked (as the new node), and locks the |
| 2078 | * new parent. It can return EDEADLK. |
| 2079 | */ |
| 2080 | error = hammer_cursor_up_locked(cursor); |
| 2081 | if (error == 0) { |
| 2082 | error = btree_remove(cursor); |
| 2083 | if (error == 0) { |
| 2084 | hammer_modify_node_all(cursor->trans, node); |
| 2085 | ondisk = node->ondisk; |
| 2086 | ondisk->type = HAMMER_BTREE_TYPE_DELETED; |
| 2087 | ondisk->count = 0; |
| 2088 | hammer_modify_node_done(node); |
| 2089 | hammer_flush_node(node); |
| 2090 | hammer_delete_node(cursor->trans, node); |
| 2091 | } else { |
| 2092 | kprintf("Warning: BTREE_REMOVE: Defering " |
| 2093 | "parent removal1 @ %016llx, skipping\n", |
| 2094 | node->node_offset); |
| 2095 | } |
| 2096 | hammer_unlock(&node->lock); |
| 2097 | hammer_rel_node(node); |
| 2098 | } else { |
| 2099 | kprintf("Warning: BTREE_REMOVE: Defering parent " |
| 2100 | "removal2 @ %016llx, skipping\n", |
| 2101 | node->node_offset); |
| 2102 | } |
| 2103 | } else { |
| 2104 | KKASSERT(parent->ondisk->count > 1); |
| 2105 | |
| 2106 | /* |
| 2107 | * Delete the subtree reference in the parent |
| 2108 | */ |
| 2109 | hammer_modify_node_all(cursor->trans, parent); |
| 2110 | ondisk = parent->ondisk; |
| 2111 | KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_INTERNAL); |
| 2112 | |
| 2113 | elm = &ondisk->elms[cursor->parent_index]; |
| 2114 | KKASSERT(elm->internal.subtree_offset == node->node_offset); |
| 2115 | KKASSERT(ondisk->count > 0); |
| 2116 | bcopy(&elm[1], &elm[0], |
| 2117 | (ondisk->count - cursor->parent_index) * esize); |
| 2118 | --ondisk->count; |
| 2119 | hammer_modify_node_done(parent); |
| 2120 | hammer_flush_node(node); |
| 2121 | hammer_delete_node(cursor->trans, node); |
| 2122 | |
| 2123 | /* |
| 2124 | * cursor->node is invalid, cursor up to make the cursor |
| 2125 | * valid again. |
| 2126 | */ |
| 2127 | error = hammer_cursor_up(cursor); |
| 2128 | } |
| 2129 | return (error); |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * Propagate a mirror TID update upwards through the B-Tree to the root. |
| 2134 | * |
| 2135 | * A locked internal node must be passed in. The node will remain locked |
| 2136 | * on return. |
| 2137 | * |
| 2138 | * This function syncs mirror_tid at the specified internal node's element, |
| 2139 | * adjusts the node's aggregation mirror_tid, and then recurses upwards. |
| 2140 | */ |
| 2141 | int |
| 2142 | hammer_btree_mirror_propagate(hammer_transaction_t trans, hammer_node_t node, |
| 2143 | int index, hammer_tid_t mirror_tid) |
| 2144 | { |
| 2145 | hammer_btree_internal_elm_t elm; |
| 2146 | hammer_node_t parent; |
| 2147 | int parent_index; |
| 2148 | int error; |
| 2149 | |
| 2150 | KKASSERT (node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL); |
| 2151 | |
| 2152 | /* |
| 2153 | * Adjust the node's element |
| 2154 | */ |
| 2155 | elm = &node->ondisk->elms[index].internal; |
| 2156 | if (elm->mirror_tid >= mirror_tid) |
| 2157 | return(0); |
| 2158 | hammer_modify_node(trans, node, &elm->mirror_tid, |
| 2159 | sizeof(elm->mirror_tid)); |
| 2160 | elm->mirror_tid = mirror_tid; |
| 2161 | hammer_modify_node_done(node); |
| 2162 | |
| 2163 | /* |
| 2164 | * Adjust the node's mirror_tid aggragator |
| 2165 | */ |
| 2166 | if (node->ondisk->mirror_tid >= mirror_tid) |
| 2167 | return(0); |
| 2168 | hammer_modify_node_field(trans, node, mirror_tid); |
| 2169 | node->ondisk->mirror_tid = mirror_tid; |
| 2170 | hammer_modify_node_done(node); |
| 2171 | |
| 2172 | error = 0; |
| 2173 | error = 0; |
| 2174 | if (node->ondisk->parent) { |
| 2175 | parent = hammer_btree_get_parent(node, &parent_index, |
| 2176 | &error, 1); |
| 2177 | if (parent) { |
| 2178 | hammer_btree_mirror_propagate(trans, parent, |
| 2179 | parent_index, mirror_tid); |
| 2180 | hammer_unlock(&parent->lock); |
| 2181 | hammer_rel_node(parent); |
| 2182 | } |
| 2183 | } |
| 2184 | return(error); |
| 2185 | } |
| 2186 | |
| 2187 | hammer_node_t |
| 2188 | hammer_btree_get_parent(hammer_node_t node, int *parent_indexp, int *errorp, |
| 2189 | int try_exclusive) |
| 2190 | { |
| 2191 | hammer_node_t parent; |
| 2192 | hammer_btree_elm_t elm; |
| 2193 | int i; |
| 2194 | |
| 2195 | /* |
| 2196 | * Get the node |
| 2197 | */ |
| 2198 | parent = hammer_get_node(node->hmp, node->ondisk->parent, 0, errorp); |
| 2199 | if (*errorp) { |
| 2200 | KKASSERT(parent == NULL); |
| 2201 | return(NULL); |
| 2202 | } |
| 2203 | KKASSERT ((parent->flags & HAMMER_NODE_DELETED) == 0); |
| 2204 | |
| 2205 | /* |
| 2206 | * Lock the node |
| 2207 | */ |
| 2208 | if (try_exclusive) { |
| 2209 | if (hammer_lock_ex_try(&parent->lock)) { |
| 2210 | hammer_rel_node(parent); |
| 2211 | *errorp = EDEADLK; |
| 2212 | return(NULL); |
| 2213 | } |
| 2214 | } else { |
| 2215 | hammer_lock_sh(&parent->lock); |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Figure out which element in the parent is pointing to the |
| 2220 | * child. |
| 2221 | */ |
| 2222 | if (node->ondisk->count) { |
| 2223 | i = hammer_btree_search_node(&node->ondisk->elms[0].base, |
| 2224 | parent->ondisk); |
| 2225 | } else { |
| 2226 | i = 0; |
| 2227 | } |
| 2228 | while (i < parent->ondisk->count) { |
| 2229 | elm = &parent->ondisk->elms[i]; |
| 2230 | if (elm->internal.subtree_offset == node->node_offset) |
| 2231 | break; |
| 2232 | ++i; |
| 2233 | } |
| 2234 | if (i == parent->ondisk->count) { |
| 2235 | hammer_unlock(&parent->lock); |
| 2236 | panic("Bad B-Tree link: parent %p node %p\n", parent, node); |
| 2237 | } |
| 2238 | *parent_indexp = i; |
| 2239 | KKASSERT(*errorp == 0); |
| 2240 | return(parent); |
| 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * The element (elm) has been moved to a new internal node (node). |
| 2245 | * |
| 2246 | * If the element represents a pointer to an internal node that node's |
| 2247 | * parent must be adjusted to the element's new location. |
| 2248 | * |
| 2249 | * XXX deadlock potential here with our exclusive locks |
| 2250 | */ |
| 2251 | int |
| 2252 | btree_set_parent(hammer_transaction_t trans, hammer_node_t node, |
| 2253 | hammer_btree_elm_t elm) |
| 2254 | { |
| 2255 | hammer_node_t child; |
| 2256 | int error; |
| 2257 | |
| 2258 | error = 0; |
| 2259 | |
| 2260 | switch(elm->base.btype) { |
| 2261 | case HAMMER_BTREE_TYPE_INTERNAL: |
| 2262 | case HAMMER_BTREE_TYPE_LEAF: |
| 2263 | child = hammer_get_node(node->hmp, elm->internal.subtree_offset, |
| 2264 | 0, &error); |
| 2265 | if (error == 0) { |
| 2266 | hammer_modify_node_field(trans, child, parent); |
| 2267 | child->ondisk->parent = node->node_offset; |
| 2268 | hammer_modify_node_done(child); |
| 2269 | hammer_rel_node(child); |
| 2270 | } |
| 2271 | break; |
| 2272 | default: |
| 2273 | break; |
| 2274 | } |
| 2275 | return(error); |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Exclusively lock all the children of node. This is used by the split |
| 2280 | * code to prevent anyone from accessing the children of a cursor node |
| 2281 | * while we fix-up its parent offset. |
| 2282 | * |
| 2283 | * If we don't lock the children we can really mess up cursors which block |
| 2284 | * trying to cursor-up into our node. |
| 2285 | * |
| 2286 | * On failure EDEADLK (or some other error) is returned. If a deadlock |
| 2287 | * error is returned the cursor is adjusted to block on termination. |
| 2288 | */ |
| 2289 | int |
| 2290 | hammer_btree_lock_children(hammer_cursor_t cursor, |
| 2291 | struct hammer_node_locklist **locklistp) |
| 2292 | { |
| 2293 | hammer_node_t node; |
| 2294 | hammer_node_locklist_t item; |
| 2295 | hammer_node_ondisk_t ondisk; |
| 2296 | hammer_btree_elm_t elm; |
| 2297 | hammer_node_t child; |
| 2298 | int error; |
| 2299 | int i; |
| 2300 | |
| 2301 | node = cursor->node; |
| 2302 | ondisk = node->ondisk; |
| 2303 | error = 0; |
| 2304 | |
| 2305 | /* |
| 2306 | * We really do not want to block on I/O with exclusive locks held, |
| 2307 | * pre-get the children before trying to lock the mess. |
| 2308 | */ |
| 2309 | for (i = 0; i < ondisk->count; ++i) { |
| 2310 | ++hammer_stats_btree_elements; |
| 2311 | elm = &ondisk->elms[i]; |
| 2312 | if (elm->base.btype != HAMMER_BTREE_TYPE_LEAF && |
| 2313 | elm->base.btype != HAMMER_BTREE_TYPE_INTERNAL) { |
| 2314 | continue; |
| 2315 | } |
| 2316 | child = hammer_get_node(node->hmp, |
| 2317 | elm->internal.subtree_offset, |
| 2318 | 0, &error); |
| 2319 | if (child) |
| 2320 | hammer_rel_node(child); |
| 2321 | } |
| 2322 | |
| 2323 | /* |
| 2324 | * Do it for real |
| 2325 | */ |
| 2326 | for (i = 0; error == 0 && i < ondisk->count; ++i) { |
| 2327 | ++hammer_stats_btree_elements; |
| 2328 | elm = &ondisk->elms[i]; |
| 2329 | |
| 2330 | switch(elm->base.btype) { |
| 2331 | case HAMMER_BTREE_TYPE_INTERNAL: |
| 2332 | case HAMMER_BTREE_TYPE_LEAF: |
| 2333 | KKASSERT(elm->internal.subtree_offset != 0); |
| 2334 | child = hammer_get_node(node->hmp, |
| 2335 | elm->internal.subtree_offset, |
| 2336 | 0, &error); |
| 2337 | break; |
| 2338 | default: |
| 2339 | child = NULL; |
| 2340 | break; |
| 2341 | } |
| 2342 | if (child) { |
| 2343 | if (hammer_lock_ex_try(&child->lock) != 0) { |
| 2344 | if (cursor->deadlk_node == NULL) { |
| 2345 | cursor->deadlk_node = child; |
| 2346 | hammer_ref_node(cursor->deadlk_node); |
| 2347 | } |
| 2348 | error = EDEADLK; |
| 2349 | hammer_rel_node(child); |
| 2350 | } else { |
| 2351 | item = kmalloc(sizeof(*item), |
| 2352 | M_HAMMER, M_WAITOK); |
| 2353 | item->next = *locklistp; |
| 2354 | item->node = child; |
| 2355 | *locklistp = item; |
| 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | if (error) |
| 2360 | hammer_btree_unlock_children(locklistp); |
| 2361 | return(error); |
| 2362 | } |
| 2363 | |
| 2364 | |
| 2365 | /* |
| 2366 | * Release previously obtained node locks. |
| 2367 | */ |
| 2368 | void |
| 2369 | hammer_btree_unlock_children(struct hammer_node_locklist **locklistp) |
| 2370 | { |
| 2371 | hammer_node_locklist_t item; |
| 2372 | |
| 2373 | while ((item = *locklistp) != NULL) { |
| 2374 | *locklistp = item->next; |
| 2375 | hammer_unlock(&item->node->lock); |
| 2376 | hammer_rel_node(item->node); |
| 2377 | kfree(item, M_HAMMER); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | /************************************************************************ |
| 2382 | * MISCELLANIOUS SUPPORT * |
| 2383 | ************************************************************************/ |
| 2384 | |
| 2385 | /* |
| 2386 | * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp). |
| 2387 | * |
| 2388 | * Note that for this particular function a return value of -1, 0, or +1 |
| 2389 | * can denote a match if create_tid is otherwise discounted. A create_tid |
| 2390 | * of zero is considered to be 'infinity' in comparisons. |
| 2391 | * |
| 2392 | * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c. |
| 2393 | */ |
| 2394 | int |
| 2395 | hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2) |
| 2396 | { |
| 2397 | if (key1->localization < key2->localization) |
| 2398 | return(-5); |
| 2399 | if (key1->localization > key2->localization) |
| 2400 | return(5); |
| 2401 | |
| 2402 | if (key1->obj_id < key2->obj_id) |
| 2403 | return(-4); |
| 2404 | if (key1->obj_id > key2->obj_id) |
| 2405 | return(4); |
| 2406 | |
| 2407 | if (key1->rec_type < key2->rec_type) |
| 2408 | return(-3); |
| 2409 | if (key1->rec_type > key2->rec_type) |
| 2410 | return(3); |
| 2411 | |
| 2412 | if (key1->key < key2->key) |
| 2413 | return(-2); |
| 2414 | if (key1->key > key2->key) |
| 2415 | return(2); |
| 2416 | |
| 2417 | /* |
| 2418 | * A create_tid of zero indicates a record which is undeletable |
| 2419 | * and must be considered to have a value of positive infinity. |
| 2420 | */ |
| 2421 | if (key1->create_tid == 0) { |
| 2422 | if (key2->create_tid == 0) |
| 2423 | return(0); |
| 2424 | return(1); |
| 2425 | } |
| 2426 | if (key2->create_tid == 0) |
| 2427 | return(-1); |
| 2428 | if (key1->create_tid < key2->create_tid) |
| 2429 | return(-1); |
| 2430 | if (key1->create_tid > key2->create_tid) |
| 2431 | return(1); |
| 2432 | return(0); |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Test a timestamp against an element to determine whether the |
| 2437 | * element is visible. A timestamp of 0 means 'infinity'. |
| 2438 | */ |
| 2439 | int |
| 2440 | hammer_btree_chkts(hammer_tid_t asof, hammer_base_elm_t base) |
| 2441 | { |
| 2442 | if (asof == 0) { |
| 2443 | if (base->delete_tid) |
| 2444 | return(1); |
| 2445 | return(0); |
| 2446 | } |
| 2447 | if (asof < base->create_tid) |
| 2448 | return(-1); |
| 2449 | if (base->delete_tid && asof >= base->delete_tid) |
| 2450 | return(1); |
| 2451 | return(0); |
| 2452 | } |
| 2453 | |
| 2454 | /* |
| 2455 | * Create a separator half way inbetween key1 and key2. For fields just |
| 2456 | * one unit apart, the separator will match key2. key1 is on the left-hand |
| 2457 | * side and key2 is on the right-hand side. |
| 2458 | * |
| 2459 | * key2 must be >= the separator. It is ok for the separator to match key2. |
| 2460 | * |
| 2461 | * NOTE: Even if key1 does not match key2, the separator may wind up matching |
| 2462 | * key2. |
| 2463 | * |
| 2464 | * NOTE: It might be beneficial to just scrap this whole mess and just |
| 2465 | * set the separator to key2. |
| 2466 | */ |
| 2467 | #define MAKE_SEPARATOR(key1, key2, dest, field) \ |
| 2468 | dest->field = key1->field + ((key2->field - key1->field + 1) >> 1); |
| 2469 | |
| 2470 | static void |
| 2471 | hammer_make_separator(hammer_base_elm_t key1, hammer_base_elm_t key2, |
| 2472 | hammer_base_elm_t dest) |
| 2473 | { |
| 2474 | bzero(dest, sizeof(*dest)); |
| 2475 | |
| 2476 | dest->rec_type = key2->rec_type; |
| 2477 | dest->key = key2->key; |
| 2478 | dest->obj_id = key2->obj_id; |
| 2479 | dest->create_tid = key2->create_tid; |
| 2480 | |
| 2481 | MAKE_SEPARATOR(key1, key2, dest, localization); |
| 2482 | if (key1->localization == key2->localization) { |
| 2483 | MAKE_SEPARATOR(key1, key2, dest, obj_id); |
| 2484 | if (key1->obj_id == key2->obj_id) { |
| 2485 | MAKE_SEPARATOR(key1, key2, dest, rec_type); |
| 2486 | if (key1->rec_type == key2->rec_type) { |
| 2487 | MAKE_SEPARATOR(key1, key2, dest, key); |
| 2488 | /* |
| 2489 | * Don't bother creating a separator for |
| 2490 | * create_tid, which also conveniently avoids |
| 2491 | * having to handle the create_tid == 0 |
| 2492 | * (infinity) case. Just leave create_tid |
| 2493 | * set to key2. |
| 2494 | * |
| 2495 | * Worst case, dest matches key2 exactly, |
| 2496 | * which is acceptable. |
| 2497 | */ |
| 2498 | } |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | #undef MAKE_SEPARATOR |
| 2504 | |
| 2505 | /* |
| 2506 | * Return whether a generic internal or leaf node is full |
| 2507 | */ |
| 2508 | static int |
| 2509 | btree_node_is_full(hammer_node_ondisk_t node) |
| 2510 | { |
| 2511 | switch(node->type) { |
| 2512 | case HAMMER_BTREE_TYPE_INTERNAL: |
| 2513 | if (node->count == HAMMER_BTREE_INT_ELMS) |
| 2514 | return(1); |
| 2515 | break; |
| 2516 | case HAMMER_BTREE_TYPE_LEAF: |
| 2517 | if (node->count == HAMMER_BTREE_LEAF_ELMS) |
| 2518 | return(1); |
| 2519 | break; |
| 2520 | default: |
| 2521 | panic("illegal btree subtype"); |
| 2522 | } |
| 2523 | return(0); |
| 2524 | } |
| 2525 | |
| 2526 | #if 0 |
| 2527 | static int |
| 2528 | btree_max_elements(u_int8_t type) |
| 2529 | { |
| 2530 | if (type == HAMMER_BTREE_TYPE_LEAF) |
| 2531 | return(HAMMER_BTREE_LEAF_ELMS); |
| 2532 | if (type == HAMMER_BTREE_TYPE_INTERNAL) |
| 2533 | return(HAMMER_BTREE_INT_ELMS); |
| 2534 | panic("btree_max_elements: bad type %d\n", type); |
| 2535 | } |
| 2536 | #endif |
| 2537 | |
| 2538 | void |
| 2539 | hammer_print_btree_node(hammer_node_ondisk_t ondisk) |
| 2540 | { |
| 2541 | hammer_btree_elm_t elm; |
| 2542 | int i; |
| 2543 | |
| 2544 | kprintf("node %p count=%d parent=%016llx type=%c\n", |
| 2545 | ondisk, ondisk->count, ondisk->parent, ondisk->type); |
| 2546 | |
| 2547 | /* |
| 2548 | * Dump both boundary elements if an internal node |
| 2549 | */ |
| 2550 | if (ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) { |
| 2551 | for (i = 0; i <= ondisk->count; ++i) { |
| 2552 | elm = &ondisk->elms[i]; |
| 2553 | hammer_print_btree_elm(elm, ondisk->type, i); |
| 2554 | } |
| 2555 | } else { |
| 2556 | for (i = 0; i < ondisk->count; ++i) { |
| 2557 | elm = &ondisk->elms[i]; |
| 2558 | hammer_print_btree_elm(elm, ondisk->type, i); |
| 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | void |
| 2564 | hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i) |
| 2565 | { |
| 2566 | kprintf(" %2d", i); |
| 2567 | kprintf("\tobj_id = %016llx\n", elm->base.obj_id); |
| 2568 | kprintf("\tkey = %016llx\n", elm->base.key); |
| 2569 | kprintf("\tcreate_tid = %016llx\n", elm->base.create_tid); |
| 2570 | kprintf("\tdelete_tid = %016llx\n", elm->base.delete_tid); |
| 2571 | kprintf("\trec_type = %04x\n", elm->base.rec_type); |
| 2572 | kprintf("\tobj_type = %02x\n", elm->base.obj_type); |
| 2573 | kprintf("\tbtype = %02x (%c)\n", |
| 2574 | elm->base.btype, |
| 2575 | (elm->base.btype ? elm->base.btype : '?')); |
| 2576 | kprintf("\tlocalization = %02x\n", elm->base.localization); |
| 2577 | |
| 2578 | switch(type) { |
| 2579 | case HAMMER_BTREE_TYPE_INTERNAL: |
| 2580 | kprintf("\tsubtree_off = %016llx\n", |
| 2581 | elm->internal.subtree_offset); |
| 2582 | break; |
| 2583 | case HAMMER_BTREE_TYPE_RECORD: |
| 2584 | kprintf("\tdata_offset = %016llx\n", elm->leaf.data_offset); |
| 2585 | kprintf("\tdata_len = %08x\n", elm->leaf.data_len); |
| 2586 | kprintf("\tdata_crc = %08x\n", elm->leaf.data_crc); |
| 2587 | break; |
| 2588 | } |
| 2589 | } |