btrfs: send: avoid copying file data
authorOmar Sandoval <osandov@fb.com>
Fri, 21 Aug 2020 07:39:52 +0000 (00:39 -0700)
committerDavid Sterba <dsterba@suse.com>
Wed, 7 Oct 2020 10:13:17 +0000 (12:13 +0200)
send_write() currently copies from the page cache to sctx->read_buf, and
then from sctx->read_buf to sctx->send_buf. Similarly, send_hole()
zeroes sctx->read_buf and then copies from sctx->read_buf to
sctx->send_buf. However, if we write the TLV header manually, we can
copy to sctx->send_buf directly and get rid of sctx->read_buf.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/send.c
fs/btrfs/send.h

index 8af5e86..491c486 100644 (file)
@@ -122,8 +122,6 @@ struct send_ctx {
 
        struct file_ra_state ra;
 
-       char *read_buf;
-
        /*
         * We process inodes by their increasing order, so if before an
         * incremental send we reverse the parent/child relationship of
@@ -4794,7 +4792,25 @@ out:
        return ret;
 }
 
-static int fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
+static inline u64 max_send_read_size(const struct send_ctx *sctx)
+{
+       return sctx->send_max_size - SZ_16K;
+}
+
+static int put_data_header(struct send_ctx *sctx, u32 len)
+{
+       struct btrfs_tlv_header *hdr;
+
+       if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
+               return -EOVERFLOW;
+       hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
+       put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
+       put_unaligned_le16(len, &hdr->tlv_len);
+       sctx->send_size += sizeof(*hdr);
+       return 0;
+}
+
+static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
 {
        struct btrfs_root *root = sctx->send_root;
        struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4804,8 +4820,11 @@ static int fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
        pgoff_t index = offset >> PAGE_SHIFT;
        pgoff_t last_index;
        unsigned pg_offset = offset_in_page(offset);
-       int ret = 0;
-       size_t read = 0;
+       int ret;
+
+       ret = put_data_header(sctx, len);
+       if (ret)
+               return ret;
 
        inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
        if (IS_ERR(inode))
@@ -4851,14 +4870,15 @@ static int fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
                }
 
                addr = kmap(page);
-               memcpy(sctx->read_buf + read, addr + pg_offset, cur_len);
+               memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset,
+                      cur_len);
                kunmap(page);
                unlock_page(page);
                put_page(page);
                index++;
                pg_offset = 0;
                len -= cur_len;
-               read += cur_len;
+               sctx->send_size += cur_len;
        }
        iput(inode);
        return ret;
@@ -4880,10 +4900,6 @@ static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
 
        btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
 
-       ret = fill_read_buf(sctx, offset, len);
-       if (ret < 0)
-               goto out;
-
        ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
        if (ret < 0)
                goto out;
@@ -4894,7 +4910,9 @@ static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
 
        TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
        TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
-       TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
+       ret = put_file_data(sctx, offset, len);
+       if (ret < 0)
+               goto out;
 
        ret = send_cmd(sctx);
 
@@ -5013,8 +5031,8 @@ out:
 static int send_hole(struct send_ctx *sctx, u64 end)
 {
        struct fs_path *p = NULL;
+       u64 read_size = max_send_read_size(sctx);
        u64 offset = sctx->cur_inode_last_extent;
-       u64 len;
        int ret = 0;
 
        /*
@@ -5041,16 +5059,19 @@ static int send_hole(struct send_ctx *sctx, u64 end)
        ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
        if (ret < 0)
                goto tlv_put_failure;
-       memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
        while (offset < end) {
-               len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
+               u64 len = min(end - offset, read_size);
 
                ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
                if (ret < 0)
                        break;
                TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
                TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
-               TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
+               ret = put_data_header(sctx, len);
+               if (ret < 0)
+                       break;
+               memset(sctx->send_buf + sctx->send_size, 0, len);
+               sctx->send_size += len;
                ret = send_cmd(sctx);
                if (ret < 0)
                        break;
@@ -5066,17 +5087,16 @@ static int send_extent_data(struct send_ctx *sctx,
                            const u64 offset,
                            const u64 len)
 {
+       u64 read_size = max_send_read_size(sctx);
        u64 sent = 0;
 
        if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
                return send_update_extent(sctx, offset, len);
 
        while (sent < len) {
-               u64 size = len - sent;
+               u64 size = min(len - sent, read_size);
                int ret;
 
-               if (size > BTRFS_SEND_READ_SIZE)
-                       size = BTRFS_SEND_READ_SIZE;
                ret = send_write(sctx, offset + sent, size);
                if (ret < 0)
                        return ret;
@@ -7145,12 +7165,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
                goto out;
        }
 
-       sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
-       if (!sctx->read_buf) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
        sctx->pending_dir_moves = RB_ROOT;
        sctx->waiting_dir_moves = RB_ROOT;
        sctx->orphan_dirs = RB_ROOT;
@@ -7354,7 +7368,6 @@ out:
 
                kvfree(sctx->clone_roots);
                kvfree(sctx->send_buf);
-               kvfree(sctx->read_buf);
 
                name_cache_free(sctx);
 
index ead397f..de91488 100644 (file)
@@ -13,7 +13,6 @@
 #define BTRFS_SEND_STREAM_VERSION 1
 
 #define BTRFS_SEND_BUF_SIZE SZ_64K
-#define BTRFS_SEND_READ_SIZE (48 * SZ_1K)
 
 enum btrfs_tlv_type {
        BTRFS_TLV_U8,