Skip to content

Commit 2f697dc

Browse files
Liu BoJosef Bacik
authored andcommitted
Btrfs: extend the checksum item as much as possible
For write, we also reserve some space for COW blocks during updating the checksum tree, and we calculate the number of blocks by checking if the number of bytes outstanding that are going to need csums needs one more block for csum. When we add these checksum into the checksum tree, we use ordered sums list. Every ordered sum contains csums for each sector, and we'll first try to look up an existing csum item, a) if we don't yet have a proper csum item, then we need to insert one, b) or if we find one but the csum item is not big enough, then we need to extend it. The point is we'll unlock the whole path and then insert or extend. So others can hack in and update the tree. Each insert or extend needs update the tree with COW on, and we may need to insert/extend for many times. That means what we've reserved for updating checksum tree is NOT enough indeed. The case is even more serious with having several write threads at the same time, it can end up eating our reserved space quickly and starting eating globle reserve pool instead. I don't yet come up with a way to calculate the worse case for updating csum, but extending the checksum item as much as possible can be helpful in my test. The idea behind is that it can reduce the times we insert/extend so that it saves us precious reserved space. Signed-off-by: Liu Bo <bo.li.liu@oracle.com> Signed-off-by: Josef Bacik <jbacik@fusionio.com>
1 parent de78b51 commit 2f697dc

1 file changed

Lines changed: 46 additions & 21 deletions

File tree

‎fs/btrfs/file-item.c‎

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,24 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
684684
return ret;
685685
}
686686

687+
static u64 btrfs_sector_sum_left(struct btrfs_ordered_sum *sums,
688+
struct btrfs_sector_sum *sector_sum,
689+
u64 total_bytes, u64 sectorsize)
690+
{
691+
u64 tmp = sectorsize;
692+
u64 next_sector = sector_sum->bytenr;
693+
struct btrfs_sector_sum *next = sector_sum + 1;
694+
695+
while ((tmp + total_bytes) < sums->len) {
696+
if (next_sector + sectorsize != next->bytenr)
697+
break;
698+
tmp += sectorsize;
699+
next_sector = next->bytenr;
700+
next++;
701+
}
702+
return tmp;
703+
}
704+
687705
int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
688706
struct btrfs_root *root,
689707
struct btrfs_ordered_sum *sums)
@@ -789,20 +807,32 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
789807
goto insert;
790808
}
791809

792-
if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
810+
if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
793811
csum_size) {
794-
u32 diff = (csum_offset + 1) * csum_size;
812+
int extend_nr;
813+
u64 tmp;
814+
u32 diff;
815+
u32 free_space;
795816

796-
/*
797-
* is the item big enough already? we dropped our lock
798-
* before and need to recheck
799-
*/
800-
if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
801-
goto csum;
817+
if (btrfs_leaf_free_space(root, leaf) <
818+
sizeof(struct btrfs_item) + csum_size * 2)
819+
goto insert;
820+
821+
free_space = btrfs_leaf_free_space(root, leaf) -
822+
sizeof(struct btrfs_item) - csum_size;
823+
tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes,
824+
root->sectorsize);
825+
tmp >>= root->fs_info->sb->s_blocksize_bits;
826+
WARN_ON(tmp < 1);
827+
828+
extend_nr = max_t(int, 1, (int)tmp);
829+
diff = (csum_offset + extend_nr) * csum_size;
830+
diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size);
802831

803832
diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
804-
if (diff != csum_size)
805-
goto insert;
833+
diff = min(free_space, diff);
834+
diff /= csum_size;
835+
diff *= csum_size;
806836

807837
btrfs_extend_item(trans, root, path, diff);
808838
goto csum;
@@ -812,19 +842,14 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
812842
btrfs_release_path(path);
813843
csum_offset = 0;
814844
if (found_next) {
815-
u64 tmp = total_bytes + root->sectorsize;
816-
u64 next_sector = sector_sum->bytenr;
817-
struct btrfs_sector_sum *next = sector_sum + 1;
845+
u64 tmp;
818846

819-
while (tmp < sums->len) {
820-
if (next_sector + root->sectorsize != next->bytenr)
821-
break;
822-
tmp += root->sectorsize;
823-
next_sector = next->bytenr;
824-
next++;
825-
}
826-
tmp = min(tmp, next_offset - file_key.offset);
847+
tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes,
848+
root->sectorsize);
827849
tmp >>= root->fs_info->sb->s_blocksize_bits;
850+
tmp = min(tmp, (next_offset - file_key.offset) >>
851+
root->fs_info->sb->s_blocksize_bits);
852+
828853
tmp = max((u64)1, tmp);
829854
tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
830855
ins_size = csum_size * tmp;

0 commit comments

Comments
 (0)