^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "reiserfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /* this is one and only function that is used outside (do_balance.c) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) int balance_internal(struct tree_balance *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) int, int, struct item_head *, struct buffer_head **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * modes of internal_shift_left, internal_shift_right and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * internal_insert_childs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define INTERNAL_SHIFT_FROM_S_TO_L 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define INTERNAL_SHIFT_FROM_R_TO_S 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define INTERNAL_SHIFT_FROM_L_TO_S 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define INTERNAL_SHIFT_FROM_S_TO_R 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define INTERNAL_INSERT_TO_S 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define INTERNAL_INSERT_TO_L 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define INTERNAL_INSERT_TO_R 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void internal_define_dest_src_infos(int shift_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct buffer_info *dest_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct buffer_info *src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int *d_key, struct buffer_head **cf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) memset(dest_bi, 0, sizeof(struct buffer_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) memset(src_bi, 0, sizeof(struct buffer_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* define dest, src, dest parent, dest position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) switch (shift_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* used in internal_shift_left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case INTERNAL_SHIFT_FROM_S_TO_L:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) src_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) dest_bi->bi_bh = tb->L[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) dest_bi->bi_parent = tb->FL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dest_bi->bi_position = get_left_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *d_key = tb->lkey[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *cf = tb->CFL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case INTERNAL_SHIFT_FROM_L_TO_S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) src_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) src_bi->bi_bh = tb->L[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) src_bi->bi_parent = tb->FL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) src_bi->bi_position = get_left_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* dest position is analog of dest->b_item_order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *d_key = tb->lkey[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *cf = tb->CFL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* used in internal_shift_left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) case INTERNAL_SHIFT_FROM_R_TO_S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) src_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) src_bi->bi_bh = tb->R[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) src_bi->bi_parent = tb->FR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) src_bi->bi_position = get_right_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *d_key = tb->rkey[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *cf = tb->CFR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) case INTERNAL_SHIFT_FROM_S_TO_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) src_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) dest_bi->bi_bh = tb->R[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) dest_bi->bi_parent = tb->FR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dest_bi->bi_position = get_right_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) *d_key = tb->rkey[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *cf = tb->CFR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case INTERNAL_INSERT_TO_L:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dest_bi->bi_bh = tb->L[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dest_bi->bi_parent = tb->FL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) dest_bi->bi_position = get_left_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case INTERNAL_INSERT_TO_S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case INTERNAL_INSERT_TO_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dest_bi->tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dest_bi->bi_bh = tb->R[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) dest_bi->bi_parent = tb->FR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dest_bi->bi_position = get_right_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) reiserfs_panic(tb->tb_sb, "ibalance-1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "shift type is unknown (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) shift_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Insert count node pointers into buffer cur before position to + 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Insert count items into buffer cur before position to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Items and node pointers are specified by inserted and bh respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void internal_insert_childs(struct buffer_info *cur_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int to, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct item_head *inserted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct buffer_head *cur = cur_bi->bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct reiserfs_key *ih;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct disk_child new_dc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) blkh = B_BLK_HEAD(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) nr = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) RFALSE(count > 2, "too many children (%d) are to be inserted", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) RFALSE(B_FREE_SPACE(cur) < count * (KEY_SIZE + DC_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) "no enough free space (%d), needed %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) B_FREE_SPACE(cur), count * (KEY_SIZE + DC_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* prepare space for count disk_child */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dc = B_N_CHILD(cur, to + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) memmove(dc + count, dc, (nr + 1 - (to + 1)) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* copy to_be_insert disk children */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) put_dc_size(&new_dc[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MAX_CHILD_SIZE(bh[i]) - B_FREE_SPACE(bh[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) put_dc_block_number(&new_dc[i], bh[i]->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) memcpy(dc, new_dc, DC_SIZE * count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* prepare space for count items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ih = internal_key(cur, ((to == -1) ? 0 : to));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) memmove(ih + count, ih,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) (nr - to) * KEY_SIZE + (nr + 1 + count) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* copy item headers (keys) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) memcpy(ih, inserted, KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memcpy(ih + 1, inserted + 1, KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* sizes, item number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) set_blkh_nr_item(blkh, blkh_nr_item(blkh) + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) set_blkh_free_space(blkh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) blkh_free_space(blkh) - count * (DC_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) KEY_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) check_internal(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (cur_bi->bi_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct disk_child *t_dc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) put_dc_size(t_dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dc_size(t_dc) + (count * (DC_SIZE + KEY_SIZE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) check_internal(cur_bi->bi_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Delete del_num items and node pointers from buffer cur starting from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * the first_i'th item and first_p'th pointers respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void internal_delete_pointers_items(struct buffer_info *cur_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int first_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int first_i, int del_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct buffer_head *cur = cur_bi->bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct reiserfs_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) RFALSE(cur == NULL, "buffer is 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) RFALSE(del_num < 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) "negative number of items (%d) can not be deleted", del_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) RFALSE(first_p < 0 || first_p + del_num > B_NR_ITEMS(cur) + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) || first_i < 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) "first pointer order (%d) < 0 or "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "no so many pointers (%d), only (%d) or "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) "first key order %d < 0", first_p, first_p + del_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) B_NR_ITEMS(cur) + 1, first_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (del_num == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) blkh = B_BLK_HEAD(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) nr = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (first_p == 0 && del_num == nr + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) RFALSE(first_i != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) "1st deleted key must have order 0, not %d", first_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) make_empty_node(cur_bi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) RFALSE(first_i + del_num > B_NR_ITEMS(cur),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) "first_i = %d del_num = %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) "no so many keys (%d) in the node (%b)(%z)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) first_i, del_num, first_i + del_num, cur, cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* deleting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dc = B_N_CHILD(cur, first_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) memmove(dc, dc + del_num, (nr + 1 - first_p - del_num) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) key = internal_key(cur, first_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) memmove(key, key + del_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) (nr - first_i - del_num) * KEY_SIZE + (nr + 1 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) del_num) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* sizes, item number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) set_blkh_free_space(blkh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) blkh_free_space(blkh) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) (del_num * (KEY_SIZE + DC_SIZE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /*&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) check_internal(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /*&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (cur_bi->bi_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct disk_child *t_dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) t_dc = B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) put_dc_size(t_dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) dc_size(t_dc) - (del_num * (KEY_SIZE + DC_SIZE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) check_internal(cur_bi->bi_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* delete n node pointers and items starting from given position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void internal_delete_childs(struct buffer_info *cur_bi, int from, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int i_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) i_from = (from == 0) ? from : from - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * delete n pointers starting from `from' position in CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * delete n keys starting from 'i_from' position in CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) internal_delete_pointers_items(cur_bi, from, i_from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * copy cpy_num node pointers and cpy_num - 1 items from buffer src to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * dest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * last_first == FIRST_TO_LAST means that we copy first items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * from src to tail of dest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * last_first == LAST_TO_FIRST means that we copy last items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * from src to head of dest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static void internal_copy_pointers_items(struct buffer_info *dest_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct buffer_head *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int last_first, int cpy_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * ATTENTION! Number of node pointers in DEST is equal to number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * of items in DEST as delimiting key have already inserted to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * buffer dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct buffer_head *dest = dest_bi->bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int nr_dest, nr_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int dest_order, src_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct reiserfs_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) nr_src = B_NR_ITEMS(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) RFALSE(dest == NULL || src == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) "src (%p) or dest (%p) buffer is 0", src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) "invalid last_first parameter (%d)", last_first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) RFALSE(nr_src < cpy_num - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) "no so many items (%d) in src (%d)", cpy_num, nr_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) RFALSE(cpy_num < 0, "cpy_num less than 0 (%d)", cpy_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) RFALSE(cpy_num - 1 + B_NR_ITEMS(dest) > (int)MAX_NR_KEY(dest),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) "cpy_num (%d) + item number in dest (%d) can not be > MAX_NR_KEY(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cpy_num, B_NR_ITEMS(dest), MAX_NR_KEY(dest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (cpy_num == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* coping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) blkh = B_BLK_HEAD(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) nr_dest = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*dest_order = (last_first == LAST_TO_FIRST) ? 0 : nr_dest; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /*src_order = (last_first == LAST_TO_FIRST) ? (nr_src - cpy_num + 1) : 0; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) (last_first == LAST_TO_FIRST) ? (dest_order = 0, src_order =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) nr_src - cpy_num + 1) : (dest_order =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) nr_dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) src_order =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* prepare space for cpy_num pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) dc = B_N_CHILD(dest, dest_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) memmove(dc + cpy_num, dc, (nr_dest - dest_order) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* insert pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) memcpy(dc, B_N_CHILD(src, src_order), DC_SIZE * cpy_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* prepare space for cpy_num - 1 item headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) key = internal_key(dest, dest_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) memmove(key + cpy_num - 1, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) KEY_SIZE * (nr_dest - dest_order) + DC_SIZE * (nr_dest +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cpy_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* insert headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) memcpy(key, internal_key(src, src_order), KEY_SIZE * (cpy_num - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* sizes, item number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) set_blkh_nr_item(blkh, blkh_nr_item(blkh) + (cpy_num - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) set_blkh_free_space(blkh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) blkh_free_space(blkh) - (KEY_SIZE * (cpy_num - 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) DC_SIZE * cpy_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) check_internal(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (dest_bi->bi_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct disk_child *t_dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) put_dc_size(t_dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) dc_size(t_dc) + (KEY_SIZE * (cpy_num - 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) DC_SIZE * cpy_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) check_internal(dest_bi->bi_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Copy cpy_num node pointers and cpy_num - 1 items from buffer src to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * buffer dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * Delete cpy_num - del_par items and node pointers from buffer src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * last_first == FIRST_TO_LAST means, that we copy/delete first items from src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * last_first == LAST_TO_FIRST means, that we copy/delete last items from src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static void internal_move_pointers_items(struct buffer_info *dest_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct buffer_info *src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int last_first, int cpy_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int del_par)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) int first_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int first_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) internal_copy_pointers_items(dest_bi, src_bi->bi_bh, last_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) cpy_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (last_first == FIRST_TO_LAST) { /* shift_left occurs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) first_pointer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) first_item = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * delete cpy_num - del_par pointers and keys starting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * pointers with first_pointer, for key - with first_item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) internal_delete_pointers_items(src_bi, first_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) first_item, cpy_num - del_par);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) } else { /* shift_right occurs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) i = (cpy_num - del_par ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) (j =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) B_NR_ITEMS(src_bi->bi_bh)) + 1) ? 0 : j - cpy_num +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) del_par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) internal_delete_pointers_items(src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) j + 1 - cpy_num + del_par, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) cpy_num - del_par);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /* Insert n_src'th key of buffer src before n_dest'th key of buffer dest. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static void internal_insert_key(struct buffer_info *dest_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /* insert key before key with n_dest number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int dest_position_before,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct buffer_head *src, int src_position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct buffer_head *dest = dest_bi->bi_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct reiserfs_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) RFALSE(dest == NULL || src == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) "source(%p) or dest(%p) buffer is 0", src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) RFALSE(dest_position_before < 0 || src_position < 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) "source(%d) or dest(%d) key number less than 0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) src_position, dest_position_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) RFALSE(dest_position_before > B_NR_ITEMS(dest) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) src_position >= B_NR_ITEMS(src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) "invalid position in dest (%d (key number %d)) or in src (%d (key number %d))",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) dest_position_before, B_NR_ITEMS(dest),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) src_position, B_NR_ITEMS(src));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) RFALSE(B_FREE_SPACE(dest) < KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) "no enough free space (%d) in dest buffer", B_FREE_SPACE(dest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) blkh = B_BLK_HEAD(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) nr = blkh_nr_item(blkh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /* prepare space for inserting key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) key = internal_key(dest, dest_position_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) memmove(key + 1, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) (nr - dest_position_before) * KEY_SIZE + (nr + 1) * DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* insert key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) memcpy(key, internal_key(src, src_position), KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Change dirt, free space, item number fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) set_blkh_free_space(blkh, blkh_free_space(blkh) - KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (dest_bi->bi_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct disk_child *t_dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) put_dc_size(t_dc, dc_size(t_dc) + KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * Insert d_key'th (delimiting) key from buffer cfl to tail of dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * Copy pointer_amount node pointers and pointer_amount - 1 items from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * buffer src to buffer dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * Replace d_key'th key in buffer cfl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * Delete pointer_amount items and node pointers from buffer src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* this can be invoked both to shift from S to L and from R to S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static void internal_shift_left(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * INTERNAL_FROM_S_TO_L | INTERNAL_FROM_R_TO_S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int h, int pointer_amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct buffer_info dest_bi, src_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct buffer_head *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int d_key_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) &d_key_position, &cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /*printk("pointer_amount = %d\n",pointer_amount); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (pointer_amount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * insert delimiting key from common father of dest and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * src to node dest into position B_NR_ITEM(dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) d_key_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (B_NR_ITEMS(src_bi.bi_bh) == pointer_amount - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (src_bi.bi_position /*src->b_item_order */ == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) replace_key(tb, cf, d_key_position,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) src_bi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) bi_parent /*src->b_parent */ , 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) replace_key(tb, cf, d_key_position, src_bi.bi_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) pointer_amount - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /* last parameter is del_parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) pointer_amount, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * Insert delimiting key to L[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * Copy n node pointers and n - 1 items from buffer S[h] to L[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * Delete n - 1 items and node pointers from buffer S[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /* it always shifts from S[h] to L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static void internal_shift1_left(struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) int h, int pointer_amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct buffer_info dest_bi, src_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) struct buffer_head *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int d_key_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) &dest_bi, &src_bi, &d_key_position, &cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /* insert lkey[h]-th key from CFL[h] to left neighbor L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (pointer_amount > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) d_key_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* last parameter is del_parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) pointer_amount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * Insert d_key'th (delimiting) key from buffer cfr to head of dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * Copy n node pointers and n - 1 items from buffer src to buffer dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * Replace d_key'th key in buffer cfr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * Delete n items and node pointers from buffer src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static void internal_shift_right(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * INTERNAL_FROM_S_TO_R | INTERNAL_FROM_L_TO_S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int h, int pointer_amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct buffer_info dest_bi, src_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct buffer_head *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int d_key_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) &d_key_position, &cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) nr = B_NR_ITEMS(src_bi.bi_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (pointer_amount > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * insert delimiting key from common father of dest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * and src to dest node into position 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) internal_insert_key(&dest_bi, 0, cf, d_key_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (nr == pointer_amount - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) RFALSE(src_bi.bi_bh != PATH_H_PBUFFER(tb->tb_path, h) /*tb->S[h] */ ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) dest_bi.bi_bh != tb->R[h],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) "src (%p) must be == tb->S[h](%p) when it disappears",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) src_bi.bi_bh, PATH_H_PBUFFER(tb->tb_path, h));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /* when S[h] disappers replace left delemiting key as well */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (tb->CFL[h])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) replace_key(tb, cf, d_key_position, tb->CFL[h],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) tb->lkey[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) replace_key(tb, cf, d_key_position, src_bi.bi_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) nr - pointer_amount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* last parameter is del_parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) pointer_amount, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * Insert delimiting key to R[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * Copy n node pointers and n - 1 items from buffer S[h] to R[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * Delete n - 1 items and node pointers from buffer S[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) /* it always shift from S[h] to R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static void internal_shift1_right(struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int h, int pointer_amount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct buffer_info dest_bi, src_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct buffer_head *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int d_key_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) &dest_bi, &src_bi, &d_key_position, &cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) /* insert rkey from CFR[h] to right neighbor R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (pointer_amount > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) internal_insert_key(&dest_bi, 0, cf, d_key_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /* last parameter is del_parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) pointer_amount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * Delete insert_num node pointers together with their left items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * and balance current node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) static void balance_internal_when_delete(struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) int h, int child_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) int insert_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct buffer_info bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) insert_num = tb->insert_size[h] / ((int)(DC_SIZE + KEY_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /* delete child-node-pointer(s) together with their left item(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) bi.bi_bh = tbSh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) internal_delete_childs(&bi, child_pos, -insert_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) RFALSE(tb->blknum[h] > 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) "tb->blknum[%d]=%d when insert_size < 0", h, tb->blknum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) n = B_NR_ITEMS(tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (tb->lnum[h] == 0 && tb->rnum[h] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (tb->blknum[h] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /* node S[h] (root of the tree) is empty now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct buffer_head *new_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) RFALSE(n
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) || B_FREE_SPACE(tbSh) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) MAX_CHILD_SIZE(tbSh) - DC_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) "buffer must have only 0 keys (%d)", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) RFALSE(bi.bi_parent, "root has parent (%p)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) bi.bi_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* choose a new root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (!tb->L[h - 1] || !B_NR_ITEMS(tb->L[h - 1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) new_root = tb->R[h - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) new_root = tb->L[h - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * switch super block's tree root block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * number to the new value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) PUT_SB_ROOT_BLOCK(tb->tb_sb, new_root->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) /*REISERFS_SB(tb->tb_sb)->s_rs->s_tree_height --; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) PUT_SB_TREE_HEIGHT(tb->tb_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) SB_TREE_HEIGHT(tb->tb_sb) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) do_balance_mark_sb_dirty(tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) REISERFS_SB(tb->tb_sb)->s_sbh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /*&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* use check_internal if new root is an internal node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (h > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) check_internal(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /*&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* do what is needed for buffer thrown from tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) reiserfs_invalidate_buffer(tb, tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /* join S[h] with L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (tb->L[h] && tb->lnum[h] == -B_NR_ITEMS(tb->L[h]) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) RFALSE(tb->rnum[h] != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) "invalid tb->rnum[%d]==%d when joining S[h] with L[h]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) h, tb->rnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, n + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) reiserfs_invalidate_buffer(tb, tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /* join S[h] with R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (tb->R[h] && tb->rnum[h] == -B_NR_ITEMS(tb->R[h]) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) RFALSE(tb->lnum[h] != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) "invalid tb->lnum[%d]==%d when joining S[h] with R[h]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) h, tb->lnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h, n + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) reiserfs_invalidate_buffer(tb, tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /* borrow from left neighbor L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (tb->lnum[h] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) RFALSE(tb->rnum[h] != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) "wrong tb->rnum[%d]==%d when borrow from L[h]", h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) tb->rnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) internal_shift_right(INTERNAL_SHIFT_FROM_L_TO_S, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) -tb->lnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /* borrow from right neighbor R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (tb->rnum[h] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) RFALSE(tb->lnum[h] != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) "invalid tb->lnum[%d]==%d when borrow from R[h]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) h, tb->lnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) internal_shift_left(INTERNAL_SHIFT_FROM_R_TO_S, tb, h, -tb->rnum[h]); /*tb->S[h], tb->CFR[h], tb->rkey[h], tb->R[h], -tb->rnum[h]); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* split S[h] into two parts and put them into neighbors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (tb->lnum[h] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) RFALSE(tb->rnum[h] == 0 || tb->lnum[h] + tb->rnum[h] != n + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) "invalid tb->lnum[%d]==%d or tb->rnum[%d]==%d when S[h](item number == %d) is split between them",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) h, tb->lnum[h], h, tb->rnum[h], n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, tb->lnum[h]); /*tb->L[h], tb->CFL[h], tb->lkey[h], tb->S[h], tb->lnum[h]); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) tb->rnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) reiserfs_invalidate_buffer(tb, tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) reiserfs_panic(tb->tb_sb, "ibalance-2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) "unexpected tb->lnum[%d]==%d or tb->rnum[%d]==%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) h, tb->lnum[h], h, tb->rnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) /* Replace delimiting key of buffers L[h] and S[h] by the given key.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static void replace_lkey(struct tree_balance *tb, int h, struct item_head *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) RFALSE(tb->L[h] == NULL || tb->CFL[h] == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) "L[h](%p) and CFL[h](%p) must exist in replace_lkey",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) tb->L[h], tb->CFL[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (B_NR_ITEMS(PATH_H_PBUFFER(tb->tb_path, h)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) memcpy(internal_key(tb->CFL[h], tb->lkey[h]), key, KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) do_balance_mark_internal_dirty(tb, tb->CFL[h], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) /* Replace delimiting key of buffers S[h] and R[h] by the given key.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) static void replace_rkey(struct tree_balance *tb, int h, struct item_head *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) RFALSE(tb->R[h] == NULL || tb->CFR[h] == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) "R[h](%p) and CFR[h](%p) must exist in replace_rkey",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) tb->R[h], tb->CFR[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) RFALSE(B_NR_ITEMS(tb->R[h]) == 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) "R[h] can not be empty if it exists (item number=%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) B_NR_ITEMS(tb->R[h]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) memcpy(internal_key(tb->CFR[h], tb->rkey[h]), key, KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) do_balance_mark_internal_dirty(tb, tb->CFR[h], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * if inserting/pasting {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * child_pos is the position of the node-pointer in S[h] that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * pointed to S[h-1] before balancing of the h-1 level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) * this means that new pointers and items must be inserted AFTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * child_pos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * it is the position of the leftmost pointer that must be deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * (together with its corresponding key to the left of the pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * as a result of the previous level's balancing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) int balance_internal(struct tree_balance *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) int h, /* level of the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) int child_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) /* key for insertion on higher level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct item_head *insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) /* node for insertion on higher level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) struct buffer_head **insert_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) struct buffer_info bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * we return this: it is 0 if there is no S[h],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * else it is tb->S[h]->b_item_order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) int order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) int insert_num, n, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) struct buffer_head *S_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) struct item_head new_insert_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) struct buffer_head *new_insert_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) struct item_head *new_insert_key_addr = insert_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) RFALSE(h < 1, "h (%d) can not be < 1 on internal level", h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) PROC_INFO_INC(tb->tb_sb, balance_at[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) order =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) (tbSh) ? PATH_H_POSITION(tb->tb_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) h + 1) /*tb->S[h]->b_item_order */ : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * Using insert_size[h] calculate the number insert_num of items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * that must be inserted to or deleted from S[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) insert_num = tb->insert_size[h] / ((int)(KEY_SIZE + DC_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /* Check whether insert_num is proper * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) RFALSE(insert_num < -2 || insert_num > 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) "incorrect number of items inserted to the internal node (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) insert_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) RFALSE(h > 1 && (insert_num > 1 || insert_num < -1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) "incorrect number of items (%d) inserted to the internal node on a level (h=%d) higher than last internal level",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) insert_num, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) /* Make balance in case insert_num < 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (insert_num < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) balance_internal_when_delete(tb, h, child_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (tb->lnum[h] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * shift lnum[h] items from S[h] to the left neighbor L[h].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * check how many of new items fall into L[h] or CFL[h] after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * shifting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) n = B_NR_ITEMS(tb->L[h]); /* number of items in L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (tb->lnum[h] <= child_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) /* new items don't fall into L[h] or CFL[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) tb->lnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) child_pos -= tb->lnum[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) } else if (tb->lnum[h] > child_pos + insert_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) /* all new items fall into L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) tb->lnum[h] - insert_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) /* insert insert_num keys and node-pointers into L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) bi.bi_bh = tb->L[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) bi.bi_parent = tb->FL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) bi.bi_position = get_left_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) internal_insert_childs(&bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) /*tb->L[h], tb->S[h-1]->b_next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) n + child_pos + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) insert_num, insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) insert_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) insert_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * some items fall into L[h] or CFL[h],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * but some don't fall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) internal_shift1_left(tb, h, child_pos + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /* calculate number of new items that fall into L[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) k = tb->lnum[h] - child_pos - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) bi.bi_bh = tb->L[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) bi.bi_parent = tb->FL[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) bi.bi_position = get_left_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) internal_insert_childs(&bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) /*tb->L[h], tb->S[h-1]->b_next, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) n + child_pos + 1, k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) insert_key, insert_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) replace_lkey(tb, h, insert_key + k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * replace the first node-ptr in S[h] by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * node-ptr to insert_ptr[k]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) dc = B_N_CHILD(tbSh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) put_dc_size(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) MAX_CHILD_SIZE(insert_ptr[k]) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) B_FREE_SPACE(insert_ptr[k]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) put_dc_block_number(dc, insert_ptr[k]->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) do_balance_mark_internal_dirty(tb, tbSh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) insert_key += k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) insert_ptr += k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) insert_num -= k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) child_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /* tb->lnum[h] > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (tb->rnum[h] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) /*shift rnum[h] items from S[h] to the right neighbor R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * check how many of new items fall into R or CFR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * after shifting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) n = B_NR_ITEMS(tbSh); /* number of items in S[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (n - tb->rnum[h] >= child_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) /* new items fall into S[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) tb->rnum[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) else if (n + insert_num - tb->rnum[h] < child_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) /* all new items fall into R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) tb->rnum[h] - insert_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) /* insert insert_num keys and node-pointers into R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) bi.bi_bh = tb->R[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) bi.bi_parent = tb->FR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) bi.bi_position = get_right_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) internal_insert_childs(&bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) /*tb->R[h],tb->S[h-1]->b_next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) child_pos - n - insert_num +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) tb->rnum[h] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) insert_num, insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) insert_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) insert_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) /* one of the items falls into CFR[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) internal_shift1_right(tb, h, n - child_pos + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) /* calculate number of new items that fall into R[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) k = tb->rnum[h] - n + child_pos - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) bi.bi_bh = tb->R[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) bi.bi_parent = tb->FR[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) bi.bi_position = get_right_neighbor_position(tb, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) internal_insert_childs(&bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) /*tb->R[h], tb->R[h]->b_child, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) 0, k, insert_key + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) insert_ptr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) replace_rkey(tb, h, insert_key + insert_num - k - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * replace the first node-ptr in R[h] by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * node-ptr insert_ptr[insert_num-k-1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) dc = B_N_CHILD(tb->R[h], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) put_dc_size(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) MAX_CHILD_SIZE(insert_ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) [insert_num - k - 1]) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) B_FREE_SPACE(insert_ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) [insert_num - k - 1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) put_dc_block_number(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) insert_ptr[insert_num - k -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) 1]->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) do_balance_mark_internal_dirty(tb, tb->R[h], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) insert_num -= (k + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /** Fill new node that appears instead of S[h] **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) RFALSE(tb->blknum[h] > 2, "blknum can not be > 2 for internal level");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) RFALSE(tb->blknum[h] < 0, "blknum can not be < 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (!tb->blknum[h]) { /* node S[h] is empty now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) RFALSE(!tbSh, "S[h] is equal NULL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) /* do what is needed for buffer thrown from tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) reiserfs_invalidate_buffer(tb, tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (!tbSh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /* create new root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct buffer_head *tbSh_1 = PATH_H_PBUFFER(tb->tb_path, h - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) struct block_head *blkh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) if (tb->blknum[h] != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) reiserfs_panic(NULL, "ibalance-3", "One new node "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) "required for creating the new root");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /* S[h] = empty buffer from the list FEB. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) tbSh = get_FEB(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) blkh = B_BLK_HEAD(tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) set_blkh_level(blkh, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /* Put the unique node-pointer to S[h] that points to S[h-1]. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) dc = B_N_CHILD(tbSh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) put_dc_block_number(dc, tbSh_1->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) put_dc_size(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) (MAX_CHILD_SIZE(tbSh_1) - B_FREE_SPACE(tbSh_1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) tb->insert_size[h] -= DC_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) set_blkh_free_space(blkh, blkh_free_space(blkh) - DC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) do_balance_mark_internal_dirty(tb, tbSh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) check_internal(tbSh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) /*&&&&&&&&&&&&&&&&&&&&&&&& */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) /* put new root into path structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) PATH_OFFSET_PBUFFER(tb->tb_path, ILLEGAL_PATH_ELEMENT_OFFSET) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) tbSh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) /* Change root in structure super block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) PUT_SB_ROOT_BLOCK(tb->tb_sb, tbSh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) PUT_SB_TREE_HEIGHT(tb->tb_sb, SB_TREE_HEIGHT(tb->tb_sb) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) do_balance_mark_sb_dirty(tb, REISERFS_SB(tb->tb_sb)->s_sbh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (tb->blknum[h] == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) int snum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) struct buffer_info dest_bi, src_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /* S_new = free buffer from list FEB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) S_new = get_FEB(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) set_blkh_level(B_BLK_HEAD(S_new), h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) dest_bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) dest_bi.bi_bh = S_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) dest_bi.bi_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) dest_bi.bi_position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) src_bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) src_bi.bi_bh = tbSh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) src_bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) src_bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) n = B_NR_ITEMS(tbSh); /* number of items in S[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) snum = (insert_num + n + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (n - snum >= child_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) /* new items don't fall into S_new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /* store the delimiting key for the next level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* new_insert_key = (n - snum)'th key in S[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) memcpy(&new_insert_key, internal_key(tbSh, n - snum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) /* last parameter is del_par */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) internal_move_pointers_items(&dest_bi, &src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) LAST_TO_FIRST, snum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) } else if (n + insert_num - snum < child_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) /* all new items fall into S_new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) /* store the delimiting key for the next level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * new_insert_key = (n + insert_item - snum)'th
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * key in S[h]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) memcpy(&new_insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) internal_key(tbSh, n + insert_num - snum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) /* last parameter is del_par */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) internal_move_pointers_items(&dest_bi, &src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) LAST_TO_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) snum - insert_num, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * insert insert_num keys and node-pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * into S_new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) internal_insert_childs(&dest_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) /*S_new,tb->S[h-1]->b_next, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) child_pos - n - insert_num +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) snum - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) insert_num, insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) insert_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) insert_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) struct disk_child *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) /* some items fall into S_new, but some don't fall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) /* last parameter is del_par */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) internal_move_pointers_items(&dest_bi, &src_bi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) LAST_TO_FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) n - child_pos + 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) /* calculate number of new items that fall into S_new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) k = snum - n + child_pos - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) internal_insert_childs(&dest_bi, /*S_new, */ 0, k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) insert_key + 1, insert_ptr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /* new_insert_key = insert_key[insert_num - k - 1] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) memcpy(&new_insert_key, insert_key + insert_num - k - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * replace first node-ptr in S_new by node-ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * to insert_ptr[insert_num-k-1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) dc = B_N_CHILD(S_new, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) put_dc_size(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) (MAX_CHILD_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) (insert_ptr[insert_num - k - 1]) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) B_FREE_SPACE(insert_ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) [insert_num - k - 1])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) put_dc_block_number(dc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) insert_ptr[insert_num - k -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 1]->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) do_balance_mark_internal_dirty(tb, S_new, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) insert_num -= (k + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) /* new_insert_ptr = node_pointer to S_new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) new_insert_ptr = S_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) RFALSE(!buffer_journaled(S_new) || buffer_journal_dirty(S_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) || buffer_dirty(S_new), "cm-00001: bad S_new (%b)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) S_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) /* S_new is released in unfix_nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) n = B_NR_ITEMS(tbSh); /*number of items in S[h] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (0 <= child_pos && child_pos <= n && insert_num > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) bi.tb = tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) bi.bi_bh = tbSh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) internal_insert_childs(&bi, /*tbSh, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) /* ( tb->S[h-1]->b_parent == tb->S[h] ) ? tb->S[h-1]->b_next : tb->S[h]->b_child->b_next, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) child_pos, insert_num, insert_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) insert_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) insert_ptr[0] = new_insert_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (new_insert_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) memcpy(new_insert_key_addr, &new_insert_key, KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) return order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }