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