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