11 #include <linux/export.h>
56 static void node_shift(
struct node *
n,
int shift)
63 BUG_ON(shift > nr_entries);
64 BUG_ON((
void *) key_ptr(n, shift) >= value_ptr(n, shift));
67 (nr_entries - shift) *
sizeof(
__le64));
70 (nr_entries - shift) * value_size);
75 nr_entries *
sizeof(
__le64));
78 nr_entries * value_size);
91 memcpy(key_ptr(left, nr_left),
94 memcpy(value_ptr(left, nr_left),
100 key_ptr(left, nr_left - shift),
102 memcpy(value_ptr(right, 0),
103 value_ptr(left, nr_left - shift),
111 static void delete_at(
struct node *n,
unsigned index)
114 unsigned nr_to_copy = nr_entries - (index + 1);
116 BUG_ON(index >= nr_entries);
120 key_ptr(n, index + 1),
121 nr_to_copy *
sizeof(
__le64));
124 value_ptr(n, index + 1),
125 nr_to_copy * value_size);
131 static unsigned merge_threshold(
struct node *n)
160 &result->
block, &inc);
169 *((
__le64 *) value_ptr(parent, index)) =
180 static void shift(
struct node *left,
struct node *right,
int count)
187 BUG_ON(max_entries != r_max_entries);
188 BUG_ON(nr_left - count > max_entries);
189 BUG_ON(nr_right + count > max_entries);
195 node_shift(right, count);
196 node_copy(left, right, count);
198 node_copy(left, right, count);
199 node_shift(right, count);
209 struct node *left = l->
n;
210 struct node *right = r->
n;
213 unsigned threshold = 2 * merge_threshold(left) + 1;
215 if (nr_left + nr_right < threshold) {
219 node_copy(left, right, -nr_right);
221 delete_at(parent, r->
index);
232 unsigned target_left = (nr_left + nr_right) / 2;
233 shift(left, right, nr_left - target_left);
234 *key_ptr(parent, r->
index) = right->
keys[0];
243 struct child left, right;
247 r = init_child(info, parent, left_index, &left);
251 r = init_child(info, parent, left_index + 1, &right);
253 exit_child(info, &left);
257 __rebalance2(info, parent, &left, &right);
259 r = exit_child(info, &left);
261 exit_child(info, &right);
265 return exit_child(info, &right);
275 struct node *left,
struct node *center,
struct node *right,
279 unsigned shift =
min(max_entries - nr_left, nr_center);
281 BUG_ON(nr_left + shift > max_entries);
282 node_copy(left, center, -shift);
285 if (shift != nr_center) {
286 shift = nr_center - shift;
287 BUG_ON((nr_right + shift) > max_entries);
288 node_shift(right, shift);
289 node_copy(center, right, shift);
292 *key_ptr(parent, r->
index) = right->
keys[0];
294 delete_at(parent, c->
index);
298 __rebalance2(info, parent, l, r);
306 struct node *left,
struct node *center,
struct node *right,
311 unsigned target = (nr_left + nr_center + nr_right) / 3;
312 BUG_ON(target > max_entries);
314 if (nr_left < nr_right) {
317 if (s < 0 && nr_center < -s) {
319 shift(left, center, nr_center);
321 shift(left, right, s);
324 shift(left, center, s);
326 shift(center, right, target - nr_right);
329 s = target - nr_right;
330 if (s > 0 && nr_center < s) {
332 shift(center, right, nr_center);
333 s = target - nr_center;
334 shift(left, right, s);
337 shift(center, right, s);
339 shift(left, center, nr_left - target);
342 *key_ptr(parent, c->
index) = center->
keys[0];
343 *key_ptr(parent, r->
index) = right->
keys[0];
349 struct node *left = l->
n;
350 struct node *center = c->
n;
351 struct node *right = r->
n;
357 unsigned threshold = merge_threshold(left) * 4 + 1;
362 if ((nr_left + nr_center + nr_right) < threshold)
363 delete_center_node(info, parent, l, c, r, left, center, right,
364 nr_left, nr_center, nr_right);
366 redistribute3(info, parent, l, c, r, left, center, right,
367 nr_left, nr_center, nr_right);
375 struct child left, center, right;
380 r = init_child(info, parent, left_index, &left);
384 r = init_child(info, parent, left_index + 1, ¢er);
386 exit_child(info, &left);
390 r = init_child(info, parent, left_index + 2, &right);
392 exit_child(info, &left);
393 exit_child(info, ¢er);
397 __rebalance3(info, parent, &left, ¢er, &right);
399 r = exit_child(info, &left);
401 exit_child(info, ¢er);
402 exit_child(info, &right);
406 r = exit_child(info, ¢er);
408 exit_child(info, &right);
412 r = exit_child(info, &right);
423 struct dm_block *
block;
439 int i,
r, has_left_sibling, has_right_sibling;
446 struct dm_block *
child;
467 r = get_nr_entries(info->
tm,
value64(n, i), &child_entries);
471 has_left_sibling = i > 0;
474 if (!has_left_sibling)
475 r = rebalance2(s, info, i);
477 else if (!has_right_sibling)
478 r = rebalance2(s, info, i - 1);
481 r = rebalance3(s, info, i - 1);
486 static int do_leaf(
struct node *n,
uint64_t key,
unsigned *index)
524 &location,
sizeof(
__le64));
530 return do_leaf(n, key, index);
532 r = rebalance_children(s, info, key);
538 return do_leaf(n, key, index);
557 int index = 0, r = 0;
562 for (level = 0; level < info->
levels; level++) {
563 r = remove_raw(&spine, info,
564 (level == last_level ?
566 root, keys[level], (
unsigned *)&index);
571 if (level != last_level) {
580 value_ptr(n, index));