dm-btree-remove.c 17.4 KB
Newer Older
J
Joe Thornber 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This file is released under the GPL.
 */

#include "dm-btree.h"
#include "dm-btree-internal.h"
#include "dm-transaction-manager.h"

11
#include <linux/export.h>
J
Joe Thornber 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

/*
 * Removing an entry from a btree
 * ==============================
 *
 * A very important constraint for our btree is that no node, except the
 * root, may have fewer than a certain number of entries.
 * (MIN_ENTRIES <= nr_entries <= MAX_ENTRIES).
 *
 * Ensuring this is complicated by the way we want to only ever hold the
 * locks on 2 nodes concurrently, and only change nodes in a top to bottom
 * fashion.
 *
 * Each node may have a left or right sibling.  When decending the spine,
 * if a node contains only MIN_ENTRIES then we try and increase this to at
 * least MIN_ENTRIES + 1.  We do this in the following ways:
 *
 * [A] No siblings => this can only happen if the node is the root, in which
 *     case we copy the childs contents over the root.
 *
 * [B] No left sibling
 *     ==> rebalance(node, right sibling)
 *
 * [C] No right sibling
 *     ==> rebalance(left sibling, node)
 *
 * [D] Both siblings, total_entries(left, node, right) <= DEL_THRESHOLD
 *     ==> delete node adding it's contents to left and right
 *
 * [E] Both siblings, total_entries(left, node, right) > DEL_THRESHOLD
 *     ==> rebalance(left, node, right)
 *
 * After these operations it's possible that the our original node no
 * longer contains the desired sub tree.  For this reason this rebalancing
 * is performed on the children of the current node.  This also avoids
 * having a special case for the root.
 *
 * Once this rebalancing has occurred we can then step into the child node
 * for internal nodes.  Or delete the entry for leaf nodes.
 */

/*
 * Some little utilities for moving node data around.
 */
56
static void node_shift(struct btree_node *n, int shift)
J
Joe Thornber 已提交
57 58 59 60 61 62 63
{
	uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
	uint32_t value_size = le32_to_cpu(n->header.value_size);

	if (shift < 0) {
		shift = -shift;
		BUG_ON(shift > nr_entries);
64
		BUG_ON((void *) key_ptr(n, shift) >= value_ptr(n, shift));
J
Joe Thornber 已提交
65 66 67
		memmove(key_ptr(n, 0),
			key_ptr(n, shift),
			(nr_entries - shift) * sizeof(__le64));
68 69
		memmove(value_ptr(n, 0),
			value_ptr(n, shift),
J
Joe Thornber 已提交
70 71 72 73 74 75
			(nr_entries - shift) * value_size);
	} else {
		BUG_ON(nr_entries + shift > le32_to_cpu(n->header.max_entries));
		memmove(key_ptr(n, shift),
			key_ptr(n, 0),
			nr_entries * sizeof(__le64));
76 77
		memmove(value_ptr(n, shift),
			value_ptr(n, 0),
J
Joe Thornber 已提交
78 79 80 81
			nr_entries * value_size);
	}
}

82
static void node_copy(struct btree_node *left, struct btree_node *right, int shift)
J
Joe Thornber 已提交
83 84 85 86 87 88 89 90 91 92 93
{
	uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
	uint32_t value_size = le32_to_cpu(left->header.value_size);
	BUG_ON(value_size != le32_to_cpu(right->header.value_size));

	if (shift < 0) {
		shift = -shift;
		BUG_ON(nr_left + shift > le32_to_cpu(left->header.max_entries));
		memcpy(key_ptr(left, nr_left),
		       key_ptr(right, 0),
		       shift * sizeof(__le64));
94 95
		memcpy(value_ptr(left, nr_left),
		       value_ptr(right, 0),
J
Joe Thornber 已提交
96 97 98 99 100 101
		       shift * value_size);
	} else {
		BUG_ON(shift > le32_to_cpu(right->header.max_entries));
		memcpy(key_ptr(right, 0),
		       key_ptr(left, nr_left - shift),
		       shift * sizeof(__le64));
102 103
		memcpy(value_ptr(right, 0),
		       value_ptr(left, nr_left - shift),
J
Joe Thornber 已提交
104 105 106 107 108 109 110
		       shift * value_size);
	}
}

