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