dm-btree-remove.c 18 KB

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