/*
 * Delete a specific entry from a leaf node.
 */
111
static void delete_at(struct btree_node *n, unsigned index)
J
Joe Thornber 已提交
112 113 114 115 116 117 118 119 120 121 122
{
	unsigned nr_entries = le32_to_cpu(n->header.nr_entries);
	unsigned nr_to_copy = nr_entries - (index + 1);
	uint32_t value_size = le32_to_cpu(n->header.value_size);
	BUG_ON(index >= nr_entries);

	if (nr_to_copy) {
		memmove(key_ptr(n, index),
			key_ptr(n, index + 1),
			nr_to_copy * sizeof(__le64));

123 124
		memmove(value_ptr(n, index),
			value_ptr(n, index + 1),
J
Joe Thornber 已提交
125 126 127 128 129 130
			nr_to_copy * value_size);
	}

	n->header.nr_entries = cpu_to_le32(nr_entries - 1);
}

131
static unsigned merge_threshold(struct btree_node *n)
J
Joe Thornber 已提交
132
{
133
	return le32_to_cpu(n->header.max_entries) / 3;
J
Joe Thornber 已提交
134 135 136 137 138
}

struct child {
	unsigned index;
	struct dm_block *block;
139
	struct btree_node *n;
J
Joe Thornber 已提交
140 141
};

J
Joe Thornber 已提交
142 143
static int init_child(struct dm_btree_info *info, struct dm_btree_value_type *vt,
		      struct btree_node *parent,
J
Joe Thornber 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		      unsigned index, struct child *result)
{
	int r, inc;
	dm_block_t root;

	result->index = index;
	root = value64(parent, index);

	r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
			       &result->block, &inc);
	if (r)
		return r;

	result->n = dm_block_data(result->block);

	if (inc)
J
Joe Thornber 已提交
160
		inc_children(info->tm, result->n, vt);
J
Joe Thornber 已提交
161

162
	*((__le64 *) value_ptr(parent, index)) =
J
Joe Thornber 已提交
163 164 165 166 167
		cpu_to_le64(dm_block_location(result->block));

	return 0;
}

168
static void exit_child(struct dm_btree_info *info, struct child *c)
J
Joe Thornber 已提交
169
{
170
	dm_tm_unlock(info->tm, c->block);
J
Joe Thornber 已提交
171 172
}

173
static void shift(struct btree_node *left, struct btree_node *right, int count)
J
Joe Thornber 已提交
174
{
175 176 177 178 179 180 181 182 183
	uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
	uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
	uint32_t max_entries = le32_to_cpu(left->header.max_entries);
	uint32_t r_max_entries = le32_to_cpu(right->header.max_entries);

	BUG_ON(max_entries != r_max_entries);
	BUG_ON(nr_left - count > max_entries);
	BUG_ON(nr_right + count > max_entries);

J
Joe Thornber 已提交
184 185 186 187 188 189 190 191 192 193 194
	if (!count)
		return;

	if (count > 0) {
		node_shift(right, count);
		node_copy(left, right, count);
	} else {
		node_copy(left, right, count);
		node_shift(right, count);
	}

195 196
	left->header.nr_entries = cpu_to_le32(nr_left - count);
	right->header.nr_entries = cpu_to_le32(nr_right + count);
J
Joe Thornber 已提交
197 198
}

