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