dm-btree-remove.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-btree.h"
  8. #include "dm-btree-internal.h"
  9. #include "dm-transaction-manager.h"
  10. #include <linux/export.h>
  11. #include <linux/device-mapper.h>
  12. #define DM_MSG_PREFIX "btree"
  13. /*
  14. * Removing an entry from a btree
  15. * ==============================
  16. *
  17. * A very important constraint for our btree is that no node, except the
  18. * root, may have fewer than a certain number of entries.
  19. * (MIN_ENTRIES <= nr_entries <= MAX_ENTRIES).
  20. *
  21. * Ensuring this is complicated by the way we want to only ever hold the
  22. * locks on 2 nodes concurrently, and only change nodes in a top to bottom
  23. * fashion.
  24. *
  25. * Each node may have a left or right sibling. When decending the spine,
  26. * if a node contains only MIN_ENTRIES then we try and increase this to at
  27. * least MIN_ENTRIES + 1. We do this in the following ways:
  28. *
  29. * [A] No siblings => this can only happen if the node is the root, in which
  30. * case we copy the childs contents over the root.
  31. *
  32. * [B] No left sibling
  33. * ==> rebalance(node, right sibling)
  34. *
  35. * [C] No right sibling
  36. * ==> rebalance(left sibling, node)
  37. *
  38. * [D] Both siblings, total_entries(left, node, right) <= DEL_THRESHOLD
  39. * ==> delete node adding it's contents to left and right
  40. *
  41. * [E] Both siblings, total_entries(left, node, right) > DEL_THRESHOLD
  42. * ==> rebalance(left, node, right)
  43. *
  44. * After these operations it's possible that the our original node no
  45. * longer contains the desired sub tree. For this reason this rebalancing
  46. * is performed on the children of the current node. This also avoids
  47. * having a special case for the root.
  48. *
  49. * Once this rebalancing has occurred we can then step into the child node
  50. * for internal nodes. Or delete the entry for leaf nodes.
  51. */
  52. /*
  53. * Some little utilities for moving node data around.
  54. */
  55. static void node_shift(struct btree_node *n, int shift)
  56. {
  57. uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
  58. uint32_t value_size = le32_to_cpu(n->header.value_size);
  59. if (shift < 0) {
  60. shift = -shift;
  61. BUG_ON(shift > nr_entries);
  62. BUG_ON((void *) key_ptr(n, shift) >= value_ptr(n, shift));
  63. memmove(key_ptr(n, 0),
  64. key_ptr(n, shift),
  65. (nr_entries - shift) * sizeof(__le64));
  66. memmove(value_ptr(n, 0),
  67. value_ptr(n, shift),
  68. (nr_entries - shift) * value_size);
  69. } else {
  70. BUG_ON(nr_entries + shift > le32_to_cpu(n->header.max_entries));
  71. memmove(key_ptr(n, shift),
  72. key_ptr(n, 0),
  73. nr_entries * sizeof(__le64));
  74. memmove(value_ptr(n, shift),
  75. value_ptr(n, 0),
  76. nr_entries * value_size);
  77. }
  78. }
  79. static int node_copy(struct btree_node *left, struct btree_node *right, int shift)
  80. {
  81. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  82. uint32_t value_size = le32_to_cpu(left->header.value_size);
  83. if (value_size != le32_to_cpu(right->header.value_size)) {
  84. DMERR("mismatched value size");
  85. return -EILSEQ;
  86. }
  87. if (shift < 0) {
  88. shift = -shift;
  89. if (nr_left + shift > le32_to_cpu(left->header.max_entries)) {
  90. DMERR("bad shift");
  91. return -EINVAL;
  92. }
  93. memcpy(key_ptr(left, nr_left),
  94. key_ptr(right, 0),
  95. shift * sizeof(__le64));
  96. memcpy(value_ptr(left, nr_left),
  97. value_ptr(right, 0),
  98. shift * value_size);
  99. } else {
  100. if (shift > le32_to_cpu(right->header.max_entries)) {
  101. DMERR("bad shift");
  102. return -EINVAL;
  103. }
  104. memcpy(key_ptr(right, 0),
  105. key_ptr(left, nr_left - shift),
  106. shift * sizeof(__le64));
  107. memcpy(value_ptr(right, 0),
  108. value_ptr(left, nr_left - shift),
  109. shift * value_size);
  110. }
  111. return 0;
  112. }
  113. /*
  114. * Delete a specific entry from a leaf node.
  115. */
  116. static void delete_at(struct btree_node *n, unsigned int index)
  117. {
  118. unsigned int nr_entries = le32_to_cpu(n->header.nr_entries);
  119. unsigned int nr_to_copy = nr_entries - (index + 1);
  120. uint32_t value_size = le32_to_cpu(n->header.value_size);
  121. BUG_ON(index >= nr_entries);
  122. if (nr_to_copy) {
  123. memmove(key_ptr(n, index),
  124. key_ptr(n, index + 1),
  125. nr_to_copy * sizeof(__le64));
  126. memmove(value_ptr(n, index),
  127. value_ptr(n, index + 1),
  128. nr_to_copy * value_size);
  129. }
  130. n->header.nr_entries = cpu_to_le32(nr_entries - 1);
  131. }
  132. static unsigned int merge_threshold(struct btree_node *n)
  133. {
  134. return le32_to_cpu(n->header.max_entries) / 3;
  135. }
  136. struct child {
  137. unsigned int index;
  138. struct dm_block *block;
  139. struct btree_node *n;
  140. };
  141. static int init_child(struct dm_btree_info *info, struct dm_btree_value_type *vt,
  142. struct btree_node *parent,
  143. unsigned int index, struct child *result)
  144. {
  145. int r, inc;
  146. dm_block_t root;
  147. result->index = index;
  148. root = value64(parent, index);
  149. r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
  150. &result->block, &inc);
  151. if (r)
  152. return r;
  153. result->n = dm_block_data(result->block);
  154. if (inc)
  155. inc_children(info->tm, result->n, vt);
  156. *((__le64 *) value_ptr(parent, index)) =
  157. cpu_to_le64(dm_block_location(result->block));
  158. return 0;
  159. }
  160. static void exit_child(struct dm_btree_info *info, struct child *c)
  161. {
  162. dm_tm_unlock(info->tm, c->block);
  163. }
  164. static int shift(struct btree_node *left, struct btree_node *right, int count)
  165. {
  166. int r;
  167. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  168. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  169. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  170. uint32_t r_max_entries = le32_to_cpu(right->header.max_entries);
  171. if (max_entries != r_max_entries) {
  172. DMERR("node max_entries mismatch");
  173. return -EILSEQ;
  174. }
  175. if (nr_left - count > max_entries) {
  176. DMERR("node shift out of bounds");
  177. return -EINVAL;
  178. }
  179. if (nr_right + count > max_entries) {
  180. DMERR("node shift out of bounds");
  181. return -EINVAL;
  182. }
  183. if (!count)
  184. return 0;
  185. if (count > 0) {
  186. node_shift(right, count);
  187. r = node_copy(left, right, count);
  188. if (r)
  189. return r;
  190. } else {
  191. r = node_copy(left, right, count);
  192. if (r)
  193. return r;
  194. node_shift(right, count);
  195. }
  196. left->header.nr_entries = cpu_to_le32(nr_left - count);
  197. right->header.nr_entries = cpu_to_le32(nr_right + count);
  198. return 0;
  199. }
  200. static int __rebalance2(struct dm_btree_info *info, struct btree_node *parent,
  201. struct child *l, struct child *r)
  202. {
  203. int ret;
  204. struct btree_node *left = l->n;
  205. struct btree_node *right = r->n;
  206. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  207. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  208. /*
  209. * Ensure the number of entries in each child will be greater
  210. * than or equal to (max_entries / 3 + 1), so no matter which
  211. * child is used for removal, the number will still be not
  212. * less than (max_entries / 3).
  213. */
  214. unsigned int threshold = 2 * (merge_threshold(left) + 1);
  215. if (nr_left + nr_right < threshold) {
  216. /*
  217. * Merge
  218. */
  219. node_copy(left, right, -nr_right);
  220. left->header.nr_entries = cpu_to_le32(nr_left + nr_right);
  221. delete_at(parent, r->index);
  222. /*
  223. * We need to decrement the right block, but not it's
  224. * children, since they're still referenced by left.
  225. */
  226. dm_tm_dec(info->tm, dm_block_location(r->block));
  227. } else {
  228. /*
  229. * Rebalance.
  230. */
  231. unsigned int target_left = (nr_left + nr_right) / 2;
  232. ret = shift(left, right, nr_left - target_left);
  233. if (ret)
  234. return ret;
  235. *key_ptr(parent, r->index) = right->keys[0];
  236. }
  237. return 0;
  238. }
  239. static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info,
  240. struct dm_btree_value_type *vt, unsigned int left_index)
  241. {
  242. int r;
  243. struct btree_node *parent;
  244. struct child left, right;
  245. parent = dm_block_data(shadow_current(s));
  246. r = init_child(info, vt, parent, left_index, &left);
  247. if (r)
  248. return r;
  249. r = init_child(info, vt, parent, left_index + 1, &right);
  250. if (r) {
  251. exit_child(info, &left);
  252. return r;
  253. }
  254. r = __rebalance2(info, parent, &left, &right);
  255. exit_child(info, &left);
  256. exit_child(info, &right);
  257. return r;
  258. }
  259. /*
  260. * We dump as many entries from center as possible into left, then the rest
  261. * in right, then rebalance2. This wastes some cpu, but I want something
  262. * simple atm.
  263. */
  264. static int delete_center_node(struct dm_btree_info *info, struct btree_node *parent,
  265. struct child *l, struct child *c, struct child *r,
  266. struct btree_node *left, struct btree_node *center, struct btree_node *right,
  267. uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
  268. {
  269. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  270. unsigned int shift = min(max_entries - nr_left, nr_center);
  271. if (nr_left + shift > max_entries) {
  272. DMERR("node shift out of bounds");
  273. return -EINVAL;
  274. }
  275. node_copy(left, center, -shift);
  276. left->header.nr_entries = cpu_to_le32(nr_left + shift);
  277. if (shift != nr_center) {
  278. shift = nr_center - shift;
  279. if ((nr_right + shift) > max_entries) {
  280. DMERR("node shift out of bounds");
  281. return -EINVAL;
  282. }
  283. node_shift(right, shift);
  284. node_copy(center, right, shift);
  285. right->header.nr_entries = cpu_to_le32(nr_right + shift);
  286. }
  287. *key_ptr(parent, r->index) = right->keys[0];
  288. delete_at(parent, c->index);
  289. r->index--;
  290. dm_tm_dec(info->tm, dm_block_location(c->block));
  291. return __rebalance2(info, parent, l, r);
  292. }
  293. /*
  294. * Redistributes entries among 3 sibling nodes.
  295. */
  296. static int redistribute3(struct dm_btree_info *info, struct btree_node *parent,
  297. struct child *l, struct child *c, struct child *r,
  298. struct btree_node *left, struct btree_node *center, struct btree_node *right,
  299. uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
  300. {
  301. int s, ret;
  302. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  303. unsigned int total = nr_left + nr_center + nr_right;
  304. unsigned int target_right = total / 3;
  305. unsigned int remainder = (target_right * 3) != total;
  306. unsigned int target_left = target_right + remainder;
  307. BUG_ON(target_left > max_entries);
  308. BUG_ON(target_right > max_entries);
  309. if (nr_left < nr_right) {
  310. s = nr_left - target_left;
  311. if (s < 0 && nr_center < -s) {
  312. /* not enough in central node */
  313. ret = shift(left, center, -nr_center);
  314. if (ret)
  315. return ret;
  316. s += nr_center;
  317. ret = shift(left, right, s);
  318. if (ret)
  319. return ret;
  320. nr_right += s;
  321. } else {
  322. ret = shift(left, center, s);
  323. if (ret)
  324. return ret;
  325. }
  326. ret = shift(center, right, target_right - nr_right);
  327. if (ret)
  328. return ret;
  329. } else {
  330. s = target_right - nr_right;
  331. if (s > 0 && nr_center < s) {
  332. /* not enough in central node */
  333. ret = shift(center, right, nr_center);
  334. if (ret)
  335. return ret;
  336. s -= nr_center;
  337. ret = shift(left, right, s);
  338. if (ret)
  339. return ret;
  340. nr_left -= s;
  341. } else {
  342. ret = shift(center, right, s);
  343. if (ret)
  344. return ret;
  345. }
  346. ret = shift(left, center, nr_left - target_left);
  347. if (ret)
  348. return ret;
  349. }
  350. *key_ptr(parent, c->index) = center->keys[0];
  351. *key_ptr(parent, r->index) = right->keys[0];
  352. return 0;
  353. }
  354. static int __rebalance3(struct dm_btree_info *info, struct btree_node *parent,
  355. struct child *l, struct child *c, struct child *r)
  356. {
  357. struct btree_node *left = l->n;
  358. struct btree_node *center = c->n;
  359. struct btree_node *right = r->n;
  360. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  361. uint32_t nr_center = le32_to_cpu(center->header.nr_entries);
  362. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  363. unsigned int threshold = merge_threshold(left) * 4 + 1;
  364. if ((left->header.max_entries != center->header.max_entries) ||
  365. (center->header.max_entries != right->header.max_entries)) {
  366. DMERR("bad btree metadata, max_entries differ");
  367. return -EILSEQ;
  368. }
  369. if ((nr_left + nr_center + nr_right) < threshold) {
  370. return delete_center_node(info, parent, l, c, r, left, center, right,
  371. nr_left, nr_center, nr_right);
  372. }
  373. return redistribute3(info, parent, l, c, r, left, center, right,
  374. nr_left, nr_center, nr_right);
  375. }
  376. static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info,
  377. struct dm_btree_value_type *vt, unsigned int left_index)
  378. {
  379. int r;
  380. struct btree_node *parent = dm_block_data(shadow_current(s));
  381. struct child left, center, right;
  382. /*
  383. * FIXME: fill out an array?
  384. */
  385. r = init_child(info, vt, parent, left_index, &left);
  386. if (r)
  387. return r;
  388. r = init_child(info, vt, parent, left_index + 1, &center);
  389. if (r) {
  390. exit_child(info, &left);
  391. return r;
  392. }
  393. r = init_child(info, vt, parent, left_index + 2, &right);
  394. if (r) {
  395. exit_child(info, &left);
  396. exit_child(info, &center);
  397. return r;
  398. }
  399. r = __rebalance3(info, parent, &left, &center, &right);
  400. exit_child(info, &left);
  401. exit_child(info, &center);
  402. exit_child(info, &right);
  403. return r;
  404. }
  405. static int rebalance_children(struct shadow_spine *s,
  406. struct dm_btree_info *info,
  407. struct dm_btree_value_type *vt, uint64_t key)
  408. {
  409. int i, r, has_left_sibling, has_right_sibling;
  410. struct btree_node *n;
  411. n = dm_block_data(shadow_current(s));
  412. if (le32_to_cpu(n->header.nr_entries) == 1) {
  413. struct dm_block *child;
  414. dm_block_t b = value64(n, 0);
  415. r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child);
  416. if (r)
  417. return r;
  418. memcpy(n, dm_block_data(child),
  419. dm_bm_block_size(dm_tm_get_bm(info->tm)));
  420. dm_tm_dec(info->tm, dm_block_location(child));
  421. dm_tm_unlock(info->tm, child);
  422. return 0;
  423. }
  424. i = lower_bound(n, key);
  425. if (i < 0)
  426. return -ENODATA;
  427. has_left_sibling = i > 0;
  428. has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1);
  429. if (!has_left_sibling)
  430. r = rebalance2(s, info, vt, i);
  431. else if (!has_right_sibling)
  432. r = rebalance2(s, info, vt, i - 1);
  433. else
  434. r = rebalance3(s, info, vt, i - 1);
  435. return r;
  436. }
  437. static int do_leaf(struct btree_node *n, uint64_t key, unsigned int *index)
  438. {
  439. int i = lower_bound(n, key);
  440. if ((i < 0) ||
  441. (i >= le32_to_cpu(n->header.nr_entries)) ||
  442. (le64_to_cpu(n->keys[i]) != key))
  443. return -ENODATA;
  444. *index = i;
  445. return 0;
  446. }
  447. /*
  448. * Prepares for removal from one level of the hierarchy. The caller must
  449. * call delete_at() to remove the entry at index.
  450. */
  451. static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info,
  452. struct dm_btree_value_type *vt, dm_block_t root,
  453. uint64_t key, unsigned int *index)
  454. {
  455. int i = *index, r;
  456. struct btree_node *n;
  457. for (;;) {
  458. r = shadow_step(s, root, vt);
  459. if (r < 0)
  460. break;
  461. /*
  462. * We have to patch up the parent node, ugly, but I don't
  463. * see a way to do this automatically as part of the spine
  464. * op.
  465. */
  466. if (shadow_has_parent(s)) {
  467. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  468. memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
  469. &location, sizeof(__le64));
  470. }
  471. n = dm_block_data(shadow_current(s));
  472. if (le32_to_cpu(n->header.flags) & LEAF_NODE)
  473. return do_leaf(n, key, index);
  474. r = rebalance_children(s, info, vt, key);
  475. if (r)
  476. break;
  477. n = dm_block_data(shadow_current(s));
  478. if (le32_to_cpu(n->header.flags) & LEAF_NODE)
  479. return do_leaf(n, key, index);
  480. i = lower_bound(n, key);
  481. /*
  482. * We know the key is present, or else
  483. * rebalance_children would have returned
  484. * -ENODATA
  485. */
  486. root = value64(n, i);
  487. }
  488. return r;
  489. }
  490. int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
  491. uint64_t *keys, dm_block_t *new_root)
  492. {
  493. unsigned int level, last_level = info->levels - 1;
  494. int index = 0, r = 0;
  495. struct shadow_spine spine;
  496. struct btree_node *n;
  497. struct dm_btree_value_type le64_vt;
  498. init_le64_type(info->tm, &le64_vt);
  499. init_shadow_spine(&spine, info);
  500. for (level = 0; level < info->levels; level++) {
  501. r = remove_raw(&spine, info,
  502. (level == last_level ?
  503. &info->value_type : &le64_vt),
  504. root, keys[level], (unsigned int *)&index);
  505. if (r < 0)
  506. break;
  507. n = dm_block_data(shadow_current(&spine));
  508. if (level != last_level) {
  509. root = value64(n, index);
  510. continue;
  511. }
  512. BUG_ON(index < 0 || index >= le32_to_cpu(n->header.nr_entries));
  513. if (info->value_type.dec)
  514. info->value_type.dec(info->value_type.context,
  515. value_ptr(n, index), 1);
  516. delete_at(n, index);
  517. }
  518. if (!r)
  519. *new_root = shadow_root(&spine);
  520. exit_shadow_spine(&spine);
  521. return r;
  522. }
  523. EXPORT_SYMBOL_GPL(dm_btree_remove);
  524. /*----------------------------------------------------------------*/
  525. static int remove_nearest(struct shadow_spine *s, struct dm_btree_info *info,
  526. struct dm_btree_value_type *vt, dm_block_t root,
  527. uint64_t key, int *index)
  528. {
  529. int i = *index, r;
  530. struct btree_node *n;
  531. for (;;) {
  532. r = shadow_step(s, root, vt);
  533. if (r < 0)
  534. break;
  535. /*
  536. * We have to patch up the parent node, ugly, but I don't
  537. * see a way to do this automatically as part of the spine
  538. * op.
  539. */
  540. if (shadow_has_parent(s)) {
  541. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  542. memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
  543. &location, sizeof(__le64));
  544. }
  545. n = dm_block_data(shadow_current(s));
  546. if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
  547. *index = lower_bound(n, key);
  548. return 0;
  549. }
  550. r = rebalance_children(s, info, vt, key);
  551. if (r)
  552. break;
  553. n = dm_block_data(shadow_current(s));
  554. if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
  555. *index = lower_bound(n, key);
  556. return 0;
  557. }
  558. i = lower_bound(n, key);
  559. /*
  560. * We know the key is present, or else
  561. * rebalance_children would have returned
  562. * -ENODATA
  563. */
  564. root = value64(n, i);
  565. }
  566. return r;
  567. }
  568. static int remove_one(struct dm_btree_info *info, dm_block_t root,
  569. uint64_t *keys, uint64_t end_key,
  570. dm_block_t *new_root, unsigned int *nr_removed)
  571. {
  572. unsigned int level, last_level = info->levels - 1;
  573. int index = 0, r = 0;
  574. struct shadow_spine spine;
  575. struct btree_node *n;
  576. struct dm_btree_value_type le64_vt;
  577. uint64_t k;
  578. init_le64_type(info->tm, &le64_vt);
  579. init_shadow_spine(&spine, info);
  580. for (level = 0; level < last_level; level++) {
  581. r = remove_raw(&spine, info, &le64_vt,
  582. root, keys[level], (unsigned int *) &index);
  583. if (r < 0)
  584. goto out;
  585. n = dm_block_data(shadow_current(&spine));
  586. root = value64(n, index);
  587. }
  588. r = remove_nearest(&spine, info, &info->value_type,
  589. root, keys[last_level], &index);
  590. if (r < 0)
  591. goto out;
  592. n = dm_block_data(shadow_current(&spine));
  593. if (index < 0)
  594. index = 0;
  595. if (index >= le32_to_cpu(n->header.nr_entries)) {
  596. r = -ENODATA;
  597. goto out;
  598. }
  599. k = le64_to_cpu(n->keys[index]);
  600. if (k >= keys[last_level] && k < end_key) {
  601. if (info->value_type.dec)
  602. info->value_type.dec(info->value_type.context,
  603. value_ptr(n, index), 1);
  604. delete_at(n, index);
  605. keys[last_level] = k + 1ull;
  606. } else
  607. r = -ENODATA;
  608. out:
  609. *new_root = shadow_root(&spine);
  610. exit_shadow_spine(&spine);
  611. return r;
  612. }
  613. int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,
  614. uint64_t *first_key, uint64_t end_key,
  615. dm_block_t *new_root, unsigned int *nr_removed)
  616. {
  617. int r;
  618. *nr_removed = 0;
  619. do {
  620. r = remove_one(info, root, first_key, end_key, &root, nr_removed);
  621. if (!r)
  622. (*nr_removed)++;
  623. } while (!r);
  624. *new_root = root;
  625. return r == -ENODATA ? 0 : r;
  626. }
  627. EXPORT_SYMBOL_GPL(dm_btree_remove_leaves);