199
static void __rebalance2(struct dm_btree_info *info, struct btree_node *parent,
J
Joe Thornber 已提交
200 201
			 struct child *l, struct child *r)
{
202 203
	struct btree_node *left = l->n;
	struct btree_node *right = r->n;
J
Joe Thornber 已提交
204 205
	uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
	uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
206 207 208 209 210 211 212
	/*
	 * Ensure the number of entries in each child will be greater
	 * than or equal to (max_entries / 3 + 1), so no matter which
	 * child is used for removal, the number will still be not
	 * less than (max_entries / 3).
	 */
	unsigned int threshold = 2 * (merge_threshold(left) + 1);
J
Joe Thornber 已提交
213

214
	if (nr_left + nr_right < threshold) {
J
Joe Thornber 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		/*
		 * Merge
		 */
		node_copy(left, right, -nr_right);
		left->header.nr_entries = cpu_to_le32(nr_left + nr_right);
		delete_at(parent, r->index);

		/*
		 * We need to decrement the right block, but not it's
		 * children, since they're still referenced by left.
		 */
		dm_tm_dec(info->tm, dm_block_location(r->block));
	} else {
		/*
		 * Rebalance.
		 */
		unsigned target_left = (nr_left + nr_right) / 2;
		shift(left, right, nr_left - target_left);
		*key_ptr(parent, r->index) = right->keys[0];
	}
}

static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info,
J
Joe Thornber 已提交
238
		      struct dm_btree_value_type *vt, unsigned left_index)
J
Joe Thornber 已提交
239 240
{
	int r;
241
	struct btree_node *parent;
J
Joe Thornber 已提交
242 243 244 245
	struct child left, right;

	parent = dm_block_data(shadow_current(s));

J
Joe Thornber 已提交
246
	r = init_child(info, vt, parent, left_index, &left);
J
Joe Thornber 已提交
247 248 249
	if (r)
		return r;

J
Joe Thornber 已提交
250
	r = init_child(info, vt, parent, left_index + 1, &right);
J
Joe Thornber 已提交
251 252 253 254 255 256 257
	if (r) {
		exit_child(info, &left);
		return r;
	}

	__rebalance2(info, parent, &left, &right);

258 259
	exit_child(info, &left);
	exit_child(info, &right);
J
Joe Thornber 已提交
260

261
	return 0;
J
Joe Thornber 已提交
262 263
}

264 265 266 267 268
/*
 * We dump as many entries from center as possible into left, then the rest
 * in right, then rebalance2.  This wastes some cpu, but I want something
 * simple atm.
 */
