Lines Matching refs:tb

19 static inline void buffer_info_init_left(struct tree_balance *tb,
22 bi->tb = tb;
23 bi->bi_bh = tb->L[0];
24 bi->bi_parent = tb->FL[0];
25 bi->bi_position = get_left_neighbor_position(tb, 0);
28 static inline void buffer_info_init_right(struct tree_balance *tb,
31 bi->tb = tb;
32 bi->bi_bh = tb->R[0];
33 bi->bi_parent = tb->FR[0];
34 bi->bi_position = get_right_neighbor_position(tb, 0);
37 static inline void buffer_info_init_tbS0(struct tree_balance *tb,
40 bi->tb = tb;
41 bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
42 bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
43 bi->bi_position = PATH_H_POSITION(tb->tb_path, 1);
46 static inline void buffer_info_init_bh(struct tree_balance *tb,
50 bi->tb = tb;
56 inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
59 journal_mark_dirty(tb->transaction_handle, bh);
67 * if deleting something ( tb->insert_size[0] < 0 )
77 static void balance_leaf_when_delete_del(struct tree_balance *tb)
79 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
80 int item_pos = PATH_LAST_POSITION(tb->tb_path);
86 RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
88 -tb->insert_size[0], ih);
90 buffer_info_init_tbS0(tb, &bi);
93 if (!item_pos && tb->CFL[0]) {
95 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
97 if (!PATH_H_POSITION(tb->tb_path, 1))
98 replace_key(tb, tb->CFL[0], tb->lkey[0],
99 PATH_H_PPARENT(tb->tb_path, 0), 0);
103 RFALSE(!item_pos && !tb->CFL[0],
104 "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
105 tb->L[0]);
109 static void balance_leaf_when_delete_cut(struct tree_balance *tb)
111 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
112 int item_pos = PATH_LAST_POSITION(tb->tb_path);
114 int pos_in_item = tb->tb_path->pos_in_item;
116 buffer_info_init_tbS0(tb, &bi);
123 * when we cut a directory tb->insert_size[0] means
126 tb->insert_size[0] = -1;
128 -tb->insert_size[0]);
130 RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
132 tb->CFL[0]);
134 if (!item_pos && !pos_in_item && tb->CFL[0])
135 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
138 -tb->insert_size[0]);
146 static int balance_leaf_when_delete_left(struct tree_balance *tb)
148 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
152 if (tb->lnum[0] == -1) {
154 if (tb->rnum[0] == -1) {
155 if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
160 if (PATH_H_POSITION(tb->tb_path, 1) == 0 &&
161 1 < B_NR_ITEMS(tb->FR[0]))
162 replace_key(tb, tb->CFL[0],
163 tb->lkey[0], tb->FR[0], 1);
165 leaf_move_items(LEAF_FROM_S_TO_L, tb, n, -1,
167 leaf_move_items(LEAF_FROM_R_TO_L, tb,
168 B_NR_ITEMS(tb->R[0]), -1,
171 reiserfs_invalidate_buffer(tb, tbS0);
172 reiserfs_invalidate_buffer(tb, tb->R[0]);
178 leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1, NULL);
179 leaf_move_items(LEAF_FROM_L_TO_R, tb,
180 B_NR_ITEMS(tb->L[0]), -1, NULL);
183 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
185 reiserfs_invalidate_buffer(tb, tbS0);
186 reiserfs_invalidate_buffer(tb, tb->L[0]);
191 RFALSE(tb->rnum[0] != 0,
192 "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
194 leaf_shift_left(tb, n, -1);
196 reiserfs_invalidate_buffer(tb, tbS0);
206 RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
207 (tb->lnum[0] + tb->rnum[0] > n + 1),
210 tb->rnum[0], tb->lnum[0], n);
211 RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
212 (tb->lbytes != -1 || tb->rbytes != -1),
215 tb->rbytes, tb->lbytes);
216 RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
217 (tb->lbytes < 1 || tb->rbytes != -1),
220 tb->rbytes, tb->lbytes);
222 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
223 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
225 reiserfs_invalidate_buffer(tb, tbS0);
239 static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
241 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
245 RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
246 "vs- 12000: level: wrong FR %z", tb->FR[0]);
247 RFALSE(tb->blknum[0] > 1,
248 "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
249 RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
252 buffer_info_init_tbS0(tb, &bi);
258 balance_leaf_when_delete_del(tb);
260 balance_leaf_when_delete_cut(tb);
271 if (tb->lnum[0])
272 return balance_leaf_when_delete_left(tb);
274 if (tb->rnum[0] == -1) {
276 leaf_shift_right(tb, n, -1);
277 reiserfs_invalidate_buffer(tb, tbS0);
281 RFALSE(tb->rnum[0],
282 "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
286 static unsigned int balance_leaf_insert_left(struct tree_balance *tb,
292 int n = B_NR_ITEMS(tb->L[0]);
295 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
299 ret = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
302 new_item_len = ih_item_len(ih) - tb->lbytes;
312 buffer_info_init_left(tb, &bi);
313 leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
314 min_t(int, tb->zeroes_num, ih_item_len(ih)));
322 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
324 add_le_ih_k_offset(ih, tb->lbytes << shift);
327 if (tb->lbytes > tb->zeroes_num) {
328 body_shift_bytes = tb->lbytes - tb->zeroes_num;
329 tb->zeroes_num = 0;
331 tb->zeroes_num -= tb->lbytes;
339 ret = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
342 buffer_info_init_left(tb, &bi);
343 leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
344 tb->zeroes_num);
345 tb->insert_size[0] = 0;
346 tb->zeroes_num = 0;
351 static void balance_leaf_paste_left_shift_dirent(struct tree_balance *tb,
355 int n = B_NR_ITEMS(tb->L[0]);
358 RFALSE(tb->zeroes_num,
362 if (tb->lbytes > tb->pos_in_item) {
365 int ret, l_pos_in_item = tb->pos_in_item;
371 ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes - 1);
372 if (ret && !tb->item_pos) {
373 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
375 (tb->lbytes - 1);
379 buffer_info_init_left(tb, &bi);
380 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret,
381 l_pos_in_item, tb->insert_size[0],
382 body, tb->zeroes_num);
395 leaf_paste_entries(&bi, n + tb->item_pos - ret,
398 body + DEH_SIZE, tb->insert_size[0]);
399 tb->insert_size[0] = 0;
406 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
410 tb->pos_in_item -= tb->lbytes;
413 static unsigned int balance_leaf_paste_left_shift(struct tree_balance *tb,
417 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
418 int n = B_NR_ITEMS(tb->L[0]);
422 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
423 balance_leaf_paste_left_shift_dirent(tb, ih, body);
427 RFALSE(tb->lbytes <= 0,
429 "lbytes=%d", tb->lbytes);
430 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
433 ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
436 if (tb->lbytes >= tb->pos_in_item) {
442 tbS0_pos_ih = item_head(tbS0, tb->item_pos);
449 l_n = tb->lbytes - tb->pos_in_item;
452 tb->insert_size[0] -= l_n;
454 RFALSE(tb->insert_size[0] <= 0,
456 "L[0]. insert_size=%d", tb->insert_size[0]);
458 ret = leaf_shift_left(tb, tb->lnum[0],
461 tbL0_ih = item_head(tb->L[0], n + tb->item_pos - ret);
464 buffer_info_init_left(tb, &bi);
465 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret,
467 min_t(int, l_n, tb->zeroes_num));
478 leaf_key(tb->L[0], n + tb->item_pos - ret)),
482 int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
490 left_delim_key = internal_key(tb->CFL[0], tb->lkey[0]);
497 if (l_n > tb->zeroes_num) {
498 body_shift_bytes = l_n - tb->zeroes_num;
499 tb->zeroes_num = 0;
501 tb->zeroes_num -= l_n;
502 tb->pos_in_item = 0;
505 leaf_key(tb->L[0],
506 B_NR_ITEMS(tb->L[0]) - 1)) ||
515 tb->pos_in_item -= tb->lbytes;
517 RFALSE(tb->pos_in_item <= 0,
519 tb->pos_in_item);
525 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
532 static void balance_leaf_paste_left_whole(struct tree_balance *tb,
536 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
537 int n = B_NR_ITEMS(tb->L[0]);
543 if (!tb->item_pos &&
549 pasted = item_head(tb->L[0], n - 1);
551 tb->pos_in_item += ih_entry_count(pasted);
553 tb->pos_in_item += ih_item_len(pasted);
560 ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
563 buffer_info_init_left(tb, &bi);
564 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret, tb->pos_in_item,
565 tb->insert_size[0], body, tb->zeroes_num);
568 pasted = item_head(tb->L[0], n + tb->item_pos - ret);
570 leaf_paste_entries(&bi, n + tb->item_pos - ret,
571 tb->pos_in_item, 1,
573 body + DEH_SIZE, tb->insert_size[0]);
582 tb->insert_size[0] = 0;
583 tb->zeroes_num = 0;
586 static unsigned int balance_leaf_paste_left(struct tree_balance *tb,
591 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1)
592 return balance_leaf_paste_left_shift(tb, ih, body);
594 balance_leaf_paste_left_whole(tb, ih, body);
599 static unsigned int balance_leaf_left(struct tree_balance *tb,
603 if (tb->lnum[0] <= 0)
607 if (tb->item_pos < tb->lnum[0]) {
611 return balance_leaf_insert_left(tb, ih, body);
613 return balance_leaf_paste_left(tb, ih, body);
616 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
621 static void balance_leaf_insert_right(struct tree_balance *tb,
626 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
631 if (n - tb->rnum[0] >= tb->item_pos) {
632 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
639 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {
645 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
657 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
658 offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << shift);
660 put_ih_item_len(ih, tb->rbytes);
663 buffer_info_init_right(tb, &bi);
664 if ((old_len - tb->rbytes) > tb->zeroes_num) {
666 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
669 r_zeroes_number = tb->zeroes_num -
670 (old_len - tb->rbytes);
671 tb->zeroes_num -= r_zeroes_number;
677 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
684 put_ih_item_len(ih, old_len - tb->rbytes);
686 tb->insert_size[0] -= tb->rbytes;
692 leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
695 buffer_info_init_right(tb, &bi);
696 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
697 ih, body, tb->zeroes_num);
699 if (tb->item_pos - n + tb->rnum[0] - 1 == 0)
700 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
702 tb->zeroes_num = tb->insert_size[0] = 0;
707 static void balance_leaf_paste_right_shift_dirent(struct tree_balance *tb,
711 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
715 RFALSE(tb->zeroes_num,
717 entry_count = ih_entry_count(item_head(tbS0, tb->item_pos));
720 if (entry_count - tb->rbytes < tb->pos_in_item) {
723 RFALSE(tb->rbytes - 1 >= entry_count || !tb->insert_size[0],
725 "rbytes=%d, entry_count=%d", tb->rbytes, entry_count);
732 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
735 paste_entry_position = tb->pos_in_item - entry_count +
736 tb->rbytes - 1;
737 buffer_info_init_right(tb, &bi);
739 tb->insert_size[0], body, tb->zeroes_num);
744 body + DEH_SIZE, tb->insert_size[0]);
748 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
750 tb->insert_size[0] = 0;
751 tb->pos_in_item++;
754 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
758 static void balance_leaf_paste_right_shift(struct tree_balance *tb,
762 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
769 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
770 balance_leaf_paste_right_shift_dirent(tb, ih, body);
780 n_shift = tb->rbytes - tb->insert_size[0];
784 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
786 "pos_in_item=%d", tb->pos_in_item,
787 ih_item_len(item_head(tbS0, tb->item_pos)));
789 leaf_shift_right(tb, tb->rnum[0], n_shift);
795 n_rem = tb->insert_size[0] - tb->rbytes;
801 version = ih_version(item_head(tb->R[0], 0));
803 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
804 int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
808 add_le_key_k_offset(version, leaf_key(tb->R[0], 0), temp_rem);
809 add_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
812 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
815 buffer_info_init_right(tb, &bi);
816 if (n_rem > tb->zeroes_num) {
818 r_body = body + n_rem - tb->zeroes_num;
821 r_zeroes_number = tb->zeroes_num - n_rem;
822 tb->zeroes_num -= r_zeroes_number;
825 leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem,
828 if (is_indirect_le_ih(item_head(tb->R[0], 0)))
829 set_ih_free_space(item_head(tb->R[0], 0), 0);
831 tb->insert_size[0] = n_rem;
833 tb->pos_in_item++;
836 static void balance_leaf_paste_right_whole(struct tree_balance *tb,
840 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
845 buffer_info_init_right(tb, &bi);
846 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
849 if (tb->pos_in_item >= 0) {
850 buffer_info_init_right(tb, &bi);
851 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0],
852 tb->pos_in_item, tb->insert_size[0], body,
853 tb->zeroes_num);
857 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
858 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
859 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
860 tb->pos_in_item, 1,
862 body + DEH_SIZE, tb->insert_size[0]);
864 if (!tb->pos_in_item) {
866 RFALSE(tb->item_pos - n + tb->rnum[0],
871 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
877 tb->zeroes_num = tb->insert_size[0] = 0;
880 static void balance_leaf_paste_right(struct tree_balance *tb,
884 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
888 if (n - tb->rnum[0] > tb->item_pos) {
889 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
895 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1)
897 balance_leaf_paste_right_shift(tb, ih, body);
900 balance_leaf_paste_right_whole(tb, ih, body);
904 static void balance_leaf_right(struct tree_balance *tb,
908 if (tb->rnum[0] <= 0)
914 balance_leaf_insert_right(tb, ih, body);
916 balance_leaf_paste_right(tb, ih, body);
919 static void balance_leaf_new_nodes_insert(struct tree_balance *tb,
926 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
932 if (n - tb->snum[i] >= tb->item_pos) {
933 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
934 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
941 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) {
946 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i] - 1, -1,
947 tb->S_new[i]);
959 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
962 ((old_len - tb->sbytes[i]) << shift));
964 put_ih_item_len(ih, tb->sbytes[i]);
967 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
969 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
971 r_body = body + (old_len - tb->sbytes[i]) -
972 tb->zeroes_num;
975 r_zeroes_number = tb->zeroes_num - (old_len -
976 tb->sbytes[i]);
977 tb->zeroes_num -= r_zeroes_number;
987 put_ih_item_len(ih, old_len - tb->sbytes[i]);
988 tb->insert_size[0] -= tb->sbytes[i];
996 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
997 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
1000 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1001 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
1002 ih, body, tb->zeroes_num);
1004 tb->zeroes_num = tb->insert_size[0] = 0;
1009 static void balance_leaf_new_nodes_paste_dirent(struct tree_balance *tb,
1016 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1017 struct item_head *aux_ih = item_head(tbS0, tb->item_pos);
1021 if (entry_count - tb->sbytes[i] < tb->pos_in_item &&
1022 tb->pos_in_item <= entry_count) {
1025 RFALSE(!tb->insert_size[0],
1027 RFALSE(tb->sbytes[i] - 1 >= entry_count,
1029 tb->sbytes[i] - 1, entry_count);
1036 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1037 tb->sbytes[i] - 1, tb->S_new[i]);
1043 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1044 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count +
1045 tb->sbytes[i] - 1, tb->insert_size[0],
1046 body, tb->zeroes_num);
1049 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count +
1050 tb->sbytes[i] - 1, 1,
1052 body + DEH_SIZE, tb->insert_size[0]);
1054 tb->insert_size[0] = 0;
1055 tb->pos_in_item++;
1058 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1059 tb->sbytes[i], tb->S_new[i]);
1064 static void balance_leaf_new_nodes_paste_shift(struct tree_balance *tb,
1071 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1072 struct item_head *aux_ih = item_head(tbS0, tb->item_pos);
1081 balance_leaf_new_nodes_paste_dirent(tb, ih, body, insert_key,
1089 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) ||
1090 tb->insert_size[0] <= 0,
1096 n_shift = tb->sbytes[i] - tb->insert_size[0];
1099 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift,
1100 tb->S_new[i]);
1106 n_rem = tb->insert_size[0] - tb->sbytes[i];
1111 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1112 if (n_rem > tb->zeroes_num) {
1114 r_body = body + n_rem - tb->zeroes_num;
1117 r_zeroes_number = tb->zeroes_num - n_rem;
1118 tb->zeroes_num -= r_zeroes_number;
1121 leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem,
1124 tmp = item_head(tb->S_new[i], 0);
1128 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
1132 tb->insert_size[0] = n_rem;
1134 tb->pos_in_item++;
1137 static void balance_leaf_new_nodes_paste_whole(struct tree_balance *tb,
1145 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1152 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
1155 (tb->pos_in_item != ih_item_len(ih_check) ||
1156 tb->insert_size[0] <= 0))
1157 reiserfs_panic(tb->tb_sb,
1162 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1163 tb->sbytes[i], tb->S_new[i]);
1170 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1171 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->snum[i],
1172 tb->pos_in_item, tb->insert_size[0],
1173 body, tb->zeroes_num);
1175 pasted = item_head(tb->S_new[i], tb->item_pos - n +
1176 tb->snum[i]);
1178 leaf_paste_entries(&bi, tb->item_pos - n + tb->snum[i],
1179 tb->pos_in_item, 1,
1181 body + DEH_SIZE, tb->insert_size[0]);
1187 tb->zeroes_num = tb->insert_size[0] = 0;
1190 static void balance_leaf_new_nodes_paste(struct tree_balance *tb,
1197 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1201 if (n - tb->snum[i] > tb->item_pos) {
1202 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1203 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
1209 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1)
1211 balance_leaf_new_nodes_paste_shift(tb, ih, body, insert_key,
1215 balance_leaf_new_nodes_paste_whole(tb, ih, body, insert_key,
1220 static void balance_leaf_new_nodes(struct tree_balance *tb,
1228 for (i = tb->blknum[0] - 2; i >= 0; i--) {
1231 RFALSE(!tb->snum[i],
1233 tb->snum[i]);
1237 tb->S_new[i] = get_FEB(tb);
1240 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
1243 balance_leaf_new_nodes_insert(tb, ih, body, insert_key,
1246 balance_leaf_new_nodes_paste(tb, ih, body, insert_key,
1249 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1250 insert_ptr[i] = tb->S_new[i];
1252 RFALSE(!buffer_journaled(tb->S_new[i])
1253 || buffer_journal_dirty(tb->S_new[i])
1254 || buffer_dirty(tb->S_new[i]),
1256 i, tb->S_new[i]);
1260 static void balance_leaf_finish_node_insert(struct tree_balance *tb,
1264 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1266 buffer_info_init_tbS0(tb, &bi);
1267 leaf_insert_into_buf(&bi, tb->item_pos, ih, body, tb->zeroes_num);
1270 if (tb->item_pos == 0) {
1271 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1272 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1277 static void balance_leaf_finish_node_paste_dirent(struct tree_balance *tb,
1281 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1282 struct item_head *pasted = item_head(tbS0, tb->item_pos);
1285 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1286 RFALSE(!tb->insert_size[0],
1290 buffer_info_init_tbS0(tb, &bi);
1291 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1292 tb->insert_size[0], body, tb->zeroes_num);
1295 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1297 body + DEH_SIZE, tb->insert_size[0]);
1299 if (!tb->item_pos && !tb->pos_in_item) {
1300 RFALSE(!tb->CFL[0] || !tb->L[0],
1302 if (tb->CFL[0])
1303 replace_key(tb, tb->CFL[0], tb->lkey[0],
1307 tb->insert_size[0] = 0;
1311 static void balance_leaf_finish_node_paste(struct tree_balance *tb,
1315 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1317 struct item_head *pasted = item_head(tbS0, tb->item_pos);
1321 balance_leaf_finish_node_paste_dirent(tb, ih, body);
1327 if (tb->pos_in_item == ih_item_len(pasted)) {
1328 RFALSE(tb->insert_size[0] <= 0,
1330 tb->insert_size[0]);
1331 buffer_info_init_tbS0(tb, &bi);
1332 leaf_paste_in_buffer(&bi, tb->item_pos,
1333 tb->pos_in_item, tb->insert_size[0], body,
1334 tb->zeroes_num);
1339 tb->insert_size[0] = 0;
1342 else if (tb->insert_size[0]) {
1344 reiserfs_panic(tb->tb_sb, "PAP-12285",
1345 "insert_size must be 0 (%d)", tb->insert_size[0]);
1355 static void balance_leaf_finish_node(struct tree_balance *tb,
1360 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) {
1362 balance_leaf_finish_node_insert(tb, ih, body);
1364 balance_leaf_finish_node_paste(tb, ih, body);
1370 * @tb: tree balance state
1382 static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1387 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1389 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
1392 if (tb->insert_size[0] < 0)
1393 return balance_leaf_when_delete(tb, flag);
1395 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
1396 tb->pos_in_item = tb->tb_path->pos_in_item,
1397 tb->zeroes_num = 0;
1399 tb->zeroes_num = ih_item_len(ih);
1406 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
1407 tb->pos_in_item *= UNFM_P_SIZE;
1409 body += balance_leaf_left(tb, ih, body, flag);
1411 /* tb->lnum[0] > 0 */
1413 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
1415 balance_leaf_right(tb, ih, body, flag);
1417 /* tb->rnum[0] > 0 */
1418 RFALSE(tb->blknum[0] > 3,
1419 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
1420 RFALSE(tb->blknum[0] < 0,
1421 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
1428 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
1430 RFALSE(!tb->lnum[0] || !tb->rnum[0],
1434 * delimiting key of the tb->L[0]'s and left delimiting key are
1437 if (tb->CFL[0]) {
1438 if (!tb->CFR[0])
1439 reiserfs_panic(tb->tb_sb, "vs-12195",
1441 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
1442 internal_key(tb->CFR[0], tb->rkey[0]));
1443 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
1446 reiserfs_invalidate_buffer(tb, tbS0);
1450 balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag);
1452 balance_leaf_finish_node(tb, ih, body, flag);
1455 if (flag == M_PASTE && tb->insert_size[0]) {
1457 reiserfs_panic(tb->tb_sb,
1459 tb->insert_size[0]);
1483 struct buffer_head *get_FEB(struct tree_balance *tb)
1489 if (tb->FEB[i] != NULL)
1493 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
1495 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
1497 set_buffer_uptodate(tb->FEB[i]);
1498 tb->used[i] = tb->FEB[i];
1499 tb->FEB[i] = NULL;
1501 return tb->used[i];
1505 static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
1510 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1512 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
1513 if (!tb->thrown[i]) {
1514 tb->thrown[i] = bh;
1518 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1522 static void free_thrown(struct tree_balance *tb)
1526 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
1527 if (tb->thrown[i]) {
1528 blocknr = tb->thrown[i]->b_blocknr;
1529 if (buffer_dirty(tb->thrown[i]))
1530 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1533 brelse(tb->thrown[i]); /* incremented in store_thrown */
1534 reiserfs_free_block(tb->transaction_handle, NULL,
1540 void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
1548 store_thrown(tb, bh);
1552 void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1576 do_balance_mark_internal_dirty(tb, dest, 0);
1579 int get_left_neighbor_position(struct tree_balance *tb, int h)
1581 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1583 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
1585 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
1588 return B_NR_ITEMS(tb->FL[h]);
1593 int get_right_neighbor_position(struct tree_balance *tb, int h)
1595 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1597 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
1599 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
1601 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1636 static int locked_or_not_in_tree(struct tree_balance *tb,
1641 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
1647 static int check_before_balancing(struct tree_balance *tb)
1651 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
1652 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1663 if (tb->lnum[0]) {
1664 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1665 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1666 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
1667 check_leaf(tb->L[0]);
1669 if (tb->rnum[0]) {
1670 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1671 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1672 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
1673 check_leaf(tb->R[0]);
1675 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1677 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1682 static void check_after_balance_leaf(struct tree_balance *tb)
1684 if (tb->lnum[0]) {
1685 if (B_FREE_SPACE(tb->L[0]) !=
1686 MAX_CHILD_SIZE(tb->L[0]) -
1688 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1690 reiserfs_panic(tb->tb_sb, "PAP-12355",
1694 if (tb->rnum[0]) {
1695 if (B_FREE_SPACE(tb->R[0]) !=
1696 MAX_CHILD_SIZE(tb->R[0]) -
1698 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1700 reiserfs_panic(tb->tb_sb, "PAP-12360",
1704 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1705 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1706 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1707 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1708 PATH_H_POSITION(tb->tb_path, 1)))))) {
1709 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1710 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1711 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1712 PATH_H_POSITION(tb->tb_path,
1715 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
1716 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1719 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1720 PATH_H_PBUFFER(tb->tb_path, 1),
1721 PATH_H_POSITION(tb->tb_path, 1),
1723 (PATH_H_PBUFFER(tb->tb_path, 1),
1724 PATH_H_POSITION(tb->tb_path, 1))),
1726 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
1730 static void check_leaf_level(struct tree_balance *tb)
1732 check_leaf(tb->L[0]);
1733 check_leaf(tb->R[0]);
1734 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1737 static void check_internal_levels(struct tree_balance *tb)
1742 for (h = 1; tb->insert_size[h]; h++) {
1743 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1745 if (tb->lnum[h])
1746 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1747 if (tb->rnum[h])
1748 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1761 * necessary to add ordered locking of tb.
1789 static inline void do_balance_starts(struct tree_balance *tb)
1793 /* store_print_tb (tb); */
1797 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1798 tb->tb_path->pos_in_item, tb, "check");
1800 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
1802 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
1806 static inline void do_balance_completed(struct tree_balance *tb)
1810 check_leaf_level(tb);
1811 check_internal_levels(tb);
1812 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
1821 REISERFS_SB(tb->tb_sb)->s_do_balance++;
1824 unfix_nodes(tb);
1826 free_thrown(tb);
1832 * @tb: tree_balance structure
1847 void do_balance(struct tree_balance *tb, struct item_head *ih,
1864 tb->tb_mode = flag;
1865 tb->need_balance_dirty = 0;
1867 if (FILESYSTEM_CHANGED_TB(tb)) {
1868 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1872 if (!tb->insert_size[0]) {
1873 reiserfs_warning(tb->tb_sb, "PAP-12350",
1875 unfix_nodes(tb);
1879 atomic_inc(&fs_generation(tb->tb_sb));
1880 do_balance_starts(tb);
1887 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1888 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
1891 check_after_balance_leaf(tb);
1895 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1896 child_pos = balance_internal(tb, h, child_pos, insert_key,
1899 do_balance_completed(tb);