37 #include <linux/time.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
58 static inline int old_item_num(
int new_num,
int affected_item_num,
int mode)
60 if (mode ==
M_PASTE || mode ==
M_CUT || new_num < affected_item_num)
66 "vs-8005: for INSERT mode and item number of inserted item");
72 "vs-8010: old_item_num: mode must be M_DELETE (mode = \'%c\'",
83 struct buffer_head *Sh;
116 for (new_num = 0; new_num < vn->
vn_nr_item; new_num++) {
140 "virtual node space consumed");
157 "vs-8040: item header of inserted item is not specified");
178 #ifdef CONFIG_REISERFS_CHECK
190 "rdkey %k, affected item==%d "
191 "(mode==%c) Must be %c",
203 static void check_left(
struct tree_balance *tb,
int h,
int cur_free)
210 RFALSE(cur_free < 0,
"vs-8050: cur_free (%d) < 0", cur_free);
228 "vs-8055: parent does not exist or invalid");
231 if ((
unsigned int)cur_free >=
237 "vs-8055: invalid mode or balance condition failed");
248 d_size = -((
int)IH_SIZE), ih_size = 0;
252 i++, ih_size =
IH_SIZE, d_size = 0, vi++) {
254 if (cur_free >= d_size) {
263 if (cur_free <= ih_size) {
283 static void check_right(
struct tree_balance *tb,
int h,
int cur_free)
290 RFALSE(cur_free < 0,
"vs-8070: cur_free < 0");
308 "vs-8075: parent does not exist or invalid");
311 if ((
unsigned int)cur_free >=
317 "vs-8080: invalid mode or balance condition failed");
328 d_size = -(
int)IH_SIZE, ih_size = 0;
332 i--, d_size = 0, ih_size = IH_SIZE, vi--) {
334 if (cur_free >= d_size) {
342 if (cur_free <= ih_size) {
366 static int get_num_ver(
int mode,
struct tree_balance *tb,
int h,
367 int from,
int from_bytes,
368 int to,
int to_bytes,
short *snum012,
int flow)
377 int total_node_size, max_node_size, current_item_size;
385 int split_item_positions[2];
389 split_item_positions[0] = -1;
390 split_item_positions[1] = -1;
397 "vs-8100: insert_size < 0 in overflow");
409 if (i == max_node_size)
411 return (i / max_node_size + 1);
417 cur_free = max_node_size;
422 start_bytes = ((from_bytes != -1) ? from_bytes : 0);
427 end_bytes = (to_bytes != -1) ? to_bytes : 0;
433 for (i = start_item; i <= end_item; i++) {
435 int skip_from_end = ((i == end_item) ? end_bytes : 0);
437 RFALSE(needed_nodes > 3,
"vs-8105: too many nodes are needed");
451 if (total_node_size + current_item_size <= max_node_size) {
452 snum012[needed_nodes - 1]++;
453 total_node_size += current_item_size;
458 if (current_item_size > max_node_size) {
463 "direct item length is %d. It can not be longer than %d",
464 current_item_size, max_node_size);
481 free_space = max_node_size - total_node_size -
IH_SIZE;
487 needed_nodes++, i--, total_node_size = 0;
496 start_bytes += units;
497 snum012[needed_nodes - 1 + 3] = units;
499 if (needed_nodes > 2)
501 "split_item_position is out of range");
502 snum012[needed_nodes - 1]++;
503 split_item_positions[needed_nodes - 1] =
i;
514 if (snum012[4] > 0) {
516 int bytes_to_r, bytes_to_l;
519 split_item_num = split_item_positions[1];
521 ((from == split_item_num
522 && from_bytes != -1) ? from_bytes : 0);
524 ((end_item == split_item_num
525 && end_bytes != -1) ? end_bytes : 0);
527 ((split_item_positions[0] ==
528 split_item_positions[1]) ? snum012[3] : 0);
533 bytes_to_r - bytes_to_l - bytes_to_S1new;
538 "not directory or indirect item");
542 if (snum012[3] > 0) {
544 int bytes_to_r, bytes_to_l;
547 split_item_num = split_item_positions[0];
549 ((from == split_item_num
550 && from_bytes != -1) ? from_bytes : 0);
552 ((end_item == split_item_num
553 && end_bytes != -1) ? end_bytes : 0);
555 ((split_item_positions[0] == split_item_positions[1]
556 && snum012[4] != -1) ? snum012[4] : 0);
561 bytes_to_r - bytes_to_l - bytes_to_S2new;
585 static void set_parameters(
struct tree_balance *tb,
int h,
int lnum,
586 int rnum,
int blk_num,
short *s012,
int lb,
int rb)
615 int to_left, to_right;
621 to_left = tb->
lnum[0] - ((tb->
lbytes != -1) ? 1 : 0);
622 to_right = tb->
rnum[0] - ((tb->
rbytes != -1) ? 1 : 0);
626 remain_items -= (to_left + to_right);
628 if (remain_items < 1) {
630 set_parameters(tb, 0, to_left, vn->
vn_nr_item - to_left, 0,
635 if (remain_items > 1 || tb->
lbytes == -1 || tb->
rbytes == -1)
645 set_parameters(tb, 0, to_left + 1, to_right + 1, 0,
NULL,
654 static int are_leaves_removable(
struct tree_balance *tb,
int lfree,
int rfree)
658 struct buffer_head *
S0;
675 "vs-8125: item number must be 1: it is %d",
683 if (is_direntry_le_ih(ih)) {
696 "vs-8130: first directory item can not be removed until directory is not empty");
702 set_parameters(tb, 0, -1, -1, -1,
NULL, -1, -1);
711 #define SET_PAR_SHIFT_LEFT \
716 to_l = (MAX_NR_KEY(Sh)+1 - lpar + vn->vn_nr_item + 1) / 2 -\
717 (MAX_NR_KEY(Sh) + 1 - lpar);\
719 set_parameters (tb, h, to_l, 0, lnver, NULL, -1, -1);\
723 if (lset==LEFT_SHIFT_FLOW)\
724 set_parameters (tb, h, lpar, 0, lnver, snum012+lset,\
727 set_parameters (tb, h, lpar - (tb->lbytes!=-1), 0, lnver, snum012+lset,\
731 #define SET_PAR_SHIFT_RIGHT \
736 to_r = (MAX_NR_KEY(Sh)+1 - rpar + vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 - rpar);\
738 set_parameters (tb, h, 0, to_r, rnver, NULL, -1, -1);\
742 if (rset==RIGHT_SHIFT_FLOW)\
743 set_parameters (tb, h, 0, rpar, rnver, snum012+rset,\
746 set_parameters (tb, h, 0, rpar - (tb->rbytes!=-1), rnver, snum012+rset,\
779 static int get_empty_nodes(
struct tree_balance *tb,
int h)
781 struct buffer_head *new_bh,
784 int counter, number_of_freeblk, amount_needed,
804 for (counter = 0, number_of_freeblk = tb->
cur_blknum;
805 counter < h; counter++)
812 amount_needed = (Sh) ? (tb->
blknum[h] - 1) : 1;
814 if (amount_needed > number_of_freeblk)
815 amount_needed -= number_of_freeblk;
820 if (reiserfs_new_form_blocknrs(tb, blocknrs,
825 for (blocknr = blocknrs, counter = 0;
826 counter < amount_needed; blocknr++, counter++) {
829 "PAP-8135: reiserfs_new_blocknrs failed when got new blocks");
831 new_bh = sb_getblk(sb, *blocknr);
832 RFALSE(buffer_dirty(new_bh) ||
833 buffer_journaled(new_bh) ||
834 buffer_journal_dirty(new_bh),
835 "PAP-8140: journaled or dirty buffer %b for the new block",
840 "PAP-8141: busy slot for new buffer");
842 set_buffer_journal_new(new_bh);
856 struct buffer_head *
l, *
f;
878 struct buffer_head *
r, *
f;
897 static int is_left_neighbor_in_cache(
struct tree_balance *tb,
int h)
899 struct buffer_head *father, *
left;
902 int left_neighbor_position;
914 !buffer_uptodate(father) ||
915 !buffer_uptodate(tb->
FL[h]),
916 "vs-8165: F[h] (%b) or FL[h] (%b) is invalid",
920 left_neighbor_position = (father == tb->
FL[
h]) ?
923 left_neighbor_blocknr =
926 if ((left = sb_find_get_block(sb, left_neighbor_blocknr))) {
929 "vs-8170: left neighbor (%b %z) is not in the tree",
938 #define LEFT_PARENTS 'l'
939 #define RIGHT_PARENTS 'r'
941 static void decrement_key(
struct cpu_key *key)
944 item_ops[cpu_key_k_type(key)]->decrement_key(key);
957 struct buffer_head **pfather,
958 struct buffer_head **pcom_father,
char c_lr_par)
960 struct buffer_head *
parent;
963 struct cpu_key s_lr_father_key;
966 first_last_position = 0,
972 counter = path_offset;
975 "PAP-8180: invalid path length");
995 if (position != first_last_position) {
996 *pcom_father = parent;
997 get_bh(*pcom_father);
1004 if (counter == FIRST_PATH_ELEMENT_OFFSET) {
1008 FIRST_PATH_ELEMENT_OFFSET)->b_blocknr ==
1010 *pfather = *pcom_father =
NULL;
1017 "PAP-8185: (%b %z) level too small",
1018 *pcom_father, *pcom_father);
1022 if (buffer_locked(*pcom_father)) {
1029 brelse(*pcom_father);
1048 decrement_key(&s_lr_father_key);
1051 (tb->
tb_sb, &s_lr_father_key, &s_path_to_neighbor_father,
1058 brelse(*pcom_father);
1065 "PAP-8190: (%b %z) level too small", *pfather, *pfather);
1066 RFALSE(s_path_to_neighbor_father.path_length <
1067 FIRST_PATH_ELEMENT_OFFSET,
"PAP-8192: path length is too small");
1069 s_path_to_neighbor_father.path_length--;
1087 struct buffer_head *curf, *curcf;
1090 if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
1112 tb->
lkey[
h] = position - 1;
1118 if ((ret = get_far_parent(tb, h + 1, &curf,
1131 "PAP-8195: FL (%b) or CFL (%b) is invalid", curf, curcf);
1141 get_far_parent(tb, h + 1, &curf, &curcf,
1150 tb->
rkey[
h] = position;
1163 "PAP-8205: FR (%b) or CFR (%b) is invalid", curf, curcf);
1170 static inline int can_node_be_removed(
int mode,
int lfree,
int sfree,
int rfree,
1192 if (sfree >= levbytes) {
1197 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1219 static int ip_check_balance(
struct tree_balance *tb,
int h)
1229 int lfree, sfree, rfree ;
1238 int nver, lnver, rnver, lrnver;
1248 short snum012[40] = { 0, };
1257 struct buffer_head *Sh;
1266 "S[0] can not be 0");
1267 switch (ret = get_empty_nodes(tb, h)) {
1269 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1277 "return value of get_empty_nodes");
1281 if ((ret = get_parents(tb, h)) !=
CARRY_ON)
1287 rfree = get_rfree(tb, h);
1288 lfree = get_lfree(tb, h);
1290 if (can_node_be_removed(vn->
vn_mode, lfree, sfree, rfree, tb, h) ==
1295 create_virtual_node(tb, h);
1302 check_left(tb, h, lfree);
1309 check_right(tb, h, rfree);
1335 "vs-8220: tree is not balanced on internal level");
1338 "vs-8225: tree is not balanced on leaf level");
1342 if (!h && is_leaf_removable(tb))
1349 if (sfree >= levbytes) {
1352 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1357 int lpar, rpar, nset, lset, rset, lrset;
1370 #define NOTHING_SHIFT_NO_FLOW 0
1371 #define NOTHING_SHIFT_FLOW 5
1372 #define LEFT_SHIFT_NO_FLOW 10
1373 #define LEFT_SHIFT_FLOW 15
1374 #define RIGHT_SHIFT_NO_FLOW 20
1375 #define RIGHT_SHIFT_FLOW 25
1376 #define LR_SHIFT_NO_FLOW 30
1377 #define LR_SHIFT_FLOW 35
1387 nver = get_num_ver(vn->
vn_mode, tb, h,
1395 nver1 = get_num_ver(vn->
vn_mode, tb, h,
1397 snum012 + NOTHING_SHIFT_FLOW, FLOW);
1409 lnver = get_num_ver(vn->
vn_mode, tb, h,
1410 lpar - ((h || tb->
lbytes == -1) ? 0 : 1),
1412 snum012 + LEFT_SHIFT_NO_FLOW, NO_FLOW);
1416 lnver1 = get_num_ver(vn->
vn_mode, tb, h,
1418 ((tb->
lbytes != -1) ? 1 : 0),
1420 snum012 + LEFT_SHIFT_FLOW, FLOW);
1432 rnver = get_num_ver(vn->
vn_mode, tb, h,
1439 snum012 + RIGHT_SHIFT_NO_FLOW, NO_FLOW);
1443 rnver1 = get_num_ver(vn->
vn_mode, tb, h,
1446 ((tb->
rbytes != -1) ? 1 : 0)),
1448 snum012 + RIGHT_SHIFT_FLOW, FLOW);
1460 lrnver = get_num_ver(vn->
vn_mode, tb, h,
1461 lpar - ((h || tb->
lbytes == -1) ? 0 : 1),
1468 snum012 + LR_SHIFT_NO_FLOW, NO_FLOW);
1472 lrnver1 = get_num_ver(vn->
vn_mode, tb, h,
1474 ((tb->
lbytes != -1) ? 1 : 0),
1477 ((tb->
rbytes != -1) ? 1 : 0)),
1479 snum012 + LR_SHIFT_FLOW, FLOW);
1480 if (lrnver > lrnver1)
1490 if (lrnver < lnver && lrnver < rnver) {
1492 (tb->
lnum[h] != 1 ||
1494 lrnver != 1 || rnver != 2 || lnver != 2
1495 || h != 1),
"vs-8230: bad h");
1496 if (lrset == LR_SHIFT_FLOW)
1497 set_parameters(tb, h, tb->
lnum[h], tb->
rnum[h],
1498 lrnver, snum012 + lrset,
1501 set_parameters(tb, h,
1503 ((tb->
lbytes == -1) ? 0 : 1),
1505 ((tb->
rbytes == -1) ? 0 : 1),
1506 lrnver, snum012 + lrset, -1, -1);
1512 if (nver == lrnver) {
1513 set_parameters(tb, h, 0, 0, nver, snum012 + nset, -1,
1522 if (lnver < rnver) {
1528 if (lnver > rnver) {
1535 if (is_left_neighbor_in_cache(tb, h)) {
1562 static int dc_check_balance_internal(
struct tree_balance *tb,
int h)
1568 struct buffer_head *Sh, *Fh;
1580 create_virtual_node(tb, h);
1584 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1590 set_parameters(tb, h, 0, 0, 0,
NULL, -1, -1);
1594 if ((ret = get_parents(tb, h)) !=
CARRY_ON)
1598 rfree = get_rfree(tb, h);
1599 lfree = get_lfree(tb, h);
1602 check_left(tb, h, lfree);
1603 check_right(tb, h, rfree);
1621 set_parameters(tb, h, -n - 1, 0, 0,
NULL, -1,
1638 set_parameters(tb, h, 0, -n - 1, 0,
NULL, -1,
1652 set_parameters(tb, h, vn->
vn_nr_item + 1 - to_r, to_r,
1658 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1665 if (is_left_neighbor_in_cache(tb, h)
1677 set_parameters(tb, h, -n - 1, 0, 0,
NULL, -1, -1);
1692 set_parameters(tb, h, 0, -n - 1, 0,
NULL, -1, -1);
1710 RFALSE(!tb->
FL[h] && !tb->
FR[h],
"vs-8235: trying to borrow for root");
1713 if (is_left_neighbor_in_cache(tb, h) || !tb->
FR[h]) {
1719 set_parameters(tb, h, -from_l, 0, 1,
NULL, -1, -1);
1723 set_parameters(tb, h, 0,
1742 static int dc_check_balance_leaf(
struct tree_balance *tb,
int h)
1755 struct buffer_head *
S0, *F0;
1768 "vs-8240: attempt to create empty buffer tree");
1770 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1774 if ((ret = get_parents(tb, h)) !=
CARRY_ON)
1778 rfree = get_rfree(tb, h);
1779 lfree = get_lfree(tb, h);
1781 create_virtual_node(tb, h);
1784 if (are_leaves_removable(tb, lfree, rfree))
1791 check_left(tb, h, lfree);
1792 check_right(tb, h, rfree);
1796 if (is_left_neighbor_in_cache(tb, h) || ((tb->
rnum[0] - ((tb->
rbytes == -1) ? 0 : 1)) < vn->
vn_nr_item) ||
1800 "vs-8245: dc_check_balance_leaf: FL[h] must exist");
1803 set_parameters(tb, h, -1, 0, 0,
NULL, -1, -1);
1809 set_parameters(tb, h, 0, -1, 0,
NULL, -1, -1);
1814 if (is_leaf_removable(tb))
1819 set_parameters(tb, h, 0, 0, 1,
NULL, -1, -1);
1836 static int dc_check_balance(
struct tree_balance *tb,
int h)
1839 "vs-8250: S is not initialized");
1842 return dc_check_balance_internal(tb, h);
1844 return dc_check_balance_leaf(tb, h);
1865 static int check_balance(
int mode,
1883 "vs-8255: ins_ih can not be 0 in insert mode");
1887 return ip_check_balance(tb, h);
1890 return dc_check_balance(tb, h);
1894 static int get_direct_parent(
struct tree_balance *tb,
int h)
1896 struct buffer_head *bh;
1902 if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
1904 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET - 1,
1905 "PAP-8260: invalid offset in the path");
1931 if (buffer_locked(bh)) {
1948 static int get_neighbors(
struct tree_balance *tb,
int h)
1952 unsigned long son_number;
1954 struct buffer_head *bh;
1965 "PAP-8270: invalid position in the parent");
1973 bh = sb_bread(sb, son_number);
1986 bh->b_blocknr,
"PAP-8275: invalid parent");
1992 "PAP-8290: invalid child size of left neighbor");
2007 "PAP-8295: invalid position in the parent");
2010 (bh == tb->
FR[
h]) ? tb->
rkey[h] + 1 : 0;
2013 bh = sb_bread(sb, son_number);
2029 "PAP-8300: invalid child size of right neighbor (%d != %d - %d)",
2037 static int get_virtual_node_size(
struct super_block *sb,
struct buffer_head *bh)
2039 int max_num_of_items;
2040 int max_num_of_entries;
2043 #define MIN_NAME_LEN 1
2052 (max_num_of_entries - 1) *
sizeof(
__u16));
2058 static int get_mem_for_virtual_node(
struct tree_balance *tb)
2085 free_buffers_in_tb(tb);
2104 #ifdef CONFIG_REISERFS_CHECK
2105 static void tb_buffer_sanity_check(
struct super_block *sb,
2106 struct buffer_head *bh,
2113 "reference counter for buffer %s[%d] "
2114 "(%b)", descr, level, bh);
2116 if (!buffer_uptodate(bh))
2118 "to date %s[%d] (%b)",
2123 "in tree %s[%d] (%b)",
2126 if (bh->b_bdev != sb->
s_bdev)
2128 "device %s[%d] (%b)",
2133 "blocksize %s[%d] (%b)",
2138 "number too high %s[%d] (%b)",
2143 static void tb_buffer_sanity_check(
struct super_block *sb,
2144 struct buffer_head *bh,
2145 const char *descr,
int level)
2150 static int clear_all_dirty_bits(
struct super_block *
s,
struct buffer_head *bh)
2155 static int wait_tb_buffers_until_unlocked(
struct tree_balance *tb)
2157 struct buffer_head *
locked;
2158 #ifdef CONFIG_REISERFS_CHECK
2159 int repeat_counter = 0;
2167 for (i = tb->
tb_path->path_length;
2173 #ifdef CONFIG_REISERFS_CHECK
2176 tb_buffer_sanity_check(tb->
tb_sb,
2183 if (!clear_all_dirty_bits(tb->
tb_sb,
2200 tb_buffer_sanity_check(tb->
tb_sb,
2203 if (!clear_all_dirty_bits
2208 if (!locked && tb->
FL[i]) {
2209 tb_buffer_sanity_check(tb->
tb_sb,
2212 if (!clear_all_dirty_bits
2217 if (!locked && tb->
CFL[i]) {
2218 tb_buffer_sanity_check(tb->
tb_sb,
2221 if (!clear_all_dirty_bits
2223 locked = tb->
CFL[
i];
2228 if (!locked && (tb->
rnum[i])) {
2231 tb_buffer_sanity_check(tb->
tb_sb,
2234 if (!clear_all_dirty_bits
2239 if (!locked && tb->
FR[i]) {
2240 tb_buffer_sanity_check(tb->
tb_sb,
2243 if (!clear_all_dirty_bits
2248 if (!locked && tb->
CFR[i]) {
2249 tb_buffer_sanity_check(tb->
tb_sb,
2252 if (!clear_all_dirty_bits
2254 locked = tb->
CFR[
i];
2268 if (!clear_all_dirty_bits
2270 locked = tb->
FEB[
i];
2275 #ifdef CONFIG_REISERFS_CHECK
2277 if ((repeat_counter % 10000) == 0) {
2279 "too many iterations waiting "
2280 "for buffer to unlock "
2331 struct item_head *ins_ih,
const void *data)
2339 int wait_tb_buffers_run = 0;
2342 ++REISERFS_SB(tb->
tb_sb)->s_fix_nodes;
2344 pos_in_item = tb->
tb_path->pos_in_item;
2361 if (buffer_locked(tbS0)) {
2368 #ifdef CONFIG_REISERFS_CHECK
2369 if (REISERFS_SB(tb->
tb_sb)->cur_tb) {
2372 "there is pending do_balance");
2377 "not uptodate at the beginning of fix_nodes "
2378 "or not in tree (mode %c)",
2379 tbS0, tbS0, op_mode);
2384 if (item_num <= 0 || item_num >
B_NR_ITEMS(tbS0))
2386 "item number %d (in S0 - %d) in case "
2387 "of insert", item_num,
2393 if (item_num < 0 || item_num >=
B_NR_ITEMS(tbS0)) {
2396 "item number(%d); mode = %c "
2414 ret = get_direct_parent(tb, h);
2418 ret = check_balance(op_mode, tb, h, item_num,
2419 pos_in_item, ins_ih, data);
2423 ret = get_neighbors(tb, h);
2434 ret = get_neighbors(tb, h);
2440 ret = get_empty_nodes(tb, h);
2449 "PAP-8350: creating new empty root");
2461 "PAP-8355: attempt to create too high of a tree");
2474 ret = wait_tb_buffers_until_unlocked(tb);
2477 wait_tb_buffers_run = 1;
2484 wait_tb_buffers_run = 1;
2498 if (wait_tb_buffers_run) {
2505 if (wait_tb_buffers_run) {
2537 if (wait_tb_buffers_run) {
2587 brelse(tb->
used[i]);