269
static void delete_center_node(struct dm_btree_info *info, struct btree_node *parent,
270
			       struct child *l, struct child *c, struct child *r,
271
			       struct btree_node *left, struct btree_node *center, struct btree_node *right,
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
			       uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
{
	uint32_t max_entries = le32_to_cpu(left->header.max_entries);
	unsigned shift = min(max_entries - nr_left, nr_center);

	BUG_ON(nr_left + shift > max_entries);
	node_copy(left, center, -shift);
	left->header.nr_entries = cpu_to_le32(nr_left + shift);

	if (shift != nr_center) {
		shift = nr_center - shift;
		BUG_ON((nr_right + shift) > max_entries);
		node_shift(right, shift);
		node_copy(center, right, shift);
		right->header.nr_entries = cpu_to_le32(nr_right + shift);
	}
	*key_ptr(parent, r->index) = right->keys[0];

	delete_at(parent, c->index);
	r->index--;

	dm_tm_dec(info->tm, dm_block_location(c->block));
	__rebalance2(info, parent, l, r);
}

/*
 * Redistributes entries among 3 sibling nodes.
 */
300
static void redistribute3(struct dm_btree_info *info, struct btree_node *parent,
301
			  struct child *l, struct child *c, struct child *r,
302
			  struct btree_node *left, struct btree_node *center, struct btree_node *right,
303 304 305 306
			  uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
{
	int s;
	uint32_t max_entries = le32_to_cpu(left->header.max_entries);
307 308 309 310 311 312 313
	unsigned total = nr_left + nr_center + nr_right;
	unsigned target_right = total / 3;
	unsigned remainder = (target_right * 3) != total;
	unsigned target_left = target_right + remainder;

	BUG_ON(target_left > max_entries);
	BUG_ON(target_right > max_entries);
314 315

	if (nr_left < nr_right) {
316
		s = nr_left - target_left;
317 318 319

		if (s < 0 && nr_center < -s) {
			/* not enough in central node */
320 321
			shift(left, center, -nr_center);
			s += nr_center;
322 323 324 325 326
			shift(left, right, s);
			nr_right += s;
		} else
			shift(left, center, s);

327
		shift(center, right, target_right - nr_right);
328 329

	} else {
330
		s = target_right - nr_right;
331 332 333
		if (s > 0 && nr_center < s) {
			/* not enough in central node */
			shift(center, right, nr_center);
334
			s -= nr_center;
335 336 337 338 339
			shift(left, right, s);
			nr_left -= s;
		} else
			shift(center, right, s);

340
		shift(left, center, nr_left - target_left);
341 342 343 344 345 346
	}

	*key_ptr(parent, c->index) = center->keys[0];
	*key_ptr(parent, r->index) = right->keys[0];
}

347
static void __rebalance3(struct dm_btree_info *info, struct btree_node *parent,
J
Joe Thornber 已提交
348 349
			 struct child *l, struct child *c, struct child *r)
{
350 351 352
	struct btree_node *left = l->n;
	struct btree_node *center = c->n;
	struct btree_node *right = r->n;
J
Joe Thornber 已提交
353 354 355 356 357

	uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
	uint32_t nr_center = le32_to_cpu(center->header.nr_entries);
	uint32_t nr_right = le32_to_cpu(right->header.nr_entries);

358
	unsigned threshold = merge_threshold(left) * 4 + 1;
J
Joe Thornber 已提交
359 360 361 362

	BUG_ON(left->header.max_entries != center->header.max_entries);
	BUG_ON(center->header.max_entries != right->header.max_entries);

363 364 365 366 367 368
	if ((nr_left + nr_center + nr_right) < threshold)
		delete_center_node(info, parent, l, c, r, left, center, right,
				   nr_left, nr_center, nr_right);
	else
		redistribute3(info, parent, l, c, r, left, center, right,
			      nr_left, nr_center, nr_right);
J
Joe Thornber 已提交
369 370 371
}

static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info,
J
Joe Thornber 已提交
372
		      struct dm_btree_value_type *vt, unsigned left_index)
J
Joe Thornber 已提交
373 374
{
	int r;
375
	struct btree_node *parent = dm_block_data(shadow_current(s));
J
Joe Thornber 已提交
376 377 378 379 380
	struct child left, center, right;

	/*
	 * FIXME: fill out an array?
	 */
J
Joe Thornber 已提交
381
	r = init_child(info, vt, parent, left_index, &left);
J
Joe Thornber 已提交
382 383 384
	if (r)
		return r;

J
Joe Thornber 已提交
385
	r = init_child(info, vt, parent, left_index + 1, &center);
J
Joe Thornber 已提交
386 387 388 389 390
	if (r) {
		exit_child(info, &left);
		return r;
	}

J
Joe Thornber 已提交
391
	r = init_child(info, vt, parent, left_index + 2, &right);
J
Joe Thornber 已提交
392 393 394 395 396 397 398 399
	if (r) {
		exit_child(info, &left);
		exit_child(info, &center);
		return r;
	}

	__rebalance3(info, parent, &left, &center, &right);

400 401 402
	exit_child(info, &left);
	exit_child(info, &center);
	exit_child(info, &right);
J
Joe Thornber 已提交
403 404 405 406 407

	return 0;
}

static int rebalance_children(struct shadow_spine *s,
J
Joe Thornber 已提交
408 409
			      struct dm_btree_info *info,
			      struct dm_btree_value_type *vt, uint64_t key)
J
Joe Thornber 已提交
410 411
{
	int i, r, has_left_sibling, has_right_sibling;
412
	struct btree_node *n;
J
Joe Thornber 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425

	n = dm_block_data(shadow_current(s));

	if (le32_to_cpu(n->header.nr_entries) == 1) {
		struct dm_block *child;
		dm_block_t b = value64(n, 0);

		r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child);
		if (r)
			return r;

		memcpy(n, dm_block_data(child),
		       dm_bm_block_size(dm_tm_get_bm(info->tm)));
426
		dm_tm_unlock(info->tm, child);
J
Joe Thornber 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439

		dm_tm_dec(info->tm, dm_block_location(child));
		return 0;
	}

	i = lower_bound(n, key);
	if (i < 0)
		return -ENODATA;

	has_left_sibling = i > 0;
	has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1);

	if (!has_left_sibling)
