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