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