5 #include <asm/uaccess.h>
6 #include <linux/string.h>
7 #include <linux/time.h>
13 int,
int,
struct item_head *,
struct buffer_head **);
16 #define INTERNAL_SHIFT_FROM_S_TO_L 0
17 #define INTERNAL_SHIFT_FROM_R_TO_S 1
18 #define INTERNAL_SHIFT_FROM_L_TO_S 2
19 #define INTERNAL_SHIFT_FROM_S_TO_R 3
20 #define INTERNAL_INSERT_TO_S 4
21 #define INTERNAL_INSERT_TO_L 5
22 #define INTERNAL_INSERT_TO_R 6
24 static void internal_define_dest_src_infos(
int shift_mode,
29 int *d_key,
struct buffer_head **
cf)
109 "shift type is unknown (%d)",
118 static void internal_insert_childs(
struct buffer_info *cur_bi,
121 struct buffer_head **bh)
123 struct buffer_head *
cur = cur_bi->
bi_bh;
137 RFALSE(count > 2,
"too many children (%d) are to be inserted", count);
139 "no enough free space (%d), needed %d bytes",
148 for (i = 0; i <
count; i++) {
195 static void internal_delete_pointers_items(
struct buffer_info *cur_bi,
197 int first_i,
int del_num)
199 struct buffer_head *cur = cur_bi->
bi_bh;
207 "negative number of items (%d) can not be deleted", del_num);
210 "first pointer order (%d) < 0 or "
211 "no so many pointers (%d), only (%d) or "
212 "first key order %d < 0", first_p, first_p + del_num,
220 if (first_p == 0 && del_num == nr + 1) {
222 "1st deleted key must have order 0, not %d", first_i);
228 "first_i = %d del_num = %d "
229 "no so many keys (%d) in the node (%b)(%z)",
230 first_i, del_num, first_i + del_num, cur, cur);
235 memmove(dc, dc + del_num, (nr + 1 - first_p - del_num) *
DC_SIZE);
238 (nr - first_i - del_num) *
KEY_SIZE + (nr + 1 -
267 static void internal_delete_childs(
struct buffer_info *cur_bi,
int from,
int n)
271 i_from = (from == 0) ? from : from - 1;
276 internal_delete_pointers_items(cur_bi, from, i_from, n);
283 static void internal_copy_pointers_items(
struct buffer_info *dest_bi,
284 struct buffer_head *
src,
285 int last_first,
int cpy_num)
289 struct buffer_head *
dest = dest_bi->
bi_bh;
291 int dest_order, src_order;
299 "src (%p) or dest (%p) buffer is 0", src, dest);
301 "invalid last_first parameter (%d)", last_first);
302 RFALSE(nr_src < cpy_num - 1,
303 "no so many items (%d) in src (%d)", cpy_num, nr_src);
304 RFALSE(cpy_num < 0,
"cpy_num less than 0 (%d)", cpy_num);
306 "cpy_num (%d) + item number in dest (%d) can not be > MAX_NR_KEY(%d)",
319 nr_src - cpy_num + 1) : (dest_order =
334 memmove(key + cpy_num - 1, key,
374 static void internal_move_pointers_items(
struct buffer_info *dest_bi,
376 int last_first,
int cpy_num,
382 internal_copy_pointers_items(dest_bi, src_bi->
bi_bh, last_first,
390 internal_delete_pointers_items(src_bi, first_pointer,
391 first_item, cpy_num - del_par);
395 i = (cpy_num - del_par ==
400 internal_delete_pointers_items(src_bi,
401 j + 1 - cpy_num + del_par, i,
407 static void internal_insert_key(
struct buffer_info *dest_bi,
int dest_position_before,
408 struct buffer_head *src,
int src_position)
410 struct buffer_head *dest = dest_bi->
bi_bh;
416 "source(%p) or dest(%p) buffer is 0", src, dest);
417 RFALSE(dest_position_before < 0 || src_position < 0,
418 "source(%d) or dest(%d) key number less than 0",
419 src_position, dest_position_before);
422 "invalid position in dest (%d (key number %d)) or in src (%d (key number %d))",
426 "no enough free space (%d) in dest buffer",
B_FREE_SPACE(dest));
462 static void internal_shift_left(
int mode,
464 int h,
int pointer_amount)
467 struct buffer_head *
cf;
470 internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
471 &d_key_position, &cf);
475 if (pointer_amount) {
490 internal_move_pointers_items(&dest_bi, &src_bi,
FIRST_TO_LAST,
500 static void internal_shift1_left(
struct tree_balance *tb,
501 int h,
int pointer_amount)
504 struct buffer_head *
cf;
508 &dest_bi, &src_bi, &d_key_position, &cf);
510 if (pointer_amount > 0)
516 internal_move_pointers_items(&dest_bi, &src_bi,
FIRST_TO_LAST,
526 static void internal_shift_right(
int mode,
528 int h,
int pointer_amount)
531 struct buffer_head *
cf;
535 internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
536 &d_key_position, &cf);
540 if (pointer_amount > 0) {
542 internal_insert_key(&dest_bi, 0, cf, d_key_position);
543 if (nr == pointer_amount - 1) {
545 dest_bi.
bi_bh != tb->
R[h],
546 "src (%p) must be == tb->S[h](%p) when it disappears",
554 nr - pointer_amount);
558 internal_move_pointers_items(&dest_bi, &src_bi,
LAST_TO_FIRST,
567 static void internal_shift1_right(
struct tree_balance *tb,
568 int h,
int pointer_amount)
571 struct buffer_head *
cf;
575 &dest_bi, &src_bi, &d_key_position, &cf);
577 if (pointer_amount > 0)
578 internal_insert_key(&dest_bi, 0, cf, d_key_position);
582 internal_move_pointers_items(&dest_bi, &src_bi,
LAST_TO_FIRST,
589 static void balance_internal_when_delete(
struct tree_balance *tb,
590 int h,
int child_pos)
605 internal_delete_childs(&
bi, child_pos, -insert_num);
608 "tb->blknum[%d]=%d when insert_size < 0", h, tb->
blknum[h]);
612 if (tb->
lnum[h] == 0 && tb->
rnum[h] == 0) {
615 struct buffer_head *new_root;
620 "buffer must have only 0 keys (%d)", n);
621 RFALSE(
bi.bi_parent,
"root has parent (%p)",
626 new_root = tb->
R[h - 1];
628 new_root = tb->
L[h - 1];
636 REISERFS_SB(tb->
tb_sb)->s_sbh,
654 "invalid tb->rnum[%d]==%d when joining S[h] with L[h]",
665 "invalid tb->lnum[%d]==%d when joining S[h] with R[h]",
674 if (tb->
lnum[h] < 0) {
676 "wrong tb->rnum[%d]==%d when borrow from L[h]", h,
684 if (tb->
rnum[h] < 0) {
686 "invalid tb->lnum[%d]==%d when borrow from R[h]",
692 if (tb->
lnum[h] > 0) {
694 "invalid tb->lnum[%d]==%d or tb->rnum[%d]==%d when S[h](item number == %d) is split between them",
706 "unexpected tb->lnum[%d]==%d or tb->rnum[%d]==%d",
714 "L[h](%p) and CFL[h](%p) must exist in replace_lkey",
715 tb->
L[h], tb->
CFL[h]);
729 "R[h](%p) and CFR[h](%p) must exist in replace_rkey",
730 tb->
R[h], tb->
CFR[h]);
732 "R[h] can not be empty if it exists (item number=%d)",
742 int child_pos,
struct item_head *insert_key,
743 struct buffer_head **insert_ptr
763 int insert_num,
n,
k;
764 struct buffer_head *S_new;
766 struct buffer_head *new_insert_ptr =
NULL;
767 struct item_head *new_insert_key_addr = insert_key;
769 RFALSE(h < 1,
"h (%d) can not be < 1 on internal level", h);
782 RFALSE(insert_num < -2 || insert_num > 2,
783 "incorrect number of items inserted to the internal node (%d)",
785 RFALSE(h > 1 && (insert_num > 1 || insert_num < -1),
786 "incorrect number of items (%d) inserted to the internal node on a level (h=%d) higher than last internal level",
790 if (insert_num < 0) {
791 balance_internal_when_delete(tb, h, child_pos);
796 if (tb->
lnum[h] > 0) {
801 if (tb->
lnum[h] <= child_pos) {
806 child_pos -= tb->
lnum[
h];
807 }
else if (tb->
lnum[h] > child_pos + insert_num) {
810 tb->
lnum[h] - insert_num);
819 internal_insert_childs(&bi,
822 insert_num, insert_key,
830 internal_shift1_left(tb, h, child_pos + 1);
832 k = tb->
lnum[
h] - child_pos - 1;
837 internal_insert_childs(&bi,
839 n + child_pos + 1, k,
840 insert_key, insert_ptr);
842 replace_lkey(tb, h, insert_key + k);
861 if (tb->
rnum[h] > 0) {
865 if (n - tb->
rnum[h] >= child_pos)
870 else if (n + insert_num - tb->
rnum[h] < child_pos) {
875 tb->
rnum[h] - insert_num);
882 internal_insert_childs(&bi,
884 child_pos - n - insert_num +
886 insert_num, insert_key,
893 internal_shift1_right(tb, h, n - child_pos + 1);
895 k = tb->
rnum[
h] - n + child_pos - 1;
900 internal_insert_childs(&bi,
902 0, k, insert_key + 1,
905 replace_rkey(tb, h, insert_key + insert_num - k - 1);
911 [insert_num - k - 1]) -
913 [insert_num - k - 1]));
915 insert_ptr[insert_num - k -
920 insert_num -= (k + 1);
925 RFALSE(tb->
blknum[h] > 2,
"blknum can not be > 2 for internal level");
929 RFALSE(!tbSh,
"S[h] is equal NULL");
944 "required for creating the new root");
986 dest_bi.
bi_bh = S_new;
995 snum = (insert_num + n + 1) / 2;
996 if (n - snum >= child_pos) {
1003 internal_move_pointers_items(&dest_bi, &src_bi,
1006 }
else if (n + insert_num - snum < child_pos) {
1014 internal_move_pointers_items(&dest_bi, &src_bi,
1016 snum - insert_num, 0);
1020 internal_insert_childs(&dest_bi,
1022 child_pos - n - insert_num +
1024 insert_num, insert_key,
1033 internal_move_pointers_items(&dest_bi, &src_bi,
1035 n - child_pos + 1, 1);
1038 k = snum - n + child_pos - 1;
1040 internal_insert_childs(&dest_bi, 0, k,
1041 insert_key + 1, insert_ptr + 1);
1044 memcpy(&new_insert_key, insert_key + insert_num - k - 1,
1051 (insert_ptr[insert_num - k - 1]) -
1053 [insert_num - k - 1])));
1055 insert_ptr[insert_num - k -
1060 insert_num -= (k + 1);
1063 new_insert_ptr = S_new;
1065 RFALSE(!buffer_journaled(S_new) || buffer_journal_dirty(S_new)
1066 || buffer_dirty(S_new),
"cm-00001: bad S_new (%b)",
1074 if (0 <= child_pos && child_pos <= n && insert_num > 0) {
1079 internal_insert_childs(&bi,
1081 child_pos, insert_num, insert_key,
1086 insert_ptr[0] = new_insert_ptr;