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