Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Red Hat, Inc. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm-btree.h" |
| 8 | #include "dm-btree-internal.h" |
| 9 | #include "dm-transaction-manager.h" |
| 10 | |
Paul Gortmaker | 1944ce6 | 2011-09-28 18:29:32 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 12 | |
| 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 | /* |
| 54 | * Some little utilities for moving node data around. |
| 55 | */ |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 56 | static void node_shift(struct btree_node *n, int shift) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 57 | { |
| 58 | uint32_t nr_entries = le32_to_cpu(n->header.nr_entries); |
| 59 | uint32_t value_size = le32_to_cpu(n->header.value_size); |
| 60 | |
| 61 | if (shift < 0) { |
| 62 | shift = -shift; |
| 63 | BUG_ON(shift > nr_entries); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 64 | BUG_ON((void *) key_ptr(n, shift) >= value_ptr(n, shift)); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 65 | memmove(key_ptr(n, 0), |
| 66 | key_ptr(n, shift), |
| 67 | (nr_entries - shift) * sizeof(__le64)); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 68 | memmove(value_ptr(n, 0), |
| 69 | value_ptr(n, shift), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 70 | (nr_entries - shift) * value_size); |
| 71 | } else { |
| 72 | BUG_ON(nr_entries + shift > le32_to_cpu(n->header.max_entries)); |
| 73 | memmove(key_ptr(n, shift), |
| 74 | key_ptr(n, 0), |
| 75 | nr_entries * sizeof(__le64)); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 76 | memmove(value_ptr(n, shift), |
| 77 | value_ptr(n, 0), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 78 | nr_entries * value_size); |
| 79 | } |
| 80 | } |
| 81 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 82 | static void node_copy(struct btree_node *left, struct btree_node *right, int shift) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 83 | { |
| 84 | uint32_t nr_left = le32_to_cpu(left->header.nr_entries); |
| 85 | uint32_t value_size = le32_to_cpu(left->header.value_size); |
| 86 | BUG_ON(value_size != le32_to_cpu(right->header.value_size)); |
| 87 | |
| 88 | if (shift < 0) { |
| 89 | shift = -shift; |
| 90 | BUG_ON(nr_left + shift > le32_to_cpu(left->header.max_entries)); |
| 91 | memcpy(key_ptr(left, nr_left), |
| 92 | key_ptr(right, 0), |
| 93 | shift * sizeof(__le64)); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 94 | memcpy(value_ptr(left, nr_left), |
| 95 | value_ptr(right, 0), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 96 | shift * value_size); |
| 97 | } else { |
| 98 | BUG_ON(shift > le32_to_cpu(right->header.max_entries)); |
| 99 | memcpy(key_ptr(right, 0), |
| 100 | key_ptr(left, nr_left - shift), |
| 101 | shift * sizeof(__le64)); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 102 | memcpy(value_ptr(right, 0), |
| 103 | value_ptr(left, nr_left - shift), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 104 | shift * value_size); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Delete a specific entry from a leaf node. |
| 110 | */ |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 111 | static void delete_at(struct btree_node *n, unsigned index) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 112 | { |
| 113 | unsigned nr_entries = le32_to_cpu(n->header.nr_entries); |
| 114 | unsigned nr_to_copy = nr_entries - (index + 1); |
| 115 | uint32_t value_size = le32_to_cpu(n->header.value_size); |
| 116 | BUG_ON(index >= nr_entries); |
| 117 | |
| 118 | if (nr_to_copy) { |
| 119 | memmove(key_ptr(n, index), |
| 120 | key_ptr(n, index + 1), |
| 121 | nr_to_copy * sizeof(__le64)); |
| 122 | |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 123 | memmove(value_ptr(n, index), |
| 124 | value_ptr(n, index + 1), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 125 | nr_to_copy * value_size); |
| 126 | } |
| 127 | |
| 128 | n->header.nr_entries = cpu_to_le32(nr_entries - 1); |
| 129 | } |
| 130 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 131 | static unsigned merge_threshold(struct btree_node *n) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 132 | { |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 133 | return le32_to_cpu(n->header.max_entries) / 3; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | struct child { |
| 137 | unsigned index; |
| 138 | struct dm_block *block; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 139 | struct btree_node *n; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 142 | static int init_child(struct dm_btree_info *info, struct dm_btree_value_type *vt, |
| 143 | struct btree_node *parent, |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 144 | unsigned index, struct child *result) |
| 145 | { |
| 146 | int r, inc; |
| 147 | dm_block_t root; |
| 148 | |
| 149 | result->index = index; |
| 150 | root = value64(parent, index); |
| 151 | |
| 152 | r = dm_tm_shadow_block(info->tm, root, &btree_node_validator, |
| 153 | &result->block, &inc); |
| 154 | if (r) |
| 155 | return r; |
| 156 | |
| 157 | result->n = dm_block_data(result->block); |
| 158 | |
| 159 | if (inc) |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 160 | inc_children(info->tm, result->n, vt); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 161 | |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 162 | *((__le64 *) value_ptr(parent, index)) = |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 163 | cpu_to_le64(dm_block_location(result->block)); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 168 | static void exit_child(struct dm_btree_info *info, struct child *c) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 169 | { |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 170 | dm_tm_unlock(info->tm, c->block); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 173 | static void shift(struct btree_node *left, struct btree_node *right, int count) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 174 | { |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 175 | uint32_t nr_left = le32_to_cpu(left->header.nr_entries); |
| 176 | uint32_t nr_right = le32_to_cpu(right->header.nr_entries); |
| 177 | uint32_t max_entries = le32_to_cpu(left->header.max_entries); |
| 178 | uint32_t r_max_entries = le32_to_cpu(right->header.max_entries); |
| 179 | |
| 180 | BUG_ON(max_entries != r_max_entries); |
| 181 | BUG_ON(nr_left - count > max_entries); |
| 182 | BUG_ON(nr_right + count > max_entries); |
| 183 | |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 184 | if (!count) |
| 185 | return; |
| 186 | |
| 187 | if (count > 0) { |
| 188 | node_shift(right, count); |
| 189 | node_copy(left, right, count); |
| 190 | } else { |
| 191 | node_copy(left, right, count); |
| 192 | node_shift(right, count); |
| 193 | } |
| 194 | |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 195 | left->header.nr_entries = cpu_to_le32(nr_left - count); |
| 196 | right->header.nr_entries = cpu_to_le32(nr_right + count); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 199 | static void __rebalance2(struct dm_btree_info *info, struct btree_node *parent, |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 200 | struct child *l, struct child *r) |
| 201 | { |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 202 | struct btree_node *left = l->n; |
| 203 | struct btree_node *right = r->n; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 204 | uint32_t nr_left = le32_to_cpu(left->header.nr_entries); |
| 205 | uint32_t nr_right = le32_to_cpu(right->header.nr_entries); |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 206 | unsigned threshold = 2 * merge_threshold(left) + 1; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 207 | |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 208 | if (nr_left + nr_right < threshold) { |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 209 | /* |
| 210 | * Merge |
| 211 | */ |
| 212 | node_copy(left, right, -nr_right); |
| 213 | left->header.nr_entries = cpu_to_le32(nr_left + nr_right); |
| 214 | delete_at(parent, r->index); |
| 215 | |
| 216 | /* |
| 217 | * We need to decrement the right block, but not it's |
| 218 | * children, since they're still referenced by left. |
| 219 | */ |
| 220 | dm_tm_dec(info->tm, dm_block_location(r->block)); |
| 221 | } else { |
| 222 | /* |
| 223 | * Rebalance. |
| 224 | */ |
| 225 | unsigned target_left = (nr_left + nr_right) / 2; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 226 | shift(left, right, nr_left - target_left); |
| 227 | *key_ptr(parent, r->index) = right->keys[0]; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info, |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 232 | struct dm_btree_value_type *vt, unsigned left_index) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 233 | { |
| 234 | int r; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 235 | struct btree_node *parent; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 236 | struct child left, right; |
| 237 | |
| 238 | parent = dm_block_data(shadow_current(s)); |
| 239 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 240 | r = init_child(info, vt, parent, left_index, &left); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 241 | if (r) |
| 242 | return r; |
| 243 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 244 | r = init_child(info, vt, parent, left_index + 1, &right); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 245 | if (r) { |
| 246 | exit_child(info, &left); |
| 247 | return r; |
| 248 | } |
| 249 | |
| 250 | __rebalance2(info, parent, &left, &right); |
| 251 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 252 | exit_child(info, &left); |
| 253 | exit_child(info, &right); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 254 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 255 | return 0; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 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 | */ |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 263 | static void delete_center_node(struct dm_btree_info *info, struct btree_node *parent, |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 264 | struct child *l, struct child *c, struct child *r, |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 265 | struct btree_node *left, struct btree_node *center, struct btree_node *right, |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 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 shift = min(max_entries - nr_left, nr_center); |
| 270 | |
| 271 | BUG_ON(nr_left + shift > max_entries); |
| 272 | node_copy(left, center, -shift); |
| 273 | left->header.nr_entries = cpu_to_le32(nr_left + shift); |
| 274 | |
| 275 | if (shift != nr_center) { |
| 276 | shift = nr_center - shift; |
| 277 | BUG_ON((nr_right + shift) > max_entries); |
| 278 | node_shift(right, shift); |
| 279 | node_copy(center, right, shift); |
| 280 | right->header.nr_entries = cpu_to_le32(nr_right + shift); |
| 281 | } |
| 282 | *key_ptr(parent, r->index) = right->keys[0]; |
| 283 | |
| 284 | delete_at(parent, c->index); |
| 285 | r->index--; |
| 286 | |
| 287 | dm_tm_dec(info->tm, dm_block_location(c->block)); |
| 288 | __rebalance2(info, parent, l, r); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Redistributes entries among 3 sibling nodes. |
| 293 | */ |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 294 | static void redistribute3(struct dm_btree_info *info, struct btree_node *parent, |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 295 | struct child *l, struct child *c, struct child *r, |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 296 | struct btree_node *left, struct btree_node *center, struct btree_node *right, |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 297 | uint32_t nr_left, uint32_t nr_center, uint32_t nr_right) |
| 298 | { |
| 299 | int s; |
| 300 | uint32_t max_entries = le32_to_cpu(left->header.max_entries); |
Joe Thornber | 2871c69 | 2015-10-21 18:36:49 +0100 | [diff] [blame] | 301 | unsigned total = nr_left + nr_center + nr_right; |
| 302 | unsigned target_right = total / 3; |
| 303 | unsigned remainder = (target_right * 3) != total; |
| 304 | unsigned target_left = target_right + remainder; |
| 305 | |
| 306 | BUG_ON(target_left > max_entries); |
| 307 | BUG_ON(target_right > max_entries); |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 308 | |
| 309 | if (nr_left < nr_right) { |
Joe Thornber | 2871c69 | 2015-10-21 18:36:49 +0100 | [diff] [blame] | 310 | s = nr_left - target_left; |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 311 | |
| 312 | if (s < 0 && nr_center < -s) { |
| 313 | /* not enough in central node */ |
Dennis Yang | 4c7e309 | 2015-06-26 15:25:48 +0100 | [diff] [blame] | 314 | shift(left, center, -nr_center); |
| 315 | s += nr_center; |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 316 | shift(left, right, s); |
| 317 | nr_right += s; |
| 318 | } else |
| 319 | shift(left, center, s); |
| 320 | |
Joe Thornber | 2871c69 | 2015-10-21 18:36:49 +0100 | [diff] [blame] | 321 | shift(center, right, target_right - nr_right); |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 322 | |
| 323 | } else { |
Joe Thornber | 2871c69 | 2015-10-21 18:36:49 +0100 | [diff] [blame] | 324 | s = target_right - nr_right; |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 325 | if (s > 0 && nr_center < s) { |
| 326 | /* not enough in central node */ |
| 327 | shift(center, right, nr_center); |
Dennis Yang | 4c7e309 | 2015-06-26 15:25:48 +0100 | [diff] [blame] | 328 | s -= nr_center; |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 329 | shift(left, right, s); |
| 330 | nr_left -= s; |
| 331 | } else |
| 332 | shift(center, right, s); |
| 333 | |
Joe Thornber | 2871c69 | 2015-10-21 18:36:49 +0100 | [diff] [blame] | 334 | shift(left, center, nr_left - target_left); |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | *key_ptr(parent, c->index) = center->keys[0]; |
| 338 | *key_ptr(parent, r->index) = right->keys[0]; |
| 339 | } |
| 340 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 341 | static void __rebalance3(struct dm_btree_info *info, struct btree_node *parent, |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 342 | struct child *l, struct child *c, struct child *r) |
| 343 | { |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 344 | struct btree_node *left = l->n; |
| 345 | struct btree_node *center = c->n; |
| 346 | struct btree_node *right = r->n; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 347 | |
| 348 | uint32_t nr_left = le32_to_cpu(left->header.nr_entries); |
| 349 | uint32_t nr_center = le32_to_cpu(center->header.nr_entries); |
| 350 | uint32_t nr_right = le32_to_cpu(right->header.nr_entries); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 351 | |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 352 | unsigned threshold = merge_threshold(left) * 4 + 1; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 353 | |
| 354 | BUG_ON(left->header.max_entries != center->header.max_entries); |
| 355 | BUG_ON(center->header.max_entries != right->header.max_entries); |
| 356 | |
Joe Thornber | b098890 | 2012-03-28 18:41:23 +0100 | [diff] [blame] | 357 | if ((nr_left + nr_center + nr_right) < threshold) |
| 358 | delete_center_node(info, parent, l, c, r, left, center, right, |
| 359 | nr_left, nr_center, nr_right); |
| 360 | else |
| 361 | redistribute3(info, parent, l, c, r, left, center, right, |
| 362 | nr_left, nr_center, nr_right); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info, |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 366 | struct dm_btree_value_type *vt, unsigned left_index) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 367 | { |
| 368 | int r; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 369 | struct btree_node *parent = dm_block_data(shadow_current(s)); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 370 | struct child left, center, right; |
| 371 | |
| 372 | /* |
| 373 | * FIXME: fill out an array? |
| 374 | */ |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 375 | r = init_child(info, vt, parent, left_index, &left); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 376 | if (r) |
| 377 | return r; |
| 378 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 379 | r = init_child(info, vt, parent, left_index + 1, ¢er); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 380 | if (r) { |
| 381 | exit_child(info, &left); |
| 382 | return r; |
| 383 | } |
| 384 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 385 | r = init_child(info, vt, parent, left_index + 2, &right); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 386 | if (r) { |
| 387 | exit_child(info, &left); |
| 388 | exit_child(info, ¢er); |
| 389 | return r; |
| 390 | } |
| 391 | |
| 392 | __rebalance3(info, parent, &left, ¢er, &right); |
| 393 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 394 | exit_child(info, &left); |
| 395 | exit_child(info, ¢er); |
| 396 | exit_child(info, &right); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 401 | static int rebalance_children(struct shadow_spine *s, |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 402 | struct dm_btree_info *info, |
| 403 | struct dm_btree_value_type *vt, uint64_t key) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 404 | { |
| 405 | int i, r, has_left_sibling, has_right_sibling; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 406 | struct btree_node *n; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 407 | |
| 408 | n = dm_block_data(shadow_current(s)); |
| 409 | |
| 410 | if (le32_to_cpu(n->header.nr_entries) == 1) { |
| 411 | struct dm_block *child; |
| 412 | dm_block_t b = value64(n, 0); |
| 413 | |
| 414 | r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child); |
| 415 | if (r) |
| 416 | return r; |
| 417 | |
| 418 | memcpy(n, dm_block_data(child), |
| 419 | dm_bm_block_size(dm_tm_get_bm(info->tm))); |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 420 | dm_tm_unlock(info->tm, child); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 421 | |
| 422 | dm_tm_dec(info->tm, dm_block_location(child)); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | i = lower_bound(n, key); |
| 427 | if (i < 0) |
| 428 | return -ENODATA; |
| 429 | |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 430 | has_left_sibling = i > 0; |
| 431 | has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1); |
| 432 | |
| 433 | if (!has_left_sibling) |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 434 | r = rebalance2(s, info, vt, i); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 435 | |
| 436 | else if (!has_right_sibling) |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 437 | r = rebalance2(s, info, vt, i - 1); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 438 | |
| 439 | else |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 440 | r = rebalance3(s, info, vt, i - 1); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 441 | |
| 442 | return r; |
| 443 | } |
| 444 | |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 445 | static int do_leaf(struct btree_node *n, uint64_t key, unsigned *index) |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 446 | { |
| 447 | int i = lower_bound(n, key); |
| 448 | |
| 449 | if ((i < 0) || |
| 450 | (i >= le32_to_cpu(n->header.nr_entries)) || |
| 451 | (le64_to_cpu(n->keys[i]) != key)) |
| 452 | return -ENODATA; |
| 453 | |
| 454 | *index = i; |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Prepares for removal from one level of the hierarchy. The caller must |
| 461 | * call delete_at() to remove the entry at index. |
| 462 | */ |
| 463 | static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info, |
| 464 | struct dm_btree_value_type *vt, dm_block_t root, |
| 465 | uint64_t key, unsigned *index) |
| 466 | { |
| 467 | int i = *index, r; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 468 | struct btree_node *n; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 469 | |
| 470 | for (;;) { |
| 471 | r = shadow_step(s, root, vt); |
| 472 | if (r < 0) |
| 473 | break; |
| 474 | |
| 475 | /* |
| 476 | * We have to patch up the parent node, ugly, but I don't |
| 477 | * see a way to do this automatically as part of the spine |
| 478 | * op. |
| 479 | */ |
| 480 | if (shadow_has_parent(s)) { |
| 481 | __le64 location = cpu_to_le64(dm_block_location(shadow_current(s))); |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 482 | memcpy(value_ptr(dm_block_data(shadow_parent(s)), i), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 483 | &location, sizeof(__le64)); |
| 484 | } |
| 485 | |
| 486 | n = dm_block_data(shadow_current(s)); |
| 487 | |
| 488 | if (le32_to_cpu(n->header.flags) & LEAF_NODE) |
| 489 | return do_leaf(n, key, index); |
| 490 | |
Joe Thornber | f046f89 | 2013-03-20 17:21:24 +0000 | [diff] [blame] | 491 | r = rebalance_children(s, info, vt, key); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 492 | if (r) |
| 493 | break; |
| 494 | |
| 495 | n = dm_block_data(shadow_current(s)); |
| 496 | if (le32_to_cpu(n->header.flags) & LEAF_NODE) |
| 497 | return do_leaf(n, key, index); |
| 498 | |
| 499 | i = lower_bound(n, key); |
| 500 | |
| 501 | /* |
| 502 | * We know the key is present, or else |
| 503 | * rebalance_children would have returned |
| 504 | * -ENODATA |
| 505 | */ |
| 506 | root = value64(n, i); |
| 507 | } |
| 508 | |
| 509 | return r; |
| 510 | } |
| 511 | |
| 512 | int dm_btree_remove(struct dm_btree_info *info, dm_block_t root, |
| 513 | uint64_t *keys, dm_block_t *new_root) |
| 514 | { |
| 515 | unsigned level, last_level = info->levels - 1; |
| 516 | int index = 0, r = 0; |
| 517 | struct shadow_spine spine; |
Mikulas Patocka | 550929f | 2012-12-21 20:23:30 +0000 | [diff] [blame] | 518 | struct btree_node *n; |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 519 | struct dm_btree_value_type le64_vt; |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 520 | |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 521 | init_le64_type(info->tm, &le64_vt); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 522 | init_shadow_spine(&spine, info); |
| 523 | for (level = 0; level < info->levels; level++) { |
| 524 | r = remove_raw(&spine, info, |
| 525 | (level == last_level ? |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 526 | &info->value_type : &le64_vt), |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 527 | root, keys[level], (unsigned *)&index); |
| 528 | if (r < 0) |
| 529 | break; |
| 530 | |
| 531 | n = dm_block_data(shadow_current(&spine)); |
| 532 | if (level != last_level) { |
| 533 | root = value64(n, index); |
| 534 | continue; |
| 535 | } |
| 536 | |
| 537 | BUG_ON(index < 0 || index >= le32_to_cpu(n->header.nr_entries)); |
| 538 | |
| 539 | if (info->value_type.dec) |
| 540 | info->value_type.dec(info->value_type.context, |
Joe Thornber | a3aefb3 | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 541 | value_ptr(n, index)); |
Joe Thornber | 3241b1d | 2011-10-31 20:19:11 +0000 | [diff] [blame] | 542 | |
| 543 | delete_at(n, index); |
| 544 | } |
| 545 | |
| 546 | *new_root = shadow_root(&spine); |
| 547 | exit_shadow_spine(&spine); |
| 548 | |
| 549 | return r; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(dm_btree_remove); |
Joe Thornber | 4ec331c | 2015-04-13 09:41:44 +0100 | [diff] [blame] | 552 | |
| 553 | /*----------------------------------------------------------------*/ |
| 554 | |
| 555 | static int remove_nearest(struct shadow_spine *s, struct dm_btree_info *info, |
| 556 | struct dm_btree_value_type *vt, dm_block_t root, |
| 557 | uint64_t key, int *index) |
| 558 | { |
| 559 | int i = *index, r; |
| 560 | struct btree_node *n; |
| 561 | |
| 562 | for (;;) { |
| 563 | r = shadow_step(s, root, vt); |
| 564 | if (r < 0) |
| 565 | break; |
| 566 | |
| 567 | /* |
| 568 | * We have to patch up the parent node, ugly, but I don't |
| 569 | * see a way to do this automatically as part of the spine |
| 570 | * op. |
| 571 | */ |
| 572 | if (shadow_has_parent(s)) { |
| 573 | __le64 location = cpu_to_le64(dm_block_location(shadow_current(s))); |
| 574 | memcpy(value_ptr(dm_block_data(shadow_parent(s)), i), |
| 575 | &location, sizeof(__le64)); |
| 576 | } |
| 577 | |
| 578 | n = dm_block_data(shadow_current(s)); |
| 579 | |
| 580 | if (le32_to_cpu(n->header.flags) & LEAF_NODE) { |
| 581 | *index = lower_bound(n, key); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | r = rebalance_children(s, info, vt, key); |
| 586 | if (r) |
| 587 | break; |
| 588 | |
| 589 | n = dm_block_data(shadow_current(s)); |
| 590 | if (le32_to_cpu(n->header.flags) & LEAF_NODE) { |
| 591 | *index = lower_bound(n, key); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | i = lower_bound(n, key); |
| 596 | |
| 597 | /* |
| 598 | * We know the key is present, or else |
| 599 | * rebalance_children would have returned |
| 600 | * -ENODATA |
| 601 | */ |
| 602 | root = value64(n, i); |
| 603 | } |
| 604 | |
| 605 | return r; |
| 606 | } |
| 607 | |
| 608 | static int remove_one(struct dm_btree_info *info, dm_block_t root, |
| 609 | uint64_t *keys, uint64_t end_key, |
| 610 | dm_block_t *new_root, unsigned *nr_removed) |
| 611 | { |
| 612 | unsigned level, last_level = info->levels - 1; |
| 613 | int index = 0, r = 0; |
| 614 | struct shadow_spine spine; |
| 615 | struct btree_node *n; |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 616 | struct dm_btree_value_type le64_vt; |
Joe Thornber | 4ec331c | 2015-04-13 09:41:44 +0100 | [diff] [blame] | 617 | uint64_t k; |
| 618 | |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 619 | init_le64_type(info->tm, &le64_vt); |
Joe Thornber | 4ec331c | 2015-04-13 09:41:44 +0100 | [diff] [blame] | 620 | init_shadow_spine(&spine, info); |
| 621 | for (level = 0; level < last_level; level++) { |
Joe Thornber | b0dc3c8 | 2015-08-12 15:12:09 +0100 | [diff] [blame] | 622 | r = remove_raw(&spine, info, &le64_vt, |
Joe Thornber | 4ec331c | 2015-04-13 09:41:44 +0100 | [diff] [blame] | 623 | root, keys[level], (unsigned *) &index); |
| 624 | if (r < 0) |
| 625 | goto out; |
| 626 | |
| 627 | n = dm_block_data(shadow_current(&spine)); |
| 628 | root = value64(n, index); |
| 629 | } |
| 630 | |
| 631 | r = remove_nearest(&spine, info, &info->value_type, |
| 632 | root, keys[last_level], &index); |
| 633 | if (r < 0) |
| 634 | goto out; |
| 635 | |
| 636 | n = dm_block_data(shadow_current(&spine)); |
| 637 | |
| 638 | if (index < 0) |
| 639 | index = 0; |
| 640 | |
| 641 | if (index >= le32_to_cpu(n->header.nr_entries)) { |
| 642 | r = -ENODATA; |
| 643 | goto out; |
| 644 | } |
| 645 | |
| 646 | k = le64_to_cpu(n->keys[index]); |
| 647 | if (k >= keys[last_level] && k < end_key) { |
| 648 | if (info->value_type.dec) |
| 649 | info->value_type.dec(info->value_type.context, |
| 650 | value_ptr(n, index)); |
| 651 | |
| 652 | delete_at(n, index); |
Joe Thornber | aa0cd28 | 2015-08-07 16:33:01 +0100 | [diff] [blame] | 653 | keys[last_level] = k + 1ull; |
Joe Thornber | 4ec331c | 2015-04-13 09:41:44 +0100 | [diff] [blame] | 654 | |
| 655 | } else |
| 656 | r = -ENODATA; |
| 657 | |
| 658 | out: |
| 659 | *new_root = shadow_root(&spine); |
| 660 | exit_shadow_spine(&spine); |
| 661 | |
| 662 | return r; |
| 663 | } |
| 664 | |
| 665 | int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root, |
| 666 | uint64_t *first_key, uint64_t end_key, |
| 667 | dm_block_t *new_root, unsigned *nr_removed) |
| 668 | { |
| 669 | int r; |
| 670 | |
| 671 | *nr_removed = 0; |
| 672 | do { |
| 673 | r = remove_one(info, root, first_key, end_key, &root, nr_removed); |
| 674 | if (!r) |
| 675 | (*nr_removed)++; |
| 676 | } while (!r); |
| 677 | |
| 678 | *new_root = root; |
| 679 | return r == -ENODATA ? 0 : r; |
| 680 | } |
| 681 | EXPORT_SYMBOL_GPL(dm_btree_remove_leaves); |