dm-btree-remove.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 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 node *left, struct 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 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 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 node *n;
  127. };
  128. static struct dm_btree_value_type le64_type = {
  129. .context = NULL,
  130. .size = sizeof(__le64),
  131. .inc = NULL,
  132. .dec = NULL,
  133. .equal = NULL
  134. };
  135. static int init_child(struct dm_btree_info *info, struct node *parent,
  136. unsigned index, struct child *result)
  137. {
  138. int r, inc;
  139. dm_block_t root;
  140. result->index = index;
  141. root = value64(parent, index);
  142. r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
  143. &result->block, &inc);
  144. if (r)
  145. return r;
  146. result->n = dm_block_data(result->block);
  147. if (inc)
  148. inc_children(info->tm, result->n, &le64_type);
  149. *((__le64 *) value_ptr(parent, index)) =
  150. cpu_to_le64(dm_block_location(result->block));
  151. return 0;
  152. }
  153. static int exit_child(struct dm_btree_info *info, struct child *c)
  154. {
  155. return dm_tm_unlock(info->tm, c->block);
  156. }
  157. static void shift(struct node *left, struct node *right, int count)
  158. {
  159. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  160. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  161. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  162. uint32_t r_max_entries = le32_to_cpu(right->header.max_entries);
  163. BUG_ON(max_entries != r_max_entries);
  164. BUG_ON(nr_left - count > max_entries);
  165. BUG_ON(nr_right + count > max_entries);
  166. if (!count)
  167. return;
  168. if (count > 0) {
  169. node_shift(right, count);
  170. node_copy(left, right, count);
  171. } else {
  172. node_copy(left, right, count);
  173. node_shift(right, count);
  174. }
  175. left->header.nr_entries = cpu_to_le32(nr_left - count);
  176. right->header.nr_entries = cpu_to_le32(nr_right + count);
  177. }
  178. static void __rebalance2(struct dm_btree_info *info, struct node *parent,
  179. struct child *l, struct child *r)
  180. {
  181. struct node *left = l->n;
  182. struct node *right = r->n;
  183. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  184. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  185. unsigned threshold = 2 * merge_threshold(left) + 1;
  186. if (nr_left + nr_right < threshold) {
  187. /*
  188. * Merge
  189. */
  190. node_copy(left, right, -nr_right);
  191. left->header.nr_entries = cpu_to_le32(nr_left + nr_right);
  192. delete_at(parent, r->index);
  193. /*
  194. * We need to decrement the right block, but not it's
  195. * children, since they're still referenced by left.
  196. */
  197. dm_tm_dec(info->tm, dm_block_location(r->block));
  198. } else {
  199. /*
  200. * Rebalance.
  201. */
  202. unsigned target_left = (nr_left + nr_right) / 2;
  203. shift(left, right, nr_left - target_left);
  204. *key_ptr(parent, r->index) = right->keys[0];
  205. }
  206. }
  207. static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info,
  208. unsigned left_index)
  209. {
  210. int r;
  211. struct node *parent;
  212. struct child left, right;
  213. parent = dm_block_data(shadow_current(s));
  214. r = init_child(info, parent, left_index, &left);
  215. if (r)
  216. return r;
  217. r = init_child(info, parent, left_index + 1, &right);
  218. if (r) {
  219. exit_child(info, &left);
  220. return r;
  221. }
  222. __rebalance2(info, parent, &left, &right);
  223. r = exit_child(info, &left);
  224. if (r) {
  225. exit_child(info, &right);
  226. return r;
  227. }
  228. return exit_child(info, &right);
  229. }
  230. /*
  231. * We dump as many entries from center as possible into left, then the rest
  232. * in right, then rebalance2. This wastes some cpu, but I want something
  233. * simple atm.
  234. */
  235. static void delete_center_node(struct dm_btree_info *info, struct node *parent,
  236. struct child *l, struct child *c, struct child *r,
  237. struct node *left, struct node *center, struct node *right,
  238. uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
  239. {
  240. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  241. unsigned shift = min(max_entries - nr_left, nr_center);
  242. BUG_ON(nr_left + shift > max_entries);
  243. node_copy(left, center, -shift);
  244. left->header.nr_entries = cpu_to_le32(nr_left + shift);
  245. if (shift != nr_center) {
  246. shift = nr_center - shift;
  247. BUG_ON((nr_right + shift) > max_entries);
  248. node_shift(right, shift);
  249. node_copy(center, right, shift);
  250. right->header.nr_entries = cpu_to_le32(nr_right + shift);
  251. }
  252. *key_ptr(parent, r->index) = right->keys[0];
  253. delete_at(parent, c->index);
  254. r->index--;
  255. dm_tm_dec(info->tm, dm_block_location(c->block));
  256. __rebalance2(info, parent, l, r);
  257. }
  258. /*
  259. * Redistributes entries among 3 sibling nodes.
  260. */
  261. static void redistribute3(struct dm_btree_info *info, struct node *parent,
  262. struct child *l, struct child *c, struct child *r,
  263. struct node *left, struct node *center, struct node *right,
  264. uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
  265. {
  266. int s;
  267. uint32_t max_entries = le32_to_cpu(left->header.max_entries);
  268. unsigned target = (nr_left + nr_center + nr_right) / 3;
  269. BUG_ON(target > max_entries);
  270. if (nr_left < nr_right) {
  271. s = nr_left - target;
  272. if (s < 0 && nr_center < -s) {
  273. /* not enough in central node */
  274. shift(left, center, nr_center);
  275. s = nr_center - target;
  276. shift(left, right, s);
  277. nr_right += s;
  278. } else
  279. shift(left, center, s);
  280. shift(center, right, target - nr_right);
  281. } else {
  282. s = target - nr_right;
  283. if (s > 0 && nr_center < s) {
  284. /* not enough in central node */
  285. shift(center, right, nr_center);
  286. s = target - nr_center;
  287. shift(left, right, s);
  288. nr_left -= s;
  289. } else
  290. shift(center, right, s);
  291. shift(left, center, nr_left - target);
  292. }
  293. *key_ptr(parent, c->index) = center->keys[0];
  294. *key_ptr(parent, r->index) = right->keys[0];
  295. }
  296. static void __rebalance3(struct dm_btree_info *info, struct node *parent,
  297. struct child *l, struct child *c, struct child *r)
  298. {
  299. struct node *left = l->n;
  300. struct node *center = c->n;
  301. struct node *right = r->n;
  302. uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
  303. uint32_t nr_center = le32_to_cpu(center->header.nr_entries);
  304. uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
  305. unsigned threshold = merge_threshold(left) * 4 + 1;
  306. BUG_ON(left->header.max_entries != center->header.max_entries);
  307. BUG_ON(center->header.max_entries != right->header.max_entries);
  308. if ((nr_left + nr_center + nr_right) < threshold)
  309. delete_center_node(info, parent, l, c, r, left, center, right,
  310. nr_left, nr_center, nr_right);
  311. else
  312. redistribute3(info, parent, l, c, r, left, center, right,
  313. nr_left, nr_center, nr_right);
  314. }
  315. static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info,
  316. unsigned left_index)
  317. {
  318. int r;
  319. struct node *parent = dm_block_data(shadow_current(s));
  320. struct child left, center, right;
  321. /*
  322. * FIXME: fill out an array?
  323. */
  324. r = init_child(info, parent, left_index, &left);
  325. if (r)
  326. return r;
  327. r = init_child(info, parent, left_index + 1, &center);
  328. if (r) {
  329. exit_child(info, &left);
  330. return r;
  331. }
  332. r = init_child(info, parent, left_index + 2, &right);
  333. if (r) {
  334. exit_child(info, &left);
  335. exit_child(info, &center);
  336. return r;
  337. }
  338. __rebalance3(info, parent, &left, &center, &right);
  339. r = exit_child(info, &left);
  340. if (r) {
  341. exit_child(info, &center);
  342. exit_child(info, &right);
  343. return r;
  344. }
  345. r = exit_child(info, &center);
  346. if (r) {
  347. exit_child(info, &right);
  348. return r;
  349. }
  350. r = exit_child(info, &right);
  351. if (r)
  352. return r;
  353. return 0;
  354. }
  355. static int get_nr_entries(struct dm_transaction_manager *tm,
  356. dm_block_t b, uint32_t *result)
  357. {
  358. int r;
  359. struct dm_block *block;
  360. struct node *n;
  361. r = dm_tm_read_lock(tm, b, &btree_node_validator, &block);
  362. if (r)
  363. return r;
  364. n = dm_block_data(block);
  365. *result = le32_to_cpu(n->header.nr_entries);
  366. return dm_tm_unlock(tm, block);
  367. }
  368. static int rebalance_children(struct shadow_spine *s,
  369. struct dm_btree_info *info, uint64_t key)
  370. {
  371. int i, r, has_left_sibling, has_right_sibling;
  372. uint32_t child_entries;
  373. struct node *n;
  374. n = dm_block_data(shadow_current(s));
  375. if (le32_to_cpu(n->header.nr_entries) == 1) {
  376. struct dm_block *child;
  377. dm_block_t b = value64(n, 0);
  378. r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child);
  379. if (r)
  380. return r;
  381. memcpy(n, dm_block_data(child),
  382. dm_bm_block_size(dm_tm_get_bm(info->tm)));
  383. r = dm_tm_unlock(info->tm, child);
  384. if (r)
  385. return r;
  386. dm_tm_dec(info->tm, dm_block_location(child));
  387. return 0;
  388. }
  389. i = lower_bound(n, key);
  390. if (i < 0)
  391. return -ENODATA;
  392. r = get_nr_entries(info->tm, value64(n, i), &child_entries);
  393. if (r)
  394. return r;
  395. has_left_sibling = i > 0;
  396. has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1);
  397. if (!has_left_sibling)
  398. r = rebalance2(s, info, i);
  399. else if (!has_right_sibling)
  400. r = rebalance2(s, info, i - 1);
  401. else
  402. r = rebalance3(s, info, i - 1);
  403. return r;
  404. }
  405. static int do_leaf(struct node *n, uint64_t key, unsigned *index)
  406. {
  407. int i = lower_bound(n, key);
  408. if ((i < 0) ||
  409. (i >= le32_to_cpu(n->header.nr_entries)) ||
  410. (le64_to_cpu(n->keys[i]) != key))
  411. return -ENODATA;
  412. *index = i;
  413. return 0;
  414. }
  415. /*
  416. * Prepares for removal from one level of the hierarchy. The caller must
  417. * call delete_at() to remove the entry at index.
  418. */
  419. static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info,
  420. struct dm_btree_value_type *vt, dm_block_t root,
  421. uint64_t key, unsigned *index)
  422. {
  423. int i = *index, r;
  424. struct node *n;
  425. for (;;) {
  426. r = shadow_step(s, root, vt);
  427. if (r < 0)
  428. break;
  429. /*
  430. * We have to patch up the parent node, ugly, but I don't
  431. * see a way to do this automatically as part of the spine
  432. * op.
  433. */
  434. if (shadow_has_parent(s)) {
  435. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  436. memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
  437. &location, sizeof(__le64));
  438. }
  439. n = dm_block_data(shadow_current(s));
  440. if (le32_to_cpu(n->header.flags) & LEAF_NODE)
  441. return do_leaf(n, key, index);
  442. r = rebalance_children(s, info, key);
  443. if (r)
  444. break;
  445. n = dm_block_data(shadow_current(s));
  446. if (le32_to_cpu(n->header.flags) & LEAF_NODE)
  447. return do_leaf(n, key, index);
  448. i = lower_bound(n, key);
  449. /*
  450. * We know the key is present, or else
  451. * rebalance_children would have returned
  452. * -ENODATA
  453. */
  454. root = value64(n, i);
  455. }
  456. return r;
  457. }
  458. int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
  459. uint64_t *keys, dm_block_t *new_root)
  460. {
  461. unsigned level, last_level = info->levels - 1;
  462. int index = 0, r = 0;
  463. struct shadow_spine spine;
  464. struct node *n;
  465. init_shadow_spine(&spine, info);
  466. for (level = 0; level < info->levels; level++) {
  467. r = remove_raw(&spine, info,
  468. (level == last_level ?
  469. &info->value_type : &le64_type),
  470. root, keys[level], (unsigned *)&index);
  471. if (r < 0)
  472. break;
  473. n = dm_block_data(shadow_current(&spine));
  474. if (level != last_level) {
  475. root = value64(n, index);
  476. continue;
  477. }
  478. BUG_ON(index < 0 || index >= le32_to_cpu(n->header.nr_entries));
  479. if (info->value_type.dec)
  480. info->value_type.dec(info->value_type.context,
  481. value_ptr(n, index));
  482. delete_at(n, index);
  483. }
  484. *new_root = shadow_root(&spine);
  485. exit_shadow_spine(&spine);
  486. return r;
  487. }
  488. EXPORT_SYMBOL_GPL(dm_btree_remove);