aboutsummaryrefslogtreecommitdiffstats
path: root/fs/reiserfs
diff options
context:
space:
mode:
authorJeff Mahoney <jeffm@suse.com>2014-04-23 10:00:46 -0400
committerJan Kara <jack@suse.cz>2014-05-07 12:31:06 -0400
commitb49fb112d4ae62ca1c549130a111fb39e85e2c93 (patch)
tree7560fef7e8a15e04994d83867b8bc409cc256d06 /fs/reiserfs
parent97fd4b97a947dc9d11dee1bbcceca7a124df9e4e (diff)
reiserfs: balance_leaf refactor, move state variables into tree_balance
This patch pushes the rest of the state variables in balance_leaf into the tree_balance structure so we can use them when we split balance_leaf into separate functions. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: Jan Kara <jack@suse.cz>
Diffstat (limited to 'fs/reiserfs')
-rw-r--r--fs/reiserfs/do_balan.c391
-rw-r--r--fs/reiserfs/fix_node.c9
-rw-r--r--fs/reiserfs/prints.c6
-rw-r--r--fs/reiserfs/reiserfs.h30
4 files changed, 214 insertions, 222 deletions
diff --git a/fs/reiserfs/do_balan.c b/fs/reiserfs/do_balan.c
index bbc282b7007c..6dbe55ff408e 100644
--- a/fs/reiserfs/do_balan.c
+++ b/fs/reiserfs/do_balan.c
@@ -310,27 +310,9 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
310 struct buffer_head **insert_ptr) 310 struct buffer_head **insert_ptr)
311{ 311{
312 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); 312 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
313 /* index into the array of item headers in S[0] of the affected item */
314 int item_pos = PATH_LAST_POSITION(tb->tb_path);
315 struct buffer_info bi; 313 struct buffer_info bi;
316 /* new nodes allocated to hold what could not fit into S */
317 struct buffer_head *S_new[2];
318 /*
319 * number of items that will be placed into S_new
320 * (includes partially shifted items)
321 */
322 int snum[2];
323 /*
324 * if an item is partially shifted into S_new then if it is a
325 * directory item it is the number of entries from the item that
326 * are shifted into S_new else it is the number of bytes from
327 * the item that are shifted into S_new
328 */
329 int sbytes[2];
330 int n, i; 314 int n, i;
331 int ret_val; 315 int ret_val;
332 int pos_in_item;
333 int zeros_num;
334 316
335 PROC_INFO_INC(tb->tb_sb, balance_at[0]); 317 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
336 318
@@ -338,29 +320,30 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
338 if (tb->insert_size[0] < 0) 320 if (tb->insert_size[0] < 0)
339 return balance_leaf_when_delete(tb, flag); 321 return balance_leaf_when_delete(tb, flag);
340 322
341 zeros_num = 0; 323 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
324 tb->pos_in_item = tb->tb_path->pos_in_item,
325 tb->zeroes_num = 0;
342 if (flag == M_INSERT && !body) 326 if (flag == M_INSERT && !body)
343 zeros_num = ih_item_len(ih); 327 tb->zeroes_num = ih_item_len(ih);
344 328
345 pos_in_item = tb->tb_path->pos_in_item;
346 /* 329 /*
347 * for indirect item pos_in_item is measured in unformatted node 330 * for indirect item pos_in_item is measured in unformatted node
348 * pointers. Recalculate to bytes 331 * pointers. Recalculate to bytes
349 */ 332 */
350 if (flag != M_INSERT 333 if (flag != M_INSERT
351 && is_indirect_le_ih(item_head(tbS0, item_pos))) 334 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
352 pos_in_item *= UNFM_P_SIZE; 335 tb->pos_in_item *= UNFM_P_SIZE;
353 336
354 if (tb->lnum[0] > 0) { 337 if (tb->lnum[0] > 0) {
355 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */ 338 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
356 if (item_pos < tb->lnum[0]) { 339 if (tb->item_pos < tb->lnum[0]) {
357 /* new item or it part falls to L[0], shift it too */ 340 /* new item or it part falls to L[0], shift it too */
358 n = B_NR_ITEMS(tb->L[0]); 341 n = B_NR_ITEMS(tb->L[0]);
359 342
360 switch (flag) { 343 switch (flag) {
361 case M_INSERT: /* insert item into L[0] */ 344 case M_INSERT: /* insert item into L[0] */
362 345
363 if (item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) { 346 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
364 /* part of new item falls into L[0] */ 347 /* part of new item falls into L[0] */
365 int new_item_len; 348 int new_item_len;
366 int version; 349 int version;
@@ -379,8 +362,8 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
379 /* Insert new item into L[0] */ 362 /* Insert new item into L[0] */
380 buffer_info_init_left(tb, &bi); 363 buffer_info_init_left(tb, &bi);
381 leaf_insert_into_buf(&bi, 364 leaf_insert_into_buf(&bi,
382 n + item_pos - ret_val, ih, body, 365 n + tb->item_pos - ret_val, ih, body,
383 zeros_num > ih_item_len(ih) ? ih_item_len(ih) : zeros_num); 366 tb->zeroes_num > ih_item_len(ih) ? ih_item_len(ih) : tb->zeroes_num);
384 367
385 version = ih_version(ih); 368 version = ih_version(ih);
386 369
@@ -389,11 +372,11 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
389 (tb-> lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0))); 372 (tb-> lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0)));
390 373
391 put_ih_item_len(ih, new_item_len); 374 put_ih_item_len(ih, new_item_len);
392 if (tb->lbytes > zeros_num) { 375 if (tb->lbytes > tb->zeroes_num) {
393 body += (tb->lbytes - zeros_num); 376 body += (tb->lbytes - tb->zeroes_num);
394 zeros_num = 0; 377 tb->zeroes_num = 0;
395 } else 378 } else
396 zeros_num -= tb->lbytes; 379 tb->zeroes_num -= tb->lbytes;
397 380
398 RFALSE(ih_item_len(ih) <= 0, 381 RFALSE(ih_item_len(ih) <= 0,
399 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d", 382 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
@@ -404,43 +387,43 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
404 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes); 387 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
405 /* Insert new item into L[0] */ 388 /* Insert new item into L[0] */
406 buffer_info_init_left(tb, &bi); 389 buffer_info_init_left(tb, &bi);
407 leaf_insert_into_buf(&bi, n + item_pos - ret_val, ih, body, zeros_num); 390 leaf_insert_into_buf(&bi, n + tb->item_pos - ret_val, ih, body, tb->zeroes_num);
408 tb->insert_size[0] = 0; 391 tb->insert_size[0] = 0;
409 zeros_num = 0; 392 tb->zeroes_num = 0;
410 } 393 }
411 break; 394 break;
412 395
413 case M_PASTE: /* append item in L[0] */ 396 case M_PASTE: /* append item in L[0] */
414 397
415 if (item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) { 398 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
416 /* we must shift the part of the appended item */ 399 /* we must shift the part of the appended item */
417 if (is_direntry_le_ih(item_head(tbS0, item_pos))) { 400 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
418 401
419 RFALSE(zeros_num, 402 RFALSE(tb->zeroes_num,
420 "PAP-12090: invalid parameter in case of a directory"); 403 "PAP-12090: invalid parameter in case of a directory");
421 /* directory item */ 404 /* directory item */
422 if (tb->lbytes > pos_in_item) { 405 if (tb->lbytes > tb->pos_in_item) {
423 /* new directory entry falls into L[0] */ 406 /* new directory entry falls into L[0] */
424 struct item_head *pasted; 407 struct item_head *pasted;
425 int l_pos_in_item = pos_in_item; 408 int l_pos_in_item = tb->pos_in_item;
426 409
427 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */ 410 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
428 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1); 411 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
429 if (ret_val && !item_pos) { 412 if (ret_val && !tb->item_pos) {
430 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1); 413 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
431 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1); 414 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
432 } 415 }
433 416
434 /* Append given directory entry to directory item */ 417 /* Append given directory entry to directory item */
435 buffer_info_init_left(tb, &bi); 418 buffer_info_init_left(tb, &bi);
436 leaf_paste_in_buffer(&bi, n + item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, zeros_num); 419 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, tb->zeroes_num);
437 420
438 /* previous string prepared space for pasting new entry, following string pastes this entry */ 421 /* previous string prepared space for pasting new entry, following string pastes this entry */
439 422
440 /* when we have merge directory item, pos_in_item has been changed too */ 423 /* when we have merge directory item, pos_in_item has been changed too */
441 424
442 /* paste new directory entry. 1 is entry number */ 425 /* paste new directory entry. 1 is entry number */
443 leaf_paste_entries(&bi, n + item_pos - ret_val, l_pos_in_item, 426 leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item,
444 1, (struct reiserfs_de_head *) body, 427 1, (struct reiserfs_de_head *) body,
445 body + DEH_SIZE, tb->insert_size[0]); 428 body + DEH_SIZE, tb->insert_size[0]);
446 tb->insert_size[0] = 0; 429 tb->insert_size[0] = 0;
@@ -450,20 +433,20 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
450 leaf_shift_left(tb, tb->lnum[0], tb->lbytes); 433 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
451 } 434 }
452 /* Calculate new position to append in item body */ 435 /* Calculate new position to append in item body */
453 pos_in_item -= tb->lbytes; 436 tb->pos_in_item -= tb->lbytes;
454 } else { 437 } else {
455 /* regular object */ 438 /* regular object */
456 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes); 439 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes);
457 RFALSE(pos_in_item != ih_item_len(item_head(tbS0, item_pos)), 440 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
458 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d", 441 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
459 ih_item_len(item_head(tbS0, item_pos)),pos_in_item); 442 ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
460 443
461 if (tb->lbytes >= pos_in_item) { 444 if (tb->lbytes >= tb->pos_in_item) {
462 /* appended item will be in L[0] in whole */ 445 /* appended item will be in L[0] in whole */
463 int l_n; 446 int l_n;
464 447
465 /* this bytes number must be appended to the last item of L[h] */ 448 /* this bytes number must be appended to the last item of L[h] */
466 l_n = tb->lbytes - pos_in_item; 449 l_n = tb->lbytes - tb->pos_in_item;
467 450
468 /* Calculate new insert_size[0] */ 451 /* Calculate new insert_size[0] */
469 tb->insert_size[0] -= l_n; 452 tb->insert_size[0] -= l_n;
@@ -472,14 +455,14 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
472 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d", 455 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
473 tb->insert_size[0]); 456 tb->insert_size[0]);
474 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len 457 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
475 (item_head(tbS0, item_pos))); 458 (item_head(tbS0, tb->item_pos)));
476 /* Append to body of item in L[0] */ 459 /* Append to body of item in L[0] */
477 buffer_info_init_left(tb, &bi); 460 buffer_info_init_left(tb, &bi);
478 leaf_paste_in_buffer 461 leaf_paste_in_buffer
479 (&bi, n + item_pos - ret_val, ih_item_len 462 (&bi, n + tb->item_pos - ret_val, ih_item_len
480 (item_head(tb->L[0], n + item_pos - ret_val)), 463 (item_head(tb->L[0], n + tb->item_pos - ret_val)),
481 l_n, body, 464 l_n, body,
482 zeros_num > l_n ? l_n : zeros_num); 465 tb->zeroes_num > l_n ? l_n : tb->zeroes_num);
483 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */ 466 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
484 { 467 {
485 int version; 468 int version;
@@ -488,9 +471,9 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
488 RFALSE(ih_item_len(item_head(tbS0, 0)), 471 RFALSE(ih_item_len(item_head(tbS0, 0)),
489 "PAP-12106: item length must be 0"); 472 "PAP-12106: item length must be 0");
490 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key 473 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
491 (tb->L[0], n + item_pos - ret_val)), 474 (tb->L[0], n + tb->item_pos - ret_val)),
492 "PAP-12107: items must be of the same file"); 475 "PAP-12107: items must be of the same file");
493 if (is_indirect_le_ih(item_head(tb->L[0], n + item_pos - ret_val))) { 476 if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) {
494 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT); 477 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
495 } 478 }
496 /* update key of first item in S0 */ 479 /* update key of first item in S0 */
@@ -503,12 +486,12 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
503 } 486 }
504 487
505 /* Calculate new body, position in item and insert_size[0] */ 488 /* Calculate new body, position in item and insert_size[0] */
506 if (l_n > zeros_num) { 489 if (l_n > tb->zeroes_num) {
507 body += (l_n - zeros_num); 490 body += (l_n - tb->zeroes_num);
508 zeros_num = 0; 491 tb->zeroes_num = 0;
509 } else 492 } else
510 zeros_num -= l_n; 493 tb->zeroes_num -= l_n;
511 pos_in_item = 0; 494 tb->pos_in_item = 0;
512 495
513 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1)) 496 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
514 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size) 497 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
@@ -517,9 +500,9 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
517 } else { /* only part of the appended item will be in L[0] */ 500 } else { /* only part of the appended item will be in L[0] */
518 501
519 /* Calculate position in item for append in S[0] */ 502 /* Calculate position in item for append in S[0] */
520 pos_in_item -= tb->lbytes; 503 tb->pos_in_item -= tb->lbytes;
521 504
522 RFALSE(pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", pos_in_item); 505 RFALSE(tb->pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", tb->pos_in_item);
523 506
524 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */ 507 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
525 leaf_shift_left(tb, tb->lnum[0], tb->lbytes); 508 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
@@ -529,29 +512,29 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
529 512
530 struct item_head *pasted; 513 struct item_head *pasted;
531 514
532 if (!item_pos && op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) { /* if we paste into first item of S[0] and it is left mergable */ 515 if (!tb->item_pos && op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) { /* if we paste into first item of S[0] and it is left mergable */
533 /* then increment pos_in_item by the size of the last item in L[0] */ 516 /* then increment pos_in_item by the size of the last item in L[0] */
534 pasted = item_head(tb->L[0], n - 1); 517 pasted = item_head(tb->L[0], n - 1);
535 if (is_direntry_le_ih(pasted)) 518 if (is_direntry_le_ih(pasted))
536 pos_in_item += ih_entry_count(pasted); 519 tb->pos_in_item += ih_entry_count(pasted);
537 else 520 else
538 pos_in_item += ih_item_len(pasted); 521 tb->pos_in_item += ih_item_len(pasted);
539 } 522 }
540 523
541 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */ 524 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
542 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes); 525 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
543 /* Append to body of item in L[0] */ 526 /* Append to body of item in L[0] */
544 buffer_info_init_left(tb, &bi); 527 buffer_info_init_left(tb, &bi);
545 leaf_paste_in_buffer(&bi, n + item_pos - ret_val, 528 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val,
546 pos_in_item, 529 tb->pos_in_item,
547 tb->insert_size[0], 530 tb->insert_size[0],
548 body, zeros_num); 531 body, tb->zeroes_num);
549 532
550 /* if appended item is directory, paste entry */ 533 /* if appended item is directory, paste entry */
551 pasted = item_head(tb->L[0], n + item_pos - ret_val); 534 pasted = item_head(tb->L[0], n + tb->item_pos - ret_val);
552 if (is_direntry_le_ih(pasted)) 535 if (is_direntry_le_ih(pasted))
553 leaf_paste_entries(&bi, n + item_pos - ret_val, 536 leaf_paste_entries(&bi, n + tb->item_pos - ret_val,
554 pos_in_item, 1, 537 tb->pos_in_item, 1,
555 (struct reiserfs_de_head *) body, 538 (struct reiserfs_de_head *) body,
556 body + DEH_SIZE, 539 body + DEH_SIZE,
557 tb->insert_size[0]); 540 tb->insert_size[0]);
@@ -559,7 +542,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
559 if (is_indirect_le_ih(pasted)) 542 if (is_indirect_le_ih(pasted))
560 set_ih_free_space(pasted, 0); 543 set_ih_free_space(pasted, 0);
561 tb->insert_size[0] = 0; 544 tb->insert_size[0] = 0;
562 zeros_num = 0; 545 tb->zeroes_num = 0;
563 } 546 }
564 break; 547 break;
565 default: /* cases d and t */ 548 default: /* cases d and t */
@@ -576,7 +559,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
576 559
577 /* tb->lnum[0] > 0 */ 560 /* tb->lnum[0] > 0 */
578 /* Calculate new item position */ 561 /* Calculate new item position */
579 item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0)); 562 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
580 563
581 if (tb->rnum[0] > 0) { 564 if (tb->rnum[0] > 0) {
582 /* shift rnum[0] items from S[0] to the right neighbor R[0] */ 565 /* shift rnum[0] items from S[0] to the right neighbor R[0] */
@@ -584,9 +567,9 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
584 switch (flag) { 567 switch (flag) {
585 568
586 case M_INSERT: /* insert item */ 569 case M_INSERT: /* insert item */
587 if (n - tb->rnum[0] < item_pos) { /* new item or its part falls to R[0] */ 570 if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */
588 if (item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */ 571 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
589 loff_t old_key_comp, old_len, r_zeros_number; 572 loff_t old_key_comp, old_len, r_zeroes_number;
590 const char *r_body; 573 const char *r_body;
591 int version; 574 int version;
592 loff_t offset; 575 loff_t offset;
@@ -604,17 +587,17 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
604 put_ih_item_len(ih, tb->rbytes); 587 put_ih_item_len(ih, tb->rbytes);
605 /* Insert part of the item into R[0] */ 588 /* Insert part of the item into R[0] */
606 buffer_info_init_right(tb, &bi); 589 buffer_info_init_right(tb, &bi);
607 if ((old_len - tb->rbytes) > zeros_num) { 590 if ((old_len - tb->rbytes) > tb->zeroes_num) {
608 r_zeros_number = 0; 591 r_zeroes_number = 0;
609 r_body = body + (old_len - tb->rbytes) - zeros_num; 592 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
610 } else { 593 } else {
611 r_body = body; 594 r_body = body;
612 r_zeros_number = zeros_num - (old_len - tb->rbytes); 595 r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes);
613 zeros_num -= r_zeros_number; 596 tb->zeroes_num -= r_zeroes_number;
614 } 597 }
615 598
616 leaf_insert_into_buf(&bi, 0, ih, r_body, 599 leaf_insert_into_buf(&bi, 0, ih, r_body,
617 r_zeros_number); 600 r_zeroes_number);
618 601
619 /* Replace right delimiting key by first key in R[0] */ 602 /* Replace right delimiting key by first key in R[0] */
620 replace_key(tb, tb->CFR[0], tb->rkey[0], 603 replace_key(tb, tb->CFR[0], tb->rkey[0],
@@ -632,16 +615,16 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
632 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); 615 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
633 /* Insert new item into R[0] */ 616 /* Insert new item into R[0] */
634 buffer_info_init_right(tb, &bi); 617 buffer_info_init_right(tb, &bi);
635 leaf_insert_into_buf(&bi, item_pos - n + tb->rnum[0] - 1, 618 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
636 ih, body, zeros_num); 619 ih, body, tb->zeroes_num);
637 620
638 if (item_pos - n + tb->rnum[0] - 1 == 0) { 621 if (tb->item_pos - n + tb->rnum[0] - 1 == 0) {
639 replace_key(tb, tb->CFR[0], 622 replace_key(tb, tb->CFR[0],
640 tb->rkey[0], 623 tb->rkey[0],
641 tb->R[0], 0); 624 tb->R[0], 0);
642 625
643 } 626 }
644 zeros_num = tb->insert_size[0] = 0; 627 tb->zeroes_num = tb->insert_size[0] = 0;
645 } 628 }
646 } else { /* new item or part of it doesn't fall into R[0] */ 629 } else { /* new item or part of it doesn't fall into R[0] */
647 630
@@ -651,17 +634,17 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
651 634
652 case M_PASTE: /* append item */ 635 case M_PASTE: /* append item */
653 636
654 if (n - tb->rnum[0] <= item_pos) { /* pasted item or part of it falls to R[0] */ 637 if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */
655 if (item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */ 638 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
656 if (is_direntry_le_ih(item_head(tbS0, item_pos))) { /* we append to directory item */ 639 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */
657 int entry_count; 640 int entry_count;
658 641
659 RFALSE(zeros_num, 642 RFALSE(tb->zeroes_num,
660 "PAP-12145: invalid parameter in case of a directory"); 643 "PAP-12145: invalid parameter in case of a directory");
661 entry_count = ih_entry_count(item_head 644 entry_count = ih_entry_count(item_head
662 (tbS0, item_pos)); 645 (tbS0, tb->item_pos));
663 if (entry_count - tb->rbytes < 646 if (entry_count - tb->rbytes <
664 pos_in_item) 647 tb->pos_in_item)
665 /* new directory entry falls into R[0] */ 648 /* new directory entry falls into R[0] */
666 { 649 {
667 int paste_entry_position; 650 int paste_entry_position;
@@ -672,9 +655,9 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
672 /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */ 655 /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
673 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1); 656 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
674 /* Paste given directory entry to directory item */ 657 /* Paste given directory entry to directory item */
675 paste_entry_position = pos_in_item - entry_count + tb->rbytes - 1; 658 paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1;
676 buffer_info_init_right(tb, &bi); 659 buffer_info_init_right(tb, &bi);
677 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, zeros_num); 660 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num);
678 /* paste entry */ 661 /* paste entry */
679 leaf_paste_entries(&bi, 0, paste_entry_position, 1, 662 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
680 (struct reiserfs_de_head *) body, 663 (struct reiserfs_de_head *) body,
@@ -686,25 +669,25 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
686 } 669 }
687 670
688 tb->insert_size[0] = 0; 671 tb->insert_size[0] = 0;
689 pos_in_item++; 672 tb->pos_in_item++;
690 } else { /* new directory entry doesn't fall into R[0] */ 673 } else { /* new directory entry doesn't fall into R[0] */
691 674
692 leaf_shift_right(tb, tb->rnum[0], tb->rbytes); 675 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
693 } 676 }
694 } else { /* regular object */ 677 } else { /* regular object */
695 678
696 int n_shift, n_rem, r_zeros_number; 679 int n_shift, n_rem, r_zeroes_number;
697 const char *r_body; 680 const char *r_body;
698 681
699 /* Calculate number of bytes which must be shifted from appended item */ 682 /* Calculate number of bytes which must be shifted from appended item */
700 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0) 683 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
701 n_shift = 0; 684 n_shift = 0;
702 685
703 RFALSE(pos_in_item != ih_item_len 686 RFALSE(tb->pos_in_item != ih_item_len
704 (item_head(tbS0, item_pos)), 687 (item_head(tbS0, tb->item_pos)),
705 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d", 688 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
706 pos_in_item, ih_item_len 689 tb->pos_in_item, ih_item_len
707 (item_head(tbS0, item_pos))); 690 (item_head(tbS0, tb->item_pos)));
708 691
709 leaf_shift_right(tb, tb->rnum[0], n_shift); 692 leaf_shift_right(tb, tb->rnum[0], n_shift);
710 /* Calculate number of bytes which must remain in body after appending to R[0] */ 693 /* Calculate number of bytes which must remain in body after appending to R[0] */
@@ -730,18 +713,18 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
730 713
731 /* Append part of body into R[0] */ 714 /* Append part of body into R[0] */
732 buffer_info_init_right(tb, &bi); 715 buffer_info_init_right(tb, &bi);
733 if (n_rem > zeros_num) { 716 if (n_rem > tb->zeroes_num) {
734 r_zeros_number = 0; 717 r_zeroes_number = 0;
735 r_body = body + n_rem - zeros_num; 718 r_body = body + n_rem - tb->zeroes_num;
736 } else { 719 } else {
737 r_body = body; 720 r_body = body;
738 r_zeros_number = zeros_num - n_rem; 721 r_zeroes_number = tb->zeroes_num - n_rem;
739 zeros_num -= r_zeros_number; 722 tb->zeroes_num -= r_zeroes_number;
740 } 723 }
741 724
742 leaf_paste_in_buffer(&bi, 0, n_shift, 725 leaf_paste_in_buffer(&bi, 0, n_shift,
743 tb->insert_size[0] - n_rem, 726 tb->insert_size[0] - n_rem,
744 r_body, r_zeros_number); 727 r_body, r_zeroes_number);
745 728
746 if (is_indirect_le_ih(item_head(tb->R[0], 0))) { 729 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
747#if 0 730#if 0
@@ -752,7 +735,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
752 } 735 }
753 tb->insert_size[0] = n_rem; 736 tb->insert_size[0] = n_rem;
754 if (!n_rem) 737 if (!n_rem)
755 pos_in_item++; 738 tb->pos_in_item++;
756 } 739 }
757 } else { /* pasted item in whole falls into R[0] */ 740 } else { /* pasted item in whole falls into R[0] */
758 741
@@ -760,22 +743,22 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
760 743
761 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes); 744 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
762 /* append item in R[0] */ 745 /* append item in R[0] */
763 if (pos_in_item >= 0) { 746 if (tb->pos_in_item >= 0) {
764 buffer_info_init_right(tb, &bi); 747 buffer_info_init_right(tb, &bi);
765 leaf_paste_in_buffer(&bi, item_pos - n + tb->rnum[0], pos_in_item, 748 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item,
766 tb->insert_size[0], body, zeros_num); 749 tb->insert_size[0], body, tb->zeroes_num);
767 } 750 }
768 751
769 /* paste new entry, if item is directory item */ 752 /* paste new entry, if item is directory item */
770 pasted = item_head(tb->R[0], item_pos - n + tb->rnum[0]); 753 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
771 if (is_direntry_le_ih(pasted) && pos_in_item >= 0) { 754 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
772 leaf_paste_entries(&bi, item_pos - n + tb->rnum[0], 755 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
773 pos_in_item, 1, 756 tb->pos_in_item, 1,
774 (struct reiserfs_de_head *) body, 757 (struct reiserfs_de_head *) body,
775 body + DEH_SIZE, tb->insert_size[0]); 758 body + DEH_SIZE, tb->insert_size[0]);
776 if (!pos_in_item) { 759 if (!tb->pos_in_item) {
777 760
778 RFALSE(item_pos - n + tb->rnum[0], 761 RFALSE(tb->item_pos - n + tb->rnum[0],
779 "PAP-12165: directory item must be first item of node when pasting is in 0th position"); 762 "PAP-12165: directory item must be first item of node when pasting is in 0th position");
780 763
781 /* update delimiting keys */ 764 /* update delimiting keys */
@@ -785,7 +768,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
785 768
786 if (is_indirect_le_ih(pasted)) 769 if (is_indirect_le_ih(pasted))
787 set_ih_free_space(pasted, 0); 770 set_ih_free_space(pasted, 0);
788 zeros_num = tb->insert_size[0] = 0; 771 tb->zeroes_num = tb->insert_size[0] = 0;
789 } 772 }
790 } else { /* new item doesn't fall into R[0] */ 773 } else { /* new item doesn't fall into R[0] */
791 774
@@ -834,39 +817,34 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
834 } 817 }
835 818
836 /* Fill new nodes that appear in place of S[0] */ 819 /* Fill new nodes that appear in place of S[0] */
837
838 /* I am told that this copying is because we need an array to enable
839 the looping code. -Hans */
840 snum[0] = tb->s1num, snum[1] = tb->s2num;
841 sbytes[0] = tb->s1bytes;
842 sbytes[1] = tb->s2bytes;
843 for (i = tb->blknum[0] - 2; i >= 0; i--) { 820 for (i = tb->blknum[0] - 2; i >= 0; i--) {
844 821
845 RFALSE(!snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i, 822 RFALSE(!tb->snum[i],
846 snum[i]); 823 "PAP-12200: snum[%d] == %d. Must be > 0", i,
824 tb->snum[i]);
847 825
848 /* here we shift from S to S_new nodes */ 826 /* here we shift from S to S_new nodes */
849 827
850 S_new[i] = get_FEB(tb); 828 tb->S_new[i] = get_FEB(tb);
851 829
852 /* initialized block type and tree level */ 830 /* initialized block type and tree level */
853 set_blkh_level(B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL); 831 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
854 832
855 n = B_NR_ITEMS(tbS0); 833 n = B_NR_ITEMS(tbS0);
856 834
857 switch (flag) { 835 switch (flag) {
858 case M_INSERT: /* insert item */ 836 case M_INSERT: /* insert item */
859 837
860 if (n - snum[i] < item_pos) { /* new item or it's part falls to first new node S_new[i] */ 838 if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */
861 if (item_pos == n - snum[i] + 1 && sbytes[i] != -1) { /* part of new item falls into S_new[i] */ 839 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */
862 int old_key_comp, old_len, r_zeros_number; 840 int old_key_comp, old_len, r_zeroes_number;
863 const char *r_body; 841 const char *r_body;
864 int version; 842 int version;
865 843
866 /* Move snum[i]-1 items from S[0] to S_new[i] */ 844 /* Move snum[i]-1 items from S[0] to S_new[i] */
867 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, 845 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
868 snum[i] - 1, -1, 846 tb->snum[i] - 1, -1,
869 S_new[i]); 847 tb->S_new[i]);
870 /* Remember key component and item length */ 848 /* Remember key component and item length */
871 version = ih_version(ih); 849 version = ih_version(ih);
872 old_key_comp = le_ih_k_offset(ih); 850 old_key_comp = le_ih_k_offset(ih);
@@ -874,59 +852,59 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
874 852
875 /* Calculate key component and item length to insert into S_new[i] */ 853 /* Calculate key component and item length to insert into S_new[i] */
876 set_le_ih_k_offset(ih, le_ih_k_offset(ih) + 854 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
877 ((old_len - sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0))); 855 ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
878 856
879 put_ih_item_len(ih, sbytes[i]); 857 put_ih_item_len(ih, tb->sbytes[i]);
880 858
881 /* Insert part of the item into S_new[i] before 0-th item */ 859 /* Insert part of the item into S_new[i] before 0-th item */
882 buffer_info_init_bh(tb, &bi, S_new[i]); 860 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
883 861
884 if ((old_len - sbytes[i]) > zeros_num) { 862 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
885 r_zeros_number = 0; 863 r_zeroes_number = 0;
886 r_body = body + (old_len - sbytes[i]) - zeros_num; 864 r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num;
887 } else { 865 } else {
888 r_body = body; 866 r_body = body;
889 r_zeros_number = zeros_num - (old_len - sbytes[i]); 867 r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]);
890 zeros_num -= r_zeros_number; 868 tb->zeroes_num -= r_zeroes_number;
891 } 869 }
892 870
893 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeros_number); 871 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
894 872
895 /* Calculate key component and item length to insert into S[i] */ 873 /* Calculate key component and item length to insert into S[i] */
896 set_le_ih_k_offset(ih, old_key_comp); 874 set_le_ih_k_offset(ih, old_key_comp);
897 put_ih_item_len(ih, old_len - sbytes[i]); 875 put_ih_item_len(ih, old_len - tb->sbytes[i]);
898 tb->insert_size[0] -= sbytes[i]; 876 tb->insert_size[0] -= tb->sbytes[i];
899 } else { /* whole new item falls into S_new[i] */ 877 } else { /* whole new item falls into S_new[i] */
900 878
901 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */ 879 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
902 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, 880 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
903 snum[i] - 1, sbytes[i], S_new[i]); 881 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
904 882
905 /* Insert new item into S_new[i] */ 883 /* Insert new item into S_new[i] */
906 buffer_info_init_bh(tb, &bi, S_new[i]); 884 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
907 leaf_insert_into_buf(&bi, item_pos - n + snum[i] - 1, 885 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
908 ih, body, zeros_num); 886 ih, body, tb->zeroes_num);
909 887
910 zeros_num = tb->insert_size[0] = 0; 888 tb->zeroes_num = tb->insert_size[0] = 0;
911 } 889 }
912 } 890 }
913 891
914 else { /* new item or it part don't falls into S_new[i] */ 892 else { /* new item or it part don't falls into S_new[i] */
915 893
916 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, 894 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
917 snum[i], sbytes[i], S_new[i]); 895 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
918 } 896 }
919 break; 897 break;
920 898
921 case M_PASTE: /* append item */ 899 case M_PASTE: /* append item */
922 900
923 if (n - snum[i] <= item_pos) { /* pasted item or part if it falls to S_new[i] */ 901 if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */
924 if (item_pos == n - snum[i] && sbytes[i] != -1) { /* we must shift part of the appended item */ 902 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */
925 struct item_head *aux_ih; 903 struct item_head *aux_ih;
926 904
927 RFALSE(ih, "PAP-12210: ih must be 0"); 905 RFALSE(ih, "PAP-12210: ih must be 0");
928 906
929 aux_ih = item_head(tbS0, item_pos); 907 aux_ih = item_head(tbS0, tb->item_pos);
930 if (is_direntry_le_ih(aux_ih)) { 908 if (is_direntry_le_ih(aux_ih)) {
931 /* we append to directory item */ 909 /* we append to directory item */
932 910
@@ -934,65 +912,65 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
934 912
935 entry_count = ih_entry_count(aux_ih); 913 entry_count = ih_entry_count(aux_ih);
936 914
937 if (entry_count - sbytes[i] < pos_in_item && pos_in_item <= entry_count) { 915 if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) {
938 /* new directory entry falls into S_new[i] */ 916 /* new directory entry falls into S_new[i] */
939 917
940 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0"); 918 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
941 RFALSE(sbytes[i] - 1 >= entry_count, 919 RFALSE(tb->sbytes[i] - 1 >= entry_count,
942 "PAP-12220: there are no so much entries (%d), only %d", 920 "PAP-12220: there are no so much entries (%d), only %d",
943 sbytes[i] - 1, entry_count); 921 tb->sbytes[i] - 1, entry_count);
944 922
945 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */ 923 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
946 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, snum[i], sbytes[i] - 1, S_new[i]); 924 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]);
947 /* Paste given directory entry to directory item */ 925 /* Paste given directory entry to directory item */
948 buffer_info_init_bh(tb, &bi, S_new[i]); 926 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
949 leaf_paste_in_buffer(&bi, 0, pos_in_item - entry_count + sbytes[i] - 1, 927 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1,
950 tb->insert_size[0], body, zeros_num); 928 tb->insert_size[0], body, tb->zeroes_num);
951 /* paste new directory entry */ 929 /* paste new directory entry */
952 leaf_paste_entries(&bi, 0, pos_in_item - entry_count + sbytes[i] - 1, 1, 930 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1,
953 (struct reiserfs_de_head *) body, 931 (struct reiserfs_de_head *) body,
954 body + DEH_SIZE, tb->insert_size[0]); 932 body + DEH_SIZE, tb->insert_size[0]);
955 tb->insert_size[0] = 0; 933 tb->insert_size[0] = 0;
956 pos_in_item++; 934 tb->pos_in_item++;
957 } else { /* new directory entry doesn't fall into S_new[i] */ 935 } else { /* new directory entry doesn't fall into S_new[i] */
958 leaf_move_items(LEAF_FROM_S_TO_SNEW,tb, snum[i], sbytes[i], S_new[i]); 936 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]);
959 } 937 }
960 } else { /* regular object */ 938 } else { /* regular object */
961 939
962 int n_shift, n_rem, r_zeros_number; 940 int n_shift, n_rem, r_zeroes_number;
963 const char *r_body; 941 const char *r_body;
964 942
965 RFALSE(pos_in_item != ih_item_len(item_head(tbS0, item_pos)) || tb->insert_size[0] <= 0, 943 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0,
966 "PAP-12225: item too short or insert_size <= 0"); 944 "PAP-12225: item too short or insert_size <= 0");
967 945
968 /* Calculate number of bytes which must be shifted from appended item */ 946 /* Calculate number of bytes which must be shifted from appended item */
969 n_shift = sbytes[i] - tb->insert_size[0]; 947 n_shift = tb->sbytes[i] - tb->insert_size[0];
970 if (n_shift < 0) 948 if (n_shift < 0)
971 n_shift = 0; 949 n_shift = 0;
972 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, snum[i], n_shift, S_new[i]); 950 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]);
973 951
974 /* Calculate number of bytes which must remain in body after append to S_new[i] */ 952 /* Calculate number of bytes which must remain in body after append to S_new[i] */
975 n_rem = tb->insert_size[0] - sbytes[i]; 953 n_rem = tb->insert_size[0] - tb->sbytes[i];
976 if (n_rem < 0) 954 if (n_rem < 0)
977 n_rem = 0; 955 n_rem = 0;
978 /* Append part of body into S_new[0] */ 956 /* Append part of body into S_new[0] */
979 buffer_info_init_bh(tb, &bi, S_new[i]); 957 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
980 if (n_rem > zeros_num) { 958 if (n_rem > tb->zeroes_num) {
981 r_zeros_number = 0; 959 r_zeroes_number = 0;
982 r_body = body + n_rem - zeros_num; 960 r_body = body + n_rem - tb->zeroes_num;
983 } else { 961 } else {
984 r_body = body; 962 r_body = body;
985 r_zeros_number = zeros_num - n_rem; 963 r_zeroes_number = tb->zeroes_num - n_rem;
986 zeros_num -= r_zeros_number; 964 tb->zeroes_num -= r_zeroes_number;
987 } 965 }
988 966
989 leaf_paste_in_buffer(&bi, 0, n_shift, 967 leaf_paste_in_buffer(&bi, 0, n_shift,
990 tb->insert_size[0] - n_rem, 968 tb->insert_size[0] - n_rem,
991 r_body, r_zeros_number); 969 r_body, r_zeroes_number);
992 { 970 {
993 struct item_head *tmp; 971 struct item_head *tmp;
994 972
995 tmp = item_head(S_new[i], 0); 973 tmp = item_head(tb->S_new[i], 0);
996 if (is_indirect_le_ih 974 if (is_indirect_le_ih
997 (tmp)) { 975 (tmp)) {
998 set_ih_free_space(tmp, 0); 976 set_ih_free_space(tmp, 0);
@@ -1004,7 +982,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1004 982
1005 tb->insert_size[0] = n_rem; 983 tb->insert_size[0] = n_rem;
1006 if (!n_rem) 984 if (!n_rem)
1007 pos_in_item++; 985 tb->pos_in_item++;
1008 } 986 }
1009 } else 987 } else
1010 /* item falls wholly into S_new[i] */ 988 /* item falls wholly into S_new[i] */
@@ -1013,10 +991,10 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1013 struct item_head *pasted; 991 struct item_head *pasted;
1014 992
1015#ifdef CONFIG_REISERFS_CHECK 993#ifdef CONFIG_REISERFS_CHECK
1016 struct item_head *ih_check = item_head(tbS0, item_pos); 994 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
1017 995
1018 if (!is_direntry_le_ih(ih_check) 996 if (!is_direntry_le_ih(ih_check)
1019 && (pos_in_item != ih_item_len(ih_check) 997 && (tb->pos_in_item != ih_item_len(ih_check)
1020 || tb->insert_size[0] <= 0)) 998 || tb->insert_size[0] <= 0))
1021 reiserfs_panic(tb->tb_sb, 999 reiserfs_panic(tb->tb_sb,
1022 "PAP-12235", 1000 "PAP-12235",
@@ -1026,27 +1004,27 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1026#endif /* CONFIG_REISERFS_CHECK */ 1004#endif /* CONFIG_REISERFS_CHECK */
1027 1005
1028 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, 1006 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
1029 tb, snum[i], 1007 tb, tb->snum[i],
1030 sbytes[i], 1008 tb->sbytes[i],
1031 S_new[i]); 1009 tb->S_new[i]);
1032 1010
1033 RFALSE(leaf_mi, 1011 RFALSE(leaf_mi,
1034 "PAP-12240: unexpected value returned by leaf_move_items (%d)", 1012 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
1035 leaf_mi); 1013 leaf_mi);
1036 1014
1037 /* paste into item */ 1015 /* paste into item */
1038 buffer_info_init_bh(tb, &bi, S_new[i]); 1016 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1039 leaf_paste_in_buffer(&bi, 1017 leaf_paste_in_buffer(&bi,
1040 item_pos - n + snum[i], 1018 tb->item_pos - n + tb->snum[i],
1041 pos_in_item, 1019 tb->pos_in_item,
1042 tb->insert_size[0], 1020 tb->insert_size[0],
1043 body, zeros_num); 1021 body, tb->zeroes_num);
1044 1022
1045 pasted = item_head(S_new[i], item_pos - n + snum[i]); 1023 pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]);
1046 if (is_direntry_le_ih(pasted)) { 1024 if (is_direntry_le_ih(pasted)) {
1047 leaf_paste_entries(&bi, 1025 leaf_paste_entries(&bi,
1048 item_pos - n + snum[i], 1026 tb->item_pos - n + tb->snum[i],
1049 pos_in_item, 1, 1027 tb->pos_in_item, 1,
1050 (struct reiserfs_de_head *)body, 1028 (struct reiserfs_de_head *)body,
1051 body + DEH_SIZE, 1029 body + DEH_SIZE,
1052 tb->insert_size[0] 1030 tb->insert_size[0]
@@ -1056,14 +1034,14 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1056 /* if we paste to indirect item update ih_free_space */ 1034 /* if we paste to indirect item update ih_free_space */
1057 if (is_indirect_le_ih(pasted)) 1035 if (is_indirect_le_ih(pasted))
1058 set_ih_free_space(pasted, 0); 1036 set_ih_free_space(pasted, 0);
1059 zeros_num = tb->insert_size[0] = 0; 1037 tb->zeroes_num = tb->insert_size[0] = 0;
1060 } 1038 }
1061 } 1039 }
1062 1040
1063 else { /* pasted item doesn't fall into S_new[i] */ 1041 else { /* pasted item doesn't fall into S_new[i] */
1064 1042
1065 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, 1043 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1066 snum[i], sbytes[i], S_new[i]); 1044 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
1067 } 1045 }
1068 break; 1046 break;
1069 default: /* cases d and t */ 1047 default: /* cases d and t */
@@ -1072,27 +1050,28 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1072 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag); 1050 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
1073 } 1051 }
1074 1052
1075 memcpy(insert_key + i, leaf_key(S_new[i], 0), KEY_SIZE); 1053 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1076 insert_ptr[i] = S_new[i]; 1054 insert_ptr[i] = tb->S_new[i];
1077 1055
1078 RFALSE(!buffer_journaled(S_new[i]) 1056 RFALSE(!buffer_journaled(tb->S_new[i])
1079 || buffer_journal_dirty(S_new[i]) 1057 || buffer_journal_dirty(tb->S_new[i])
1080 || buffer_dirty(S_new[i]), "PAP-12247: S_new[%d] : (%b)", 1058 || buffer_dirty(tb->S_new[i]),
1081 i, S_new[i]); 1059 "PAP-12247: S_new[%d] : (%b)",
1060 i, tb->S_new[i]);
1082 } 1061 }
1083 1062
1084 /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the 1063 /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
1085 affected item which remains in S */ 1064 affected item which remains in S */
1086 if (0 <= item_pos && item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */ 1065 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */
1087 1066
1088 switch (flag) { 1067 switch (flag) {
1089 case M_INSERT: /* insert item into S[0] */ 1068 case M_INSERT: /* insert item into S[0] */
1090 buffer_info_init_tbS0(tb, &bi); 1069 buffer_info_init_tbS0(tb, &bi);
1091 leaf_insert_into_buf(&bi, item_pos, ih, body, 1070 leaf_insert_into_buf(&bi, tb->item_pos, ih,
1092 zeros_num); 1071 body, tb->zeroes_num);
1093 1072
1094 /* If we insert the first key change the delimiting key */ 1073 /* If we insert the first key change the delimiting key */
1095 if (item_pos == 0) { 1074 if (tb->item_pos == 0) {
1096 if (tb->CFL[0]) /* can be 0 in reiserfsck */ 1075 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1097 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); 1076 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1098 } 1077 }
@@ -1101,26 +1080,26 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1101 case M_PASTE:{ /* append item in S[0] */ 1080 case M_PASTE:{ /* append item in S[0] */
1102 struct item_head *pasted; 1081 struct item_head *pasted;
1103 1082
1104 pasted = item_head(tbS0, item_pos); 1083 pasted = item_head(tbS0, tb->item_pos);
1105 /* when directory, may be new entry already pasted */ 1084 /* when directory, may be new entry already pasted */
1106 if (is_direntry_le_ih(pasted)) { 1085 if (is_direntry_le_ih(pasted)) {
1107 if (pos_in_item >= 0 && pos_in_item <= ih_entry_count(pasted)) { 1086 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1108 1087
1109 RFALSE(!tb->insert_size[0], 1088 RFALSE(!tb->insert_size[0],
1110 "PAP-12260: insert_size is 0 already"); 1089 "PAP-12260: insert_size is 0 already");
1111 1090
1112 /* prepare space */ 1091 /* prepare space */
1113 buffer_info_init_tbS0(tb, &bi); 1092 buffer_info_init_tbS0(tb, &bi);
1114 leaf_paste_in_buffer(&bi, item_pos, pos_in_item, 1093 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1115 tb->insert_size[0], body, 1094 tb->insert_size[0], body,
1116 zeros_num); 1095 tb->zeroes_num);
1117 1096
1118 /* paste entry */ 1097 /* paste entry */
1119 leaf_paste_entries(&bi, item_pos, pos_in_item, 1, 1098 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1120 (struct reiserfs_de_head *)body, 1099 (struct reiserfs_de_head *)body,
1121 body + DEH_SIZE, 1100 body + DEH_SIZE,
1122 tb->insert_size[0]); 1101 tb->insert_size[0]);
1123 if (!item_pos && !pos_in_item) { 1102 if (!tb->item_pos && !tb->pos_in_item) {
1124 RFALSE(!tb->CFL[0] || !tb->L[0], 1103 RFALSE(!tb->CFL[0] || !tb->L[0],
1125 "PAP-12270: CFL[0]/L[0] must be specified"); 1104 "PAP-12270: CFL[0]/L[0] must be specified");
1126 if (tb->CFL[0]) 1105 if (tb->CFL[0])
@@ -1129,14 +1108,14 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1129 tb->insert_size[0] = 0; 1108 tb->insert_size[0] = 0;
1130 } 1109 }
1131 } else { /* regular object */ 1110 } else { /* regular object */
1132 if (pos_in_item == ih_item_len(pasted)) { 1111 if (tb->pos_in_item == ih_item_len(pasted)) {
1133 1112
1134 RFALSE(tb->insert_size[0] <= 0, 1113 RFALSE(tb->insert_size[0] <= 0,
1135 "PAP-12275: insert size must not be %d", 1114 "PAP-12275: insert size must not be %d",
1136 tb->insert_size[0]); 1115 tb->insert_size[0]);
1137 buffer_info_init_tbS0(tb, &bi); 1116 buffer_info_init_tbS0(tb, &bi);
1138 leaf_paste_in_buffer(&bi, item_pos, pos_in_item, 1117 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1139 tb->insert_size[0], body, zeros_num); 1118 tb->insert_size[0], body, tb->zeroes_num);
1140 1119
1141 if (is_indirect_le_ih(pasted)) { 1120 if (is_indirect_le_ih(pasted)) {
1142#if 0 1121#if 0
diff --git a/fs/reiserfs/fix_node.c b/fs/reiserfs/fix_node.c
index 46b014623fd6..6b0ddb2a9091 100644
--- a/fs/reiserfs/fix_node.c
+++ b/fs/reiserfs/fix_node.c
@@ -631,10 +631,11 @@ static void set_parameters(struct tree_balance *tb, int h, int lnum,
631 /* only for leaf level */ 631 /* only for leaf level */
632 if (h == 0) { 632 if (h == 0) {
633 if (s012 != NULL) { 633 if (s012 != NULL) {
634 tb->s0num = *s012++, 634 tb->s0num = *s012++;
635 tb->s1num = *s012++, tb->s2num = *s012++; 635 tb->snum[0] = *s012++;
636 tb->s1bytes = *s012++; 636 tb->snum[1] = *s012++;
637 tb->s2bytes = *s012; 637 tb->sbytes[0] = *s012++;
638 tb->sbytes[1] = *s012;
638 } 639 }
639 tb->lbytes = lb; 640 tb->lbytes = lb;
640 tb->rbytes = rb; 641 tb->rbytes = rb;
diff --git a/fs/reiserfs/prints.c b/fs/reiserfs/prints.c
index c534db47df9f..c9b47e91baf8 100644
--- a/fs/reiserfs/prints.c
+++ b/fs/reiserfs/prints.c
@@ -673,9 +673,9 @@ void store_print_tb(struct tree_balance *tb)
673 "* h * size * ln * lb * rn * rb * blkn * s0 * s1 * s1b * s2 * s2b * curb * lk * rk *\n" 673 "* h * size * ln * lb * rn * rb * blkn * s0 * s1 * s1b * s2 * s2b * curb * lk * rk *\n"
674 "* 0 * %4d * %2d * %2d * %2d * %2d * %4d * %2d * %2d * %3d * %2d * %3d * %4d * %2d * %2d *\n", 674 "* 0 * %4d * %2d * %2d * %2d * %2d * %4d * %2d * %2d * %3d * %2d * %3d * %4d * %2d * %2d *\n",
675 tb->insert_size[0], tb->lnum[0], tb->lbytes, tb->rnum[0], 675 tb->insert_size[0], tb->lnum[0], tb->lbytes, tb->rnum[0],
676 tb->rbytes, tb->blknum[0], tb->s0num, tb->s1num, tb->s1bytes, 676 tb->rbytes, tb->blknum[0], tb->s0num, tb->snum[0],
677 tb->s2num, tb->s2bytes, tb->cur_blknum, tb->lkey[0], 677 tb->sbytes[0], tb->snum[1], tb->sbytes[1],
678 tb->rkey[0]); 678 tb->cur_blknum, tb->lkey[0], tb->rkey[0]);
679 679
680 /* this prints balance parameters for non-leaf levels */ 680 /* this prints balance parameters for non-leaf levels */
681 h = 0; 681 h = 0;
diff --git a/fs/reiserfs/reiserfs.h b/fs/reiserfs/reiserfs.h
index 40bb5cebab65..bf53888c7f59 100644
--- a/fs/reiserfs/reiserfs.h
+++ b/fs/reiserfs/reiserfs.h
@@ -2471,12 +2471,6 @@ struct tree_balance {
2471 /* number of items that fall into left most node when S[0] splits */ 2471 /* number of items that fall into left most node when S[0] splits */
2472 int s0num; 2472 int s0num;
2473 2473
2474 /* number of items that fall into first new node when S[0] splits */
2475 int s1num;
2476
2477 /* number of items that fall into second new node when S[0] splits */
2478 int s2num;
2479
2480 /* 2474 /*
2481 * number of bytes which can flow to the left neighbor from the left 2475 * number of bytes which can flow to the left neighbor from the left
2482 * most liquid item that cannot be shifted from S[0] entirely 2476 * most liquid item that cannot be shifted from S[0] entirely
@@ -2491,12 +2485,30 @@ struct tree_balance {
2491 */ 2485 */
2492 int rbytes; 2486 int rbytes;
2493 2487
2488
2489 /*
2490 * index into the array of item headers in
2491 * S[0] of the affected item
2492 */
2493 int item_pos;
2494
2495 /* new nodes allocated to hold what could not fit into S */
2496 struct buffer_head *S_new[2];
2497
2498 /*
2499 * number of items that will be placed into nodes in S_new
2500 * when S[0] splits
2501 */
2502 int snum[2];
2503
2494 /* 2504 /*
2495 * number of bytes which flow to the first new node when S[0] splits 2505 * number of bytes which flow to nodes in S_new when S[0] splits
2496 * note: if S[0] splits into 3 nodes, then items do not need to be cut 2506 * note: if S[0] splits into 3 nodes, then items do not need to be cut
2497 */ 2507 */
2498 int s1bytes; 2508 int sbytes[2];
2499 int s2bytes; 2509
2510 int pos_in_item;
2511 int zeroes_num;
2500 2512
2501 /* 2513 /*
2502 * buffers which are to be freed after do_balance finishes 2514 * buffers which are to be freed after do_balance finishes