J
Joe Thornber 已提交
440
		r = rebalance2(s, info, vt, i);
J
Joe Thornber 已提交
441 442

	else if (!has_right_sibling)
J
Joe Thornber 已提交
443
		r = rebalance2(s, info, vt, i - 1);
J
Joe Thornber 已提交
444 445

	else
J
Joe Thornber 已提交
446
		r = rebalance3(s, info, vt, i - 1);
J
Joe Thornber 已提交
447 448 449 450

	return r;
}

451
static int do_leaf(struct btree_node *n, uint64_t key, unsigned *index)
J
Joe Thornber 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
	int i = lower_bound(n, key);

	if ((i < 0) ||
	    (i >= le32_to_cpu(n->header.nr_entries)) ||
	    (le64_to_cpu(n->keys[i]) != key))
		return -ENODATA;

	*index = i;

	return 0;
}

/*
 * Prepares for removal from one level of the hierarchy.  The caller must
 * call delete_at() to remove the entry at index.
 */
static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info,
		      struct dm_btree_value_type *vt, dm_block_t root,
		      uint64_t key, unsigned *index)
{
	int i = *index, r;
474
	struct btree_node *n;
J
Joe Thornber 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487

	for (;;) {
		r = shadow_step(s, root, vt);
		if (r < 0)
			break;

		/*
		 * We have to patch up the parent node, ugly, but I don't
		 * see a way to do this automatically as part of the spine
		 * op.
		 */
		if (shadow_has_parent(s)) {
			__le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
488
			memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
J
Joe Thornber 已提交
489 490 491 492 493 494 495 496
			       &location, sizeof(__le64));
		}

		n = dm_block_data(shadow_current(s));

		if (le32_to_cpu(n->header.flags) & LEAF_NODE)
			return do_leaf(n, key, index);

J
Joe Thornber 已提交
497
		r = rebalance_children(s, info, vt, key);
J
Joe Thornber 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		if (r)
			break;

		n = dm_block_data(shadow_current(s));
		if (le32_to_cpu(n->header.flags) & LEAF_NODE)
			return do_leaf(n, key, index);

		i = lower_bound(n, key);

		/*
		 * We know the key is present, or else
		 * rebalance_children would have returned
		 * -ENODATA
		 */
		root = value64(n, i);
	}

	return r;
}

int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
		    uint64_t *keys, dm_block_t *new_root)
{
	unsigned level, last_level = info->levels - 1;
	int index = 0, r = 0;
	struct shadow_spine spine;
524
	struct btree_node *n;
525
	struct dm_btree_value_type le64_vt;
J
Joe Thornber 已提交
526

527
	init_le64_type(info->tm, &le64_vt);
J
Joe Thornber 已提交
528 529 530 531
	init_shadow_spine(&spine, info);
	for (level = 0; level < info->levels; level++) {
		r = remove_raw(&spine, info,
			       (level == last_level ?
532
				&info->value_type : &le64_vt),
J
Joe Thornber 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546
			       root, keys[level], (unsigned *)&index);
		if (r < 0)
			break;

		n = dm_block_data(shadow_current(&spine));
		if (level != last_level) {
			root = value64(n, index);
			continue;
		}

		BUG_ON(index < 0 || index >= le32_to_cpu(n->header.nr_entries));

		if (info->value_type.dec)
			info->value_type.dec(info->value_type.context,
547
					     value_ptr(n, index));
J
Joe Thornber 已提交
548 549 550 551 552 553 554 555 556 557

		delete_at(n, index);
	}

	*new_root = shadow_root(&spine);
	exit_shadow_spine(&spine);

	return r;
}
EXPORT_SYMBOL_GPL(dm_btree_remove);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

