Skip to content

Commit cc14600

Browse files
fdmananakdave
authored andcommitted
btrfs: make checksum item extension more efficient
When we want to add checksums into the checksums tree, or a log tree, we try whenever possible to extend existing checksum items, as this helps reduce amount of metadata space used, since adding a new item uses extra metadata space for a btrfs_item structure (25 bytes). However we have two inefficiencies in the current approach: 1) After finding a checksum item that covers a range with an end offset that matches the start offset of the checksum range we want to insert, we release the search path populated by btrfs_lookup_csum() and then do another COW search on tree with the goal of getting additional space for at least one checksum. Doing this path release and then searching again is a waste of time because very often the leaf already has enough free space for at least one more checksum; 2) After the COW search that guarantees we get free space in the leaf for at least one more checksum, we end up not doing the extension of the previous checksum item, and fallback to insertion of a new checksum item, if the leaf doesn't have an amount of free space larger then the space required for 2 checksums plus one btrfs_item structure - this is pointless for two reasons: a) We want to extend an existing item, so we don't need to account for a btrfs_item structure (25 bytes); b) We made the COW search with an insertion size for 1 single checksum, so if the leaf ends up with a free space amount smaller then 2 checksums plus the size of a btrfs_item structure, we give up on the extension of the existing item and jump to the 'insert' label, where we end up releasing the path and then doing yet another search to insert a new checksum item for a single checksum. Fix these inefficiencies by doing the following: - For case 1), before releasing the path just check if the leaf already has enough space for at least 1 more checksum, and if it does, jump directly to the item extension code, with releasing our current path, which was already COWed by btrfs_lookup_csum(); - For case 2), fix the logic so that for item extension we require only that the leaf has enough free space for 1 checksum, and not a minimum of 2 checksums plus space for a btrfs_item structure. Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent e289f03 commit cc14600

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

fs/btrfs/file-item.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,22 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
907907
}
908908

909909
/*
910-
* at this point, we know the tree has an item, but it isn't big
911-
* enough yet to put our csum in. Grow it
910+
* At this point, we know the tree has a checksum item that ends at an
911+
* offset matching the start of the checksum range we want to insert.
912+
* We try to extend that item as much as possible and then add as many
913+
* checksums to it as they fit.
914+
*
915+
* First check if the leaf has enough free space for at least one
916+
* checksum. If it has go directly to the item extension code, otherwise
917+
* release the path and do a search for insertion before the extension.
912918
*/
919+
if (btrfs_leaf_free_space(leaf) >= csum_size) {
920+
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
921+
csum_offset = (bytenr - found_key.offset) >>
922+
fs_info->sb->s_blocksize_bits;
923+
goto extend_csum;
924+
}
925+
913926
btrfs_release_path(path);
914927
ret = btrfs_search_slot(trans, root, &file_key, path,
915928
csum_size, 1);
@@ -933,19 +946,13 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
933946
goto insert;
934947
}
935948

949+
extend_csum:
936950
if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
937951
csum_size) {
938952
int extend_nr;
939953
u64 tmp;
940954
u32 diff;
941-
u32 free_space;
942-
943-
if (btrfs_leaf_free_space(leaf) <
944-
sizeof(struct btrfs_item) + csum_size * 2)
945-
goto insert;
946955

947-
free_space = btrfs_leaf_free_space(leaf) -
948-
sizeof(struct btrfs_item) - csum_size;
949956
tmp = sums->len - total_bytes;
950957
tmp >>= fs_info->sb->s_blocksize_bits;
951958
WARN_ON(tmp < 1);
@@ -956,7 +963,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
956963
MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
957964

958965
diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
959-
diff = min(free_space, diff);
966+
diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
960967
diff /= csum_size;
961968
diff *= csum_size;
962969

0 commit comments

Comments
 (0)