dm-btree-remove.c 17 KB

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