/*----------------------------------------------------------------*/

static int remove_nearest(struct shadow_spine *s, struct dm_btree_info *info,
			  struct dm_btree_value_type *vt, dm_block_t root,
			  uint64_t key, int *index)
{
	int i = *index, r;
	struct btree_node *n;

	for (;;) {
		r = shadow_step(s, root, vt);
		if (r < 0)
			break;

		/*
		 * We have to patch up the parent node, ugly, but I don't
		 * see a way to do this automatically as part of the spine
		 * op.
		 */
		if (shadow_has_parent(s)) {
			__le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
			memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
			       &location, sizeof(__le64));
		}

		n = dm_block_data(shadow_current(s));

		if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
			*index = lower_bound(n, key);
			return 0;
		}

		r = rebalance_children(s, info, vt, key);
		if (r)
			break;

		n = dm_block_data(shadow_current(s));
		if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
			*index = lower_bound(n, key);
			return 0;
		}

		i = lower_bound(n, key);

		/*
		 * We know the key is present, or else
		 * rebalance_children would have returned
		 * -ENODATA
		 */
		root = value64(n, i);
	}

	return r;
}

static int remove_one(struct dm_btree_info *info, dm_block_t root,
		      uint64_t *keys, uint64_t end_key,
		      dm_block_t *new_root, unsigned *nr_removed)
{
	unsigned level, last_level = info->levels - 1;
	int index = 0, r = 0;
	struct shadow_spine spine;
	struct btree_node *n;
622
	struct dm_btree_value_type le64_vt;
623 624
	uint64_t k;

625
	init_le64_type(info->tm, &le64_vt);
626 627
	init_shadow_spine(&spine, info);
	for (level = 0; level < last_level; level++) {
628
		r = remove_raw(&spine, info, &le64_vt,
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
			       root, keys[level], (unsigned *) &index);
		if (r < 0)
			goto out;

		n = dm_block_data(shadow_current(&spine));
		root = value64(n, index);
	}

	r = remove_nearest(&spine, info, &info->value_type,
			   root, keys[last_level], &index);
	if (r < 0)
		goto out;

	n = dm_block_data(shadow_current(&spine));

	if (index < 0)
		index = 0;

	if (index >= le32_to_cpu(n->header.nr_entries)) {
		r = -ENODATA;
		goto out;
	}

	k = le64_to_cpu(n->keys[index]);
	if (k >= keys[last_level] && k < end_key) {
		if (info->value_type.dec)
			info->value_type.dec(info->value_type.context,
					     value_ptr(n, index));

		delete_at(n, index);
659
		keys[last_level] = k + 1ull;
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

	} else
		r = -ENODATA;

out:
	*new_root = shadow_root(&spine);
	exit_shadow_spine(&spine);

	return r;
}

int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,
			   uint64_t *first_key, uint64_t end_key,
			   dm_block_t *new_root, unsigned *nr_removed)
{
	int r;

	*nr_removed = 0;
	do {
		r = remove_one(info, root, first_key, end_key, &root, nr_removed);
		if (!r)
			(*nr_removed)++;
	} while (!r);

	*new_root = root;
	return r == -ENODATA ? 0 : r;
}
EXPORT_SYMBOL_GPL(dm_btree_remove_leaves);