dm-btree-remove.c 14 KB

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