📄 ibalance.c
字号:
/*&&&&&&&&&&&&&&&&&&&&&&&&*/ check_internal (dest); /*&&&&&&&&&&&&&&&&&&&&&&&&*/ if (dest_bi->bi_parent) { struct disk_child *t_dc; t_dc = B_N_CHILD(dest_bi->bi_parent,dest_bi->bi_position); put_dc_size( t_dc, dc_size(t_dc) + (KEY_SIZE * (cpy_num - 1) + DC_SIZE * cpy_num) ); do_balance_mark_internal_dirty (dest_bi->tb, dest_bi->bi_parent,0); /*&&&&&&&&&&&&&&&&&&&&&&&&*/ check_internal (dest_bi->bi_parent); /*&&&&&&&&&&&&&&&&&&&&&&&&*/ }}/* Copy cpy_num node pointers and cpy_num - 1 items from buffer src to buffer dest. * Delete cpy_num - del_par items and node pointers from buffer src. * last_first == FIRST_TO_LAST means, that we copy/delete first items from src. * last_first == LAST_TO_FIRST means, that we copy/delete last items from src. */static void internal_move_pointers_items (struct buffer_info * dest_bi, struct buffer_info * src_bi, int last_first, int cpy_num, int del_par){ int first_pointer; int first_item; internal_copy_pointers_items (dest_bi, src_bi->bi_bh, last_first, cpy_num); if (last_first == FIRST_TO_LAST) { /* shift_left occurs */ first_pointer = 0; first_item = 0; /* delete cpy_num - del_par pointers and keys starting for pointers with first_pointer, for key - with first_item */ internal_delete_pointers_items (src_bi, first_pointer, first_item, cpy_num - del_par); } else { /* shift_right occurs */ int i, j; i = ( cpy_num - del_par == ( j = B_NR_ITEMS(src_bi->bi_bh)) + 1 ) ? 0 : j - cpy_num + del_par; internal_delete_pointers_items (src_bi, j + 1 - cpy_num + del_par, i, cpy_num - del_par); }}/* Insert n_src'th key of buffer src before n_dest'th key of buffer dest. */static void internal_insert_key (struct buffer_info * dest_bi, int dest_position_before, /* insert key before key with n_dest number */ struct buffer_head * src, int src_position){ struct buffer_head * dest = dest_bi->bi_bh; int nr; struct block_head * blkh; struct key * key; RFALSE( dest == NULL || src == NULL, "source(%p) or dest(%p) buffer is 0", src, dest); RFALSE( dest_position_before < 0 || src_position < 0, "source(%d) or dest(%d) key number less than 0", src_position, dest_position_before); RFALSE( dest_position_before > B_NR_ITEMS (dest) || src_position >= B_NR_ITEMS(src), "invalid position in dest (%d (key number %d)) or in src (%d (key number %d))", dest_position_before, B_NR_ITEMS (dest), src_position, B_NR_ITEMS(src)); RFALSE( B_FREE_SPACE (dest) < KEY_SIZE, "no enough free space (%d) in dest buffer", B_FREE_SPACE (dest)); blkh = B_BLK_HEAD(dest); nr = blkh_nr_item(blkh); /* prepare space for inserting key */ key = B_N_PDELIM_KEY (dest, dest_position_before); memmove (key + 1, key, (nr - dest_position_before) * KEY_SIZE + (nr + 1) * DC_SIZE); /* insert key */ memcpy (key, B_N_PDELIM_KEY(src, src_position), KEY_SIZE); /* Change dirt, free space, item number fields. */ set_blkh_nr_item( blkh, blkh_nr_item(blkh) + 1 ); set_blkh_free_space( blkh, blkh_free_space(blkh) - KEY_SIZE ); do_balance_mark_internal_dirty (dest_bi->tb, dest, 0); if (dest_bi->bi_parent) { struct disk_child *t_dc; t_dc = B_N_CHILD(dest_bi->bi_parent,dest_bi->bi_position); put_dc_size( t_dc, dc_size(t_dc) + KEY_SIZE ); do_balance_mark_internal_dirty (dest_bi->tb, dest_bi->bi_parent,0); }}/* Insert d_key'th (delimiting) key from buffer cfl to tail of dest. * Copy pointer_amount node pointers and pointer_amount - 1 items from buffer src to buffer dest. * Replace d_key'th key in buffer cfl. * Delete pointer_amount items and node pointers from buffer src. *//* this can be invoked both to shift from S to L and from R to S */static void internal_shift_left ( int mode, /* INTERNAL_FROM_S_TO_L | INTERNAL_FROM_R_TO_S */ struct tree_balance * tb, int h, int pointer_amount ){ struct buffer_info dest_bi, src_bi; struct buffer_head * cf; int d_key_position; internal_define_dest_src_infos (mode, tb, h, &dest_bi, &src_bi, &d_key_position, &cf); /*printk("pointer_amount = %d\n",pointer_amount);*/ if (pointer_amount) { /* insert delimiting key from common father of dest and src to node dest into position B_NR_ITEM(dest) */ internal_insert_key (&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf, d_key_position); if (B_NR_ITEMS(src_bi.bi_bh) == pointer_amount - 1) { if (src_bi.bi_position/*src->b_item_order*/ == 0) replace_key (tb, cf, d_key_position, src_bi.bi_parent/*src->b_parent*/, 0); } else replace_key (tb, cf, d_key_position, src_bi.bi_bh, pointer_amount - 1); } /* last parameter is del_parameter */ internal_move_pointers_items (&dest_bi, &src_bi, FIRST_TO_LAST, pointer_amount, 0);}/* Insert delimiting key to L[h]. * Copy n node pointers and n - 1 items from buffer S[h] to L[h]. * Delete n - 1 items and node pointers from buffer S[h]. *//* it always shifts from S[h] to L[h] */static void internal_shift1_left ( struct tree_balance * tb, int h, int pointer_amount ){ struct buffer_info dest_bi, src_bi; struct buffer_head * cf; int d_key_position; internal_define_dest_src_infos (INTERNAL_SHIFT_FROM_S_TO_L, tb, h, &dest_bi, &src_bi, &d_key_position, &cf); if ( pointer_amount > 0 ) /* insert lkey[h]-th key from CFL[h] to left neighbor L[h] */ internal_insert_key (&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf, d_key_position); /* internal_insert_key (tb->L[h], B_NR_ITEM(tb->L[h]), tb->CFL[h], tb->lkey[h]);*/ /* last parameter is del_parameter */ internal_move_pointers_items (&dest_bi, &src_bi, FIRST_TO_LAST, pointer_amount, 1); /* internal_move_pointers_items (tb->L[h], tb->S[h], FIRST_TO_LAST, pointer_amount, 1);*/}/* Insert d_key'th (delimiting) key from buffer cfr to head of dest. * Copy n node pointers and n - 1 items from buffer src to buffer dest. * Replace d_key'th key in buffer cfr. * Delete n items and node pointers from buffer src. */static void internal_shift_right ( int mode, /* INTERNAL_FROM_S_TO_R | INTERNAL_FROM_L_TO_S */ struct tree_balance * tb, int h, int pointer_amount ){ struct buffer_info dest_bi, src_bi; struct buffer_head * cf; int d_key_position; int nr; internal_define_dest_src_infos (mode, tb, h, &dest_bi, &src_bi, &d_key_position, &cf); nr = B_NR_ITEMS (src_bi.bi_bh); if (pointer_amount > 0) { /* insert delimiting key from common father of dest and src to dest node into position 0 */ internal_insert_key (&dest_bi, 0, cf, d_key_position); if (nr == pointer_amount - 1) { RFALSE( src_bi.bi_bh != PATH_H_PBUFFER (tb->tb_path, h)/*tb->S[h]*/ || dest_bi.bi_bh != tb->R[h], "src (%p) must be == tb->S[h](%p) when it disappears", src_bi.bi_bh, PATH_H_PBUFFER (tb->tb_path, h)); /* when S[h] disappers replace left delemiting key as well */ if (tb->CFL[h]) replace_key (tb, cf, d_key_position, tb->CFL[h], tb->lkey[h]); } else replace_key (tb, cf, d_key_position, src_bi.bi_bh, nr - pointer_amount); } /* last parameter is del_parameter */ internal_move_pointers_items (&dest_bi, &src_bi, LAST_TO_FIRST, pointer_amount, 0);}/* Insert delimiting key to R[h]. * Copy n node pointers and n - 1 items from buffer S[h] to R[h]. * Delete n - 1 items and node pointers from buffer S[h]. *//* it always shift from S[h] to R[h] */static void internal_shift1_right ( struct tree_balance * tb, int h, int pointer_amount ){ struct buffer_info dest_bi, src_bi; struct buffer_head * cf; int d_key_position; internal_define_dest_src_infos (INTERNAL_SHIFT_FROM_S_TO_R, tb, h, &dest_bi, &src_bi, &d_key_position, &cf); if (pointer_amount > 0) /* insert rkey from CFR[h] to right neighbor R[h] */ internal_insert_key (&dest_bi, 0, cf, d_key_position); /* internal_insert_key (tb->R[h], 0, tb->CFR[h], tb->rkey[h]);*/ /* last parameter is del_parameter */ internal_move_pointers_items (&dest_bi, &src_bi, LAST_TO_FIRST, pointer_amount, 1); /* internal_move_pointers_items (tb->R[h], tb->S[h], LAST_TO_FIRST, pointer_amount, 1);*/}/* Delete insert_num node pointers together with their left items * and balance current node.*/static void balance_internal_when_delete (struct tree_balance * tb, int h, int child_pos){ int insert_num; int n; struct buffer_head * tbSh = PATH_H_PBUFFER (tb->tb_path, h); struct buffer_info bi; insert_num = tb->insert_size[h] / ((int)(DC_SIZE + KEY_SIZE)); /* delete child-node-pointer(s) together with their left item(s) */ bi.tb = tb; bi.bi_bh = tbSh; bi.bi_parent = PATH_H_PPARENT (tb->tb_path, h); bi.bi_position = PATH_H_POSITION (tb->tb_path, h + 1); internal_delete_childs (&bi, child_pos, -insert_num); RFALSE( tb->blknum[h] > 1, "tb->blknum[%d]=%d when insert_size < 0", h, tb->blknum[h]); n = B_NR_ITEMS(tbSh); if ( tb->lnum[h] == 0 && tb->rnum[h] == 0 ) { if ( tb->blknum[h] == 0 ) { /* node S[h] (root of the tree) is empty now */ struct buffer_head *new_root; RFALSE( n || B_FREE_SPACE (tbSh) != MAX_CHILD_SIZE(tbSh) - DC_SIZE, "buffer must have only 0 keys (%d)", n); RFALSE( bi.bi_parent, "root has parent (%p)", bi.bi_parent); /* choose a new root */ if ( ! tb->L[h-1] || ! B_NR_ITEMS(tb->L[h-1]) ) new_root = tb->R[h-1]; else new_root = tb->L[h-1]; /* switch super block's tree root block number to the new value */ PUT_SB_ROOT_BLOCK( tb->tb_sb, new_root->b_blocknr ); //tb->tb_sb->u.reiserfs_sb.s_rs->s_tree_height --; PUT_SB_TREE_HEIGHT( tb->tb_sb, SB_TREE_HEIGHT(tb->tb_sb) - 1 ); do_balance_mark_sb_dirty (tb, tb->tb_sb->u.reiserfs_sb.s_sbh, 1); /*&&&&&&&&&&&&&&&&&&&&&&*/ if (h > 1) /* use check_internal if new root is an internal node */ check_internal (new_root); /*&&&&&&&&&&&&&&&&&&&&&&*/ tb->tb_sb->s_dirt = 1; /* do what is needed for buffer thrown from tree */ reiserfs_invalidate_buffer(tb, tbSh); return; } return; } if ( tb->L[h] && tb->lnum[h] == -B_NR_ITEMS(tb->L[h]) - 1 ) { /* join S[h] with L[h] */ RFALSE( tb->rnum[h] != 0, "invalid tb->rnum[%d]==%d when joining S[h] with L[h]", h, tb->rnum[h]); internal_shift_left (INTERNAL_SHIFT_FROM_S_TO_L, tb, h, n + 1); reiserfs_invalidate_buffer(tb, tbSh); return; } if ( tb->R[h] && tb->rnum[h] == -B_NR_ITEMS(tb->R[h]) - 1 ) { /* join S[h] with R[h] */ RFALSE( tb->lnum[h] != 0, "invalid tb->lnum[%d]==%d when joining S[h] with R[h]", h, tb->lnum[h]); internal_shift_right (INTERNAL_SHIFT_FROM_S_TO_R, tb, h, n + 1); reiserfs_invalidate_buffer(tb,tbSh); return; } if ( tb->lnum[h] < 0 ) { /* borrow from left neighbor L[h] */ RFALSE( tb->rnum[h] != 0, "wrong tb->rnum[%d]==%d when borrow from L[h]", h, tb->rnum[h]); /*internal_shift_right (tb, h, tb->L[h], tb->CFL[h], tb->lkey[h], tb->S[h], -tb->lnum[h]);*/ internal_shift_right (INTERNAL_SHIFT_FROM_L_TO_S, tb, h, -tb->lnum[h]); return; } if ( tb->rnum[h] < 0 ) { /* borrow from right neighbor R[h] */ RFALSE( tb->lnum[h] != 0, "invalid tb->lnum[%d]==%d when borrow from R[h]", h, tb->lnum[h]); 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]);*/ return; } if ( tb->lnum[h] > 0 ) { /* split S[h] into two parts and put them into neighbors */ RFALSE( tb->rnum[h] == 0 || tb->lnum[h] + tb->rnum[h] != n + 1, "invalid tb->lnum[%d]==%d or tb->rnum[%d]==%d when S[h](item number == %d) is split between them", h, tb->lnum[h], h, tb->rnum[h], n); 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]);*/ internal_shift_right (INTERNAL_SHIFT_FROM_S_TO_R, tb, h, tb->rnum[h]); reiserfs_invalidate_buffer (tb, tbSh); return; } reiserfs_panic (tb->tb_sb, "balance_internal_when_delete: unexpected tb->lnum[%d]==%d or tb->rnum[%d]==%d", h, tb->lnum[h], h, tb->rnum[h]);}/* Replace delimiting key of buffers L[h] and S[h] by the given key.*/void replace_lkey ( struct tree_balance * tb, int h, struct item_head * key ){ RFALSE( tb->L[h] == NULL || tb->CFL[h] == NULL, "L[h](%p) and CFL[h](%p) must exist in replace_lkey",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -