btrfs: some optimizations for send
cover btrfs: some optimizations for send
From: Filipe Manana <[email protected]>
The following are two optimizations for send, one to avoid sending unnecessary holes (writes full of zeros), which can waste a lot of space at the receiver and increase stream size for cases where sparse files are used, such as images of thin provisioned filesystems for example as recently reported by a user. The second is just a small optimization to avoid repeating a btree search. More details in the respective change logs.
Filipe Manana (2):
btrfs: send: don't issue unnecessary zero writes for trailing hole
btrfs: send: avoid duplicated search for last extent when sending hole
fs/btrfs/send.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/2 btrfs: send: don't issue unnecessary zero writes for trailing hole
From: Filipe Manana <[email protected]>
If we have a sparse file with a trailing hole (from the last extent's end to i_size) and then create an extent in the file that ends before the file's i_size, then when doing an incremental send we will issue a write full of zeroes for the range that starts immediately after the new extent ends up to i_size. While this isn't incorrect because the file ends up with exactly the same data, it unnecessarily results in using extra space at the destination with one or more extents full of zeroes instead of having a hole. In same cases this results in using megabytes or even gigabytes of unnecessary space.
Example, reproducer:
$ cat test.sh #!/bin/bash
DEV=/dev/sdh
MNT=/mnt/sdh
mkfs.btrfs -f $DEV
mount $DEV $MNT
# Create 1G sparse file.
xfs_io -f -c "truncate 1G" $MNT/foobar
# Create base snapshot.
btrfs subvolume snapshot -r $MNT $MNT/mysnap1
# Create send stream (full send) for the base snapshot. btrfs send -f /tmp/1.snap $MNT/mysnap1
# Now write one extent at the beginning of the file and one somewhere
# in the middle, leaving a gap between the end of this second extent
# and the file's size.
xfs_io -c "pwrite -S 0xab 0 128K" \
-c "pwrite -S 0xcd 512M 128K" \
$MNT/foobar# Now create a second snapshot which is going to be used for an
# incremental send operation. btrfs subvolume snapshot -r $MNT $MNT/mysnap2
# Create send stream (incremental send) for the second snapshot. btrfs send -p $MNT/mysnap1 -f /tmp/2.snap $MNT/mysnap2
# Now recreate the filesystem by receiving both send streams and
# verify we get the same content that the original filesystem had # and file foobar has only two extents with a size of 128K each. umount $MNT mkfs.btrfs -f $DEV mount $DEV $MNT
btrfs receive -f /tmp/1.snap $MNT
btrfs receive -f /tmp/2.snap $MNT
echo -e "\nFile fiemap in the second snapshot:" # Should have:
#
# 128K extent at file range [0, 128K[
# hole at file range [128K, 512M[ # 128K extent file range [512M, 512M + 128K[ # hole at file range [512M + 128K, 1G[ xfs_io -r -c "fiemap -v" $MNT/mysnap2/foobar
# File should be using 256K of data (two 128K extents). echo -e "\nSpace used by the file: $(du -h $MNT/mysnap2/foobar | cut -f 1)"
umount $MNT
Running the test, we can see with fiemap that we get an extent for the range [512M, 1G[, while in the source filesystem we have an extent for the range [512M, 512M + 128K[ and a hole for the rest of the file (the range [512M + 128K, 1G[):
$ ./test.sh
(...)
File fiemap in the second snapshot:
/mnt/sdh/mysnap2/foobar:
EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS
0: [0..255]: 26624..26879 256 0x0
1: [256..1048575]: hole 1048320
2: [1048576..2097151]: 2156544..3205119 1048576 0x1Space used by the file: 513M
This happens because once we finish processing an inode, at finish_inode_if_needed(), we always issue a hole (write operations full of zeros) if there's a gap between the end of the last processed extent and the file's size, even if that range is already a hole in the parent snapshot. Fix this by issuing the hole only if the range is not already a hole.
After this change, running the test above, we get the expected layout:
$ ./test.sh
(...)
File fiemap in the second snapshot:
/mnt/sdh/mysnap2/foobar:
EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS
0: [0..255]: 26624..26879 256 0x0
1: [256..1048575]: hole 1048320
2: [1048576..1048831]: 26880..27135 256 0x1
3: [1048832..2097151]: hole 1048320Space used by the file: 256K
A test case for fstests will follow soon.
--- fs/btrfs/send.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
fs/btrfs/send.c +13 -4
@@ -6723,11 +6723,20 @@ static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)if (ret) goto out; } -if (sctx->cur_inode_last_extent < - sctx->cur_inode_size) { - ret =send_hole (sctx, sctx->cur_inode_size); -if (ret) +if (sctx->cur_inode_last_extent < sctx->cur_inode_size) { + ret =range_is_hole_in_parent (sctx, + sctx->cur_inode_last_extent, + sctx->cur_inode_size); +if (ret <0 ) { goto out; + }else if (ret ==0 ) { + ret =send_hole (sctx, sctx->cur_inode_size); +if (ret <0 ) + goto out; + }else { +/* Range is already a hole, skip. */ + ret =0 ; + } } }if (need_truncate) {
Signature · 1 line
2.40.1
PATCH 2/2 btrfs: send: avoid duplicated search for last extent when sending hole
In reply to [email protected]From: Filipe Manana <[email protected]>
During an incremental send, before determining if we need to send a hole (write operations full of zeroes) we will search for the last extent's end offset if we are at the first slot of a leaf and the last processed extent's end offset is smaller then the current extent's start offset. However we are repeating this search in case we had the last extent's end offset undefined (set to the (u64)-1 value) when we entered maybe_send_hole(), wasting time.
So avoid this duplicated search by combining the two conditions that trigger a search for the last extent's end offset into a single if statement.
--- fs/btrfs/send.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
fs/btrfs/send.c +12 -15
@@ -6476,21 +6476,18 @@ static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,if (sctx->cur_ino != key->objectid || !need_send_hole (sctx))return 0 ; -if (sctx->cur_inode_last_extent == (u64)-1 ) { - ret =get_last_extent (sctx, key->offset -1 ); -if (ret) -return ret; - } - -if (path->slots[0 ] ==0 && - sctx->cur_inode_last_extent < key->offset) { -/* -* We might have skipped entire leafs that contained only -* file extent items for our current inode. These leafs have -* a generation number smaller (older) than the one in the -* current leaf and the leaf our last extent came from, and -* are located between these 2 leafs. -*/ +/* +* Get last extent's end offset (exclusive) if we haven't determined it +* yet (we're processing the first file extent item that is new), or if +* we're at the first slot of a leaf and the last extent's end is less +* than the current extent's offset, because we might have skipped +* entire leaves that contained only file extent items for our current +* inode. These leaves have a generation number smaller (older) than the +* one in the current leaf and the leaf our last extent came from, and +* are located between these 2 leaves. +*/ +if ((sctx->cur_inode_last_extent == (u64)-1 ) || + (path->slots[0 ] ==0 && sctx->cur_inode_last_extent < key->offset)) { ret =get_last_extent (sctx, key->offset -1 );if (ret)return ret;
Signature · 1 line
2.40.1
Quoted 9 lines · On 2/19/24 06:59, [email protected] wrote:
From: Filipe Manana <[email protected]>
The following are two optimizations for send, one to avoid sending unnecessary holes (writes full of zeros), which can waste a lot of space at the receiver and increase stream size for cases where sparse files are used, such as images of thin provisioned filesystems for example as recently reported by a user. The second is just a small optimization to avoid repeating a btree search. More details in the respective change logs.
Quoted 7 lines
Filipe Manana (2):
btrfs: send: don't issue unnecessary zero writes for trailing hole
btrfs: send: avoid duplicated search for last extent when sending holefs/btrfs/send.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-)
For both:
Quoted 9 lines · On Mon, Feb 19, 2024 at 11:59:29AM +0000, [email protected] wrote:
From: Filipe Manana <[email protected]>
The following are two optimizations for send, one to avoid sending unnecessary holes (writes full of zeros), which can waste a lot of space at the receiver and increase stream size for cases where sparse files are used, such as images of thin provisioned filesystems for example as recently reported by a user. The second is just a small optimization to avoid repeating a btree search. More details in the respective change logs.
Quoted 3 lines
Filipe Manana (2):
btrfs: send: don't issue unnecessary zero writes for trailing hole
btrfs: send: avoid duplicated search for last extent when sending hole
Quoted 9 lines · On Mon, Feb 19, 2024 at 11:59:29AM +0000, [email protected] wrote:
From: Filipe Manana <[email protected]>
The following are two optimizations for send, one to avoid sending unnecessary holes (writes full of zeros), which can waste a lot of space at the receiver and increase stream size for cases where sparse files are used, such as images of thin provisioned filesystems for example as recently reported by a user. The second is just a small optimization to avoid repeating a btree search. More details in the respective change logs.
Thanks,
Josef