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