Skip to content

Commit b4b872a

Browse files
fdmananakdave
authored andcommitted
btrfs: send: use fallocate for hole punching with send stream v2
Currently holes are sent as writes full of zeroes, which results in unnecessarily using disk space at the receiving end and increasing the stream size. In some cases we avoid sending writes of zeroes, like during a full send operation where we just skip writes for holes. But for some cases we fill previous holes with writes of zeroes too, like in this scenario: 1) We have a file with a hole in the range [2M, 3M), we snapshot the subvolume and do a full send. The range [2M, 3M) stays as a hole at the receiver since we skip sending write commands full of zeroes; 2) We punch a hole for the range [3M, 4M) in our file, so that now it has a 2M hole in the range [2M, 4M), and snapshot the subvolume. Now if we do an incremental send, we will send write commands full of zeroes for the range [2M, 4M), removing the hole for [2M, 3M) at the receiver. We could improve cases such as this last one by doing additional comparisons of file extent items (or their absence) between the parent and send snapshots, but that's a lot of code to add plus additional CPU and IO costs. Since the send stream v2 already has a fallocate command and btrfs-progs implements a callback to execute fallocate since the send stream v2 support was added to it, update the kernel to use fallocate for punching holes for V2+ streams. Test coverage is provided by btrfs/284 which is a version of btrfs/007 that exercises send stream v2 instead of v1, using fsstress with random operations and fssum to verify file contents. Link: kdave/btrfs-progs#1001 Reviewed-by: Boris Burkov <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 6cbfbf3 commit b4b872a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

fs/btrfs/send.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/bsearch.h>
7+
#include <linux/falloc.h>
78
#include <linux/fs.h>
89
#include <linux/file.h>
910
#include <linux/sort.h>
@@ -5405,13 +5406,45 @@ static int send_update_extent(struct send_ctx *sctx,
54055406
return ret;
54065407
}
54075408

5409+
static int send_fallocate(struct send_ctx *sctx, u32 mode, u64 offset, u64 len)
5410+
{
5411+
struct fs_path *path;
5412+
int ret;
5413+
5414+
path = get_cur_inode_path(sctx);
5415+
if (IS_ERR(path))
5416+
return PTR_ERR(path);
5417+
5418+
ret = begin_cmd(sctx, BTRFS_SEND_C_FALLOCATE);
5419+
if (ret < 0)
5420+
return ret;
5421+
5422+
TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
5423+
TLV_PUT_U32(sctx, BTRFS_SEND_A_FALLOCATE_MODE, mode);
5424+
TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5425+
TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5426+
5427+
ret = send_cmd(sctx);
5428+
5429+
tlv_put_failure:
5430+
return ret;
5431+
}
5432+
54085433
static int send_hole(struct send_ctx *sctx, u64 end)
54095434
{
54105435
struct fs_path *p = NULL;
54115436
u64 read_size = max_send_read_size(sctx);
54125437
u64 offset = sctx->cur_inode_last_extent;
54135438
int ret = 0;
54145439

5440+
/*
5441+
* Starting with send stream v2 we have fallocate and can use it to
5442+
* punch holes instead of sending writes full of zeroes.
5443+
*/
5444+
if (proto_cmd_ok(sctx, BTRFS_SEND_C_FALLOCATE))
5445+
return send_fallocate(sctx, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
5446+
offset, end - offset);
5447+
54155448
/*
54165449
* A hole that starts at EOF or beyond it. Since we do not yet support
54175450
* fallocate (for extent preallocation and hole punching), sending a

0 commit comments

Comments
 (0)