diff options
author | Jeff Mahoney <jeffm@suse.com> | 2014-04-23 10:00:49 -0400 |
---|---|---|
committer | Jan Kara <jack@suse.cz> | 2014-05-07 12:34:44 -0400 |
commit | e80ef3d1488e3bfb8eb39b0643cfaffb25ed9814 (patch) | |
tree | 2705e283900a62763aa339ab9d71a16f20921b53 | |
parent | cf22df182bfce50670c25ce432e679e03aff3745 (diff) |
reiserfs: balance_leaf refactor, pull out balance_leaf_insert_right
This patch factors out a new balance_leaf_insert_right from the code in
balance_leaf responsible for inserting new items into the node to
the right of S[0] in the tree.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Jan Kara <jack@suse.cz>
-rw-r--r-- | fs/reiserfs/do_balan.c | 138 |
1 files changed, 75 insertions, 63 deletions
diff --git a/fs/reiserfs/do_balan.c b/fs/reiserfs/do_balan.c index f8fab372e32c..fc15e676a651 100644 --- a/fs/reiserfs/do_balan.c +++ b/fs/reiserfs/do_balan.c | |||
@@ -508,6 +508,80 @@ static void balance_leaf_paste_left(struct tree_balance *tb, | |||
508 | 508 | ||
509 | } | 509 | } |
510 | 510 | ||
511 | static void balance_leaf_insert_right(struct tree_balance *tb, | ||
512 | struct item_head *ih, const char *body) | ||
513 | { | ||
514 | |||
515 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | ||
516 | int n = B_NR_ITEMS(tbS0); | ||
517 | struct buffer_info bi; | ||
518 | int ret_val; | ||
519 | if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */ | ||
520 | if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */ | ||
521 | loff_t old_key_comp, old_len, r_zeroes_number; | ||
522 | const char *r_body; | ||
523 | int version; | ||
524 | loff_t offset; | ||
525 | |||
526 | leaf_shift_right(tb, tb->rnum[0] - 1, -1); | ||
527 | |||
528 | version = ih_version(ih); | ||
529 | /* Remember key component and item length */ | ||
530 | old_key_comp = le_ih_k_offset(ih); | ||
531 | old_len = ih_item_len(ih); | ||
532 | |||
533 | /* Calculate key component and item length to insert into R[0] */ | ||
534 | offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)); | ||
535 | set_le_ih_k_offset(ih, offset); | ||
536 | put_ih_item_len(ih, tb->rbytes); | ||
537 | /* Insert part of the item into R[0] */ | ||
538 | buffer_info_init_right(tb, &bi); | ||
539 | if ((old_len - tb->rbytes) > tb->zeroes_num) { | ||
540 | r_zeroes_number = 0; | ||
541 | r_body = body + (old_len - tb->rbytes) - tb->zeroes_num; | ||
542 | } else { | ||
543 | r_body = body; | ||
544 | r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes); | ||
545 | tb->zeroes_num -= r_zeroes_number; | ||
546 | } | ||
547 | |||
548 | leaf_insert_into_buf(&bi, 0, ih, r_body, | ||
549 | r_zeroes_number); | ||
550 | |||
551 | /* Replace right delimiting key by first key in R[0] */ | ||
552 | replace_key(tb, tb->CFR[0], tb->rkey[0], | ||
553 | tb->R[0], 0); | ||
554 | |||
555 | /* Calculate key component and item length to insert into S[0] */ | ||
556 | set_le_ih_k_offset(ih, old_key_comp); | ||
557 | put_ih_item_len(ih, old_len - tb->rbytes); | ||
558 | |||
559 | tb->insert_size[0] -= tb->rbytes; | ||
560 | |||
561 | } else { /* whole new item falls into R[0] */ | ||
562 | |||
563 | /* Shift rnum[0]-1 items to R[0] */ | ||
564 | ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); | ||
565 | /* Insert new item into R[0] */ | ||
566 | buffer_info_init_right(tb, &bi); | ||
567 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1, | ||
568 | ih, body, tb->zeroes_num); | ||
569 | |||
570 | if (tb->item_pos - n + tb->rnum[0] - 1 == 0) { | ||
571 | replace_key(tb, tb->CFR[0], | ||
572 | tb->rkey[0], | ||
573 | tb->R[0], 0); | ||
574 | |||
575 | } | ||
576 | tb->zeroes_num = tb->insert_size[0] = 0; | ||
577 | } | ||
578 | } else { /* new item or part of it doesn't fall into R[0] */ | ||
579 | |||
580 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); | ||
581 | } | ||
582 | |||
583 | } | ||
584 | |||
511 | /** | 585 | /** |
512 | * balance_leaf - reiserfs tree balancing algorithm | 586 | * balance_leaf - reiserfs tree balancing algorithm |
513 | * @tb: tree balance state | 587 | * @tb: tree balance state |
@@ -588,69 +662,7 @@ static int balance_leaf(struct tree_balance *tb, struct item_head *ih, | |||
588 | switch (flag) { | 662 | switch (flag) { |
589 | 663 | ||
590 | case M_INSERT: /* insert item */ | 664 | case M_INSERT: /* insert item */ |
591 | if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */ | 665 | balance_leaf_insert_right(tb, ih, body); |
592 | if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */ | ||
593 | loff_t old_key_comp, old_len, r_zeroes_number; | ||
594 | const char *r_body; | ||
595 | int version; | ||
596 | loff_t offset; | ||
597 | |||
598 | leaf_shift_right(tb, tb->rnum[0] - 1, -1); | ||
599 | |||
600 | version = ih_version(ih); | ||
601 | /* Remember key component and item length */ | ||
602 | old_key_comp = le_ih_k_offset(ih); | ||
603 | old_len = ih_item_len(ih); | ||
604 | |||
605 | /* Calculate key component and item length to insert into R[0] */ | ||
606 | offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)); | ||
607 | set_le_ih_k_offset(ih, offset); | ||
608 | put_ih_item_len(ih, tb->rbytes); | ||
609 | /* Insert part of the item into R[0] */ | ||
610 | buffer_info_init_right(tb, &bi); | ||
611 | if ((old_len - tb->rbytes) > tb->zeroes_num) { | ||
612 | r_zeroes_number = 0; | ||
613 | r_body = body + (old_len - tb->rbytes) - tb->zeroes_num; | ||
614 | } else { | ||
615 | r_body = body; | ||
616 | r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes); | ||
617 | tb->zeroes_num -= r_zeroes_number; | ||
618 | } | ||
619 | |||
620 | leaf_insert_into_buf(&bi, 0, ih, r_body, | ||
621 | r_zeroes_number); | ||
622 | |||
623 | /* Replace right delimiting key by first key in R[0] */ | ||
624 | replace_key(tb, tb->CFR[0], tb->rkey[0], | ||
625 | tb->R[0], 0); | ||
626 | |||
627 | /* Calculate key component and item length to insert into S[0] */ | ||
628 | set_le_ih_k_offset(ih, old_key_comp); | ||
629 | put_ih_item_len(ih, old_len - tb->rbytes); | ||
630 | |||
631 | tb->insert_size[0] -= tb->rbytes; | ||
632 | |||
633 | } else { /* whole new item falls into R[0] */ | ||
634 | |||
635 | /* Shift rnum[0]-1 items to R[0] */ | ||
636 | ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); | ||
637 | /* Insert new item into R[0] */ | ||
638 | buffer_info_init_right(tb, &bi); | ||
639 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1, | ||
640 | ih, body, tb->zeroes_num); | ||
641 | |||
642 | if (tb->item_pos - n + tb->rnum[0] - 1 == 0) { | ||
643 | replace_key(tb, tb->CFR[0], | ||
644 | tb->rkey[0], | ||
645 | tb->R[0], 0); | ||
646 | |||
647 | } | ||
648 | tb->zeroes_num = tb->insert_size[0] = 0; | ||
649 | } | ||
650 | } else { /* new item or part of it doesn't fall into R[0] */ | ||
651 | |||
652 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); | ||
653 | } | ||
654 | break; | 666 | break; |
655 | 667 | ||
656 | case M_PASTE: /* append item */ | 668 | case M_PASTE: /* append item */ |