Remove udbp(4) form tree. It was never connected to the build and supports
[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 *
4e97774c 34 * $DragonFly: src/sys/vfs/hammer/hammer_btree.c,v 1.47 2008/05/13 05:04:39 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.
814387f6
MD
551 *
552 * Set ATEDISK when iterating backwards to skip the current entry,
553 * which after an ENOENT lookup will be pointing beyond our end point.
32c90105
MD
554 */
555int
556hammer_btree_last(hammer_cursor_t cursor)
557{
558 struct hammer_base_elm save;
559 int error;
560
561 save = cursor->key_beg;
562 cursor->key_beg = cursor->key_end;
563 error = hammer_btree_lookup(cursor);
564 cursor->key_beg = save;
565 if (error == ENOENT ||
566 (cursor->flags & HAMMER_CURSOR_END_INCLUSIVE) == 0) {
814387f6 567 cursor->flags |= HAMMER_CURSOR_ATEDISK;
32c90105
MD
568 error = hammer_btree_iterate_reverse(cursor);
569 }
570 cursor->flags |= HAMMER_CURSOR_ATEDISK;
571 return(error);
572}
573
8cd0a023
MD
574/*
575 * Extract the record and/or data associated with the cursor's current
576 * position. Any prior record or data stored in the cursor is replaced.
577 * The cursor must be positioned at a leaf node.
578 *
47197d71 579 * NOTE: All extractions occur at the leaf of the B-Tree.
8cd0a023 580 */
66325755 581int
8cd0a023 582hammer_btree_extract(hammer_cursor_t cursor, int flags)
66325755 583{
47197d71 584 hammer_mount_t hmp;
8cd0a023
MD
585 hammer_node_ondisk_t node;
586 hammer_btree_elm_t elm;
47197d71 587 hammer_off_t data_off;
19619882 588 int32_t data_len;
427e5fc6 589 int error;
427e5fc6 590
8cd0a023 591 /*
427e5fc6
MD
592 * The case where the data reference resolves to the same buffer
593 * as the record reference must be handled.
594 */
8cd0a023 595 node = cursor->node->ondisk;
8cd0a023 596 elm = &node->elms[cursor->index];
40043e7f
MD
597 cursor->data = NULL;
598 hmp = cursor->node->hmp;
47197d71 599 flags |= cursor->flags & HAMMER_CURSOR_DATAEXTOK;
66325755 600
d26d0ae9 601 /*
fe7678ee 602 * There is nothing to extract for an internal element.
d26d0ae9 603 */
fe7678ee
MD
604 if (node->type == HAMMER_BTREE_TYPE_INTERNAL)
605 return(EINVAL);
606
47197d71
MD
607 /*
608 * Only record types have data.
609 */
fe7678ee 610 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
11ad5ade 611 cursor->leaf = &elm->leaf;
47197d71
MD
612 if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD)
613 flags &= ~HAMMER_CURSOR_GET_DATA;
614 data_off = elm->leaf.data_offset;
19619882 615 data_len = elm->leaf.data_len;
47197d71
MD
616 if (data_off == 0)
617 flags &= ~HAMMER_CURSOR_GET_DATA;
d26d0ae9 618
11ad5ade
MD
619 error = 0;
620 if ((flags & HAMMER_CURSOR_GET_DATA)) {
621 /*
622 * Data and record are in different buffers.
623 */
624 cursor->data = hammer_bread(hmp, data_off, &error,
40043e7f 625 &cursor->data_buffer);
19619882
MD
626 KKASSERT(data_len >= 0 && data_len <= HAMMER_BUFSIZE);
627 if (data_len &&
628 crc32(cursor->data, data_len) != elm->leaf.data_crc) {
629 Debugger("CRC FAILED: DATA");
630 }
427e5fc6
MD
631 }
632 return(error);
633}
634
635
636/*
8cd0a023
MD
637 * Insert a leaf element into the B-Tree at the current cursor position.
638 * The cursor is positioned such that the element at and beyond the cursor
639 * are shifted to make room for the new record.
640 *
a89aec1b 641 * The caller must call hammer_btree_lookup() with the HAMMER_CURSOR_INSERT
8cd0a023
MD
642 * flag set and that call must return ENOENT before this function can be
643 * called.
644 *
d36ec43b 645 * The caller may depend on the cursor's exclusive lock after return to
1f07f686 646 * interlock frontend visibility (see HAMMER_RECF_CONVERT_DELETE).
d36ec43b 647 *
8cd0a023 648 * ENOSPC is returned if there is no room to insert a new record.
427e5fc6
MD
649 */
650int
11ad5ade 651hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm)
427e5fc6 652{
8cd0a023 653 hammer_node_ondisk_t node;
427e5fc6 654 int i;
6a37e7e4
MD
655 int error;
656
657 if ((error = hammer_cursor_upgrade(cursor)) != 0)
658 return(error);
427e5fc6 659
427e5fc6
MD
660 /*
661 * Insert the element at the leaf node and update the count in the
662 * parent. It is possible for parent to be NULL, indicating that
47197d71
MD
663 * the filesystem's ROOT B-Tree node is a leaf itself, which is
664 * possible. The root inode can never be deleted so the leaf should
665 * never be empty.
427e5fc6
MD
666 *
667 * Remember that the right-hand boundary is not included in the
668 * count.
669 */
36f82b23 670 hammer_modify_node_all(cursor->trans, cursor->node);
8cd0a023 671 node = cursor->node->ondisk;
427e5fc6 672 i = cursor->index;
fe7678ee 673 KKASSERT(elm->base.btype != 0);
8cd0a023
MD
674 KKASSERT(node->type == HAMMER_BTREE_TYPE_LEAF);
675 KKASSERT(node->count < HAMMER_BTREE_LEAF_ELMS);
676 if (i != node->count) {
677 bcopy(&node->elms[i], &node->elms[i+1],
678 (node->count - i) * sizeof(*elm));
679 }
11ad5ade 680 node->elms[i].leaf = *elm;
8cd0a023 681 ++node->count;
10a5d1ba 682 hammer_modify_node_done(cursor->node);
427e5fc6 683
eaeff70d 684 /*
47197d71 685 * Debugging sanity checks.
eaeff70d 686 */
11ad5ade
MD
687 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->base) <= 0);
688 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->base) > 0);
eaeff70d 689 if (i) {
11ad5ade 690 KKASSERT(hammer_btree_cmp(&node->elms[i-1].leaf.base, &elm->base) < 0);
eaeff70d 691 }
b3deaf57 692 if (i != node->count - 1)
11ad5ade 693 KKASSERT(hammer_btree_cmp(&node->elms[i+1].leaf.base, &elm->base) > 0);
b3deaf57 694
427e5fc6
MD
695 return(0);
696}
697
698/*
fe7678ee 699 * Delete a record from the B-Tree at the current cursor position.
8cd0a023
MD
700 * The cursor is positioned such that the current element is the one
701 * to be deleted.
702 *
195c19a1
MD
703 * On return the cursor will be positioned after the deleted element and
704 * MAY point to an internal node. It will be suitable for the continuation
705 * of an iteration but not for an insertion or deletion.
8cd0a023 706 *
195c19a1 707 * Deletions will attempt to partially rebalance the B-Tree in an upward
fe7678ee 708 * direction, but will terminate rather then deadlock. Empty leaves are
47197d71
MD
709 * not allowed. An early termination will leave an internal node with an
710 * element whos subtree_offset is 0, a case detected and handled by
711 * btree_search().
46fe7ae1
MD
712 *
713 * This function can return EDEADLK, requiring the caller to retry the
714 * operation after clearing the deadlock.
427e5fc6
MD
715 */
716int
8cd0a023 717hammer_btree_delete(hammer_cursor_t cursor)
427e5fc6 718{
8cd0a023
MD
719 hammer_node_ondisk_t ondisk;
720 hammer_node_t node;
721 hammer_node_t parent;
8cd0a023 722 int error;
427e5fc6
MD
723 int i;
724
6a37e7e4
MD
725 if ((error = hammer_cursor_upgrade(cursor)) != 0)
726 return(error);
727
427e5fc6 728 /*
8cd0a023 729 * Delete the element from the leaf node.
427e5fc6 730 *
8cd0a023 731 * Remember that leaf nodes do not have boundaries.
427e5fc6 732 */
8cd0a023
MD
733 node = cursor->node;
734 ondisk = node->ondisk;
427e5fc6
MD
735 i = cursor->index;
736
8cd0a023 737 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_LEAF);
fe7678ee 738 KKASSERT(i >= 0 && i < ondisk->count);
36f82b23 739 hammer_modify_node_all(cursor->trans, node);
8cd0a023
MD
740 if (i + 1 != ondisk->count) {
741 bcopy(&ondisk->elms[i+1], &ondisk->elms[i],
742 (ondisk->count - i - 1) * sizeof(ondisk->elms[0]));
743 }
744 --ondisk->count;
10a5d1ba 745 hammer_modify_node_done(node);
fe7678ee
MD
746
747 /*
748 * Validate local parent
749 */
750 if (ondisk->parent) {
8cd0a023 751 parent = cursor->parent;
fe7678ee
MD
752
753 KKASSERT(parent != NULL);
754 KKASSERT(parent->node_offset == ondisk->parent);
427e5fc6 755 }
427e5fc6 756
8cd0a023 757 /*
fe7678ee 758 * If the leaf becomes empty it must be detached from the parent,
47197d71 759 * potentially recursing through to the filesystem root.
195c19a1
MD
760 *
761 * This may reposition the cursor at one of the parent's of the
762 * current node.
6a37e7e4
MD
763 *
764 * Ignore deadlock errors, that simply means that btree_remove
765 * was unable to recurse and had to leave the subtree_offset
766 * in the parent set to 0.
8cd0a023 767 */
b3deaf57 768 KKASSERT(cursor->index <= ondisk->count);
8cd0a023 769 if (ondisk->count == 0) {
fe7678ee 770 do {
46fe7ae1 771 error = btree_remove(cursor);
fe7678ee 772 } while (error == EAGAIN);
6a37e7e4
MD
773 if (error == EDEADLK)
774 error = 0;
8cd0a023 775 } else {
8cd0a023
MD
776 error = 0;
777 }
eaeff70d
MD
778 KKASSERT(cursor->parent == NULL ||
779 cursor->parent_index < cursor->parent->ondisk->count);
8cd0a023
MD
780 return(error);
781}
427e5fc6
MD
782
783/*
8cd0a023
MD
784 * PRIMAY B-TREE SEARCH SUPPORT PROCEDURE
785 *
47197d71 786 * Search the filesystem B-Tree for cursor->key_beg, return the matching node.
8cd0a023 787 *
d26d0ae9
MD
788 * The search can begin ANYWHERE in the B-Tree. As a first step the search
789 * iterates up the tree as necessary to properly position itself prior to
790 * actually doing the sarch.
791 *
8cd0a023 792 * INSERTIONS: The search will split full nodes and leaves on its way down
d26d0ae9
MD
793 * and guarentee that the leaf it ends up on is not full. If we run out
794 * of space the search continues to the leaf (to position the cursor for
795 * the spike), but ENOSPC is returned.
427e5fc6 796 *
fbc6e32a
MD
797 * The search is only guarenteed to end up on a leaf if an error code of 0
798 * is returned, or if inserting and an error code of ENOENT is returned.
d26d0ae9 799 * Otherwise it can stop at an internal node. On success a search returns
47197d71 800 * a leaf node.
eaeff70d
MD
801 *
802 * COMPLEXITY WARNING! This is the core B-Tree search code for the entire
803 * filesystem, and it is not simple code. Please note the following facts:
804 *
805 * - Internal node recursions have a boundary on the left AND right. The
9582c7da 806 * right boundary is non-inclusive. The create_tid is a generic part
eaeff70d
MD
807 * of the key for internal nodes.
808 *
47197d71 809 * - Leaf nodes contain terminal elements only now.
eaeff70d
MD
810 *
811 * - Filesystem lookups typically set HAMMER_CURSOR_ASOF, indicating a
b33e2cc0
MD
812 * historical search. ASOF and INSERT are mutually exclusive. When
813 * doing an as-of lookup btree_search() checks for a right-edge boundary
9582c7da
MD
814 * case. If while recursing down the left-edge differs from the key
815 * by ONLY its create_tid, HAMMER_CURSOR_CREATE_CHECK is set along
816 * with cursor->create_check. This is used by btree_lookup() to iterate.
817 * The iteration backwards because as-of searches can wind up going
b33e2cc0 818 * down the wrong branch of the B-Tree.
427e5fc6 819 */
8cd0a023 820static
427e5fc6 821int
8cd0a023 822btree_search(hammer_cursor_t cursor, int flags)
427e5fc6 823{
8cd0a023 824 hammer_node_ondisk_t node;
61aeeb33 825 hammer_btree_elm_t elm;
8cd0a023 826 int error;
d26d0ae9 827 int enospc = 0;
8cd0a023
MD
828 int i;
829 int r;
b33e2cc0 830 int s;
8cd0a023
MD
831
832 flags |= cursor->flags;
833
b3deaf57 834 if (hammer_debug_btree) {
a84a197d 835 kprintf("SEARCH %016llx[%d] %016llx %02x key=%016llx cre=%016llx (td = %p)\n",
eaeff70d
MD
836 cursor->node->node_offset,
837 cursor->index,
b3deaf57
MD
838 cursor->key_beg.obj_id,
839 cursor->key_beg.rec_type,
d113fda1 840 cursor->key_beg.key,
a84a197d
MD
841 cursor->key_beg.create_tid,
842 curthread
b3deaf57 843 );
a84a197d
MD
844 if (cursor->parent)
845 kprintf("SEARCHP %016llx[%d] (%016llx/%016llx %016llx/%016llx) (%p/%p %p/%p)\n",
846 cursor->parent->node_offset, cursor->parent_index,
847 cursor->left_bound->obj_id,
848 cursor->parent->ondisk->elms[cursor->parent_index].internal.base.obj_id,
849 cursor->right_bound->obj_id,
850 cursor->parent->ondisk->elms[cursor->parent_index+1].internal.base.obj_id,
851 cursor->left_bound,
852 &cursor->parent->ondisk->elms[cursor->parent_index],
853 cursor->right_bound,
854 &cursor->parent->ondisk->elms[cursor->parent_index+1]
855 );
b3deaf57
MD
856 }
857
8cd0a023
MD
858 /*
859 * Move our cursor up the tree until we find a node whos range covers
47197d71 860 * the key we are trying to locate.
8cd0a023
MD
861 *
862 * The left bound is inclusive, the right bound is non-inclusive.
47197d71 863 * It is ok to cursor up too far.
8cd0a023 864 */
b33e2cc0
MD
865 for (;;) {
866 r = hammer_btree_cmp(&cursor->key_beg, cursor->left_bound);
867 s = hammer_btree_cmp(&cursor->key_beg, cursor->right_bound);
868 if (r >= 0 && s < 0)
869 break;
9944ae54 870 KKASSERT(cursor->parent);
6a37e7e4 871 error = hammer_cursor_up(cursor);
8cd0a023
MD
872 if (error)
873 goto done;
427e5fc6 874 }
427e5fc6 875
b33e2cc0
MD
876 /*
877 * The delete-checks below are based on node, not parent. Set the
878 * initial delete-check based on the parent.
879 */
9582c7da
MD
880 if (r == 1) {
881 KKASSERT(cursor->left_bound->create_tid != 1);
882 cursor->create_check = cursor->left_bound->create_tid - 1;
883 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
b33e2cc0
MD
884 }
885
8cd0a023 886 /*
47197d71 887 * We better have ended up with a node somewhere.
8cd0a023 888 */
47197d71 889 KKASSERT(cursor->node != NULL);
8cd0a023
MD
890
891 /*
892 * If we are inserting we can't start at a full node if the parent
893 * is also full (because there is no way to split the node),
b33e2cc0 894 * continue running up the tree until the requirement is satisfied
47197d71 895 * or we hit the root of the filesystem.
9582c7da
MD
896 *
897 * (If inserting we aren't doing an as-of search so we don't have
898 * to worry about create_check).
8cd0a023 899 */
61aeeb33 900 while ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
eaeff70d
MD
901 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
902 if (btree_node_is_full(cursor->node->ondisk) == 0)
903 break;
904 } else {
47197d71 905 if (btree_node_is_full(cursor->node->ondisk) ==0)
eaeff70d
MD
906 break;
907 }
b33e2cc0
MD
908 if (cursor->node->ondisk->parent == 0 ||
909 cursor->parent->ondisk->count != HAMMER_BTREE_INT_ELMS) {
8cd0a023 910 break;
b33e2cc0 911 }
6a37e7e4 912 error = hammer_cursor_up(cursor);
47197d71 913 /* node may have become stale */
8cd0a023
MD
914 if (error)
915 goto done;
427e5fc6 916 }
427e5fc6 917
47197d71 918re_search:
8cd0a023
MD
919 /*
920 * Push down through internal nodes to locate the requested key.
921 */
8cd0a023
MD
922 node = cursor->node->ondisk;
923 while (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
8cd0a023
MD
924 /*
925 * Scan the node to find the subtree index to push down into.
fbc6e32a 926 * We go one-past, then back-up.
d113fda1 927 *
fe7678ee
MD
928 * We must proactively remove deleted elements which may
929 * have been left over from a deadlocked btree_remove().
930 *
eaeff70d 931 * The left and right boundaries are included in the loop
d5530d22 932 * in order to detect edge cases.
9944ae54 933 *
9582c7da 934 * If the separator only differs by create_tid (r == 1)
eaeff70d
MD
935 * and we are doing an as-of search, we may end up going
936 * down a branch to the left of the one containing the
937 * desired key. This requires numerous special cases.
8cd0a023 938 */
46fe7ae1 939 if (hammer_debug_btree) {
47197d71 940 kprintf("SEARCH-I %016llx count=%d\n",
46fe7ae1
MD
941 cursor->node->node_offset,
942 node->count);
943 }
9944ae54 944 for (i = 0; i <= node->count; ++i) {
61aeeb33
MD
945 elm = &node->elms[i];
946 r = hammer_btree_cmp(&cursor->key_beg, &elm->base);
b33e2cc0
MD
947 if (hammer_debug_btree > 2) {
948 kprintf(" IELM %p %d r=%d\n",
949 &node->elms[i], i, r);
950 }
9582c7da 951 if (r < 0)
8cd0a023 952 break;
9582c7da
MD
953 if (r == 1) {
954 KKASSERT(elm->base.create_tid != 1);
955 cursor->create_check = elm->base.create_tid - 1;
956 cursor->flags |= HAMMER_CURSOR_CREATE_CHECK;
b33e2cc0 957 }
8cd0a023 958 }
eaeff70d 959 if (hammer_debug_btree) {
46fe7ae1
MD
960 kprintf("SEARCH-I preI=%d/%d r=%d\n",
961 i, node->count, r);
eaeff70d 962 }
8cd0a023
MD
963
964 /*
9944ae54
MD
965 * These cases occur when the parent's idea of the boundary
966 * is wider then the child's idea of the boundary, and
967 * require special handling. If not inserting we can
968 * terminate the search early for these cases but the
969 * child's boundaries cannot be unconditionally modified.
8cd0a023 970 */
fbc6e32a 971 if (i == 0) {
9944ae54
MD
972 /*
973 * If i == 0 the search terminated to the LEFT of the
974 * left_boundary but to the RIGHT of the parent's left
975 * boundary.
976 */
fbc6e32a 977 u_int8_t save;
d26d0ae9 978
eaeff70d
MD
979 elm = &node->elms[0];
980
981 /*
982 * If we aren't inserting we can stop here.
983 */
11ad5ade
MD
984 if ((flags & (HAMMER_CURSOR_INSERT |
985 HAMMER_CURSOR_PRUNING)) == 0) {
fbc6e32a
MD
986 cursor->index = 0;
987 return(ENOENT);
988 }
9944ae54 989
d5530d22
MD
990 /*
991 * Correct a left-hand boundary mismatch.
6a37e7e4 992 *
eaeff70d 993 * We can only do this if we can upgrade the lock.
10a5d1ba
MD
994 *
995 * WARNING: We can only do this if inserting, i.e.
996 * we are running on the backend.
d5530d22 997 */
eaeff70d
MD
998 if ((error = hammer_cursor_upgrade(cursor)) != 0)
999 return(error);
10a5d1ba 1000 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
c9b9e29d
MD
1001 hammer_modify_node_field(cursor->trans, cursor->node,
1002 elms[0]);
fe7678ee 1003 save = node->elms[0].base.btype;
d5530d22 1004 node->elms[0].base = *cursor->left_bound;
fe7678ee 1005 node->elms[0].base.btype = save;
10a5d1ba 1006 hammer_modify_node_done(cursor->node);
9944ae54 1007 } else if (i == node->count + 1) {
d26d0ae9 1008 /*
9944ae54
MD
1009 * If i == node->count + 1 the search terminated to
1010 * the RIGHT of the right boundary but to the LEFT
eaeff70d
MD
1011 * of the parent's right boundary. If we aren't
1012 * inserting we can stop here.
d113fda1 1013 *
9944ae54
MD
1014 * Note that the last element in this case is
1015 * elms[i-2] prior to adjustments to 'i'.
d26d0ae9 1016 */
9944ae54 1017 --i;
11ad5ade
MD
1018 if ((flags & (HAMMER_CURSOR_INSERT |
1019 HAMMER_CURSOR_PRUNING)) == 0) {
9944ae54 1020 cursor->index = i;
eaeff70d 1021 return (ENOENT);
d26d0ae9
MD
1022 }
1023
d5530d22
MD
1024 /*
1025 * Correct a right-hand boundary mismatch.
1026 * (actual push-down record is i-2 prior to
1027 * adjustments to i).
6a37e7e4 1028 *
eaeff70d 1029 * We can only do this if we can upgrade the lock.
10a5d1ba
MD
1030 *
1031 * WARNING: We can only do this if inserting, i.e.
1032 * we are running on the backend.
d5530d22 1033 */
eaeff70d
MD
1034 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1035 return(error);
9944ae54 1036 elm = &node->elms[i];
10a5d1ba 1037 KKASSERT(cursor->flags & HAMMER_CURSOR_BACKEND);
36f82b23
MD
1038 hammer_modify_node(cursor->trans, cursor->node,
1039 &elm->base, sizeof(elm->base));
d5530d22 1040 elm->base = *cursor->right_bound;
10a5d1ba 1041 hammer_modify_node_done(cursor->node);
d5530d22 1042 --i;
fbc6e32a
MD
1043 } else {
1044 /*
9944ae54
MD
1045 * The push-down index is now i - 1. If we had
1046 * terminated on the right boundary this will point
1047 * us at the last element.
fbc6e32a
MD
1048 */
1049 --i;
1050 }
8cd0a023 1051 cursor->index = i;
6a37e7e4 1052 elm = &node->elms[i];
8cd0a023 1053
b3deaf57 1054 if (hammer_debug_btree) {
47197d71 1055 kprintf("RESULT-I %016llx[%d] %016llx %02x "
9582c7da 1056 "key=%016llx cre=%016llx\n",
eaeff70d
MD
1057 cursor->node->node_offset,
1058 i,
b3deaf57
MD
1059 elm->internal.base.obj_id,
1060 elm->internal.base.rec_type,
d113fda1 1061 elm->internal.base.key,
9582c7da 1062 elm->internal.base.create_tid
b3deaf57
MD
1063 );
1064 }
1065
6a37e7e4
MD
1066 /*
1067 * When searching try to clean up any deleted
1068 * internal elements left over from btree_remove()
1069 * deadlocks.
1070 *
1071 * If we fail and we are doing an insertion lookup,
1072 * we have to return EDEADLK, because an insertion lookup
1073 * must terminate at a leaf.
1074 */
1075 if (elm->internal.subtree_offset == 0) {
1076 error = btree_remove_deleted_element(cursor);
1077 if (error == 0)
47197d71 1078 goto re_search;
eaeff70d
MD
1079 if (error == EDEADLK &&
1080 (flags & HAMMER_CURSOR_INSERT) == 0) {
1081 error = ENOENT;
1082 }
1083 return(error);
6a37e7e4
MD
1084 }
1085
1086
8cd0a023
MD
1087 /*
1088 * Handle insertion and deletion requirements.
1089 *
1090 * If inserting split full nodes. The split code will
1091 * adjust cursor->node and cursor->index if the current
1092 * index winds up in the new node.
61aeeb33 1093 *
9944ae54
MD
1094 * If inserting and a left or right edge case was detected,
1095 * we cannot correct the left or right boundary and must
1096 * prepend and append an empty leaf node in order to make
1097 * the boundary correction.
1098 *
61aeeb33
MD
1099 * If we run out of space we set enospc and continue on
1100 * to a leaf to provide the spike code with a good point
47197d71 1101 * of entry.
8cd0a023 1102 */
61aeeb33 1103 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0) {
fe7678ee 1104 if (btree_node_is_full(node)) {
8cd0a023 1105 error = btree_split_internal(cursor);
d26d0ae9
MD
1106 if (error) {
1107 if (error != ENOSPC)
1108 goto done;
1109 enospc = 1;
d26d0ae9 1110 }
8cd0a023
MD
1111 /*
1112 * reload stale pointers
1113 */
1114 i = cursor->index;
1115 node = cursor->node->ondisk;
1116 }
d26d0ae9 1117 }
427e5fc6
MD
1118
1119 /*
8cd0a023 1120 * Push down (push into new node, existing node becomes
d26d0ae9 1121 * the parent) and continue the search.
427e5fc6 1122 */
8cd0a023 1123 error = hammer_cursor_down(cursor);
47197d71 1124 /* node may have become stale */
8cd0a023
MD
1125 if (error)
1126 goto done;
1127 node = cursor->node->ondisk;
427e5fc6 1128 }
427e5fc6 1129
8cd0a023
MD
1130 /*
1131 * We are at a leaf, do a linear search of the key array.
d26d0ae9 1132 *
fe7678ee 1133 * If we encounter a spike element type within the necessary
47197d71 1134 * range we push into it.
fe7678ee 1135 *
d26d0ae9
MD
1136 * On success the index is set to the matching element and 0
1137 * is returned.
1138 *
1139 * On failure the index is set to the insertion point and ENOENT
1140 * is returned.
8cd0a023
MD
1141 *
1142 * Boundaries are not stored in leaf nodes, so the index can wind
1143 * up to the left of element 0 (index == 0) or past the end of
1144 * the array (index == node->count).
1145 */
fe7678ee 1146 KKASSERT (node->type == HAMMER_BTREE_TYPE_LEAF);
8cd0a023 1147 KKASSERT(node->count <= HAMMER_BTREE_LEAF_ELMS);
46fe7ae1 1148 if (hammer_debug_btree) {
47197d71 1149 kprintf("SEARCH-L %016llx count=%d\n",
46fe7ae1
MD
1150 cursor->node->node_offset,
1151 node->count);
1152 }
8cd0a023
MD
1153
1154 for (i = 0; i < node->count; ++i) {
fe7678ee
MD
1155 elm = &node->elms[i];
1156
1157 r = hammer_btree_cmp(&cursor->key_beg, &elm->leaf.base);
427e5fc6 1158
d5530d22
MD
1159 if (hammer_debug_btree > 1)
1160 kprintf(" ELM %p %d r=%d\n", &node->elms[i], i, r);
1161
427e5fc6 1162 /*
fe7678ee 1163 * We are at a record element. Stop if we've flipped past
9582c7da
MD
1164 * key_beg, not counting the create_tid test. Allow the
1165 * r == 1 case (key_beg > element but differs only by its
1166 * create_tid) to fall through to the AS-OF check.
427e5fc6 1167 */
fe7678ee
MD
1168 KKASSERT (elm->leaf.base.btype == HAMMER_BTREE_TYPE_RECORD);
1169
9582c7da 1170 if (r < 0)
d5530d22 1171 goto failed;
9582c7da 1172 if (r > 1)
d5530d22 1173 continue;
427e5fc6 1174
66325755 1175 /*
9582c7da 1176 * Check our as-of timestamp against the element.
66325755 1177 */
eaeff70d 1178 if (flags & HAMMER_CURSOR_ASOF) {
fe7678ee 1179 if (hammer_btree_chkts(cursor->asof,
d113fda1
MD
1180 &node->elms[i].base) != 0) {
1181 continue;
1182 }
eaeff70d
MD
1183 /* success */
1184 } else {
9582c7da
MD
1185 if (r > 0) /* can only be +1 */
1186 continue;
eaeff70d 1187 /* success */
66325755 1188 }
d5530d22
MD
1189 cursor->index = i;
1190 error = 0;
eaeff70d 1191 if (hammer_debug_btree) {
47197d71
MD
1192 kprintf("RESULT-L %016llx[%d] (SUCCESS)\n",
1193 cursor->node->node_offset, i);
eaeff70d 1194 }
d5530d22
MD
1195 goto done;
1196 }
1197
1198 /*
eaeff70d 1199 * The search of the leaf node failed. i is the insertion point.
d5530d22 1200 */
d5530d22 1201failed:
b3deaf57 1202 if (hammer_debug_btree) {
47197d71
MD
1203 kprintf("RESULT-L %016llx[%d] (FAILED)\n",
1204 cursor->node->node_offset, i);
b3deaf57
MD
1205 }
1206
8cd0a023
MD
1207 /*
1208 * No exact match was found, i is now at the insertion point.
1209 *
1210 * If inserting split a full leaf before returning. This
1211 * may have the side effect of adjusting cursor->node and
1212 * cursor->index.
1213 */
1214 cursor->index = i;
eaeff70d 1215 if ((flags & HAMMER_CURSOR_INSERT) && enospc == 0 &&
47197d71 1216 btree_node_is_full(node)) {
8cd0a023 1217 error = btree_split_leaf(cursor);
d26d0ae9
MD
1218 if (error) {
1219 if (error != ENOSPC)
1220 goto done;
1221 enospc = 1;
d26d0ae9
MD
1222 }
1223 /*
1224 * reload stale pointers
1225 */
8cd0a023
MD
1226 /* NOT USED
1227 i = cursor->index;
1228 node = &cursor->node->internal;
1229 */
8cd0a023 1230 }
d26d0ae9
MD
1231
1232 /*
1233 * We reached a leaf but did not find the key we were looking for.
1234 * If this is an insert we will be properly positioned for an insert
1235 * (ENOENT) or spike (ENOSPC) operation.
1236 */
1237 error = enospc ? ENOSPC : ENOENT;
8cd0a023 1238done:
427e5fc6
MD
1239 return(error);
1240}
1241
8cd0a023 1242
427e5fc6 1243/************************************************************************
8cd0a023 1244 * SPLITTING AND MERGING *
427e5fc6
MD
1245 ************************************************************************
1246 *
1247 * These routines do all the dirty work required to split and merge nodes.
1248 */
1249
1250/*
8cd0a023 1251 * Split an internal node into two nodes and move the separator at the split
fe7678ee 1252 * point to the parent.
427e5fc6 1253 *
8cd0a023
MD
1254 * (cursor->node, cursor->index) indicates the element the caller intends
1255 * to push into. We will adjust node and index if that element winds
427e5fc6 1256 * up in the split node.
8cd0a023 1257 *
47197d71
MD
1258 * If we are at the root of the filesystem a new root must be created with
1259 * two elements, one pointing to the original root and one pointing to the
8cd0a023 1260 * newly allocated split node.
427e5fc6
MD
1261 */
1262static
1263int
8cd0a023 1264btree_split_internal(hammer_cursor_t cursor)
427e5fc6 1265{
8cd0a023
MD
1266 hammer_node_ondisk_t ondisk;
1267 hammer_node_t node;
1268 hammer_node_t parent;
1269 hammer_node_t new_node;
1270 hammer_btree_elm_t elm;
1271 hammer_btree_elm_t parent_elm;
b33e2cc0 1272 hammer_node_locklist_t locklist = NULL;
36f82b23 1273 hammer_mount_t hmp = cursor->trans->hmp;
427e5fc6
MD
1274 int parent_index;
1275 int made_root;
1276 int split;
1277 int error;
7f7c1f84 1278 int i;
8cd0a023 1279 const int esize = sizeof(*elm);
427e5fc6 1280
6a37e7e4
MD
1281 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1282 return(error);
47197d71
MD
1283 error = hammer_btree_lock_children(cursor, &locklist);
1284 if (error)
1285 goto done;
6a37e7e4 1286
427e5fc6
MD
1287 /*
1288 * We are splitting but elms[split] will be promoted to the parent,
1289 * leaving the right hand node with one less element. If the
1290 * insertion point will be on the left-hand side adjust the split
1291 * point to give the right hand side one additional node.
1292 */
8cd0a023
MD
1293 node = cursor->node;
1294 ondisk = node->ondisk;
1295 split = (ondisk->count + 1) / 2;
427e5fc6
MD
1296 if (cursor->index <= split)
1297 --split;
427e5fc6
MD
1298
1299 /*
47197d71
MD
1300 * If we are at the root of the filesystem, create a new root node
1301 * with 1 element and split normally. Avoid making major
1302 * modifications until we know the whole operation will work.
427e5fc6 1303 */
8cd0a023 1304 if (ondisk->parent == 0) {
36f82b23 1305 parent = hammer_alloc_btree(cursor->trans, &error);
427e5fc6 1306 if (parent == NULL)
6a37e7e4 1307 goto done;
8cd0a023 1308 hammer_lock_ex(&parent->lock);
36f82b23 1309 hammer_modify_node_noundo(cursor->trans, parent);
8cd0a023
MD
1310 ondisk = parent->ondisk;
1311 ondisk->count = 1;
1312 ondisk->parent = 0;
1313 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
47197d71 1314 ondisk->elms[0].base = hmp->root_btree_beg;
fe7678ee 1315 ondisk->elms[0].base.btype = node->ondisk->type;
8cd0a023 1316 ondisk->elms[0].internal.subtree_offset = node->node_offset;
47197d71 1317 ondisk->elms[1].base = hmp->root_btree_end;
10a5d1ba 1318 hammer_modify_node_done(parent);
fe7678ee 1319 /* ondisk->elms[1].base.btype - not used */
427e5fc6 1320 made_root = 1;
8cd0a023 1321 parent_index = 0; /* index of current node in parent */
427e5fc6
MD
1322 } else {
1323 made_root = 0;
8cd0a023
MD
1324 parent = cursor->parent;
1325 parent_index = cursor->parent_index;
427e5fc6 1326 }
427e5fc6
MD
1327
1328 /*
1329 * Split node into new_node at the split point.
1330 *
1331 * B O O O P N N B <-- P = node->elms[split]
1332 * 0 1 2 3 4 5 6 <-- subtree indices
1333 *
1334 * x x P x x
1335 * s S S s
1336 * / \
1337 * B O O O B B N N B <--- inner boundary points are 'P'
1338 * 0 1 2 3 4 5 6
1339 *
1340 */
36f82b23 1341 new_node = hammer_alloc_btree(cursor->trans, &error);
427e5fc6 1342 if (new_node == NULL) {
8cd0a023
MD
1343 if (made_root) {
1344 hammer_unlock(&parent->lock);
36f82b23 1345 hammer_delete_node(cursor->trans, parent);
8cd0a023
MD
1346 hammer_rel_node(parent);
1347 }
6a37e7e4 1348 goto done;
427e5fc6 1349 }
8cd0a023 1350 hammer_lock_ex(&new_node->lock);
427e5fc6
MD
1351
1352 /*
8cd0a023 1353 * Create the new node. P becomes the left-hand boundary in the
427e5fc6
MD
1354 * new node. Copy the right-hand boundary as well.
1355 *
1356 * elm is the new separator.
1357 */
36f82b23
MD
1358 hammer_modify_node_noundo(cursor->trans, new_node);
1359 hammer_modify_node_all(cursor->trans, node);
8cd0a023
MD
1360 ondisk = node->ondisk;
1361 elm = &ondisk->elms[split];
1362 bcopy(elm, &new_node->ondisk->elms[0],
1363 (ondisk->count - split + 1) * esize);
1364 new_node->ondisk->count = ondisk->count - split;
1365 new_node->ondisk->parent = parent->node_offset;
1366 new_node->ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
1367 KKASSERT(ondisk->type == new_node->ondisk->type);
427e5fc6
MD
1368
1369 /*
fe7678ee
MD
1370 * Cleanup the original node. Elm (P) becomes the new boundary,
1371 * its subtree_offset was moved to the new node. If we had created
427e5fc6
MD
1372 * a new root its parent pointer may have changed.
1373 */
8cd0a023 1374 elm->internal.subtree_offset = 0;
c0ade690 1375 ondisk->count = split;
427e5fc6
MD
1376
1377 /*
1378 * Insert the separator into the parent, fixup the parent's
1379 * reference to the original node, and reference the new node.
1380 * The separator is P.
1381 *
1382 * Remember that base.count does not include the right-hand boundary.
1383 */
36f82b23 1384 hammer_modify_node_all(cursor->trans, parent);
8cd0a023 1385 ondisk = parent->ondisk;
d26d0ae9 1386 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
8cd0a023 1387 parent_elm = &ondisk->elms[parent_index+1];
427e5fc6 1388 bcopy(parent_elm, parent_elm + 1,
8cd0a023
MD
1389 (ondisk->count - parent_index) * esize);
1390 parent_elm->internal.base = elm->base; /* separator P */
fe7678ee 1391 parent_elm->internal.base.btype = new_node->ondisk->type;
8cd0a023 1392 parent_elm->internal.subtree_offset = new_node->node_offset;
76376933 1393 ++ondisk->count;
10a5d1ba 1394 hammer_modify_node_done(parent);
427e5fc6 1395
7f7c1f84
MD
1396 /*
1397 * The children of new_node need their parent pointer set to new_node.
b33e2cc0
MD
1398 * The children have already been locked by
1399 * hammer_btree_lock_children().
7f7c1f84
MD
1400 */
1401 for (i = 0; i < new_node->ondisk->count; ++i) {
1402 elm = &new_node->ondisk->elms[i];
36f82b23 1403 error = btree_set_parent(cursor->trans, new_node, elm);
7f7c1f84
MD
1404 if (error) {
1405 panic("btree_split_internal: btree-fixup problem");
1406 }
1407 }
10a5d1ba 1408 hammer_modify_node_done(new_node);
7f7c1f84 1409
427e5fc6 1410 /*
47197d71 1411 * The filesystem's root B-Tree pointer may have to be updated.
427e5fc6
MD
1412 */
1413 if (made_root) {
47197d71
MD
1414 hammer_volume_t volume;
1415
1416 volume = hammer_get_root_volume(hmp, &error);
1417 KKASSERT(error == 0);
1418
e8599db1
MD
1419 hammer_modify_volume_field(cursor->trans, volume,
1420 vol0_btree_root);
47197d71 1421 volume->ondisk->vol0_btree_root = parent->node_offset;
10a5d1ba 1422 hammer_modify_volume_done(volume);
8cd0a023
MD
1423 node->ondisk->parent = parent->node_offset;
1424 if (cursor->parent) {
1425 hammer_unlock(&cursor->parent->lock);
1426 hammer_rel_node(cursor->parent);
1427 }
1428 cursor->parent = parent; /* lock'd and ref'd */
47197d71 1429 hammer_rel_volume(volume, 0);
427e5fc6 1430 }
10a5d1ba 1431 hammer_modify_node_done(node);
427e5fc6 1432
8cd0a023 1433
427e5fc6
MD
1434 /*
1435 * Ok, now adjust the cursor depending on which element the original
1436 * index was pointing at. If we are >= the split point the push node
1437 * is now in the new node.
1438 *
1439 * NOTE: If we are at the split point itself we cannot stay with the
1440 * original node because the push index will point at the right-hand
1441 * boundary, which is illegal.
8cd0a023
MD
1442 *
1443 * NOTE: The cursor's parent or parent_index must be adjusted for
1444 * the case where a new parent (new root) was created, and the case
1445 * where the cursor is now pointing at the split node.
427e5fc6
MD
1446 */
1447 if (cursor->index >= split) {
8cd0a023 1448 cursor->parent_index = parent_index + 1;
427e5fc6 1449 cursor->index -= split;
8cd0a023
MD
1450 hammer_unlock(&cursor->node->lock);
1451 hammer_rel_node(cursor->node);
1452 cursor->node = new_node; /* locked and ref'd */
1453 } else {
1454 cursor->parent_index = parent_index;
1455 hammer_unlock(&new_node->lock);
1456 hammer_rel_node(new_node);
427e5fc6 1457 }
76376933
MD
1458
1459 /*
1460 * Fixup left and right bounds
1461 */
1462 parent_elm = &parent->ondisk->elms[cursor->parent_index];
fbc6e32a
MD
1463 cursor->left_bound = &parent_elm[0].internal.base;
1464 cursor->right_bound = &parent_elm[1].internal.base;
b3deaf57
MD
1465 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1466 &cursor->node->ondisk->elms[0].internal.base) <= 0);
1467 KKASSERT(hammer_btree_cmp(cursor->right_bound,
9944ae54 1468 &cursor->node->ondisk->elms[cursor->node->ondisk->count].internal.base) >= 0);
76376933 1469
6a37e7e4 1470done:
b33e2cc0 1471 hammer_btree_unlock_children(&locklist);
6a37e7e4
MD
1472 hammer_cursor_downgrade(cursor);
1473 return (error);
427e5fc6
MD
1474}
1475
1476/*
1477 * Same as the above, but splits a full leaf node.
6a37e7e4
MD
1478 *
1479 * This function
427e5fc6
MD
1480 */
1481static
1482int
8cd0a023 1483btree_split_leaf(hammer_cursor_t cursor)
427e5fc6 1484{
8cd0a023
MD
1485 hammer_node_ondisk_t ondisk;
1486 hammer_node_t parent;
1487 hammer_node_t leaf;
47197d71 1488 hammer_mount_t hmp;
8cd0a023
MD
1489 hammer_node_t new_leaf;
1490 hammer_btree_elm_t elm;
1491 hammer_btree_elm_t parent_elm;
b3deaf57 1492 hammer_base_elm_t mid_boundary;
427e5fc6
MD
1493 int parent_index;
1494 int made_root;
1495 int split;
1496 int error;
8cd0a023 1497 const size_t esize = sizeof(*elm);
427e5fc6 1498
6a37e7e4
MD
1499 if ((error = hammer_cursor_upgrade(cursor)) != 0)
1500 return(error);
1501
36f82b23
MD
1502 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1503 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
1504 KKASSERT(hammer_btree_cmp(cursor->right_bound,
1505 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
1506
427e5fc6 1507 /*
8cd0a023
MD
1508 * Calculate the split point. If the insertion point will be on
1509 * the left-hand side adjust the split point to give the right
1510 * hand side one additional node.
fe7678ee
MD
1511 *
1512 * Spikes are made up of two leaf elements which cannot be
1513 * safely split.
427e5fc6 1514 */
8cd0a023
MD
1515 leaf = cursor->node;
1516 ondisk = leaf->ondisk;
1517 split = (ondisk->count + 1) / 2;
427e5fc6
MD
1518 if (cursor->index <= split)
1519 --split;
1520 error = 0;
40043e7f 1521 hmp = leaf->hmp;
427e5fc6 1522
fe7678ee 1523 elm = &ondisk->elms[split];
fe7678ee 1524
36f82b23
MD
1525 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm[-1].leaf.base) <= 0);
1526 KKASSERT(hammer_btree_cmp(cursor->left_bound, &elm->leaf.base) <= 0);
1527 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm->leaf.base) > 0);
1528 KKASSERT(hammer_btree_cmp(cursor->right_bound, &elm[1].leaf.base) > 0);
1529
427e5fc6
MD
1530 /*
1531 * If we are at the root of the tree, create a new root node with
1532 * 1 element and split normally. Avoid making major modifications
1533 * until we know the whole operation will work.
1534 */
8cd0a023 1535 if (ondisk->parent == 0) {
36f82b23 1536 parent = hammer_alloc_btree(cursor->trans, &error);
427e5fc6 1537 if (parent == NULL)
6a37e7e4 1538 goto done;
8cd0a023 1539 hammer_lock_ex(&parent->lock);
36f82b23 1540 hammer_modify_node_noundo(cursor->trans, parent);
8cd0a023
MD
1541 ondisk = parent->ondisk;
1542 ondisk->count = 1;
1543 ondisk->parent = 0;
1544 ondisk->type = HAMMER_BTREE_TYPE_INTERNAL;
47197d71 1545 ondisk->elms[0].base = hmp->root_btree_beg;
fe7678ee 1546 ondisk->elms[0].base.btype = leaf->ondisk->type;
8cd0a023 1547 ondisk->elms[0].internal.subtree_offset = leaf->node_offset;
47197d71 1548 ondisk->elms[1].base = hmp->root_btree_end;
fe7678ee 1549 /* ondisk->elms[1].base.btype = not used */
10a5d1ba 1550 hammer_modify_node_done(parent);
427e5fc6 1551 made_root = 1;
8cd0a023 1552 parent_index = 0; /* insertion point in parent */
427e5fc6
MD
1553 } else {
1554 made_root = 0;
8cd0a023
MD
1555 parent = cursor->parent;
1556 parent_index = cursor->parent_index;
427e5fc6 1557 }
427e5fc6
MD
1558
1559 /*
1560 * Split leaf into new_leaf at the split point. Select a separator
1561 * value in-between the two leafs but with a bent towards the right
1562 * leaf since comparisons use an 'elm >= separator' inequality.
1563 *
1564 * L L L L L L L L
1565 *
1566 * x x P x x
1567 * s S S s
1568 * / \
1569 * L L L L L L L L
1570 */
36f82b23 1571 new_leaf = hammer_alloc_btree(cursor->trans, &error);
427e5fc6 1572 if (new_leaf == NULL) {
8cd0a023
MD
1573 if (made_root) {
1574 hammer_unlock(&parent->lock);
36f82b23 1575 hammer_delete_node(cursor->trans, parent);
8cd0a023
MD
1576 hammer_rel_node(parent);
1577 }
6a37e7e4 1578 goto done;
427e5fc6 1579 }
8cd0a023 1580 hammer_lock_ex(&new_leaf->lock);
427e5fc6
MD
1581
1582 /*
36f82b23
MD
1583 * Create the new node and copy the leaf elements from the split
1584 * point on to the new node.
427e5fc6 1585 */
36f82b23
MD
1586 hammer_modify_node_all(cursor->trans, leaf);
1587 hammer_modify_node_noundo(cursor->trans, new_leaf);
8cd0a023
MD
1588 ondisk = leaf->ondisk;
1589 elm = &ondisk->elms[split];
1590 bcopy(elm, &new_leaf->ondisk->elms[0], (ondisk->count - split) * esize);
1591 new_leaf->ondisk->count = ondisk->count - split;
1592 new_leaf->ondisk->parent = parent->node_offset;
1593 new_leaf->ondisk->type = HAMMER_BTREE_TYPE_LEAF;
1594 KKASSERT(ondisk->type == new_leaf->ondisk->type);
10a5d1ba 1595 hammer_modify_node_done(new_leaf);
427e5fc6
MD
1596
1597 /*
8cd0a023
MD
1598 * Cleanup the original node. Because this is a leaf node and
1599 * leaf nodes do not have a right-hand boundary, there
c0ade690
MD
1600 * aren't any special edge cases to clean up. We just fixup the
1601 * count.
427e5fc6 1602 */
c0ade690 1603 ondisk->count = split;
427e5fc6
MD
1604
1605 /*
1606 * Insert the separator into the parent, fixup the parent's
1607 * reference to the original node, and reference the new node.
1608 * The separator is P.
1609 *
1610 * Remember that base.count does not include the right-hand boundary.
1611 * We are copying parent_index+1 to parent_index+2, not +0 to +1.
1612 */
36f82b23 1613 hammer_modify_node_all(cursor->trans, parent);
8cd0a023 1614 ondisk = parent->ondisk;
36f82b23 1615 KKASSERT(split != 0);
d26d0ae9 1616 KKASSERT(ondisk->count != HAMMER_BTREE_INT_ELMS);
8cd0a023 1617 parent_elm = &ondisk->elms[parent_index+1];
d26d0ae9
MD
1618 bcopy(parent_elm, parent_elm + 1,
1619 (ondisk->count - parent_index) * esize);
eaeff70d 1620
47197d71 1621 hammer_make_separator(&elm[-1].base, &elm[0].base, &parent_elm->base);
fe7678ee 1622 parent_elm->internal.base.btype = new_leaf->ondisk->type;
8cd0a023 1623 parent_elm->internal.subtree_offset = new_leaf->node_offset;
b3deaf57 1624 mid_boundary = &parent_elm->base;
76376933 1625 ++ondisk->count;
10a5d1ba 1626 hammer_modify_node_done(parent);
427e5fc6 1627
fe7678ee 1628 /*
47197d71 1629 * The filesystem's root B-Tree pointer may have to be updated.
427e5fc6
MD
1630 */
1631 if (made_root) {
47197d71
MD
1632 hammer_volume_t volume;
1633
1634 volume = hammer_get_root_volume(hmp, &error);
1635 KKASSERT(error == 0);
1636
e8599db1
MD
1637 hammer_modify_volume_field(cursor->trans, volume,
1638 vol0_btree_root);
47197d71 1639 volume->ondisk->vol0_btree_root = parent->node_offset;
10a5d1ba 1640 hammer_modify_volume_done(volume);
8cd0a023
MD
1641 leaf->ondisk->parent = parent->node_offset;
1642 if (cursor->parent) {
1643 hammer_unlock(&cursor->parent->lock);
1644 hammer_rel_node(cursor->parent);
1645 }
1646 cursor->parent = parent; /* lock'd and ref'd */
47197d71 1647 hammer_rel_volume(volume, 0);
427e5fc6 1648 }
10a5d1ba 1649 hammer_modify_node_done(leaf);
8cd0a023 1650
427e5fc6
MD
1651 /*
1652 * Ok, now adjust the cursor depending on which element the original
1653 * index was pointing at. If we are >= the split point the push node
1654 * is now in the new node.
1655 *
b3deaf57
MD
1656 * NOTE: If we are at the split point itself we need to select the
1657 * old or new node based on where key_beg's insertion point will be.
1658 * If we pick the wrong side the inserted element will wind up in
1659 * the wrong leaf node and outside that node's bounds.
427e5fc6 1660 */
b3deaf57
MD
1661 if (cursor->index > split ||
1662 (cursor->index == split &&
1663 hammer_btree_cmp(&cursor->key_beg, mid_boundary) >= 0)) {
8cd0a023 1664 cursor->parent_index = parent_index + 1;
427e5fc6 1665 cursor->index -= split;
8cd0a023
MD
1666 hammer_unlock(&cursor->node->lock);
1667 hammer_rel_node(cursor->node);
1668 cursor->node = new_leaf;
1669 } else {
1670 cursor->parent_index = parent_index;
1671 hammer_unlock(&new_leaf->lock);
1672 hammer_rel_node(new_leaf);
427e5fc6 1673 }
76376933
MD
1674
1675 /*
1676 * Fixup left and right bounds
1677 */
1678 parent_elm = &parent->ondisk->elms[cursor->parent_index];
fbc6e32a
MD
1679 cursor->left_bound = &parent_elm[0].internal.base;
1680 cursor->right_bound = &parent_elm[1].internal.base;
eaeff70d
MD
1681
1682 /*
47197d71 1683 * Assert that the bounds are correct.
eaeff70d 1684 */
b3deaf57
MD
1685 KKASSERT(hammer_btree_cmp(cursor->left_bound,
1686 &cursor->node->ondisk->elms[0].leaf.base) <= 0);
1687 KKASSERT(hammer_btree_cmp(cursor->right_bound,
47197d71 1688 &cursor->node->ondisk->elms[cursor->node->ondisk->count-1].leaf.base) > 0);
36f82b23
MD
1689 KKASSERT(hammer_btree_cmp(cursor->left_bound, &cursor->key_beg) <= 0);
1690 KKASSERT(hammer_btree_cmp(cursor->right_bound, &cursor->key_beg) > 0);
76376933 1691
6a37e7e4
MD
1692done:
1693 hammer_cursor_downgrade(cursor);
1694 return (error);
427e5fc6
MD
1695}
1696
32c90105
MD
1697/*
1698 * Recursively correct the right-hand boundary's create_tid to (tid) as
1699 * long as the rest of the key matches. We have to recurse upward in
1700 * the tree as well as down the left side of each parent's right node.
1701 *
1702 * Return EDEADLK if we were only partially successful, forcing the caller
1703 * to try again. The original cursor is not modified. This routine can
1704 * also fail with EDEADLK if it is forced to throw away a portion of its
1705 * record history.
1706 *
1707 * The caller must pass a downgraded cursor to us (otherwise we can't dup it).
1708 */
1709struct hammer_rhb {
1710 TAILQ_ENTRY(hammer_rhb) entry;
1711 hammer_node_t node;
1712 int index;
1713};
1714
1715TAILQ_HEAD(hammer_rhb_list, hammer_rhb);
1716
1717int
1718hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid)
1719{
1720 struct hammer_rhb_list rhb_list;
1721 hammer_base_elm_t elm;
1722 hammer_node_t orig_node;
1723 struct hammer_rhb *rhb;
1724 int orig_index;
1725 int error;
1726
1727 TAILQ_INIT(&rhb_list);
1728
1729 /*
1730 * Save our position so we can restore it on return. This also
1731 * gives us a stable 'elm'.
1732 */
1733 orig_node = cursor->node;
1734 hammer_ref_node(orig_node);
1735 hammer_lock_sh(&orig_node->lock);
1736 orig_index = cursor->index;
1737 elm = &orig_node->ondisk->elms[orig_index].base;
1738
1739 /*
1740 * Now build a list of parents going up, allocating a rhb
1741 * structure for each one.
1742 */
1743 while (cursor->parent) {
1744 /*
1745 * Stop if we no longer have any right-bounds to fix up
1746 */
1747 if (elm->obj_id != cursor->right_bound->obj_id ||
1748 elm->rec_type != cursor->right_bound->rec_type ||
1749 elm->key != cursor->right_bound->key) {
1750 break;
1751 }
1752
1753 /*
1754 * Stop if the right-hand bound's create_tid does not
47197d71 1755 * need to be corrected.
32c90105
MD
1756 */
1757 if (cursor->right_bound->create_tid >= tid)
1758 break;
1759
32c90105
MD
1760 rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO);
1761 rhb->node = cursor->parent;
1762 rhb->index = cursor->parent_index;
1763 hammer_ref_node(rhb->node);
1764 hammer_lock_sh(&rhb->node->lock);
1765 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
1766
1767 hammer_cursor_up(cursor);
1768 }
1769
1770 /*
1771 * now safely adjust the right hand bound for each rhb. This may
1772 * also require taking the right side of the tree and iterating down
1773 * ITS left side.
1774 */
1775 error = 0;
1776 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1777 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
77062c8a 1778 hkprintf("CORRECT RHB %016llx index %d type=%c\n",
47197d71 1779 rhb->node->node_offset,
32c90105
MD
1780 rhb->index, cursor->node->ondisk->type);
1781 if (error)
1782 break;
1783 TAILQ_REMOVE(&rhb_list, rhb, entry);
1784 hammer_unlock(&rhb->node->lock);
1785 hammer_rel_node(rhb->node);
1786 kfree(rhb, M_HAMMER);
1787
1788 switch (cursor->node->ondisk->type) {
1789 case HAMMER_BTREE_TYPE_INTERNAL:
1790 /*
1791 * Right-boundary for parent at internal node
1792 * is one element to the right of the element whos
1793 * right boundary needs adjusting. We must then
1794 * traverse down the left side correcting any left
1795 * bounds (which may now be too far to the left).
1796 */
1797 ++cursor->index;
1798 error = hammer_btree_correct_lhb(cursor, tid);
1799 break;
32c90105
MD
1800 default:
1801 panic("hammer_btree_correct_rhb(): Bad node type");
1802 error = EINVAL;
1803 break;
1804 }
1805 }
1806
1807 /*
1808 * Cleanup
1809 */
1810 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1811 TAILQ_REMOVE(&rhb_list, rhb, entry);
1812 hammer_unlock(&rhb->node->lock);
1813 hammer_rel_node(rhb->node);
1814 kfree(rhb, M_HAMMER);
1815 }
1816 error = hammer_cursor_seek(cursor, orig_node, orig_index);
1817 hammer_unlock(&orig_node->lock);
1818 hammer_rel_node(orig_node);
1819 return (error);
1820}
1821
1822/*
1823 * Similar to rhb (in fact, rhb calls lhb), but corrects the left hand
1824 * bound going downward starting at the current cursor position.
1825 *
1826 * This function does not restore the cursor after use.
1827 */
1828int
1829hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid)
1830{
1831 struct hammer_rhb_list rhb_list;
1832 hammer_base_elm_t elm;
1833 hammer_base_elm_t cmp;
1834 struct hammer_rhb *rhb;
1835 int error;
1836
1837 TAILQ_INIT(&rhb_list);
1838
1839 cmp = &cursor->node->ondisk->elms[cursor->index].base;
1840
1841 /*
1842 * Record the node and traverse down the left-hand side for all
1843 * matching records needing a boundary correction.
1844 */
1845 error = 0;
1846 for (;;) {
1847 rhb = kmalloc(sizeof(*rhb), M_HAMMER, M_WAITOK|M_ZERO);
1848 rhb->node = cursor->node;
1849 rhb->index = cursor->index;
1850 hammer_ref_node(rhb->node);
1851 hammer_lock_sh(&rhb->node->lock);
1852 TAILQ_INSERT_HEAD(&rhb_list, rhb, entry);
1853
1854 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
1855 /*
1856 * Nothing to traverse down if we are at the right
1857 * boundary of an internal node.
1858 */
1859 if (cursor->index == cursor->node->ondisk->count)
1860 break;
1861 } else {
1862 elm = &cursor->node->ondisk->elms[cursor->index].base;
1863 if (elm->btype == HAMMER_BTREE_TYPE_RECORD)
1864 break;
47197d71 1865 panic("Illegal leaf record type %02x", elm->btype);
32c90105
MD
1866 }
1867 error = hammer_cursor_down(cursor);
1868 if (error)
1869 break;
1870
1871 elm = &cursor->node->ondisk->elms[cursor->index].base;
1872 if (elm->obj_id != cmp->obj_id ||
1873 elm->rec_type != cmp->rec_type ||
1874 elm->key != cmp->key) {
1875 break;
1876 }
1877 if (elm->create_tid >= tid)
1878 break;
1879
1880 }
1881
1882 /*
1883 * Now we can safely adjust the left-hand boundary from the bottom-up.
1884 * The last element we remove from the list is the caller's right hand
1885 * boundary, which must also be adjusted.
1886 */
1887 while (error == 0 && (rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1888 error = hammer_cursor_seek(cursor, rhb->node, rhb->index);
1889 if (error)
1890 break;
1891 TAILQ_REMOVE(&rhb_list, rhb, entry);
1892 hammer_unlock(&rhb->node->lock);
1893 hammer_rel_node(rhb->node);
1894 kfree(rhb, M_HAMMER);
1895
1896 elm = &cursor->node->ondisk->elms[cursor->index].base;
1897 if (cursor->node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
77062c8a 1898 hkprintf("hammer_btree_correct_lhb-I @%016llx[%d]\n",
32c90105 1899 cursor->node->node_offset, cursor->index);
36f82b23 1900 hammer_modify_node(cursor->trans, cursor->node,
19619882
MD
1901 &elm->create_tid,
1902 sizeof(elm->create_tid));
32c90105 1903 elm->create_tid = tid;
10a5d1ba 1904 hammer_modify_node_done(cursor->node);
32c90105
MD
1905 } else {
1906 panic("hammer_btree_correct_lhb(): Bad element type");
1907 }
1908 }
1909
1910 /*
1911 * Cleanup
1912 */
1913 while ((rhb = TAILQ_FIRST(&rhb_list)) != NULL) {
1914 TAILQ_REMOVE(&rhb_list, rhb, entry);
1915 hammer_unlock(&rhb->node->lock);
1916 hammer_rel_node(rhb->node);
1917 kfree(rhb, M_HAMMER);
1918 }
1919 return (error);
1920}
1921
427e5fc6 1922/*
195c19a1
MD
1923 * Attempt to remove the empty B-Tree node at (cursor->node). Returns 0
1924 * on success, EAGAIN if we could not acquire the necessary locks, or some
fe7678ee 1925 * other error. This node can be a leaf node or an internal node.
8cd0a023 1926 *
195c19a1 1927 * On return the cursor may end up pointing at an internal node, suitable
b3deaf57 1928 * for further iteration but not for an immediate insertion or deletion.
8cd0a023 1929 *
4e97774c
MD
1930 * cursor->node may be an internal node or a leaf node. The cursor must be
1931 * locked (node and parent).
b3deaf57
MD
1932 *
1933 * NOTE: If cursor->node has one element it is the parent trying to delete
1934 * that element, make sure cursor->index is properly adjusted on success.
8cd0a023
MD
1935 */
1936int
46fe7ae1 1937btree_remove(hammer_cursor_t cursor)
8cd0a023
MD
1938{
1939 hammer_node_ondisk_t ondisk;
195c19a1 1940 hammer_btree_elm_t elm;
195c19a1 1941 hammer_node_t node;
8cd0a023 1942 hammer_node_t parent;
fe7678ee 1943 const int esize = sizeof(*elm);
8cd0a023 1944 int error;
8cd0a023 1945
fe7678ee
MD
1946 node = cursor->node;
1947
47197d71
MD
1948 /*
1949 * When deleting the root of the filesystem convert it to
1950 * an empty leaf node. Internal nodes cannot be empty.
1951 */
fe7678ee 1952 if (node->ondisk->parent == 0) {
36f82b23 1953 hammer_modify_node_all(cursor->trans, node);
fe7678ee 1954 ondisk = node->ondisk;
195c19a1
MD
1955 ondisk->type = HAMMER_BTREE_TYPE_LEAF;
1956 ondisk->count = 0;
10a5d1ba 1957 hammer_modify_node_done(node);
b3deaf57 1958 cursor->index = 0;
47197d71 1959 return(0);
8cd0a023
MD
1960 }
1961
1962 /*
fe7678ee
MD
1963 * Zero-out the parent's reference to the child and flag the
1964 * child for destruction. This ensures that the child is not
1965 * reused while other references to it exist.
8cd0a023 1966 */
fe7678ee 1967 parent = cursor->parent;
36f82b23 1968 hammer_modify_node_all(cursor->trans, parent);
fe7678ee
MD
1969 ondisk = parent->ondisk;
1970 KKASSERT(ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
1971 elm = &ondisk->elms[cursor->parent_index];
1972 KKASSERT(elm->internal.subtree_offset == node->node_offset);
1973 elm->internal.subtree_offset = 0;
1974
1975 hammer_flush_node(node);
36f82b23 1976 hammer_delete_node(cursor->trans, node);
8cd0a023
MD
1977
1978 /*
fe7678ee
MD
1979 * If the parent would otherwise not become empty we can physically
1980 * remove the zero'd element. Note however that in order to
1981 * guarentee a valid cursor we still need to be able to cursor up
1982 * because we no longer have a node.
1983 *
1984 * This collapse will change the parent's boundary elements, making
1985 * them wider. The new boundaries are recursively corrected in
1986 * btree_search().
195c19a1 1987 *
fe7678ee
MD
1988 * XXX we can theoretically recalculate the midpoint but there isn't
1989 * much of a reason to do it.
8cd0a023 1990 */
6a37e7e4
MD
1991 error = hammer_cursor_up(cursor);
1992 if (error == 0)
1993 error = hammer_cursor_upgrade(cursor);
1994
195c19a1 1995 if (error) {
4e97774c
MD
1996 kprintf("Warning: BTREE_REMOVE: Defering parent removal "
1997 "@ %016llx, skipping\n", cursor->parent->node_offset);
10a5d1ba 1998 hammer_modify_node_done(parent);
fe7678ee 1999 return (0);
8cd0a023 2000 }
195c19a1
MD
2001
2002 /*
fe7678ee
MD
2003 * Remove the internal element from the parent. The bcopy must
2004 * include the right boundary element.
9944ae54 2005 */
fe7678ee
MD
2006 KKASSERT(parent == cursor->node && ondisk == parent->ondisk);
2007 node = parent;
2008 parent = NULL;
2009 /* ondisk is node's ondisk */
2010 /* elm is node's element */
2011
6a37e7e4
MD
2012 /*
2013 * Remove the internal element that we zero'd out. Tell the caller
2014 * to loop if it hits zero (to try to avoid eating up precious kernel
2015 * stack).
2016 */
fe7678ee
MD
2017 KKASSERT(ondisk->count > 0);
2018 bcopy(&elm[1], &elm[0], (ondisk->count - cursor->index) * esize);
195c19a1 2019 --ondisk->count;
fe7678ee
MD
2020 if (ondisk->count == 0)
2021 error = EAGAIN;
ec4e8497 2022 hammer_modify_node_done(node);
b3deaf57 2023 return(error);
8cd0a023
MD
2024}
2025
6a37e7e4
MD
2026/*
2027 * Attempt to remove the deleted internal element at the current cursor
2028 * position. If we are unable to remove the element we return EDEADLK.
2029 *
2030 * If the current internal node becomes empty we delete it in the parent
2031 * and cursor up, looping until we finish or we deadlock.
2032 *
2033 * On return, if successful, the cursor will be pointing at the next
2034 * iterative position in the B-Tree. If unsuccessful the cursor will be
2035 * pointing at the last deleted internal element that could not be
2036 * removed.
2037 */
2038static
2039int
2040btree_remove_deleted_element(hammer_cursor_t cursor)
2041{
2042 hammer_node_t node;
2043 hammer_btree_elm_t elm;
2044 int error;
2045
2046 if ((error = hammer_cursor_upgrade(cursor)) != 0)
2047 return(error);
2048 node = cursor->node;
2049 elm = &node->ondisk->elms[cursor->index];
2050 if (elm->internal.subtree_offset == 0) {
2051 do {
46fe7ae1 2052 error = btree_remove(cursor);
77062c8a 2053 hkprintf("BTREE REMOVE DELETED ELEMENT %d\n", error);
6a37e7e4
MD
2054 } while (error == EAGAIN);
2055 }
2056 return(error);
2057}
2058
7f7c1f84 2059/*
fe7678ee
MD
2060 * The element (elm) has been moved to a new internal node (node).
2061 *
2062 * If the element represents a pointer to an internal node that node's
2063 * parent must be adjusted to the element's new location.
2064 *
6a37e7e4 2065 * XXX deadlock potential here with our exclusive locks
7f7c1f84
MD
2066 */
2067static
2068int
36f82b23
MD
2069btree_set_parent(hammer_transaction_t trans, hammer_node_t node,
2070 hammer_btree_elm_t elm)
7f7c1f84 2071{
7f7c1f84
MD
2072 hammer_node_t child;
2073 int error;
2074
2075 error = 0;
2076
fe7678ee 2077 switch(elm->base.btype) {
7f7c1f84 2078 case HAMMER_BTREE_TYPE_INTERNAL:
fe7678ee 2079 case HAMMER_BTREE_TYPE_LEAF:
19619882
MD
2080 child = hammer_get_node(node->hmp, elm->internal.subtree_offset,
2081 0, &error);
7f7c1f84 2082 if (error == 0) {
c9b9e29d 2083 hammer_modify_node_field(trans, child, parent);
7f7c1f84 2084 child->ondisk->parent = node->node_offset;
10a5d1ba 2085 hammer_modify_node_done(child);
7f7c1f84
MD
2086 hammer_rel_node(child);
2087 }
2088 break;
7f7c1f84 2089 default:
fe7678ee 2090 break;
7f7c1f84
MD
2091 }
2092 return(error);
2093}
2094
b33e2cc0
MD
2095/*
2096 * Exclusively lock all the children of node. This is used by the split
2097 * code to prevent anyone from accessing the children of a cursor node
2098 * while we fix-up its parent offset.
2099 *
2100 * If we don't lock the children we can really mess up cursors which block
2101 * trying to cursor-up into our node.
2102 *
b33e2cc0
MD
2103 * On failure EDEADLK (or some other error) is returned. If a deadlock
2104 * error is returned the cursor is adjusted to block on termination.
2105 */
2106int
2107hammer_btree_lock_children(hammer_cursor_t cursor,
2108 struct hammer_node_locklist **locklistp)
2109{
2110 hammer_node_t node;
2111 hammer_node_locklist_t item;
2112 hammer_node_ondisk_t ondisk;
2113 hammer_btree_elm_t elm;
b33e2cc0
MD
2114 hammer_node_t child;
2115 int error;
2116 int i;
2117
2118 node = cursor->node;
2119 ondisk = node->ondisk;
2120 error = 0;
2121 for (i = 0; error == 0 && i < ondisk->count; ++i) {
2122 elm = &ondisk->elms[i];
2123
b33e2cc0
MD
2124 switch(elm->base.btype) {
2125 case HAMMER_BTREE_TYPE_INTERNAL:
2126 case HAMMER_BTREE_TYPE_LEAF:
40043e7f 2127 child = hammer_get_node(node->hmp,
b33e2cc0 2128 elm->internal.subtree_offset,
19619882 2129 0, &error);
b33e2cc0 2130 break;
b33e2cc0 2131 default:
47197d71 2132 child = NULL;
b33e2cc0
MD
2133 break;
2134 }
2135 if (child) {
2136 if (hammer_lock_ex_try(&child->lock) != 0) {
2137 if (cursor->deadlk_node == NULL) {
1ff9f58e 2138 cursor->deadlk_node = child;
b33e2cc0
MD
2139 hammer_ref_node(cursor->deadlk_node);
2140 }
2141 error = EDEADLK;
a84a197d 2142 hammer_rel_node(child);
b33e2cc0
MD
2143 } else {
2144 item = kmalloc(sizeof(*item),
2145 M_HAMMER, M_WAITOK);
2146 item->next = *locklistp;
2147 item->node = child;
2148 *locklistp = item;
2149 }
2150 }
2151 }
2152 if (error)
2153 hammer_btree_unlock_children(locklistp);
2154 return(error);
2155}
2156
2157
2158/*
2159 * Release previously obtained node locks.
2160 */
36f82b23 2161static void
b33e2cc0
MD
2162hammer_btree_unlock_children(struct hammer_node_locklist **locklistp)
2163{
2164 hammer_node_locklist_t item;
2165
2166 while ((item = *locklistp) != NULL) {
2167 *locklistp = item->next;
2168 hammer_unlock(&item->node->lock);
2169 hammer_rel_node(item->node);
2170 kfree(item, M_HAMMER);
2171 }
2172}
2173
8cd0a023
MD
2174/************************************************************************
2175 * MISCELLANIOUS SUPPORT *
2176 ************************************************************************/
2177
2178/*
d26d0ae9 2179 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
8cd0a023 2180 *
d113fda1 2181 * Note that for this particular function a return value of -1, 0, or +1
9582c7da 2182 * can denote a match if create_tid is otherwise discounted. A create_tid
d5530d22 2183 * of zero is considered to be 'infinity' in comparisons.
d113fda1 2184 *
8cd0a023 2185 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
8cd0a023
MD
2186 */
2187int
2188hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
2189{
d26d0ae9
MD
2190 if (key1->obj_id < key2->obj_id)
2191 return(-4);
2192 if (key1->obj_id > key2->obj_id)
2193 return(4);
8cd0a023 2194
d26d0ae9
MD
2195 if (key1->rec_type < key2->rec_type)
2196 return(-3);
2197 if (key1->rec_type > key2->rec_type)
2198 return(3);
8cd0a023 2199
8cd0a023
MD
2200 if (key1->key < key2->key)
2201 return(-2);
2202 if (key1->key > key2->key)
2203 return(2);
d113fda1 2204
d5530d22 2205 /*
9582c7da
MD
2206 * A create_tid of zero indicates a record which is undeletable
2207 * and must be considered to have a value of positive infinity.
d5530d22 2208 */
9582c7da
MD
2209 if (key1->create_tid == 0) {
2210 if (key2->create_tid == 0)
d5530d22
MD
2211 return(0);
2212 return(1);
2213 }
9582c7da 2214 if (key2->create_tid == 0)
d5530d22 2215 return(-1);
9582c7da 2216 if (key1->create_tid < key2->create_tid)
d113fda1 2217 return(-1);
9582c7da 2218 if (key1->create_tid > key2->create_tid)
d113fda1 2219 return(1);
8cd0a023
MD
2220 return(0);
2221}
2222
c0ade690 2223/*
d5530d22
MD
2224 * Test a timestamp against an element to determine whether the
2225 * element is visible. A timestamp of 0 means 'infinity'.
c0ade690
MD
2226 */
2227int
d5530d22 2228hammer_btree_chkts(hammer_tid_t asof, hammer_base_elm_t base)
c0ade690 2229{
d5530d22
MD
2230 if (asof == 0) {
2231 if (base->delete_tid)
2232 return(1);
2233 return(0);
2234 }
2235 if (asof < base->create_tid)
d26d0ae9 2236 return(-1);
d5530d22 2237 if (base->delete_tid && asof >= base->delete_tid)
d26d0ae9 2238 return(1);
c0ade690
MD
2239 return(0);
2240}
2241
8cd0a023
MD
2242/*
2243 * Create a separator half way inbetween key1 and key2. For fields just
d5530d22
MD
2244 * one unit apart, the separator will match key2. key1 is on the left-hand
2245 * side and key2 is on the right-hand side.
8cd0a023 2246 *
9391cded 2247 * key2 must be >= the separator. It is ok for the separator to match key2.
36f82b23 2248 *
9391cded
MD
2249 * NOTE: Even if key1 does not match key2, the separator may wind up matching
2250 * key2.
2251 *
2252 * NOTE: It might be beneficial to just scrap this whole mess and just
2253 * set the separator to key2.
8cd0a023
MD
2254 */
2255#define MAKE_SEPARATOR(key1, key2, dest, field) \
2256 dest->field = key1->field + ((key2->field - key1->field + 1) >> 1);
2257
2258static void
2259hammer_make_separator(hammer_base_elm_t key1, hammer_base_elm_t key2,
2260 hammer_base_elm_t dest)
2261{
2262 bzero(dest, sizeof(*dest));
d5530d22 2263
9391cded
MD
2264 dest->rec_type = key2->rec_type;
2265 dest->key = key2->key;
2266 dest->create_tid = key2->create_tid;
2267
2268 MAKE_SEPARATOR(key1, key2, dest, obj_id);
2269 if (key1->obj_id == key2->obj_id) {
2270 MAKE_SEPARATOR(key1, key2, dest, rec_type);
2271 if (key1->rec_type == key2->rec_type) {
2272 MAKE_SEPARATOR(key1, key2, dest, key);
b33e2cc0 2273 /*
9391cded
MD
2274 * Don't bother creating a separator for create_tid,
2275 * which also conveniently avoids having to handle
2276 * the create_tid == 0 (infinity) case. Just leave
2277 * create_tid set to key2.
2278 *
2279 * Worst case, dest matches key2 exactly, which is
2280 * acceptable.
d5530d22 2281 */
d5530d22 2282 }
d113fda1 2283 }
8cd0a023
MD
2284}
2285
2286#undef MAKE_SEPARATOR
2287
2288/*
2289 * Return whether a generic internal or leaf node is full
2290 */
2291static int
2292btree_node_is_full(hammer_node_ondisk_t node)
2293{
2294 switch(node->type) {
2295 case HAMMER_BTREE_TYPE_INTERNAL:
2296 if (node->count == HAMMER_BTREE_INT_ELMS)
2297 return(1);
2298 break;
2299 case HAMMER_BTREE_TYPE_LEAF:
2300 if (node->count == HAMMER_BTREE_LEAF_ELMS)
2301 return(1);
2302 break;
2303 default:
2304 panic("illegal btree subtype");
2305 }
2306 return(0);
2307}
9944ae54 2308
8cd0a023
MD
2309#if 0
2310static int
2311btree_max_elements(u_int8_t type)
2312{
2313 if (type == HAMMER_BTREE_TYPE_LEAF)
2314 return(HAMMER_BTREE_LEAF_ELMS);
2315 if (type == HAMMER_BTREE_TYPE_INTERNAL)
2316 return(HAMMER_BTREE_INT_ELMS);
2317 panic("btree_max_elements: bad type %d\n", type);
2318}
2319#endif
2320
c0ade690
MD
2321void
2322hammer_print_btree_node(hammer_node_ondisk_t ondisk)
2323{
2324 hammer_btree_elm_t elm;
2325 int i;
2326
47197d71 2327 kprintf("node %p count=%d parent=%016llx type=%c\n",
c0ade690
MD
2328 ondisk, ondisk->count, ondisk->parent, ondisk->type);
2329
2330 /*
2331 * Dump both boundary elements if an internal node
2332 */
2333 if (ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
2334 for (i = 0; i <= ondisk->count; ++i) {
2335 elm = &ondisk->elms[i];
2336 hammer_print_btree_elm(elm, ondisk->type, i);
2337 }
2338 } else {
2339 for (i = 0; i < ondisk->count; ++i) {
2340 elm = &ondisk->elms[i];
2341 hammer_print_btree_elm(elm, ondisk->type, i);
2342 }
2343 }
2344}
2345
2346void
2347hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i)
2348{
2349 kprintf(" %2d", i);
7dc57964 2350 kprintf("\tobj_id = %016llx\n", elm->base.obj_id);
c0ade690
MD
2351 kprintf("\tkey = %016llx\n", elm->base.key);
2352 kprintf("\tcreate_tid = %016llx\n", elm->base.create_tid);
2353 kprintf("\tdelete_tid = %016llx\n", elm->base.delete_tid);
2354 kprintf("\trec_type = %04x\n", elm->base.rec_type);
2355 kprintf("\tobj_type = %02x\n", elm->base.obj_type);
fe7678ee
MD
2356 kprintf("\tbtype = %02x (%c)\n",
2357 elm->base.btype,
2358 (elm->base.btype ? elm->base.btype : '?'));
2359
2360 switch(type) {
2361 case HAMMER_BTREE_TYPE_INTERNAL:
47197d71 2362 kprintf("\tsubtree_off = %016llx\n",
fe7678ee
MD
2363 elm->internal.subtree_offset);
2364 break;
fe7678ee 2365 case HAMMER_BTREE_TYPE_RECORD:
11ad5ade 2366 kprintf("\tatime = %016llx\n", elm->leaf.atime);
47197d71 2367 kprintf("\tdata_offset = %016llx\n", elm->leaf.data_offset);
c0ade690
MD
2368 kprintf("\tdata_len = %08x\n", elm->leaf.data_len);
2369 kprintf("\tdata_crc = %08x\n", elm->leaf.data_crc);
fe7678ee 2370 break;
c0ade690
MD
2371 }
2372}