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