btrfs: add ioctls and send/receive support for reading/writing compressed data
Subsystems (3)
v13 cover btrfs: add ioctls and send/receive support for reading/writing compressed data
From: Omar Sandoval <[email protected]>
This series has three parts: new Btrfs ioctls for reading/writing compressed data, support for sending compressed data via Btrfs send, and btrfs-progs support for sending/receiving compressed data and writing it with the new ioctl.
Patches 1 and 2 are VFS changes exporting a couple of helpers for checks needed by reads and writes. Patches 3-8 are preparatory Btrfs changes for compressed reads and writes. Patch 6 is a cleanup. Patch 9 adds the compressed read ioctl and patch 10 adds the compressed write ioctl.
The main use-case for this interface is Btrfs send/receive. Currently, when sending data from one compressed filesystem to another, the sending side decompresses the data and the receiving side recompresses it before writing it out. This is wasteful and can be avoided if we can just send and write compressed extents.
Patches 11 and 12 are cleanups for Btrfs send. Patches 13-17 add the Btrfs send support. See the previous posting for more details and benchmarks [1]. Patches 13-15 prepare some protocol changes for send stream v2. Patch 16 implements compressed send. Patch 17 enables send stream v2 and compressed send in the send ioctl when requested.
These patches are based on Dave Sterba's Btrfs misc-next branch [2], which is in turn currently based on v5.17-rc3. Test cases are here [3].
Changes since v12 [4]:
- Changed some LZO code to use filesystem sector size instead of page size.
- Changed to use btrfs_inode_lock() instead of inode_lock().
- Added explanation of why we don't need to hold mmap lock for encoded reads to commit message.
- Got rid of GFP_HIGHMEM allocations.
- Changed send protocol definitions to use explicit numbers.
- Changed btrfs send patch to default to protocol v1 for now.
- Style cleanups.
1: https://lore.kernel.org/linux-btrfs/[email protected]/
2: https://github.com/kdave/btrfs-devel/tree/misc-next
3: https://github.com/osandov/xfstests/tree/btrfs-encoded-io
4: https://lore.kernel.org/linux-btrfs/[email protected]/
Omar Sandoval (17):
fs: export rw_verify_area()
fs: export variant of generic_write_checks without iov_iter
btrfs: don't advance offset for compressed bios in
btrfs_csum_one_bio()
btrfs: add ram_bytes and offset to btrfs_ordered_extent
btrfs: support different disk extent size for delalloc
btrfs: clean up cow_file_range_inline()
btrfs: optionally extend i_size in cow_file_range_inline()
btrfs: add definitions + documentation for encoded I/O ioctls
btrfs: add BTRFS_IOC_ENCODED_READ
btrfs: add BTRFS_IOC_ENCODED_WRITE
btrfs: send: remove unused send_ctx::{total,cmd}_send_size
btrfs: send: explicitly number commands and attributes
btrfs: add send stream v2 definitions
btrfs: send: write larger chunks when using stream v2
btrfs: send: allocate send buffer with alloc_page() and vmap() for v2
btrfs: send: send compressed extents with encoded writes
btrfs: send: enable support for stream v2 and compressed writes
fs/btrfs/compression.c | 10 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 19 +- fs/btrfs/delalloc-space.c | 18 +- fs/btrfs/file-item.c | 32 +- fs/btrfs/file.c | 68 ++- fs/btrfs/inode.c | 930 +++++++++++++++++++++++++++++++++---- fs/btrfs/ioctl.c | 208 +++++++++ fs/btrfs/ordered-data.c | 131 ++---- fs/btrfs/ordered-data.h | 25 +- fs/btrfs/relocation.c | 2 +- fs/btrfs/send.c | 324 +++++++++++-- fs/btrfs/send.h | 142 +++--- fs/internal.h | 5 - fs/read_write.c | 34 +- include/linux/fs.h | 2 + include/uapi/linux/btrfs.h | 142 +++++- 17 files changed, 1760 insertions(+), 338 deletions(-)
The btrfs-progs patches were written by Boris Burkov with some updates from me. Patches 1-4 are preparation. Patch 5 implements encoded writes. Patch 6 implements the fallback to decompressing. Patches 7 and 8 implement the other commands. Patch 9 adds the new btrfs send options. Patch 10 adds a test case.
Boris Burkov (8):
btrfs-progs: receive: support v2 send stream larger tlv_len
btrfs-progs: receive: dynamically allocate sctx->read_buf
btrfs-progs: receive: support v2 send stream DATA tlv format
btrfs-progs: receive: process encoded_write commands
btrfs-progs: receive: encoded_write fallback to explicit decode and
write
btrfs-progs: receive: process fallocate commands
btrfs-progs: receive: process setflags ioctl commands
btrfs-progs: receive: add tests for basic encoded_write send/receive
Omar Sandoval (2):
btrfs-progs: receive: add send stream v2 cmds and attrs to send.h
btrfs-progs: send: stream v2 ioctl flags
Documentation/btrfs-receive.rst | 5 + Documentation/btrfs-send.rst | 22 ++ cmds/receive-dump.c | 31 +- cmds/receive.c | 347 +++++++++++++++++- cmds/send.c | 100 ++++- common/send-stream.c | 165 +++++++-- common/send-stream.h | 7 + ioctl.h | 151 +++++++- kernel-shared/send.h | 146 +++++--- libbtrfs/send-stream.c | 2 +- .../052-receive-write-encoded/test.sh | 114 ++++++ 11 files changed, 993 insertions(+), 97 deletions(-) create mode 100755 tests/misc-tests/052-receive-write-encoded/test.sh
Signature · 1 line
2.35.1
PATCH v13 1/17 fs: export rw_verify_area()
From: Omar Sandoval <[email protected]>
I'm adding Btrfs ioctls to read and write compressed data, and rather than duplicating the checks in rw_verify_area(), let's just export it.
--- fs/internal.h | 5 ----- fs/read_write.c | 1 + include/linux/fs.h | 1 + 3 files changed, 2 insertions(+), 5 deletions(-)
fs/internal.h +0 -5
@@ -157,11 +157,6 @@ extern char *simple_dname(struct dentry *, char *, int);extern void dput_to_list (struct dentry *,struct list_head *);extern void shrink_dentry_list (struct list_head *); -/* -* read_write.c -*/ -extern int rw_verify_area (int ,struct file *,const loff_t *,size_t ); -/* * pipe.c */
fs/read_write.c +1 -0
@@ -385,6 +385,7 @@ int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_treturn security_file_permission (file, read_write ==READ ?MAY_READ :MAY_WRITE ); } +EXPORT_SYMBOL (rw_verify_area);static ssize_t new_sync_read (struct file *filp,char __user *buf,size_t len,loff_t *ppos) {
include/linux/fs.h +1 -0
@@ -3173,6 +3173,7 @@ extern loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size);extern loff_t no_seek_end_llseek_size (struct file *,loff_t ,int ,loff_t );extern loff_t no_seek_end_llseek (struct file *,loff_t ,int ); +extern int rw_verify_area (int ,struct file *,const loff_t *,size_t );extern int generic_file_open (struct inode * inode,struct file * filp);extern int nonseekable_open (struct inode * inode,struct file * filp);extern int stream_open (struct inode * inode,struct file * filp);
Signature · 1 line
2.35.1
PATCH v13 2/17 fs: export variant of generic_write_checks without iov_iter
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Encoded I/O in Btrfs needs to check a write with a given logical size without an iov_iter that matches that size (because the iov_iter we have is for the compressed data). So, factor out the parts of generic_write_check() that don't need an iov_iter into a new generic_write_checks_count() function and export that.
--- fs/read_write.c | 33 ++++++++++++++++++++------------- include/linux/fs.h | 1 + 2 files changed, 21 insertions(+), 13 deletions(-)
fs/read_write.c +20 -13
@@ -1618,24 +1618,16 @@ int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)return 0 ; } -/* -* Performs necessary checks before doing a write -* -* Can adjust writing position or amount of bytes to write. -* Returns appropriate error code that caller should return or -* zero in case that write should be allowed. -*/ -ssize_t generic_write_checks (struct kiocb *iocb,struct iov_iter *from) +/* Like generic_write_checks(), but takes size of write instead of iter. */ +int generic_write_checks_count (struct kiocb *iocb,loff_t *count) {struct file *file = iocb->ki_filp;struct inode *inode = file->f_mapping->host; -loff_t count; -int ret;if (IS_SWAPFILE (inode))return -ETXTBSY ; -if (!iov_iter_count (from)) +if (!*count)return 0 ;/* FIXME: this is for backwards compatibility with 2.4 */
@@ -1645,8 +1637,23 @@ ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)if ((iocb->ki_flags &IOCB_NOWAIT ) && !(iocb->ki_flags &IOCB_DIRECT ))return -EINVAL ; - count =iov_iter_count (from); - ret =generic_write_check_limits (file, iocb->ki_pos, &count); +return generic_write_check_limits (iocb->ki_filp, iocb->ki_pos, count); +} +EXPORT_SYMBOL (generic_write_checks_count); + +/* +* Performs necessary checks before doing a write +* +* Can adjust writing position or amount of bytes to write. +* Returns appropriate error code that caller should return or +* zero in case that write should be allowed. +*/ +ssize_t generic_write_checks (struct kiocb *iocb,struct iov_iter *from) +{ +loff_t count =iov_iter_count (from); +int ret; + + ret =generic_write_checks_count (iocb, &count);if (ret)return ret;
include/linux/fs.h +1 -0
@@ -3130,6 +3130,7 @@ extern int sb_min_blocksize(struct super_block *, int);extern int generic_file_mmap (struct file *,struct vm_area_struct *);extern int generic_file_readonly_mmap (struct file *,struct vm_area_struct *);extern ssize_t generic_write_checks (struct kiocb *,struct iov_iter *); +extern int generic_write_checks_count (struct kiocb *iocb,loff_t *count);extern int generic_write_check_limits (struct file *file,loff_t pos,loff_t *count);extern int generic_file_rw_checks (struct file *file_in,struct file *file_out);
Signature · 1 line
2.35.1
PATCH v13 3/17 btrfs: don't advance offset for compressed bios in btrfs_csum_one_bio()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
btrfs_csum_one_bio() loops over each filesystem block in the bio while keeping a cursor of its current logical position in the file in order to look up the ordered extent to add the checksums to. However, this doesn't make much sense for compressed extents, as a sector on disk does not correspond to a sector of decompressed file data. It happens to work because 1) the compressed bio always covers one ordered extent and 2) the size of the bio is always less than the size of the ordered extent. However, the second point will not always be true for encoded writes.
Let's add a boolean parameter to btrfs_csum_one_bio() to indicate that it can assume that the bio only covers one ordered extent. Since we're already changing the signature, let's get rid of the contig parameter and make it implied by the offset parameter, similar to the change we recently made to btrfs_lookup_bio_sums(). Additionally, let's rename nr_sectors to blockcount to make it clear that it's the number of filesystem blocks, not the number of 512-byte sectors.
--- fs/btrfs/compression.c | 3 ++- fs/btrfs/ctree.h | 2 +- fs/btrfs/file-item.c | 32 ++++++++++++++------------------ fs/btrfs/inode.c | 8 ++++---- 4 files changed, 21 insertions(+), 24 deletions(-)
fs/btrfs/compression.c +2 -1
@@ -591,7 +591,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,if (submit) {if (!skip_sum) { - ret =btrfs_csum_one_bio (inode, bio, start,1 ); + ret =btrfs_csum_one_bio (inode, bio, start, + true);if (ret) goto finish_cb; }
fs/btrfs/ctree.h +1 -1
@@ -3193,7 +3193,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,struct btrfs_root *root, struct btrfs_ordered_sum *sums);blk_status_t btrfs_csum_one_bio (struct btrfs_inode *inode,struct bio *bio, -u64 file_start,int contig); +u64 offset,bool one_ordered);int btrfs_lookup_csums_range (struct btrfs_root *root,u64 start,u64 end,struct list_head *list,int search_commit); void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
fs/btrfs/file-item.c +14 -18
@@ -616,28 +616,28 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,* btrfs_csum_one_bio - Calculates checksums of the data contained inside a bio * @inode: Owner of the data inside the bio * @bio: Contains the data to be checksummed -* @file_start: offset in file this bio begins to describe -* @contig: Boolean. If true/1 means all bio vecs in this bio are -* contiguous and they begin at @file_start in the file. False/0 -* means this bio can contain potentially discontiguous bio vecs -* so the logical offset of each should be calculated separately. +* @offset: If (u64)-1, @bio may contain discontiguous bio vecs, so the +* file offsets are determined from the page offsets in the bio. +* Otherwise, this is the starting file offset of the bio vecs in +* @bio, which must be contiguous. +* @one_ordered: If true, @bio only refers to one ordered extent. */ blk_status_t btrfs_csum_one_bio (struct btrfs_inode *inode,struct bio *bio, -u64 file_start,int contig) +u64 offset,bool one_ordered) {struct btrfs_fs_info *fs_info = inode->root->fs_info;SHASH_DESC_ON_STACK (shash, fs_info->csum_shash);struct btrfs_ordered_sum *sums;struct btrfs_ordered_extent *ordered =NULL ; +const bool use_page_offsets = (offset == (u64)-1 );char *data;struct bvec_iter iter;struct bio_vec bvec;int index; -int nr_sectors; +int blockcount;unsigned long total_bytes =0 ;unsigned long this_sum_bytes =0 ;int i; -u64 offset;unsigned nofs_flag; nofs_flag =memalloc_nofs_save ();
@@ -651,18 +651,13 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, sums->len = bio->bi_iter.bi_size;INIT_LIST_HEAD (&sums->list); -if (contig) - offset = file_start; -else - offset =0 ;/* shut up gcc */ - sums->bytenr = bio->bi_iter.bi_sector <<9 ; index =0 ; shash->tfm = fs_info->csum_shash;bio_for_each_segment (bvec, bio, iter) { -if (!contig) +if (use_page_offsets) offset =page_offset (bvec.bv_page) + bvec.bv_offset;if (!ordered) {
@@ -681,13 +676,14 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, } } - nr_sectors =BTRFS_BYTES_TO_BLKS (fs_info, + blockcount =BTRFS_BYTES_TO_BLKS (fs_info, bvec.bv_len + fs_info->sectorsize -1 ); -for (i =0 ; i < nr_sectors; i++) { -if (offset >= ordered->file_offset + ordered->num_bytes || - offset < ordered->file_offset) { +for (i =0 ; i < blockcount; i++) { +if (!one_ordered && + !in_range (offset, ordered->file_offset, + ordered->num_bytes)) {unsigned long bytes_left; sums->len = this_sum_bytes;
fs/btrfs/inode.c +4 -4
@@ -2314,7 +2314,7 @@ void btrfs_clear_delalloc_extent(struct inode *vfs_inode,static blk_status_t btrfs_submit_bio_start (struct inode *inode,struct bio *bio,u64 dio_file_offset) { -return btrfs_csum_one_bio (BTRFS_I (inode), bio,0 ,0 ); +return btrfs_csum_one_bio (BTRFS_I (inode), bio, (u64)-1 , false); }/*
@@ -2566,7 +2566,7 @@ blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, 0, btrfs_submit_bio_start); goto out; }else if (!skip_sum) { - ret =btrfs_csum_one_bio (BTRFS_I (inode), bio,0 ,0 ); + ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, (u64)-1 , false);if (ret) goto out; }
@@ -7817,7 +7817,7 @@ static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,struct bio *bio, u64 dio_file_offset) { - return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1); + return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, false); }static void btrfs_end_dio_bio (struct bio *bio)
@@ -7874,7 +7874,7 @@ static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,* If we aren't doing async submit, calculate the csum of the * bio now. */ - ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, file_offset,1 ); + ret =btrfs_csum_one_bio (BTRFS_I (inode), bio, file_offset, false);if (ret) goto err; }else {
Signature · 1 line
2.35.1
PATCH v13 4/17 btrfs: add ram_bytes and offset to btrfs_ordered_extent
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, we only create ordered extents when ram_bytes == num_bytes and offset == 0. However, BTRFS_IOC_ENCODED_WRITE writes may create extents which only refer to a subset of the full unencoded extent, so we need to plumb these fields through the ordered extent infrastructure and pass them down to insert_reserved_file_extent().
Since we're changing the btrfs_add_ordered_extent* signature, let's get rid of the trivial wrappers and add a kernel-doc.
--- fs/btrfs/inode.c | 58 ++++++++++++-------- fs/btrfs/ordered-data.c | 119 ++++++++++++---------------------------- fs/btrfs/ordered-data.h | 22 +++++--- 3 files changed, 82 insertions(+), 117 deletions(-)
fs/btrfs/inode.c +35 -23
@@ -985,11 +985,14 @@ static int submit_one_async_extent(struct btrfs_inode *inode, }free_extent_map (em); - ret =btrfs_add_ordered_extent_compress (inode, start,/* file_offset */ - ins.objectid,/* disk_bytenr */ - async_extent->ram_size,/* num_bytes */ - ins.offset,/* disk_num_bytes */ - async_extent->compress_type); + ret =btrfs_add_ordered_extent (inode, start,/* file_offset */ + async_extent->ram_size,/* num_bytes */ + async_extent->ram_size,/* ram_bytes */ + ins.objectid,/* disk_bytenr */ + ins.offset,/* disk_num_bytes */ +0 ,/* offset */ +1 <<BTRFS_ORDERED_COMPRESSED , + async_extent->compress_type);if (ret) {btrfs_drop_extent_cache (inode, start, end,0 ); goto out_free_reserve;
@@ -1238,9 +1241,10 @@ static noinline int cow_file_range(struct btrfs_inode *inode, }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, start, ins.objectid, - ram_size, cur_alloc_size, -BTRFS_ORDERED_REGULAR ); + ret =btrfs_add_ordered_extent (inode, start, ram_size, ram_size, + ins.objectid, cur_alloc_size,0 , +1 <<BTRFS_ORDERED_REGULAR , +BTRFS_COMPRESS_NONE );if (ret) goto out_drop_extent_cache;
@@ -1899,10 +1903,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, goto error; }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, cur_offset, - disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_PREALLOC ); + ret =btrfs_add_ordered_extent (inode, + cur_offset, num_bytes, num_bytes, + disk_bytenr, num_bytes,0 , +1 <<BTRFS_ORDERED_PREALLOC , +BTRFS_COMPRESS_NONE );if (ret) { btrfs_drop_extent_cache(inode, cur_offset, cur_offset + num_bytes - 1,
@@ -1911,9 +1916,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, } }else { ret =btrfs_add_ordered_extent (inode, cur_offset, + num_bytes, num_bytes, disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_NOCOW ); +0 , +1 <<BTRFS_ORDERED_NOCOW , +BTRFS_COMPRESS_NONE );if (ret) goto error; }
@@ -2874,6 +2881,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,struct btrfs_key ins;u64 disk_num_bytes =btrfs_stack_file_extent_disk_num_bytes (stack_fi);u64 disk_bytenr =btrfs_stack_file_extent_disk_bytenr (stack_fi); +u64 offset =btrfs_stack_file_extent_offset (stack_fi);u64 num_bytes =btrfs_stack_file_extent_num_bytes (stack_fi);u64 ram_bytes =btrfs_stack_file_extent_ram_bytes (stack_fi);struct btrfs_drop_extents_args drop_args = {0 };
@@ -2948,7 +2956,8 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, goto out; ret =btrfs_alloc_reserved_file_extent (trans, root,btrfs_ino (inode), - file_pos, qgroup_reserved, &ins); + file_pos - offset, + qgroup_reserved, &ins); out:btrfs_free_path (path);
@@ -2974,20 +2983,20 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, struct btrfs_ordered_extent *oe) {struct btrfs_file_extent_item stack_fi; -u64 logical_len;bool update_inode_bytes; +u64 num_bytes = oe->num_bytes; +u64 ram_bytes = oe->ram_bytes;memset (&stack_fi,0 ,sizeof (stack_fi));btrfs_set_stack_file_extent_type (&stack_fi,BTRFS_FILE_EXTENT_REG );btrfs_set_stack_file_extent_disk_bytenr (&stack_fi, oe->disk_bytenr);btrfs_set_stack_file_extent_disk_num_bytes (&stack_fi, oe->disk_num_bytes); +btrfs_set_stack_file_extent_offset (&stack_fi, oe->offset);if (test_bit (BTRFS_ORDERED_TRUNCATED , &oe->flags)) - logical_len = oe->truncated_len; -else - logical_len = oe->num_bytes; -btrfs_set_stack_file_extent_num_bytes (&stack_fi, logical_len); -btrfs_set_stack_file_extent_ram_bytes (&stack_fi, logical_len); + num_bytes = ram_bytes = oe->truncated_len; +btrfs_set_stack_file_extent_num_bytes (&stack_fi, num_bytes); +btrfs_set_stack_file_extent_ram_bytes (&stack_fi, ram_bytes);btrfs_set_stack_file_extent_compression (&stack_fi, oe->compress_type);/* Encryption and other encoding is reserved and all 0 */
@@ -7054,8 +7063,11 @@ static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,if (IS_ERR (em)) goto out; } - ret =btrfs_add_ordered_extent_dio (inode, start, block_start, len, - block_len, type); + ret =btrfs_add_ordered_extent (inode, start, len, len, block_start, + block_len,0 , + (1 << type) | + (1 <<BTRFS_ORDERED_DIRECT ), +BTRFS_COMPRESS_NONE );if (ret) {if (em) {free_extent_map (em);
fs/btrfs/ordered-data.c +34 -85
167 lines not on this page · raw message
fs/btrfs/ordered-data.h +13 -9
@@ -76,6 +76,13 @@ enum { BTRFS_ORDERED_PENDING, }; +/* BTRFS_ORDERED_* flags that specify the type of the extent. */ +#define BTRFS_ORDERED_TYPE_FLAGS ((1UL << BTRFS_ORDERED_REGULAR) | \ + (1UL << BTRFS_ORDERED_NOCOW) | \ + (1UL << BTRFS_ORDERED_PREALLOC) | \ + (1UL << BTRFS_ORDERED_COMPRESSED) | \ + (1UL << BTRFS_ORDERED_DIRECT)) +struct btrfs_ordered_extent {/* logical offset in the file */ u64 file_offset;
@@ -84,9 +91,11 @@ struct btrfs_ordered_extent {* These fields directly correspond to the same fields in * btrfs_file_extent_item. */ -u64 disk_bytenr;u64 num_bytes; +u64 ram_bytes; +u64 disk_bytenr;u64 disk_num_bytes; +u64 offset;/* number of bytes that still need writing */ u64 bytes_left;
@@ -179,14 +188,9 @@ bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,struct btrfs_ordered_extent **cached, u64 file_offset, u64 io_size);int btrfs_add_ordered_extent (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes,u64 disk_num_bytes, -int type); -int btrfs_add_ordered_extent_dio (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes, -u64 disk_num_bytes,int type); -int btrfs_add_ordered_extent_compress (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes, -u64 disk_num_bytes,int compress_type); +u64 num_bytes,u64 ram_bytes,u64 disk_bytenr, +u64 disk_num_bytes,u64 offset,int flags, +int compress_type);void btrfs_add_ordered_sum (struct btrfs_ordered_extent *entry,struct btrfs_ordered_sum *sum); struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
Signature · 1 line
2.35.1
Re: [PATCH v13 04/17] btrfs: add ram_bytes and offset to btrfs_ordered_extent
Quoted 158 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
Currently, we only create ordered extents when
ram_bytes==num_bytesand offset == 0. However,BTRFS_IOC_ENCODED_WRITEwrites may create extents which only refer to a subset of the full unencoded extent, so we need to plumb these fields through the ordered extent infrastructure and pass them down toinsert_reserved_file_extent().Since we're changing the btrfs_add_ordered_extent* signature, let's get rid of the trivial wrappers and add a kernel-doc.
Reviewed-by Nikolay Borisov <[email protected]>Reviewed-by Josef Bacik <[email protected]>Signed-off-by Omar Sandoval <[email protected]>--- fs/btrfs/inode.c | 58 ++++++++++++-------- fs/btrfs/ordered-data.c | 119 ++++++++++++---------------------------- fs/btrfs/ordered-data.h | 22 +++++--- 3 files changed, 82 insertions(+), 117 deletions(-)fs/btrfs/inode.c +35
-23@@ -985,11 +985,14 @@ static int submit_one_async_extent(struct btrfs_inode *inode, }free_extent_map (em); - ret =btrfs_add_ordered_extent_compress (inode, start,/* file_offset */ - ins.objectid,/* disk_bytenr */ - async_extent->ram_size,/* num_bytes */ - ins.offset,/* disk_num_bytes */ - async_extent->compress_type); + ret =btrfs_add_ordered_extent (inode, start,/* file_offset */ + async_extent->ram_size,/* num_bytes */ + async_extent->ram_size,/* ram_bytes */ + ins.objectid,/* disk_bytenr */ + ins.offset,/* disk_num_bytes */ +0 ,/* offset */ +1 <<BTRFS_ORDERED_COMPRESSED , + async_extent->compress_type);if (ret) {btrfs_drop_extent_cache (inode, start, end,0 ); goto out_free_reserve;@@ -1238,9 +1241,10 @@ static noinline int cow_file_range(struct btrfs_inode *inode, }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, start, ins.objectid, - ram_size, cur_alloc_size, -BTRFS_ORDERED_REGULAR ); + ret =btrfs_add_ordered_extent (inode, start, ram_size, ram_size, + ins.objectid, cur_alloc_size,0 , +1 <<BTRFS_ORDERED_REGULAR , +BTRFS_COMPRESS_NONE );if (ret) goto out_drop_extent_cache;@@ -1899,10 +1903,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, goto error; }free_extent_map (em); - ret =btrfs_add_ordered_extent (inode, cur_offset, - disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_PREALLOC ); + ret =btrfs_add_ordered_extent (inode, + cur_offset, num_bytes, num_bytes, + disk_bytenr, num_bytes,0 , +1 <<BTRFS_ORDERED_PREALLOC , +BTRFS_COMPRESS_NONE );if (ret) { btrfs_drop_extent_cache(inode, cur_offset, cur_offset + num_bytes - 1,@@ -1911,9 +1916,11 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, } }else { ret =btrfs_add_ordered_extent (inode, cur_offset, + num_bytes, num_bytes, disk_bytenr, num_bytes, - num_bytes, -BTRFS_ORDERED_NOCOW ); +0 , +1 <<BTRFS_ORDERED_NOCOW , +BTRFS_COMPRESS_NONE );if (ret) goto error; }@@ -2874,6 +2881,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,struct btrfs_key ins;u64 disk_num_bytes =btrfs_stack_file_extent_disk_num_bytes (stack_fi);u64 disk_bytenr =btrfs_stack_file_extent_disk_bytenr (stack_fi); +u64 offset =btrfs_stack_file_extent_offset (stack_fi);u64 num_bytes =btrfs_stack_file_extent_num_bytes (stack_fi);u64 ram_bytes =btrfs_stack_file_extent_ram_bytes (stack_fi);struct btrfs_drop_extents_args drop_args = {0 };@@ -2948,7 +2956,8 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, goto out; ret =btrfs_alloc_reserved_file_extent (trans, root,btrfs_ino (inode), - file_pos, qgroup_reserved, &ins); + file_pos - offset, + qgroup_reserved, &ins); out:btrfs_free_path (path);@@ -2974,20 +2983,20 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, struct btrfs_ordered_extent *oe) {struct btrfs_file_extent_item stack_fi; -u64 logical_len;bool update_inode_bytes; +u64 num_bytes = oe->num_bytes; +u64 ram_bytes = oe->ram_bytes;memset (&stack_fi,0 ,sizeof (stack_fi));btrfs_set_stack_file_extent_type (&stack_fi,BTRFS_FILE_EXTENT_REG );btrfs_set_stack_file_extent_disk_bytenr (&stack_fi, oe->disk_bytenr);btrfs_set_stack_file_extent_disk_num_bytes (&stack_fi, oe->disk_num_bytes); +btrfs_set_stack_file_extent_offset (&stack_fi, oe->offset);if (test_bit (BTRFS_ORDERED_TRUNCATED , &oe->flags)) - logical_len = oe->truncated_len; -else - logical_len = oe->num_bytes; -btrfs_set_stack_file_extent_num_bytes (&stack_fi, logical_len); -btrfs_set_stack_file_extent_ram_bytes (&stack_fi, logical_len); + num_bytes = ram_bytes = oe->truncated_len; +btrfs_set_stack_file_extent_num_bytes (&stack_fi, num_bytes); +btrfs_set_stack_file_extent_ram_bytes (&stack_fi, ram_bytes);btrfs_set_stack_file_extent_compression (&stack_fi, oe->compress_type);/* Encryption and other encoding is reserved and all 0 */ @@ -7054,8 +7063,11 @@ static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,if (IS_ERR (em)) goto out; } - ret =btrfs_add_ordered_extent_dio (inode, start, block_start, len, - block_len, type); + ret =btrfs_add_ordered_extent (inode, start, len, len, block_start, + block_len,0 , + (1 << type) | + (1 <<BTRFS_ORDERED_DIRECT ), +BTRFS_COMPRESS_NONE );if (ret) {if (em) {free_extent_map (em);fs/btrfs/ordered-data.c +1
-2@@ -143,16 +143,27 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,return ret; } -/* -* Allocate and add a new ordered_extent into the per-inode tree. +/**
fs/btrfs/ordered-data.c static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, · line 156
Quoted 10 lines
fs/btrfs/ordered-data.c +10
-0+* btrfs_add_ordered_extent - Add an ordered extent to the per-inode tree. +* @inode: inode that this extent is for. +* @file_offset: Logical offset in file where the extent starts. +* @num_bytes: Logical length of extent in file. +* @ram_bytes: Full length of unencoded data. +* @disk_bytenr: Offset of extent on disk. +* @disk_num_bytes: Size of extent on disk. +* @offset: Offset into unencoded data where file data starts. +* @flags: Flags specifying type of extent (1 << BTRFS_ORDERED_*). +* @compress_type: Compression algorithm used for data.
The parameter descriptions should be aligned, also you can drop the function name from the first line. Fixed.
Quoted 5 lines
fs/btrfs/ordered-data.c +2
-2* -* The tree is given a single reference on the ordered extent that was -* inserted. +* Most of these parameters correspond to &struct btrfs_file_extent_item. The +* tree is given a single reference on the ordered extent that was inserted.
fs/btrfs/ordered-data.c static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, · line 165
Quoted 10 lines
fs/btrfs/ordered-data.c +5
-4+* +* Return: 0 or -ENOMEM. */ -static int __btrfs_add_ordered_extent (struct btrfs_inode *inode,u64 file_offset, -u64 disk_bytenr,u64 num_bytes, -u64 disk_num_bytes,int type,int dio, -int compress_type) +int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset, + u64 num_bytes, u64 ram_bytes, u64 disk_bytenr, + u64 disk_num_bytes, u64 offset, int flags,
All flags variables should be unsgined, so the bitwise ops work safely.
PATCH v13 5/17 btrfs: support different disk extent size for delalloc
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, we always reserve the same extent size in the file and extent size on disk for delalloc because the former is the worst case for the latter. For BTRFS_IOC_ENCODED_WRITE writes, we know the exact size of the extent on disk, which may be less than or greater than (for bookends) the size in the file. Add a disk_num_bytes parameter to btrfs_delalloc_reserve_metadata() so that we can reserve the correct amount of csum bytes. No functional change.
--- fs/btrfs/ctree.h | 3 ++- fs/btrfs/delalloc-space.c | 18 ++++++++++-------- fs/btrfs/file.c | 3 ++- fs/btrfs/inode.c | 4 ++-- fs/btrfs/relocation.c | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-)
fs/btrfs/ctree.h +2 -1
@@ -2877,7 +2877,8 @@ void btrfs_subvolume_release_metadata(struct btrfs_root *root, struct btrfs_block_rsv *rsv);void btrfs_delalloc_release_extents (struct btrfs_inode *inode,u64 num_bytes); -int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes); +int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes, +u64 disk_num_bytes);u64 btrfs_account_ro_block_groups_free_space (struct btrfs_space_info *sinfo);int btrfs_error_unpin_extent_range (struct btrfs_fs_info *fs_info,u64 start,u64 end);
fs/btrfs/delalloc-space.c +10 -8
@@ -270,11 +270,11 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info, }static void calc_inode_reservations (struct btrfs_fs_info *fs_info, -u64 num_bytes,u64 *meta_reserve, -u64 *qgroup_reserve) +u64 num_bytes,u64 disk_num_bytes, +u64 *meta_reserve,u64 *qgroup_reserve) {u64 nr_extents =count_max_extents (num_bytes); -u64 csum_leaves =btrfs_csum_bytes_to_leaves (fs_info, num_bytes); +u64 csum_leaves =btrfs_csum_bytes_to_leaves (fs_info, disk_num_bytes);u64 inode_update =btrfs_calc_metadata_size (fs_info,1 ); *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
@@ -288,7 +288,8 @@ static void calc_inode_reservations(struct btrfs_fs_info *fs_info, *qgroup_reserve = nr_extents * fs_info->nodesize; } -int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes) +int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes, +u64 disk_num_bytes) {struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;
@@ -318,6 +319,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) } num_bytes =ALIGN (num_bytes, fs_info->sectorsize); + disk_num_bytes =ALIGN (disk_num_bytes, fs_info->sectorsize);/* * We always want to do it this way, every other way is wrong and ends
@@ -329,8 +331,8 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)* everything out and try again, which is bad. This way we just * over-reserve slightly, and clean up the mess when we are done. */ -calc_inode_reservations (fs_info, num_bytes, &meta_reserve, - &qgroup_reserve); +calc_inode_reservations (fs_info, num_bytes, disk_num_bytes, + &meta_reserve, &qgroup_reserve); ret =btrfs_qgroup_reserve_meta_prealloc (root, qgroup_reserve, true);if (ret)return ret;
@@ -349,7 +351,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)spin_lock (&inode->lock); nr_extents =count_max_extents (num_bytes);btrfs_mod_outstanding_extents (inode, nr_extents); - inode->csum_bytes += num_bytes; + inode->csum_bytes += disk_num_bytes;btrfs_calculate_inode_block_rsv_size (fs_info, inode);spin_unlock (&inode->lock);
@@ -454,7 +456,7 @@ int btrfs_delalloc_reserve_space(struct btrfs_inode *inode, ret =btrfs_check_data_free_space (inode, reserved, start, len);if (ret <0 )return ret; - ret =btrfs_delalloc_reserve_metadata (inode, len); + ret =btrfs_delalloc_reserve_metadata (inode, len, len);if (ret <0 ) {btrfs_free_reserved_data_space (inode, *reserved, start, len);extent_changeset_free (*reserved);
fs/btrfs/file.c +2 -1
@@ -1746,7 +1746,8 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, fs_info->sectorsize);WARN_ON (reserve_bytes ==0 ); ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), - reserve_bytes); + reserve_bytes, + reserve_bytes);if (ret) {if (!only_release_metadata) btrfs_free_reserved_data_space(BTRFS_I(inode),
fs/btrfs/inode.c +2 -2
@@ -4697,7 +4697,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, goto out; } } - ret =btrfs_delalloc_reserve_metadata (inode, blocksize); + ret =btrfs_delalloc_reserve_metadata (inode, blocksize, blocksize);if (ret <0 ) {if (!only_release_metadata) btrfs_free_reserved_data_space(inode, data_reserved,
@@ -7467,7 +7467,7 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map,struct extent_map *em2;/* We can NOCOW, so only need to reserve metadata space. */ - ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), len); + ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), len, len);if (ret <0 ) {/* Our caller expects us to free the input extent map. */ free_extent_map (em);
fs/btrfs/relocation.c +1 -1
@@ -2997,7 +2997,7 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,/* Reserve metadata for this range */ ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), - clamped_len); + clamped_len, clamped_len);if (ret) goto release_page;
Signature · 1 line
2.35.1
PATCH v13 6/17 btrfs: clean up cow_file_range_inline()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The start parameter to cow_file_range_inline() (and insert_inline_extent()) is always 0, so get rid of it and simplify the logic in those two functions. Pass btrfs_inode to insert_inline_extent() and remove the redundant root parameter. Also document the requirements for creating an inline extent. No functional change.
--- fs/btrfs/inode.c | 88 +++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 50 deletions(-)
fs/btrfs/inode.c +38 -50
@@ -239,12 +239,13 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,* no overlapping inline items exist in the btree */ static int insert_inline_extent (struct btrfs_trans_handle *trans, -struct btrfs_path *path,bool extent_inserted, -struct btrfs_root *root,struct inode *inode, -u64 start,size_t size,size_t compressed_size, +struct btrfs_path *path, +struct btrfs_inode *inode,bool extent_inserted, +size_t size,size_t compressed_size,int compress_type,struct page **compressed_pages) { +struct btrfs_root *root = inode->root;struct extent_buffer *leaf;struct page *page =NULL ;char *kaddr;
@@ -252,7 +253,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,struct btrfs_file_extent_item *ei;int ret;size_t cur_size = size; -unsigned long offset;ASSERT ((compressed_size >0 && compressed_pages) || (compressed_size ==0 && !compressed_pages));
@@ -264,8 +264,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,struct btrfs_key key;size_t datasize; - key.objectid =btrfs_ino (BTRFS_I (inode)); - key.offset = start; + key.objectid =btrfs_ino (inode); + key.offset =0 ; key.type =BTRFS_EXTENT_DATA_KEY ; datasize =btrfs_file_extent_calc_inline_size (cur_size);
@@ -303,12 +303,10 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,btrfs_set_file_extent_compression (leaf, ei, compress_type); }else { - page =find_get_page (inode->i_mapping, - start >>PAGE_SHIFT ); + page =find_get_page (inode->vfs_inode.i_mapping,0 );btrfs_set_file_extent_compression (leaf, ei,0 ); kaddr =kmap_atomic (page); - offset =offset_in_page (start); -write_extent_buffer (leaf, kaddr + offset, ptr, size); +write_extent_buffer (leaf, kaddr, ptr, size);kunmap_atomic (kaddr);put_page (page); }
@@ -319,8 +317,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* We align size to sectorsize for inline extents just for simplicity * sake. */ - size =ALIGN (size, root->fs_info->sectorsize); - ret =btrfs_inode_set_file_extent_range (BTRFS_I (inode), start, size); + ret =btrfs_inode_set_file_extent_range (inode,0 , +ALIGN (size, root->fs_info->sectorsize));if (ret) goto fail;
@@ -333,7 +331,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* before we unlock the pages. Otherwise we * could end up racing with unlink. */ -BTRFS_I (inode)->disk_i_size = inode->i_size; + inode->disk_i_size =i_size_read (&inode->vfs_inode); + fail:return ret; }
@@ -344,8 +343,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* does the checks required to make sure the data is small enough * to fit as an inline extent. */ -static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, -u64 end,size_t compressed_size, +static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, +size_t compressed_size,int compress_type,struct page **compressed_pages) {
@@ -353,26 +352,21 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_trans_handle *trans; -u64 isize =i_size_read (&inode->vfs_inode); -u64 actual_end =min (end +1 , isize); -u64 inline_len = actual_end - start; -u64 aligned_end =ALIGN (end, fs_info->sectorsize); -u64 data_len = inline_len; +u64 data_len = compressed_size ? compressed_size : size;int ret;struct btrfs_path *path; -if (compressed_size) - data_len = compressed_size; - -if (start >0 || - actual_end > fs_info->sectorsize || +/* +* We can create an inline extent if it ends at or beyond the current +* i_size, is no larger than a sector (decompressed), and the (possibly +* compressed) data fits in a leaf and the configured maximum inline +* size. +*/ +if (size <i_size_read (&inode->vfs_inode) || + size > fs_info->sectorsize || data_len >BTRFS_MAX_INLINE_DATA_SIZE (fs_info) || - (!compressed_size && - (actual_end & (fs_info->sectorsize -1 )) ==0 ) || - end +1 < isize || - data_len > fs_info->max_inline) { + data_len > fs_info->max_inline)return 1 ; - } path =btrfs_alloc_path ();if (!path)
@@ -386,30 +380,21 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, trans->block_rsv = &inode->block_rsv; drop_args.path = path; - drop_args.start = start; - drop_args.end = aligned_end; + drop_args.start =0 ; + drop_args.end = fs_info->sectorsize; drop_args.drop_cache = true; drop_args.replace_extent = true; - -if (compressed_size && compressed_pages) - drop_args.extent_item_size =btrfs_file_extent_calc_inline_size ( - compressed_size); -else - drop_args.extent_item_size =btrfs_file_extent_calc_inline_size ( - inline_len); - + drop_args.extent_item_size =btrfs_file_extent_calc_inline_size (data_len); ret =btrfs_drop_extents (trans, root, inode, &drop_args);if (ret) {btrfs_abort_transaction (trans, ret); goto out; } -if (isize > actual_end) - inline_len =min_t (u64, isize, actual_end); - ret =insert_inline_extent (trans, path, drop_args.extent_inserted, - root, &inode->vfs_inode, start, - inline_len, compressed_size, - compress_type, compressed_pages); + ret =insert_inline_extent (trans, path, inode, + drop_args.extent_inserted, size, + compressed_size, compress_type, + compressed_pages);if (ret && ret != -ENOSPC ) {btrfs_abort_transaction (trans, ret); goto out;
@@ -418,7 +403,7 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, goto out; } -btrfs_update_inode_bytes (inode, inline_len, drop_args.bytes_found); +btrfs_update_inode_bytes (inode, size, drop_args.bytes_found); ret =btrfs_update_inode (trans, root, inode);if (ret && ret != -ENOSPC ) {btrfs_abort_transaction (trans, ret);
@@ -739,12 +724,12 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)/* we didn't compress the entire range, try * to make an uncompressed inline extent. */ - ret =cow_file_range_inline (BTRFS_I (inode), start, end, + ret =cow_file_range_inline (BTRFS_I (inode), actual_end,0 ,BTRFS_COMPRESS_NONE ,NULL ); }else {/* try making a compressed inline extent */ - ret =cow_file_range_inline (BTRFS_I (inode), start, end, + ret =cow_file_range_inline (BTRFS_I (inode), actual_end, total_compressed, compress_type, pages); }
@@ -1159,8 +1144,11 @@ static noinline int cow_file_range(struct btrfs_inode *inode,* So here we skip inline extent creation completely. */ if (start ==0 && fs_info->sectorsize ==PAGE_SIZE ) { +u64 actual_end =min_t (u64,i_size_read (&inode->vfs_inode), + end +1 ); +/* lets try to make an inline extent */ - ret =cow_file_range_inline (inode, start, end,0 , + ret =cow_file_range_inline (inode, actual_end,0 ,BTRFS_COMPRESS_NONE ,NULL );if (ret ==0 ) {/*
Signature · 1 line
2.35.1
PATCH v13 7/17 btrfs: optionally extend i_size in cow_file_range_inline()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Currently, an inline extent is always created after i_size is extended from btrfs_dirty_pages(). However, for encoded writes, we only want to update i_size after we successfully created the inline extent. Add an update_i_size parameter to cow_file_range_inline() and insert_inline_extent() and pass in the size of the extent rather than determining it from i_size.
--- fs/btrfs/inode.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
fs/btrfs/inode.c +16 -7
@@ -243,7 +243,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, struct btrfs_inode *inode, bool extent_inserted, size_t size, size_t compressed_size, int compress_type, - struct page **compressed_pages) + struct page **compressed_pages, + bool update_i_size) { struct btrfs_root *root = inode->root;struct extent_buffer *leaf;
@@ -253,6 +254,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,struct btrfs_file_extent_item *ei;int ret;size_t cur_size = size; +u64 i_size;ASSERT ((compressed_size >0 && compressed_pages) || (compressed_size ==0 && !compressed_pages));
@@ -331,7 +333,12 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,* before we unlock the pages. Otherwise we * could end up racing with unlink. */ - inode->disk_i_size =i_size_read (&inode->vfs_inode); + i_size =i_size_read (&inode->vfs_inode); +if (update_i_size && size > i_size) { +i_size_write (&inode->vfs_inode, size); + i_size = size; + } + inode->disk_i_size = i_size; fail:return ret;
@@ -346,7 +353,8 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size,size_t compressed_size,int compress_type, -struct page **compressed_pages) +struct page **compressed_pages, +bool update_i_size) {struct btrfs_drop_extents_args drop_args = {0 };struct btrfs_root *root = inode->root;
@@ -394,7 +402,7 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, ret =insert_inline_extent (trans, path, inode, drop_args.extent_inserted, size, compressed_size, compress_type, - compressed_pages); + compressed_pages, update_i_size);if (ret && ret != -ENOSPC ) {btrfs_abort_transaction (trans, ret); goto out;
@@ -726,12 +734,13 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)*/ ret =cow_file_range_inline (BTRFS_I (inode), actual_end,0 ,BTRFS_COMPRESS_NONE , -NULL ); +NULL , false); }else {/* try making a compressed inline extent */ ret =cow_file_range_inline (BTRFS_I (inode), actual_end, total_compressed, - compress_type, pages); + compress_type, pages, + false); }if (ret <=0 ) { unsigned long clear_flags = EXTENT_DELALLOC |
@@ -1149,7 +1158,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,/* lets try to make an inline extent */ ret =cow_file_range_inline (inode, actual_end,0 , -BTRFS_COMPRESS_NONE ,NULL ); +BTRFS_COMPRESS_NONE ,NULL , false);if (ret ==0 ) {/* * We use DO_ACCOUNTING here because we need the
Signature · 1 line
2.35.1
PATCH v13 8/17 btrfs: add definitions + documentation for encoded I/O ioctls
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
In order to allow sending and receiving compressed data without decompressing it, we need an interface to write pre-compressed data directly to the filesystem and the matching interface to read compressed data without decompressing it. This adds the definitions for ioctls to do that and detailed explanations of how to use them.
--- include/uapi/linux/btrfs.h | 132 +++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+)
include/uapi/linux/btrfs.h +132 -0
@@ -869,6 +869,134 @@ struct btrfs_ioctl_get_subvol_rootref_args {__u8 align[7 ]; }; +/* +* Data and metadata for an encoded read or write. +* +* Encoded I/O bypasses any encoding automatically done by the filesystem (e.g., +* compression). This can be used to read the compressed contents of a file or +* write pre-compressed data directly to a file. +* +* BTRFS_IOC_ENCODED_READ and BTRFS_IOC_ENCODED_WRITE are essentially +* preadv/pwritev with additional metadata about how the data is encoded and the +* size of the unencoded data. +* +* BTRFS_IOC_ENCODED_READ fills the given iovecs with the encoded data, fills +* the metadata fields, and returns the size of the encoded data. It reads one +* extent per call. It can also read data which is not encoded. +* +* BTRFS_IOC_ENCODED_WRITE uses the metadata fields, writes the encoded data +* from the iovecs, and returns the size of the encoded data. Note that the +* encoded data is not validated when it is written; if it is not valid (e.g., +* it cannot be decompressed), then a subsequent read may return an error. +* +* Since the filesystem page cache contains decoded data, encoded I/O bypasses +* the page cache. Encoded I/O requires CAP_SYS_ADMIN. +*/ +struct btrfs_ioctl_encoded_io_args { +/* Input parameters for both reads and writes. */ + +/* +* iovecs containing encoded data. +* +* For reads, if the size of the encoded data is larger than the sum of +* iov[n].iov_len for 0 <= n < iovcnt, then the ioctl fails with +* ENOBUFS. +* +* For writes, the size of the encoded data is the sum of iov[n].iov_len +* for 0 <= n < iovcnt. This must be less than 128 KiB (this limit may +* increase in the future). This must also be less than or equal to +* unencoded_len. +*/ +const struct iovec __user *iov; +/* Number of iovecs. */ +unsigned long iovcnt; +/* +* Offset in file. +* +* For writes, must be aligned to the sector size of the filesystem. +*/ +__s64 offset; +/* Currently must be zero. */ +__u64 flags; + +/* +* For reads, the following members are output parameters that will +* contain the returned metadata for the encoded data. +* For writes, the following members must be set to the metadata for the +* encoded data. +*/ + +/* +* Length of the data in the file. +* +* Must be less than or equal to unencoded_len - unencoded_offset. For +* writes, must be aligned to the sector size of the filesystem unless +* the data ends at or beyond the current end of the file. +*/ +__u64 len; +/* +* Length of the unencoded (i.e., decrypted and decompressed) data. +* +* For writes, must be no more than 128 KiB (this limit may increase in +* the future). If the unencoded data is actually longer than +* unencoded_len, then it is truncated; if it is shorter, then it is +* extended with zeroes. +*/ +__u64 unencoded_len; +/* +* Offset from the first byte of the unencoded data to the first byte of +* logical data in the file. +* +* Must be less than unencoded_len. +*/ +__u64 unencoded_offset; +/* +* BTRFS_ENCODED_IO_COMPRESSION_* type. +* +* For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE. +*/ +__u32 compression; +/* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */ +__u32 encryption; +/* +* Reserved for future expansion. +* +* For reads, always returned as zero. Users should check for non-zero +* bytes. If there are any, then the kernel has a newer version of this +* structure with additional information that the user definition is +* missing. +* +* For writes, must be zeroed. +*/ +__u8 reserved[32 ]; +}; + +/* Data is not compressed. */ +#define BTRFS_ENCODED_IO_COMPRESSION_NONE 0 +/* Data is compressed as a single zlib stream. */ +#define BTRFS_ENCODED_IO_COMPRESSION_ZLIB 1 +/* +* Data is compressed as a single zstd frame with the windowLog compression +* parameter set to no more than 17. +*/ +#define BTRFS_ENCODED_IO_COMPRESSION_ZSTD 2 +/* +* Data is compressed sector by sector (using the sector size indicated by the +* name of the constant) with LZO1X and wrapped in the format documented in +* fs/btrfs/lzo.c. For writes, the compression sector size must match the +* filesystem sector size. +*/ +#define BTRFS_ENCODED_IO_COMPRESSION_LZO_4K 3 +#define BTRFS_ENCODED_IO_COMPRESSION_LZO_8K 4 +#define BTRFS_ENCODED_IO_COMPRESSION_LZO_16K 5 +#define BTRFS_ENCODED_IO_COMPRESSION_LZO_32K 6 +#define BTRFS_ENCODED_IO_COMPRESSION_LZO_64K 7 +#define BTRFS_ENCODED_IO_COMPRESSION_TYPES 8 + +/* Data is not encrypted. */ +#define BTRFS_ENCODED_IO_ENCRYPTION_NONE 0 +#define BTRFS_ENCODED_IO_ENCRYPTION_TYPES 1 +/* Error codes as returned by the kernel */ enum btrfs_err_code {BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET =1 ,
@@ -997,5 +1125,9 @@ enum btrfs_err_code { struct btrfs_ioctl_ino_lookup_user_args)#define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \ struct btrfs_ioctl_vol_args_v2) +#define BTRFS_IOC_ENCODED_READ _IOR(BTRFS_IOCTL_MAGIC, 64, \ + struct btrfs_ioctl_encoded_io_args) +#define BTRFS_IOC_ENCODED_WRITE _IOW(BTRFS_IOCTL_MAGIC, 64, \ + struct btrfs_ioctl_encoded_io_args)#endif /* _UAPI_LINUX_BTRFS_H */
Signature · 1 line
2.35.1
Re: [PATCH v13 08/17] btrfs: add definitions + documentation for encoded I/O ioctls
Quoted 112 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
In order to allow sending and receiving compressed data without decompressing it, we need an interface to write pre-compressed data directly to the filesystem and the matching interface to read compressed data without decompressing it. This adds the definitions for ioctls to do that and detailed explanations of how to use them.
Reviewed-by Nikolay Borisov <[email protected]>Signed-off-by Omar Sandoval <[email protected]>--- include/uapi/linux/btrfs.h | 132 +++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+)include/uapi/linux/btrfs.h +90
-0@@ -869,6 +869,134 @@ struct btrfs_ioctl_get_subvol_rootref_args {__u8 align[7 ]; }; +/* +* Data and metadata for an encoded read or write. +* +* Encoded I/O bypasses any encoding automatically done by the filesystem (e.g., +* compression). This can be used to read the compressed contents of a file or +* write pre-compressed data directly to a file. +* +* BTRFS_IOC_ENCODED_READ and BTRFS_IOC_ENCODED_WRITE are essentially +* preadv/pwritev with additional metadata about how the data is encoded and the +* size of the unencoded data. +* +* BTRFS_IOC_ENCODED_READ fills the given iovecs with the encoded data, fills +* the metadata fields, and returns the size of the encoded data. It reads one +* extent per call. It can also read data which is not encoded. +* +* BTRFS_IOC_ENCODED_WRITE uses the metadata fields, writes the encoded data +* from the iovecs, and returns the size of the encoded data. Note that the +* encoded data is not validated when it is written; if it is not valid (e.g., +* it cannot be decompressed), then a subsequent read may return an error. +* +* Since the filesystem page cache contains decoded data, encoded I/O bypasses +* the page cache. Encoded I/O requires CAP_SYS_ADMIN. +*/ +struct btrfs_ioctl_encoded_io_args { +/* Input parameters for both reads and writes. */ + +/* +* iovecs containing encoded data. +* +* For reads, if the size of the encoded data is larger than the sum of +* iov[n].iov_len for 0 <= n < iovcnt, then the ioctl fails with +* ENOBUFS. +* +* For writes, the size of the encoded data is the sum of iov[n].iov_len +* for 0 <= n < iovcnt. This must be less than 128 KiB (this limit may +* increase in the future). This must also be less than or equal to +* unencoded_len. +*/ +const struct iovec __user *iov; +/* Number of iovecs. */ +unsigned long iovcnt; +/* +* Offset in file. +* +* For writes, must be aligned to the sector size of the filesystem. +*/ +__s64 offset; +/* Currently must be zero. */ +__u64 flags; + +/* +* For reads, the following members are output parameters that will +* contain the returned metadata for the encoded data. +* For writes, the following members must be set to the metadata for the +* encoded data. +*/ + +/* +* Length of the data in the file. +* +* Must be less than or equal to unencoded_len - unencoded_offset. For +* writes, must be aligned to the sector size of the filesystem unless +* the data ends at or beyond the current end of the file. +*/ +__u64 len; +/* +* Length of the unencoded (i.e., decrypted and decompressed) data. +* +* For writes, must be no more than 128 KiB (this limit may increase in +* the future). If the unencoded data is actually longer than +* unencoded_len, then it is truncated; if it is shorter, then it is +* extended with zeroes. +*/ +__u64 unencoded_len; +/* +* Offset from the first byte of the unencoded data to the first byte of +* logical data in the file. +* +* Must be less than unencoded_len. +*/ +__u64 unencoded_offset; +/* +* BTRFS_ENCODED_IO_COMPRESSION_* type. +* +* For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE. +*/ +__u32 compression; +/* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */ +__u32 encryption; +/*
include/uapi/linux/btrfs.h struct btrfs_ioctl_get_subvol_rootref_args { · line 971
Quoted 10 lines
include/uapi/linux/btrfs.h +10
-0+* Reserved for future expansion. +* +* For reads, always returned as zero. Users should check for non-zero +* bytes. If there are any, then the kernel has a newer version of this +* structure with additional information that the user definition is +* missing. +* +* For writes, must be zeroed. +*/ +__u8 reserved[32 ];
This is 32 bytes, so 4 x u64, that's not bad but for future expanstion I'd rather add more than less. Now the structure size is 96 bytes, so if it's 128 bytes then it's a power of two and we'd get 64 reserved bytes.
Re: [PATCH v13 08/17] btrfs: add definitions + documentation for encoded I/O ioctls
Quoted 119 lines · David Sterba
Quoted 118 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
In order to allow sending and receiving compressed data without decompressing it, we need an interface to write pre-compressed data directly to the filesystem and the matching interface to read compressed data without decompressing it. This adds the definitions for ioctls to do that and detailed explanations of how to use them.
Reviewed-by Nikolay Borisov <[email protected]>Signed-off-by Omar Sandoval <[email protected]>--- include/uapi/linux/btrfs.h | 132 +++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+)include/uapi/linux/btrfs.h +96
-0@@ -869,6 +869,134 @@ struct btrfs_ioctl_get_subvol_rootref_args {__u8 align[7 ]; }; +/* +* Data and metadata for an encoded read or write. +* +* Encoded I/O bypasses any encoding automatically done by the filesystem (e.g., +* compression). This can be used to read the compressed contents of a file or +* write pre-compressed data directly to a file. +* +* BTRFS_IOC_ENCODED_READ and BTRFS_IOC_ENCODED_WRITE are essentially +* preadv/pwritev with additional metadata about how the data is encoded and the +* size of the unencoded data. +* +* BTRFS_IOC_ENCODED_READ fills the given iovecs with the encoded data, fills +* the metadata fields, and returns the size of the encoded data. It reads one +* extent per call. It can also read data which is not encoded. +* +* BTRFS_IOC_ENCODED_WRITE uses the metadata fields, writes the encoded data +* from the iovecs, and returns the size of the encoded data. Note that the +* encoded data is not validated when it is written; if it is not valid (e.g., +* it cannot be decompressed), then a subsequent read may return an error. +* +* Since the filesystem page cache contains decoded data, encoded I/O bypasses +* the page cache. Encoded I/O requires CAP_SYS_ADMIN. +*/ +struct btrfs_ioctl_encoded_io_args { +/* Input parameters for both reads and writes. */ + +/* +* iovecs containing encoded data. +* +* For reads, if the size of the encoded data is larger than the sum of +* iov[n].iov_len for 0 <= n < iovcnt, then the ioctl fails with +* ENOBUFS. +* +* For writes, the size of the encoded data is the sum of iov[n].iov_len +* for 0 <= n < iovcnt. This must be less than 128 KiB (this limit may +* increase in the future). This must also be less than or equal to +* unencoded_len. +*/ +const struct iovec __user *iov; +/* Number of iovecs. */ +unsigned long iovcnt; +/* +* Offset in file. +* +* For writes, must be aligned to the sector size of the filesystem. +*/ +__s64 offset; +/* Currently must be zero. */ +__u64 flags; + +/* +* For reads, the following members are output parameters that will +* contain the returned metadata for the encoded data. +* For writes, the following members must be set to the metadata for the +* encoded data. +*/ + +/* +* Length of the data in the file. +* +* Must be less than or equal to unencoded_len - unencoded_offset. For +* writes, must be aligned to the sector size of the filesystem unless +* the data ends at or beyond the current end of the file. +*/ +__u64 len; +/* +* Length of the unencoded (i.e., decrypted and decompressed) data. +* +* For writes, must be no more than 128 KiB (this limit may increase in +* the future). If the unencoded data is actually longer than +* unencoded_len, then it is truncated; if it is shorter, then it is +* extended with zeroes. +*/ +__u64 unencoded_len; +/* +* Offset from the first byte of the unencoded data to the first byte of +* logical data in the file. +* +* Must be less than unencoded_len. +*/ +__u64 unencoded_offset; +/* +* BTRFS_ENCODED_IO_COMPRESSION_* type. +* +* For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE. +*/ +__u32 compression; +/* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */ +__u32 encryption; +/* +* Reserved for future expansion. +* +* For reads, always returned as zero. Users should check for non-zero +* bytes. If there are any, then the kernel has a newer version of this +* structure with additional information that the user definition is +* missing.
Quoted 8 lines
include/uapi/linux/btrfs.h struct btrfs_ioctl_get_subvol_rootref_args { · line 971
Quoted 4 lines
include/uapi/linux/btrfs.h +4
-0+* +* For writes, must be zeroed. +*/ +__u8 reserved[32 ];This is 32 bytes, so 4 x u64, that's not bad but for future expanstion I'd rather add more than less. Now the structure size is 96 bytes, so if it's 128 bytes then it's a power of two and we'd get 64 reserved bytes.
That seems reasonable. This changes the ABI (including the ioctl request number), so we should probably have it finalized soon. Would you like me to send another version with this change and the others you mentioned, or will you fix it in for-next?
Re: [PATCH v13 08/17] btrfs: add definitions + documentation for encoded I/O ioctls
Quoted 25 lines · Omar Sandoval
Quoted 15 lines · David Sterba
Quoted 14 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
include/uapi/linux/btrfs.h +13
-0+* +* For writes, must not be BTRFS_ENCODED_IO_COMPRESSION_NONE. +*/ +__u32 compression; +/* Currently always BTRFS_ENCODED_IO_ENCRYPTION_NONE. */ +__u32 encryption; +/* +* Reserved for future expansion. +* +* For reads, always returned as zero. Users should check for non-zero +* bytes. If there are any, then the kernel has a newer version of this +* structure with additional information that the user definition is +* missing. Quoted 8 lines
include/uapi/linux/btrfs.h struct btrfs_ioctl_get_subvol_rootref_args { · line 971
Quoted 4 lines
include/uapi/linux/btrfs.h +4
-0+* +* For writes, must be zeroed. +*/ +__u8 reserved[32 ];This is 32 bytes, so 4 x u64, that's not bad but for future expanstion I'd rather add more than less. Now the structure size is 96 bytes, so if it's 128 bytes then it's a power of two and we'd get 64 reserved bytes.
Quoted 2 lines
That seems reasonable. This changes the ABI (including the ioctl request number), so we should probably have it finalized soon.
Technically we have the whole development cycle to get the API/ABI right, so it's not a big deal (yet).
Quoted 3 lines
Would you like me
to send another version with this change and the others you mentioned, or will you fix it in for-next?
I'll fix the issues in the local topic branch, I made more changes (coding style) so resending would need more work.
PATCH v13 9/17 btrfs: add BTRFS_IOC_ENCODED_READ
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
There are 4 main cases:
1. Inline extents: we copy the data straight out of the extent buffer.
2. Hole/preallocated extents: we fill in zeroes.
3. Regular, uncompressed extents: we read the sectors we need directly from disk.
4. Regular, compressed extents: we read the entire compressed extent from disk and indicate what subset of the decompressed extent is in the file.
This initial implementation simplifies a few things that can be improved in the future:
- Cases 1, 3, and 4 allocate temporary memory to read into before copying out to userspace.
- We don't do read repair, because it turns out that read repair is currently broken for compressed data.
- We hold the inode lock during the operation.
Note that we don't need to hold the mmap lock. We may race with btrfs_page_mkwrite() and read the old data from before the page was dirtied:
btrfs_page_mkwrite btrfs_encoded_read
---------------------------------------------------
(enter) (enter)
btrfs_wait_ordered_rangelock_extent_bitsbtrfs_page_set_dirtyunlock_extent_cached
(exit)
lock_extent_bits
read extent (dirty page hasn't been flushed,
so this is the old data)
unlock_extent_cached
(exit)
we read the old data from before the page was dirtied. But, that's true even if we were to hold the mmap lock:
btrfs_page_mkwrite btrfs_encoded_read
-------------------------------------------------------------------
(enter) (enter)
btrfs_inode_lock(BTRFS_ILOCK_MMAP)down_read(i_mmap_lock) (blocked)
btrfs_wait_ordered_range lock_extent_bits
read extent (page hasn't been dirtied,
so this is the old data)
unlock_extent_cached btrfs_inode_unlock(BTRFS_ILOCK_MMAP)down_read(i_mmap_lock) returnslock_extent_bitsbtrfs_page_set_dirtyunlock_extent_cached
In other words, this is inherently racy, so it's fine that we return the old data in this tiny window.
--- fs/btrfs/ctree.h | 4 + fs/btrfs/inode.c | 501 +++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.c | 106 ++++++++++ 3 files changed, 611 insertions(+)
fs/btrfs/ctree.h +4 -0
@@ -3295,6 +3295,10 @@ int btrfs_writepage_cow_fixup(struct page *page);void btrfs_writepage_endio_finish_ordered (struct btrfs_inode *inode,struct page *page,u64 start,u64 end,bool uptodate); +struct btrfs_ioctl_encoded_io_args ; +ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter, +struct btrfs_ioctl_encoded_io_args *encoded); +extern const struct dentry_operations btrfs_dentry_operations;extern const struct iomap_ops btrfs_dio_iomap_ops;extern const struct iomap_dio_ops btrfs_dio_ops;
fs/btrfs/inode.c +501 -0
507 lines not on this page · raw message
fs/btrfs/ioctl.c +106 -0
130 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v13 10/17 btrfs: add BTRFS_IOC_ENCODED_WRITE
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The implementation resembles direct I/O: we have to flush any ordered extents, invalidate the page cache, and do the io tree/delalloc/extent map/ordered extent dance. From there, we can reuse the compression code with a minor modification to distinguish the write from writeback. This also creates inline extents when possible.
--- fs/btrfs/compression.c | 7 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 4 + fs/btrfs/file.c | 65 ++++++++-- fs/btrfs/inode.c | 255 +++++++++++++++++++++++++++++++++++++++- fs/btrfs/ioctl.c | 102 ++++++++++++++++ fs/btrfs/ordered-data.c | 12 +- fs/btrfs/ordered-data.h | 5 +- 8 files changed, 436 insertions(+), 20 deletions(-)
fs/btrfs/compression.c +5 -2
@@ -383,7 +383,8 @@ static void finish_compressed_bio_write(struct compressed_bio *cb) cb->start, cb->start + cb->len -1 , !cb->errors); -end_compressed_writeback (inode, cb); +if (cb->writeback) +end_compressed_writeback (inode, cb);/* Note, our inode could be gone now */ /*
@@ -506,7 +507,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,struct page **compressed_pages, unsigned int nr_pages, unsigned int write_flags, - struct cgroup_subsys_state *blkcg_css) + struct cgroup_subsys_state *blkcg_css, + bool writeback) { struct btrfs_fs_info *fs_info = inode->root->fs_info;struct bio *bio =NULL ;
@@ -531,6 +533,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start, cb->mirror_num =0 ; cb->compressed_pages = compressed_pages; cb->compressed_len = compressed_len; + cb->writeback = writeback; cb->orig_bio =NULL ; cb->nr_pages = nr_pages;
fs/btrfs/compression.h +5 -1
@@ -54,6 +54,9 @@ struct compressed_bio {/* The compression algorithm for this bio */ u8 compress_type; +/* Whether this is a write for writeback. */ +bool writeback; +/* IO errors */ u8 errors;int mirror_num;
@@ -97,7 +100,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,struct page **compressed_pages, unsigned int nr_pages, unsigned int write_flags, - struct cgroup_subsys_state *blkcg_css); + struct cgroup_subsys_state *blkcg_css, + bool writeback);blk_status_t btrfs_submit_compressed_read (struct inode *inode,struct bio *bio,int mirror_num,unsigned long bio_flags);
fs/btrfs/ctree.h +4 -0
@@ -3298,6 +3298,8 @@ void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,struct btrfs_ioctl_encoded_io_args ;ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter,struct btrfs_ioctl_encoded_io_args *encoded); +ssize_t btrfs_do_encoded_write (struct kiocb *iocb,struct iov_iter *from, +const struct btrfs_ioctl_encoded_io_args *encoded);extern const struct dentry_operations btrfs_dentry_operations;extern const struct iomap_ops btrfs_dio_iomap_ops;
@@ -3361,6 +3363,8 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode, struct btrfs_trans_handle **trans_out);int btrfs_mark_extent_written (struct btrfs_trans_handle *trans,struct btrfs_inode *inode,u64 start,u64 end); +ssize_t btrfs_do_write_iter (struct kiocb *iocb,struct iov_iter *from, +const struct btrfs_ioctl_encoded_io_args *encoded);int btrfs_release_file (struct inode *inode,struct file *file); int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages, size_t num_pages, loff_t pos, size_t write_bytes,
fs/btrfs/file.c +54 -11
94 lines not on this page · raw message
fs/btrfs/inode.c +253 -2
279 lines not on this page · raw message
fs/btrfs/ioctl.c +102 -0
123 lines not on this page · raw message
fs/btrfs/ordered-data.c +9 -3
@@ -521,9 +521,15 @@ void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,spin_lock (&btrfs_inode->lock);btrfs_mod_outstanding_extents (btrfs_inode,-1 );spin_unlock (&btrfs_inode->lock); -if (root != fs_info->tree_root) -btrfs_delalloc_release_metadata (btrfs_inode, entry->num_bytes, - false); +if (root != fs_info->tree_root) { +u64 release; + +if (test_bit (BTRFS_ORDERED_ENCODED , &entry->flags)) + release = entry->disk_num_bytes; +else + release = entry->num_bytes; +btrfs_delalloc_release_metadata (btrfs_inode, release, false); + }percpu_counter_add_batch (&fs_info->ordered_bytes, -entry->num_bytes, fs_info->delalloc_batch);
fs/btrfs/ordered-data.h +4 -1
@@ -74,6 +74,8 @@ enum {BTRFS_ORDERED_LOGGED_CSUM ,/* We wait for this extent to complete in the current transaction */ BTRFS_ORDERED_PENDING , +/* BTRFS_IOC_ENCODED_WRITE */ + BTRFS_ORDERED_ENCODED, };/* BTRFS_ORDERED_* flags that specify the type of the extent. */
@@ -81,7 +83,8 @@ enum { (1UL <<BTRFS_ORDERED_NOCOW ) | \ (1UL <<BTRFS_ORDERED_PREALLOC ) | \ (1UL <<BTRFS_ORDERED_COMPRESSED ) | \ - (1UL << BTRFS_ORDERED_DIRECT)) + (1UL <<BTRFS_ORDERED_DIRECT ) | \ + (1UL << BTRFS_ORDERED_ENCODED)) struct btrfs_ordered_extent {/* logical offset in the file */
Signature · 1 line
2.35.1
PATCH v13 11/17 btrfs: send: remove unused send_ctx::{total,cmd}_send_size
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
We collect these statistics but have never used them for anything.
--- fs/btrfs/send.c | 4 ---- 1 file changed, 4 deletions(-)
fs/btrfs/send.c +0 -4
@@ -82,8 +82,6 @@ struct send_ctx {char *send_buf;u32 send_size;u32 send_max_size; -u64 total_send_size; -u64 cmd_send_size[BTRFS_SEND_C_MAX +1 ];u64 flags;/* 'flags' member of btrfs_ioctl_send_args is u64 */ /* Protocol version compatibility requested */ u32 proto;
@@ -727,8 +725,6 @@ static int send_cmd(struct send_ctx *sctx) ret =write_buf (sctx->send_filp, sctx->send_buf, sctx->send_size, &sctx->send_off); - sctx->total_send_size += sctx->send_size; - sctx->cmd_send_size[get_unaligned_le16 (&hdr->cmd)] += sctx->send_size; sctx->send_size =0 ;return ret;
Signature · 1 line
2.35.1
PATCH v13 12/17 btrfs: send: explicitly number commands and attributes
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Commit e77fbf990316 ("btrfs: send: prepare for v2 protocol") added _BTRFS_SEND_C_MAX_V* macros equal to the maximum command number for the version plus 1, but as written this creates gaps in the number space. The maximum command number is currently 22, and __BTRFS_SEND_C_MAX_V1 is accordingly 23. But then __BTRFS_SEND_C_MAX_V2 is 24, suggesting that v2 has a command numbered 23, and __BTRFS_SEND_C_MAX is 25, suggesting that 23 and 24 are valid commands.
Instead, let's explicitly number all of the commands, attributes, and sentinel MAX constants.
--- fs/btrfs/send.c | 4 +- fs/btrfs/send.h | 106 ++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 56 deletions(-)
fs/btrfs/send.c +2 -2
@@ -326,8 +326,8 @@ __maybe_unusedstatic bool proto_cmd_ok (const struct send_ctx *sctx,int cmd) {switch (sctx->proto) { -case 1 :return cmd < __BTRFS_SEND_C_MAX_V1; -case 2 :return cmd < __BTRFS_SEND_C_MAX_V2; +case 1 :return cmd <=BTRFS_SEND_C_MAX_V1 ; +case 2 :return cmd <=BTRFS_SEND_C_MAX_V2 ;default :return false; } }
fs/btrfs/send.h +52 -54
136 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v13 13/17 btrfs: add send stream v2 definitions
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
This adds the definitions of the new commands for send stream version 2 and their respective attributes: fallocate, FS_IOC_SETFLAGS (a.k.a. chattr), and encoded writes. It also documents two changes to the send stream format in v2: the receiver shouldn't assume a maximum command size, and the DATA attribute is encoded differently to allow for writes larger than 64k. These will be implemented in subsequent changes, and then the ioctl will accept the new version and flag.
--- fs/btrfs/send.c | 2 +- fs/btrfs/send.h | 40 ++++++++++++++++++++++++++++++++++---- include/uapi/linux/btrfs.h | 7 +++++++ 3 files changed, 44 insertions(+), 5 deletions(-)
fs/btrfs/send.c +1 -1
@@ -7564,7 +7564,7 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg) sctx->clone_roots_cnt = arg->clone_sources_count; - sctx->send_max_size =BTRFS_SEND_BUF_SIZE ; + sctx->send_max_size =BTRFS_SEND_BUF_SIZE_V1 ; sctx->send_buf =kvmalloc (sctx->send_max_size,GFP_KERNEL );if (!sctx->send_buf) { ret = -ENOMEM ;
fs/btrfs/send.h +36 -4
@@ -12,7 +12,11 @@#define BTRFS_SEND_STREAM_MAGIC "btrfs-stream"#define BTRFS_SEND_STREAM_VERSION 1 -#define BTRFS_SEND_BUF_SIZE SZ_64K +/* +* In send stream v1, no command is larger than 64k. In send stream v2, no limit +* should be assumed. +*/ +#define BTRFS_SEND_BUF_SIZE_V1 SZ_64Kenum btrfs_tlv_type {BTRFS_TLV_U8 ,
@@ -80,16 +84,20 @@ enum btrfs_send_cmd {BTRFS_SEND_C_MAX_V1 =22 ,/* Version 2 */ -BTRFS_SEND_C_MAX_V2 =22 , +BTRFS_SEND_C_FALLOCATE =23 , +BTRFS_SEND_C_SETFLAGS =24 , +BTRFS_SEND_C_ENCODED_WRITE =25 , +BTRFS_SEND_C_MAX_V2 =25 ,/* End */ - BTRFS_SEND_C_MAX = 22, + BTRFS_SEND_C_MAX = 25, };/* attributes in send stream */ enum {BTRFS_SEND_A_UNSPEC =0 , +/* Version 1 */ BTRFS_SEND_A_UUID =1 , BTRFS_SEND_A_CTRANSID = 2,
@@ -112,6 +120,11 @@ enum {BTRFS_SEND_A_PATH_LINK =17 ,BTRFS_SEND_A_FILE_OFFSET =18 , +/* +* As of send stream v2, this attribute is special: it must be the last +* attribute in a command, its header contains only the type, and its +* length is implicitly the remaining length of the command. +*/ BTRFS_SEND_A_DATA =19 , BTRFS_SEND_A_CLONE_UUID = 20,
@@ -120,7 +133,26 @@ enum {BTRFS_SEND_A_CLONE_OFFSET =23 ,BTRFS_SEND_A_CLONE_LEN =24 , - BTRFS_SEND_A_MAX = 24, +BTRFS_SEND_A_MAX_V1 =24 , + +/* Version 2 */ +BTRFS_SEND_A_FALLOCATE_MODE =25 , + +BTRFS_SEND_A_SETFLAGS_FLAGS =26 , + +BTRFS_SEND_A_UNENCODED_FILE_LEN =27 , +BTRFS_SEND_A_UNENCODED_LEN =28 , +BTRFS_SEND_A_UNENCODED_OFFSET =29 , +/* +* COMPRESSION and ENCRYPTION default to NONE (0) if omitted from +* BTRFS_SEND_C_ENCODED_WRITE. +*/ +BTRFS_SEND_A_COMPRESSION =30 , +BTRFS_SEND_A_ENCRYPTION =31 , +BTRFS_SEND_A_MAX_V2 =31 , + +/* End */ + BTRFS_SEND_A_MAX = 31, };#ifdef __KERNEL__
include/uapi/linux/btrfs.h +7 -0
@@ -777,6 +777,13 @@ struct btrfs_ioctl_received_subvol_args {*/ #define BTRFS_SEND_FLAG_VERSION 0x8 +/* +* Send compressed data using the ENCODED_WRITE command instead of decompressing +* the data and sending it with the WRITE command. This requires protocol +* version >= 2. +*/ +#define BTRFS_SEND_FLAG_COMPRESSED 0x10 +#define BTRFS_SEND_FLAG_MASK \ (BTRFS_SEND_FLAG_NO_FILE_DATA | \ BTRFS_SEND_FLAG_OMIT_STREAM_HEADER | \
Signature · 1 line
2.35.1
PATCH v13 14/17 btrfs: send: write larger chunks when using stream v2
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The length field of the send stream TLV header is 16 bits. This means that the maximum amount of data that can be sent for one write is 64k minus one. However, encoded writes must be able to send the maximum compressed extent (128k) in one command. To support this, send stream version 2 encodes the DATA attribute differently: it has no length field, and the length is implicitly up to the end of containing command (which has a 32-bit length field). Although this is necessary for encoded writes, normal writes can benefit from it, too.
Also add a check to enforce that the DATA attribute is last. It is only strictly necessary for v2, but we might as well make v1 consistent with it.
For v2, let's bump up the send buffer to the maximum compressed extent size plus 16k for the other metadata (144k total). Since this will most likely be vmalloc'd (and always will be after the next commit), we round it up to the next page since we might as well use the rest of the page on systems with >16k pages.
--- fs/btrfs/send.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-)
fs/btrfs/send.c +34 -8
@@ -82,6 +82,7 @@ struct send_ctx {char *send_buf;u32 send_size;u32 send_max_size; +bool put_data;u64 flags;/* 'flags' member of btrfs_ioctl_send_args is u64 */ /* Protocol version compatibility requested */ u32 proto;
@@ -589,6 +590,9 @@ static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)int total_len =sizeof (*hdr) + len;int left = sctx->send_max_size - sctx->send_size; +if (WARN_ON_ONCE (sctx->put_data)) +return -EINVAL ; +if (unlikely (left < total_len))return -EOVERFLOW ;
@@ -726,6 +730,7 @@ static int send_cmd(struct send_ctx *sctx) &sctx->send_off); sctx->send_size =0 ; + sctx->put_data = false;return ret; }
@@ -4927,14 +4932,30 @@ static inline u64 max_send_read_size(const struct send_ctx *sctx)static int put_data_header (struct send_ctx *sctx,u32 len) { -struct btrfs_tlv_header *hdr; +if (WARN_ON_ONCE (sctx->put_data)) +return -EINVAL ; + sctx->put_data = true; +if (sctx->proto >=2 ) { +/* +* In v2, the data attribute header doesn't include a length; it +* is implicitly to the end of the command. +*/ +if (sctx->send_max_size - sctx->send_size <2 + len) +return -EOVERFLOW ; +put_unaligned_le16 (BTRFS_SEND_A_DATA , + sctx->send_buf + sctx->send_size); + sctx->send_size +=2 ; + }else { +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); +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 ; }
@@ -7564,7 +7585,12 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg) sctx->clone_roots_cnt = arg->clone_sources_count; - sctx->send_max_size =BTRFS_SEND_BUF_SIZE_V1 ; +if (sctx->proto >=2 ) { + sctx->send_max_size =ALIGN (SZ_16K +BTRFS_MAX_COMPRESSED , +PAGE_SIZE ); + }else { + sctx->send_max_size =BTRFS_SEND_BUF_SIZE_V1 ; + } sctx->send_buf =kvmalloc (sctx->send_max_size,GFP_KERNEL );if (!sctx->send_buf) { ret = -ENOMEM ;
Signature · 1 line
2.35.1
PATCH v13 15/17 btrfs: send: allocate send buffer with alloc_page() and vmap() for v2
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
For encoded writes, we need the raw pages for reading compressed data directly via a bio. So, replace kvmalloc() with vmap() so we have access to the raw pages. 144k is large enough that it usually gets allocated with vmalloc(), anyways.
--- fs/btrfs/send.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
fs/btrfs/send.c +31 -2
@@ -83,6 +83,7 @@ struct send_ctx {u32 send_size;u32 send_max_size;bool put_data; +struct page **send_buf_pages;u64 flags;/* 'flags' member of btrfs_ioctl_send_args is u64 */ /* Protocol version compatibility requested */ u32 proto;
@@ -7497,6 +7498,7 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)struct btrfs_root *clone_root;struct send_ctx *sctx =NULL ;u32 i; +u32 send_buf_num_pages =0 ;u64 *clone_sources_tmp =NULL ;int clone_sources_to_rollback =0 ;size_t alloc_size;
@@ -7588,10 +7590,28 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)if (sctx->proto >=2 ) { sctx->send_max_size =ALIGN (SZ_16K +BTRFS_MAX_COMPRESSED ,PAGE_SIZE ); + send_buf_num_pages = sctx->send_max_size >>PAGE_SHIFT ; + sctx->send_buf_pages =kcalloc (send_buf_num_pages, +sizeof (*sctx->send_buf_pages), +GFP_KERNEL ); +if (!sctx->send_buf_pages) { + send_buf_num_pages =0 ; + ret = -ENOMEM ; + goto out; + } +for (i =0 ; i < send_buf_num_pages; i++) { + sctx->send_buf_pages[i] =alloc_page (GFP_KERNEL ); +if (!sctx->send_buf_pages[i]) { + ret = -ENOMEM ; + goto out; + } + } + sctx->send_buf =vmap (sctx->send_buf_pages, send_buf_num_pages, +VM_MAP ,PAGE_KERNEL ); }else { sctx->send_max_size =BTRFS_SEND_BUF_SIZE_V1 ; + sctx->send_buf =kvmalloc (sctx->send_max_size,GFP_KERNEL ); } - sctx->send_buf =kvmalloc (sctx->send_max_size,GFP_KERNEL );if (!sctx->send_buf) { ret = -ENOMEM ; goto out;
@@ -7784,7 +7804,16 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)fput (sctx->send_filp);kvfree (sctx->clone_roots); -kvfree (sctx->send_buf); +if (sctx->proto >=2 ) { +vunmap (sctx->send_buf); +for (i =0 ; i < send_buf_num_pages; i++) { +if (sctx->send_buf_pages[i]) +__free_page (sctx->send_buf_pages[i]); + } +kfree (sctx->send_buf_pages); + }else { +kvfree (sctx->send_buf); + }name_cache_free (sctx);
Signature · 1 line
2.35.1
PATCH v13 16/17 btrfs: send: send compressed extents with encoded writes
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Now that all of the pieces are in place, we can use the ENCODED_WRITE command to send compressed extents when appropriate.
--- fs/btrfs/ctree.h | 6 ++ fs/btrfs/inode.c | 13 +-- fs/btrfs/send.c | 234 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 228 insertions(+), 25 deletions(-)
fs/btrfs/ctree.h +6 -0
@@ -3295,6 +3295,12 @@ int btrfs_writepage_cow_fixup(struct page *page);void btrfs_writepage_endio_finish_ordered (struct btrfs_inode *inode,struct page *page,u64 start,u64 end,bool uptodate); +int btrfs_encoded_io_compression_from_extent (struct btrfs_fs_info *fs_info, +int compress_type); +int btrfs_encoded_read_regular_fill_pages (struct btrfs_inode *inode, +u64 file_offset,u64 disk_bytenr, +u64 disk_io_size, +struct page **pages);struct btrfs_ioctl_encoded_io_args ;ssize_t btrfs_encoded_read (struct kiocb *iocb,struct iov_iter *iter,struct btrfs_ioctl_encoded_io_args *encoded);
fs/btrfs/inode.c +5 -8
@@ -10133,9 +10133,8 @@ void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) } } -static int btrfs_encoded_io_compression_from_extent ( -struct btrfs_fs_info *fs_info, -int compress_type) +int btrfs_encoded_io_compression_from_extent (struct btrfs_fs_info *fs_info, +int compress_type) {switch (compress_type) {case BTRFS_COMPRESS_NONE :
@@ -10342,11 +10341,9 @@ static void btrfs_encoded_read_endio(struct bio *bio)bio_put (bio); } -static int btrfs_encoded_read_regular_fill_pages (struct btrfs_inode *inode, -u64 file_offset, -u64 disk_bytenr, -u64 disk_io_size, -struct page **pages) +int btrfs_encoded_read_regular_fill_pages (struct btrfs_inode *inode, +u64 file_offset,u64 disk_bytenr, +u64 disk_io_size,struct page **pages) {struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_encoded_read_private priv = {
fs/btrfs/send.c +217 -17
296 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v13 17/17 btrfs: send: enable support for stream v2 and compressed writes
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Now that the new support is implemented, allow the ioctl to accept v2 and the compressed flag, and update the version in sysfs.
--- fs/btrfs/send.c | 7 +++++-- fs/btrfs/send.h | 2 +- include/uapi/linux/btrfs.h | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-)
fs/btrfs/send.c +5 -2
@@ -690,8 +690,7 @@ static int send_header(struct send_ctx *sctx)struct btrfs_stream_header hdr;strcpy (hdr.magic,BTRFS_SEND_STREAM_MAGIC ); - hdr.version =cpu_to_le32 (BTRFS_SEND_STREAM_VERSION ); - + hdr.version =cpu_to_le32 (sctx->proto);return write_buf (sctx->send_filp, &hdr,sizeof (hdr), &sctx->send_off); }
@@ -7768,6 +7767,10 @@ long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg) }else { sctx->proto =1 ; } +if ((arg->flags &BTRFS_SEND_FLAG_COMPRESSED ) && sctx->proto <2 ) { + ret = -EINVAL ; + goto out; + } sctx->send_filp =fget (arg->send_fd);if (!sctx->send_filp) {
fs/btrfs/send.h +1 -1
@@ -10,7 +10,7 @@#include "ctree.h" #define BTRFS_SEND_STREAM_MAGIC "btrfs-stream" -#define BTRFS_SEND_STREAM_VERSION 1 +#define BTRFS_SEND_STREAM_VERSION 2/* * In send stream v1, no command is larger than 64k. In send stream v2, no limit
include/uapi/linux/btrfs.h +2 -1
@@ -788,7 +788,8 @@ struct btrfs_ioctl_received_subvol_args { (BTRFS_SEND_FLAG_NO_FILE_DATA | \BTRFS_SEND_FLAG_OMIT_STREAM_HEADER | \BTRFS_SEND_FLAG_OMIT_END_CMD | \ - BTRFS_SEND_FLAG_VERSION) +BTRFS_SEND_FLAG_VERSION | \ + BTRFS_SEND_FLAG_COMPRESSED)struct btrfs_ioctl_send_args {__s64 send_fd;/* in */
Signature · 1 line
2.35.1
PATCH v13 1/10 btrfs-progs: receive: support v2 send stream larger tlv_len
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
An encoded extent can be up to 128K in length, which exceeds the largest value expressible by the current send stream format's 16 bit tlv_len field. Since encoded writes cannot be split into multiple writes by btrfs send, the send stream format must change to accommodate encoded writes.
Supporting this changed format requires retooling how we store the commands we have processed. We currently store pointers to the struct btrfs_tlv_headers in the command buffer. This is not sufficient to represent the new BTRFS_SEND_A_DATA format. Instead, parse the attribute headers and store them in a new struct btrfs_send_attribute which has a 32-bit length field. This is transparent to users of the various TLV_GET macros.
--- common/send-stream.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)
common/send-stream.c +25 -9
@@ -24,13 +24,23 @@#include "crypto/crc32c.h" #include "common/utils.h" +struct btrfs_send_attribute { +u16 tlv_type; +/* +* Note: in btrfs_tlv_header, this is __le16, but we need 32 bits for +* attributes with file data as of version 2 of the send stream format +*/ +u32 tlv_len; +char *data; +}; +struct btrfs_send_stream {char read_buf[BTRFS_SEND_BUF_SIZE ];int fd;int cmd;struct btrfs_cmd_header *cmd_hdr; -struct btrfs_tlv_header *cmd_attrs[BTRFS_SEND_A_MAX +1 ]; +struct btrfs_send_attribute cmd_attrs[BTRFS_SEND_A_MAX +1 ];u32 version;/*
@@ -152,6 +162,7 @@ static int read_cmd(struct btrfs_send_stream *sctx)struct btrfs_tlv_header *tlv_hdr;u16 tlv_type;u16 tlv_len; +struct btrfs_send_attribute *send_attr; tlv_hdr = (struct btrfs_tlv_header *)data; tlv_type =le16_to_cpu (tlv_hdr->tlv_type);
@@ -164,10 +175,15 @@ static int read_cmd(struct btrfs_send_stream *sctx) goto out; } - sctx->cmd_attrs[tlv_type] = tlv_hdr; + send_attr = &sctx->cmd_attrs[tlv_type]; + send_attr->tlv_type = tlv_type; + send_attr->tlv_len = tlv_len; + pos +=sizeof (*tlv_hdr); + data +=sizeof (*tlv_hdr); - data +=sizeof (*tlv_hdr) + tlv_len; - pos +=sizeof (*tlv_hdr) + tlv_len; + send_attr->data = data; + pos += send_attr->tlv_len; + data += send_attr->tlv_len; } sctx->cmd = cmd;
@@ -180,7 +196,7 @@ out:static int tlv_get (struct btrfs_send_stream *sctx,int attr,void **data,int *len) {int ret; -struct btrfs_tlv_header *hdr; +struct btrfs_send_attribute *send_attr;if (attr <=0 || attr >BTRFS_SEND_A_MAX ) {error ("invalid attribute requested, attr = %d" , attr);
@@ -188,15 +204,15 @@ static int tlv_get(struct btrfs_send_stream *sctx, int attr, void **data, int *l goto out; } - hdr = sctx->cmd_attrs[attr]; -if (!hdr) { + send_attr = &sctx->cmd_attrs[attr]; +if (!send_attr->data) {error ("attribute %d requested but not present" , attr); ret = -ENOENT ; goto out; } - *len =le16_to_cpu (hdr->tlv_len); - *data = hdr +1 ; + *len = send_attr->tlv_len; + *data = send_attr->data; ret =0 ;
Signature · 1 line
2.35.1
PATCH v13 2/10 btrfs-progs: receive: dynamically allocate sctx->read_buf
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
In send stream v2, write commands can now be an arbitrary size. For that reason, we can no longer allocate a fixed array in sctx for read_cmd. Instead, read_cmd dynamically allocates sctx->read_buf. To avoid needless reallocations, we reuse read_buf between read_cmd calls by also keeping track of the size of the allocated buffer in sctx->read_buf_sz.
We do the first allocation of the old default size at the start of processing the stream, and we only reallocate if we encounter a command that needs a larger buffer.
--- common/send-stream.c | 56 ++++++++++++++++++++++++++++-------------- kernel-shared/send.h | 6 ++++- libbtrfs/send-stream.c | 2 +- 3 files changed, 43 insertions(+), 21 deletions(-)
common/send-stream.c +37 -19
@@ -35,11 +35,11 @@ struct btrfs_send_attribute { };struct btrfs_send_stream { -char read_buf[BTRFS_SEND_BUF_SIZE ]; +char *read_buf; +size_t read_buf_sz;int fd;int cmd; -struct btrfs_cmd_header *cmd_hdr;struct btrfs_send_attribute cmd_attrs[BTRFS_SEND_A_MAX +1 ];u32 version;
@@ -111,11 +111,12 @@ static int read_cmd(struct btrfs_send_stream *sctx)u32 pos;u32 crc;u32 crc2; +struct btrfs_cmd_header *cmd_hdr; +size_t buf_len;memset (sctx->cmd_attrs,0 ,sizeof (sctx->cmd_attrs)); -ASSERT (sizeof (*sctx->cmd_hdr) <=sizeof (sctx->read_buf)); - ret =read_buf (sctx, sctx->read_buf,sizeof (*sctx->cmd_hdr)); + ret =read_buf (sctx, sctx->read_buf,sizeof (*cmd_hdr));if (ret <0 ) goto out;if (ret) {
@@ -124,18 +125,25 @@ static int read_cmd(struct btrfs_send_stream *sctx) goto out; } - sctx->cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; - cmd =le16_to_cpu (sctx->cmd_hdr->cmd); - cmd_len =le32_to_cpu (sctx->cmd_hdr->len); + cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; + cmd_len =le32_to_cpu (cmd_hdr->len); + cmd =le16_to_cpu (cmd_hdr->cmd); + buf_len =sizeof (*cmd_hdr) + cmd_len; +if (sctx->read_buf_sz < buf_len) { +void *new_read_buf; -if (cmd_len +sizeof (*sctx->cmd_hdr) >=sizeof (sctx->read_buf)) { - ret = -EINVAL ; -error ("command length %u too big for buffer %zu" , - cmd_len,sizeof (sctx->read_buf)); - goto out; + new_read_buf =realloc (sctx->read_buf, buf_len); +if (!new_read_buf) { + ret = -ENOMEM ; +error ("failed to reallocate read buffer for cmd" ); + goto out; + } + sctx->read_buf = new_read_buf; + sctx->read_buf_sz = buf_len; +/* We need to reset cmd_hdr after realloc of sctx->read_buf */ + cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; } - - data = sctx->read_buf +sizeof (*sctx->cmd_hdr); + data = sctx->read_buf +sizeof (*cmd_hdr); ret =read_buf (sctx, data, cmd_len);if (ret <0 ) goto out;
@@ -145,11 +153,12 @@ static int read_cmd(struct btrfs_send_stream *sctx) goto out; } - crc =le32_to_cpu (sctx->cmd_hdr->crc); - sctx->cmd_hdr->crc =0 ; + crc =le32_to_cpu (cmd_hdr->crc); +/* in send, crc is computed with header crc = 0, replicate that */ + cmd_hdr->crc =0 ; crc2 =crc32c (0 , (unsigned char *)sctx->read_buf, -sizeof (*sctx->cmd_hdr) + cmd_len); +sizeof (*cmd_hdr) + cmd_len);if (crc != crc2) { ret = -EINVAL ;
@@ -537,19 +546,28 @@ int btrfs_read_and_process_send_stream(int fd, goto out; } + sctx.read_buf =malloc (BTRFS_SEND_BUF_SIZE_V1 ); +if (!sctx.read_buf) { + ret = -ENOMEM ; +error ("unable to allocate send stream read buffer" ); + goto out; + } + sctx.read_buf_sz =BTRFS_SEND_BUF_SIZE_V1 ; +while (1 ) { ret =read_and_process_cmd (&sctx);if (ret <0 ) { last_err = ret; errors++;if (max_errors >0 && errors >= max_errors) - goto out; +break ; }else if (ret >0 ) {if (!honor_end_cmd) ret =0 ; - goto out; +break ; } } +free (sctx.read_buf); out:if (last_err && !ret)
kernel-shared/send.h +5 -1
@@ -33,7 +33,11 @@ extern "C" {#define BTRFS_SEND_STREAM_MAGIC "btrfs-stream"#define BTRFS_SEND_STREAM_VERSION 1 -#define BTRFS_SEND_BUF_SIZE (64 * 1024) +/* +* In send stream v1, no command is larger than 64k. In send stream v2, no limit +* should be assumed. +*/ +#define BTRFS_SEND_BUF_SIZE_V1 (64 * 1024)#define BTRFS_SEND_READ_SIZE (1024 * 48)enum btrfs_tlv_type {
libbtrfs/send-stream.c +1 -1
@@ -22,7 +22,7 @@#include "crypto/crc32c.h" struct btrfs_send_stream { -char read_buf[BTRFS_SEND_BUF_SIZE ]; +char read_buf[BTRFS_SEND_BUF_SIZE_V1 ];int fd;int cmd;
Signature · 1 line
2.35.1
PATCH v13 3/10 btrfs-progs: receive: support v2 send stream DATA tlv format
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
The new format privileges the BTRFS_SEND_A_DATA attribute by guaranteeing it will always be the last attribute in any command that needs it, and by implicitly encoding the data length as the difference between the total command length in the command header and the sizes of the rest of the attributes (and of course the tlv_type identifying the DATA attribute). To parse the new stream, we must read the tlv_type and if it is not DATA, we proceed normally, but if it is DATA, we don't parse a tlv_len but simply compute the length.
In addition, we add some bounds checking when parsing each chunk of data, as well as for the tlv_len itself.
--- common/send-stream.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)
common/send-stream.c +26 -10
@@ -168,28 +168,44 @@ static int read_cmd(struct btrfs_send_stream *sctx) pos =0 ;while (pos < cmd_len) { -struct btrfs_tlv_header *tlv_hdr;u16 tlv_type; -u16 tlv_len;struct btrfs_send_attribute *send_attr; - tlv_hdr = (struct btrfs_tlv_header *)data; - tlv_type =le16_to_cpu (tlv_hdr->tlv_type); - tlv_len =le16_to_cpu (tlv_hdr->tlv_len); +if (cmd_len - pos <sizeof (__le16)) { +error ("send stream is truncated" ); + ret = -EINVAL ; + goto out; + } + tlv_type =le16_to_cpu (*(__le16 *)data);if (tlv_type ==0 || tlv_type >BTRFS_SEND_A_MAX ) { -error ("invalid tlv in cmd tlv_type = %hu, tlv_len = %hu" , - tlv_type, tlv_len); +error ("invalid tlv in cmd tlv_type = %hu" , tlv_type); ret = -EINVAL ; goto out; } send_attr = &sctx->cmd_attrs[tlv_type]; send_attr->tlv_type = tlv_type; - send_attr->tlv_len = tlv_len; - pos +=sizeof (*tlv_hdr); - data +=sizeof (*tlv_hdr); + pos +=sizeof (tlv_type); + data +=sizeof (tlv_type); +if (sctx->version >=2 && tlv_type ==BTRFS_SEND_A_DATA ) { + send_attr->tlv_len = cmd_len - pos; + }else { +if (cmd_len - pos <sizeof (__le16)) { +error ("send stream is truncated" ); + ret = -EINVAL ; + goto out; + } + send_attr->tlv_len =le16_to_cpu (*(__le16 *)data); + pos +=sizeof (__le16); + data +=sizeof (__le16); + } +if (cmd_len - pos < send_attr->tlv_len) { +error ("send stream is truncated" ); + ret = -EINVAL ; + goto out; + } send_attr->data = data; pos += send_attr->tlv_len; data += send_attr->tlv_len;
Signature · 1 line
2.35.1
PATCH v13 4/10 btrfs-progs: receive: add send stream v2 cmds and attrs to send.h
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Update our copy of send.h from the kernel. This adds the new commands and attributes for v2 as well as explicit enum numbering.
--- kernel-shared/send.h | 138 ++++++++++++++++++++++++++----------------- 1 file changed, 85 insertions(+), 53 deletions(-)
kernel-shared/send.h +85 -53
@@ -38,7 +38,6 @@ extern "C" {* should be assumed. */ #define BTRFS_SEND_BUF_SIZE_V1 (64 * 1024) -#define BTRFS_SEND_READ_SIZE (1024 * 48)enum btrfs_tlv_type {BTRFS_TLV_U8 ,
@@ -72,77 +71,110 @@ struct btrfs_tlv_header {/* commands */ enum btrfs_send_cmd { -BTRFS_SEND_C_UNSPEC , +BTRFS_SEND_C_UNSPEC =0 , -BTRFS_SEND_C_SUBVOL , -BTRFS_SEND_C_SNAPSHOT , +/* Version 1 */ +BTRFS_SEND_C_SUBVOL =1 , +BTRFS_SEND_C_SNAPSHOT =2 , -BTRFS_SEND_C_MKFILE , -BTRFS_SEND_C_MKDIR , -BTRFS_SEND_C_MKNOD , -BTRFS_SEND_C_MKFIFO , -BTRFS_SEND_C_MKSOCK , -BTRFS_SEND_C_SYMLINK , +BTRFS_SEND_C_MKFILE =3 , +BTRFS_SEND_C_MKDIR =4 , +BTRFS_SEND_C_MKNOD =5 , +BTRFS_SEND_C_MKFIFO =6 , +BTRFS_SEND_C_MKSOCK =7 , +BTRFS_SEND_C_SYMLINK =8 , -BTRFS_SEND_C_RENAME , -BTRFS_SEND_C_LINK , -BTRFS_SEND_C_UNLINK , -BTRFS_SEND_C_RMDIR , +BTRFS_SEND_C_RENAME =9 , +BTRFS_SEND_C_LINK =10 , +BTRFS_SEND_C_UNLINK =11 , +BTRFS_SEND_C_RMDIR =12 , -BTRFS_SEND_C_SET_XATTR , -BTRFS_SEND_C_REMOVE_XATTR , +BTRFS_SEND_C_SET_XATTR =13 , +BTRFS_SEND_C_REMOVE_XATTR =14 , -BTRFS_SEND_C_WRITE , -BTRFS_SEND_C_CLONE , +BTRFS_SEND_C_WRITE =15 , +BTRFS_SEND_C_CLONE =16 , -BTRFS_SEND_C_TRUNCATE , -BTRFS_SEND_C_CHMOD , -BTRFS_SEND_C_CHOWN , -BTRFS_SEND_C_UTIMES , +BTRFS_SEND_C_TRUNCATE =17 , +BTRFS_SEND_C_CHMOD =18 , +BTRFS_SEND_C_CHOWN =19 , +BTRFS_SEND_C_UTIMES =20 , -BTRFS_SEND_C_END , -BTRFS_SEND_C_UPDATE_EXTENT , - __BTRFS_SEND_C_MAX, +BTRFS_SEND_C_END =21 , +BTRFS_SEND_C_UPDATE_EXTENT =22 , +BTRFS_SEND_C_MAX_V1 =22 , + +/* Version 2 */ +BTRFS_SEND_C_FALLOCATE =23 , +BTRFS_SEND_C_SETFLAGS =24 , +BTRFS_SEND_C_ENCODED_WRITE =25 , +BTRFS_SEND_C_MAX_V2 =25 , + +/* End */ +BTRFS_SEND_C_MAX =25 , }; -#define BTRFS_SEND_C_MAX (__BTRFS_SEND_C_MAX - 1)/* attributes in send stream */ enum { -BTRFS_SEND_A_UNSPEC , +BTRFS_SEND_A_UNSPEC =0 , -BTRFS_SEND_A_UUID , -BTRFS_SEND_A_CTRANSID , +/* Version 1 */ +BTRFS_SEND_A_UUID =1 , +BTRFS_SEND_A_CTRANSID =2 , -BTRFS_SEND_A_INO , -BTRFS_SEND_A_SIZE , -BTRFS_SEND_A_MODE , -BTRFS_SEND_A_UID , -BTRFS_SEND_A_GID , -BTRFS_SEND_A_RDEV , -BTRFS_SEND_A_CTIME , -BTRFS_SEND_A_MTIME , -BTRFS_SEND_A_ATIME , -BTRFS_SEND_A_OTIME , +BTRFS_SEND_A_INO =3 , +BTRFS_SEND_A_SIZE =4 , +BTRFS_SEND_A_MODE =5 , +BTRFS_SEND_A_UID =6 , +BTRFS_SEND_A_GID =7 , +BTRFS_SEND_A_RDEV =8 , +BTRFS_SEND_A_CTIME =9 , +BTRFS_SEND_A_MTIME =10 , +BTRFS_SEND_A_ATIME =11 , +BTRFS_SEND_A_OTIME =12 , -BTRFS_SEND_A_XATTR_NAME , -BTRFS_SEND_A_XATTR_DATA , +BTRFS_SEND_A_XATTR_NAME =13 , +BTRFS_SEND_A_XATTR_DATA =14 , -BTRFS_SEND_A_PATH , -BTRFS_SEND_A_PATH_TO , -BTRFS_SEND_A_PATH_LINK , +BTRFS_SEND_A_PATH =15 , +BTRFS_SEND_A_PATH_TO =16 , +BTRFS_SEND_A_PATH_LINK =17 , -BTRFS_SEND_A_FILE_OFFSET , -BTRFS_SEND_A_DATA , +BTRFS_SEND_A_FILE_OFFSET =18 , +/* +* As of send stream v2, this attribute is special: it must be the last +* attribute in a command, its header contains only the type, and its +* length is implicitly the remaining length of the command. +*/ +BTRFS_SEND_A_DATA =19 , -BTRFS_SEND_A_CLONE_UUID , -BTRFS_SEND_A_CLONE_CTRANSID , -BTRFS_SEND_A_CLONE_PATH , -BTRFS_SEND_A_CLONE_OFFSET , -BTRFS_SEND_A_CLONE_LEN , +BTRFS_SEND_A_CLONE_UUID =20 , +BTRFS_SEND_A_CLONE_CTRANSID =21 , +BTRFS_SEND_A_CLONE_PATH =22 , +BTRFS_SEND_A_CLONE_OFFSET =23 , +BTRFS_SEND_A_CLONE_LEN =24 , - __BTRFS_SEND_A_MAX, +BTRFS_SEND_A_MAX_V1 =24 , + +/* Version 2 */ +BTRFS_SEND_A_FALLOCATE_MODE =25 , + +BTRFS_SEND_A_SETFLAGS_FLAGS =26 , + +BTRFS_SEND_A_UNENCODED_FILE_LEN =27 , +BTRFS_SEND_A_UNENCODED_LEN =28 , +BTRFS_SEND_A_UNENCODED_OFFSET =29 , +/* +* COMPRESSION and ENCRYPTION default to NONE (0) if omitted from +* BTRFS_SEND_C_ENCODED_WRITE. +*/ +BTRFS_SEND_A_COMPRESSION =30 , +BTRFS_SEND_A_ENCRYPTION =31 , +BTRFS_SEND_A_MAX_V2 =31 , + +/* End */ +BTRFS_SEND_A_MAX =31 , }; -#define BTRFS_SEND_A_MAX (__BTRFS_SEND_A_MAX - 1) #ifdef __cplusplus }
Signature · 1 line
2.35.1
PATCH v13 5/10 btrfs-progs: receive: process encoded_write commands
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
Add a new btrfs_send_op and support for both dumping and proper receive processing which does actual encoded writes.
Encoded writes are only allowed on a file descriptor opened with an extra flag that allows encoded writes, so we also add support for this flag when opening or reusing a file for writing.
--- cmds/receive-dump.c | 16 +++++- cmds/receive.c | 48 ++++++++++++++++ common/send-stream.c | 29 ++++++++++ common/send-stream.h | 4 ++ ioctl.h | 132 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 228 insertions(+), 1 deletion(-)
cmds/receive-dump.c +15 -1
@@ -318,6 +318,19 @@ static int print_update_extent(const char *path, u64 offset, u64 len, offset, len); } +static int print_encoded_write (const char *path,const void *data,u64 offset, +u64 len,u64 unencoded_file_len, +u64 unencoded_len,u64 unencoded_offset, +u32 compression,u32 encryption,void *user) +{ +return PRINT_DUMP (user, path,"encoded_write" , +"offset=%llu len=%llu, unencoded_file_len=%llu, " +"unencoded_len=%llu, unencoded_offset=%llu, " +"compression=%u, encryption=%u" , + offset, len, unencoded_file_len, unencoded_len, + unencoded_offset, compression, encryption); +} +struct btrfs_send_ops btrfs_print_send_ops = { .subvol = print_subvol, .snapshot = print_snapshot,
@@ -339,5 +352,6 @@ struct btrfs_send_ops btrfs_print_send_ops = { .chmod = print_chmod, .chown = print_chown, .utimes = print_utimes, - .update_extent = print_update_extent + .update_extent = print_update_extent, + .encoded_write = print_encoded_write, };
cmds/receive.c +48 -0
78 lines not on this page · raw message
common/send-stream.c +29 -0
@@ -357,6 +357,8 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)struct timespec mt;u8 uuid[BTRFS_UUID_SIZE ];u8 clone_uuid[BTRFS_UUID_SIZE ]; +u32 compression; +u32 encryption;u64 tmp;u64 tmp2;u64 ctransid;
@@ -366,6 +368,9 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)u64 clone_offset;u64 offset;u64 ino; +u64 unencoded_file_len; +u64 unencoded_len; +u64 unencoded_offset;int len;int xattr_len;
@@ -452,6 +457,30 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)TLV_GET (sctx,BTRFS_SEND_A_DATA , &data, &len); ret = sctx->ops->write (path, data, offset, len, sctx->user);break ; +case BTRFS_SEND_C_ENCODED_WRITE : +TLV_GET_STRING (sctx,BTRFS_SEND_A_PATH , &path); +TLV_GET_U64 (sctx,BTRFS_SEND_A_FILE_OFFSET , &offset); +TLV_GET_U64 (sctx,BTRFS_SEND_A_UNENCODED_FILE_LEN , + &unencoded_file_len); +TLV_GET_U64 (sctx,BTRFS_SEND_A_UNENCODED_LEN , &unencoded_len); +TLV_GET_U64 (sctx,BTRFS_SEND_A_UNENCODED_OFFSET , + &unencoded_offset); +/* Compression and encryption default to none if omitted. */ +if (sctx->cmd_attrs[BTRFS_SEND_A_COMPRESSION ].data) +TLV_GET_U32 (sctx,BTRFS_SEND_A_COMPRESSION , &compression); +else + compression =BTRFS_ENCODED_IO_COMPRESSION_NONE ; +if (sctx->cmd_attrs[BTRFS_SEND_A_ENCRYPTION ].data) +TLV_GET_U32 (sctx,BTRFS_SEND_A_ENCRYPTION , &encryption); +else + encryption =BTRFS_ENCODED_IO_ENCRYPTION_NONE ; +TLV_GET (sctx,BTRFS_SEND_A_DATA , &data, &len); + ret = sctx->ops->encoded_write (path, data, offset, len, + unencoded_file_len, + unencoded_len, unencoded_offset, + compression, encryption, + sctx->user); +break ;case BTRFS_SEND_C_CLONE :TLV_GET_STRING (sctx,BTRFS_SEND_A_PATH , &path);TLV_GET_U64 (sctx,BTRFS_SEND_A_FILE_OFFSET , &offset);
common/send-stream.h +4 -0
@@ -53,6 +53,10 @@ struct btrfs_send_ops { struct timespec *mt, struct timespec *ct, void *user);int (*update_extent)(const char *path,u64 offset,u64 len,void *user); +int (*encoded_write)(const char *path,const void *data,u64 offset, +u64 len,u64 unencoded_file_len,u64 unencoded_len, +u64 unencoded_offset,u32 compression, +u32 encryption,void *user); }; int btrfs_read_and_process_send_stream(int fd,
ioctl.h +132 -0
144 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v13 6/10 btrfs-progs: receive: encoded_write fallback to explicit decode and write
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
An encoded_write can fail if the file system it is being applied to does not support encoded writes or if it can't find enough contiguous space to accommodate the encoded extent. In those cases, we can likely still process an encoded_write by explicitly decoding the data and doing a normal write.
Add the necessary fallback path for decoding data compressed with zlib, lzo, or zstd. zlib and zstd have reusable decoding context data structures which we cache in the receive context so that we don't have to recreate them on every encoded_write.
Finally, add a command line flag for force-decompress which causes receive to always use the fallback path rather than first attempting the encoded write.
--- Documentation/btrfs-receive.rst | 5 + cmds/receive.c | 261 +++++++++++++++++++++++++++++++- 2 files changed, 259 insertions(+), 7 deletions(-)
Documentation/btrfs-receive.rst +5 -0
@@ -57,6 +57,11 @@ A subvolume is made read-only after the receiving process finishes successfully If */proc* is not accessible, eg. in a chroot environment, use this option to tell us where this filesystem is mounted. +--force-decompress + if the stream contains compressed data (see *--compressed-data* in + ``btrfs-send(8)``), always decompress it instead of writing it with + encoded I/O + --dump dump the stream metadata, one line per operation
cmds/receive.c +254 -7
315 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v13 7/10 btrfs-progs: receive: process fallocate commands
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
Send stream v2 can emit fallocate commands, so receive must support them as well. The implementation simply passes along the arguments to the syscall. Note that mode is encoded as a u32 in send stream but fallocate takes an int, so there is a unsigned->signed conversion there.
--- cmds/receive-dump.c | 9 +++++++++ cmds/receive.c | 25 +++++++++++++++++++++++++ common/send-stream.c | 9 +++++++++ common/send-stream.h | 2 ++ 4 files changed, 45 insertions(+)
cmds/receive-dump.c +9 -0
@@ -331,6 +331,14 @@ static int print_encoded_write(const char *path, const void *data, u64 offset, unencoded_offset, compression, encryption); } +static int print_fallocate (const char *path,int mode,u64 offset,u64 len, +void *user) +{ +return PRINT_DUMP (user, path,"fallocate" , +"mode=%d offset=%llu len=%llu" , + mode, offset, len); +} +struct btrfs_send_ops btrfs_print_send_ops = { .subvol = print_subvol, .snapshot = print_snapshot,
@@ -354,4 +362,5 @@ struct btrfs_send_ops btrfs_print_send_ops = { .utimes = print_utimes, .update_extent = print_update_extent, .encoded_write = print_encoded_write, + .fallocate = print_fallocate, };
cmds/receive.c +25 -0
@@ -1260,6 +1260,30 @@ static int process_encoded_write(const char *path, const void *data, u64 offset, compression); } +static int process_fallocate (const char *path,int mode,u64 offset,u64 len, +void *user) +{ +int ret; +struct btrfs_receive *rctx = user; +char full_path[PATH_MAX ]; + + ret =path_cat_out (full_path, rctx->full_subvol_path, path); +if (ret <0 ) { +error ("fallocate: path invalid: %s" , path); +return ret; + } + ret =open_inode_for_write (rctx, full_path); +if (ret <0 ) +return ret; + ret =fallocate (rctx->write_fd, mode, offset, len); +if (ret <0 ) { + ret = -errno; +error ("fallocate: fallocate on %s failed: %m" , path); +return ret; + } +return 0 ; +} +static struct btrfs_send_ops send_ops = { .subvol = process_subvol, .snapshot = process_snapshot,
@@ -1283,6 +1307,7 @@ static struct btrfs_send_ops send_ops = { .utimes = process_utimes, .update_extent = process_update_extent, .encoded_write = process_encoded_write, + .fallocate = process_fallocate, }; static int do_receive(struct btrfs_receive *rctx, const char *tomnt,
common/send-stream.c +9 -0
@@ -373,6 +373,7 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)u64 unencoded_offset;int len;int xattr_len; +int fallocate_mode; ret =read_cmd (sctx);if (ret)
@@ -537,6 +538,14 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)case BTRFS_SEND_C_END : ret =1 ;break ; +case BTRFS_SEND_C_FALLOCATE : +TLV_GET_STRING (sctx,BTRFS_SEND_A_PATH , &path); +TLV_GET_U32 (sctx,BTRFS_SEND_A_FALLOCATE_MODE , &fallocate_mode); +TLV_GET_U64 (sctx,BTRFS_SEND_A_FILE_OFFSET , &offset); +TLV_GET_U64 (sctx,BTRFS_SEND_A_SIZE , &tmp); + ret = sctx->ops->fallocate (path, fallocate_mode, offset, tmp, + sctx->user); +break ; } tlv_get_failed:
common/send-stream.h +2 -0
@@ -57,6 +57,8 @@ struct btrfs_send_ops { u64 len, u64 unencoded_file_len, u64 unencoded_len, u64 unencoded_offset, u32 compression, u32 encryption, void *user); +int (*fallocate)(const char *path,int mode,u64 offset,u64 len, +void *user); }; int btrfs_read_and_process_send_stream(int fd,
Signature · 1 line
2.35.1
PATCH v13 8/10 btrfs-progs: receive: process setflags ioctl commands
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
In send stream v2, send can emit a command for setting inode flags via the setflags ioctl. Pass the flags attribute through to the ioctl call in receive.
--- cmds/receive-dump.c | 6 ++++++ cmds/receive.c | 25 +++++++++++++++++++++++++ common/send-stream.c | 7 +++++++ common/send-stream.h | 1 + 4 files changed, 39 insertions(+)
cmds/receive-dump.c +6 -0
@@ -339,6 +339,11 @@ static int print_fallocate(const char *path, int mode, u64 offset, u64 len, mode, offset, len); } +static int print_setflags (const char *path,int flags,void *user) +{ +return PRINT_DUMP (user, path,"setflags" ,"flags=%d" , flags); +} +struct btrfs_send_ops btrfs_print_send_ops = { .subvol = print_subvol, .snapshot = print_snapshot,
@@ -363,4 +368,5 @@ struct btrfs_send_ops btrfs_print_send_ops = { .update_extent = print_update_extent, .encoded_write = print_encoded_write, .fallocate = print_fallocate, + .setflags = print_setflags, };
cmds/receive.c +25 -0
@@ -38,6 +38,7 @@#include <sys/types.h> #include <sys/uio.h> #include <sys/xattr.h> +#include <linux/fs.h> #include <uuid/uuid.h> #include <lzo/lzo1x.h>
@@ -1284,6 +1285,29 @@ static int process_fallocate(const char *path, int mode, u64 offset, u64 len,return 0 ; } +static int process_setflags (const char *path,int flags,void *user) +{ +int ret; +struct btrfs_receive *rctx = user; +char full_path[PATH_MAX ]; + + ret =path_cat_out (full_path, rctx->full_subvol_path, path); +if (ret <0 ) { +error ("setflags: path invalid: %s" , path); +return ret; + } + ret =open_inode_for_write (rctx, full_path); +if (ret <0 ) +return ret; + ret =ioctl (rctx->write_fd,FS_IOC_SETFLAGS , &flags); +if (ret <0 ) { + ret = -errno; +error ("setflags: setflags ioctl on %s failed: %m" , path); +return ret; + } +return 0 ; +} +static struct btrfs_send_ops send_ops = { .subvol = process_subvol, .snapshot = process_snapshot,
@@ -1308,6 +1332,7 @@ static struct btrfs_send_ops send_ops = { .update_extent = process_update_extent, .encoded_write = process_encoded_write, .fallocate = process_fallocate, + .setflags = process_setflags, }; static int do_receive(struct btrfs_receive *rctx, const char *tomnt,
common/send-stream.c +7 -0
@@ -374,6 +374,7 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx)int len;int xattr_len;int fallocate_mode; +int setflags_flags; ret =read_cmd (sctx);if (ret)
@@ -546,8 +547,14 @@ static int read_and_process_cmd(struct btrfs_send_stream *sctx) ret = sctx->ops->fallocate (path, fallocate_mode, offset, tmp, sctx->user);break ; +case BTRFS_SEND_C_SETFLAGS : +TLV_GET_STRING (sctx,BTRFS_SEND_A_PATH , &path); +TLV_GET_U32 (sctx,BTRFS_SEND_A_SETFLAGS_FLAGS , &setflags_flags); + ret = sctx->ops->setflags (path, setflags_flags, sctx->user); +break ; } + tlv_get_failed: out:free (path);
common/send-stream.h +1 -0
@@ -59,6 +59,7 @@ struct btrfs_send_ops { u32 encryption, void *user);int (*fallocate)(const char *path,int mode,u64 offset,u64 len,void *user); +int (*setflags)(const char *path,int flags,void *user); }; int btrfs_read_and_process_send_stream(int fd,
Signature · 1 line
2.35.1
PATCH v13 9/10 btrfs-progs: send: stream v2 ioctl flags
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
First, add a --proto option to allow specifying the desired send protocol version. It defaults to zero, which tells the kernel to pick the latest version. This is based on Dave Sterba's patch.
Also add a --compressed-data flag to instruct the kernel to use encoded_write commands for compressed extents. This requires an explicit opt in separate from the protocol version because:
1. The user may not want compression on the receiving side, or may want a different compression algorithm/level on the receiving side.
2. It has a soft requirement for kernel support on the receiving side (btrfs-progs can fall back to decompressing and writing if the kernel doesn't support BTRFS_IOC_ENCODED_WRITE, but the user may not be prepared to pay that CPU cost). Going forward, since it's easier to update progs than the kernel, I think we'll want to make new send features that require kernel support opt-in, whereas anything that only requires a progs update can happen automatically.
--- Documentation/btrfs-send.rst | 22 ++++++++ cmds/send.c | 100 ++++++++++++++++++++++++++++++++++- ioctl.h | 19 ++++++- kernel-shared/send.h | 2 +- 4 files changed, 138 insertions(+), 5 deletions(-)
Documentation/btrfs-send.rst +22 -0
@@ -60,6 +60,28 @@ please see section *SUBVOLUME FLAGS* in ``btrfs-subvolume(8)``. used to transfer changes. This mode is faster and is useful to show the differences in metadata. +--proto <N> + use send protocol version N + + The default is 1, which was the original protocol version. Version 2 + encodes file data slightly more efficiently; it is also required for + sending compressed data directly (see *--compressed-data*). Version 2 + requires at least btrfs-progs 5.18 on both the sender and receiver and + at least Linux 5.18 on the sender. Passing 0 means to use the highest + version supported by the running kernel. + +--compressed-data + send data that is compressed on the filesystem directly without + decompressing it + + If the receiver supports the *BTRFS_IOC_ENCODED_WRITE* ioctl (added in + Linux 5.18), it can also write it directly without decompressing it. + Otherwise, the receiver will fall back to decompressing it and writing + it normally. + + This requires protocol version 2 or higher. If *--proto* was not used, + then *--compressed-data* implies *--proto 2*. + -q|--quiet (deprecated) alias for global *-q* option
cmds/send.c +98 -2
161 lines not on this page · raw message
ioctl.h +17 -2
@@ -655,10 +655,24 @@ BUILD_ASSERT(sizeof(struct btrfs_ioctl_received_subvol_args_32) == 192);*/ #define BTRFS_SEND_FLAG_OMIT_END_CMD 0x4 +/* +* Read the protocol version in the structure +*/ +#define BTRFS_SEND_FLAG_VERSION 0x8 + +/* +* Send compressed data using the ENCODED_WRITE command instead of decompressing +* the data and sending it with the WRITE command. This requires protocol +* version >= 2. +*/ +#define BTRFS_SEND_FLAG_COMPRESSED 0x10 +#define BTRFS_SEND_FLAG_MASK \ (BTRFS_SEND_FLAG_NO_FILE_DATA | \ BTRFS_SEND_FLAG_OMIT_STREAM_HEADER | \ - BTRFS_SEND_FLAG_OMIT_END_CMD) + BTRFS_SEND_FLAG_OMIT_END_CMD | \ + BTRFS_SEND_FLAG_VERSION | \ + BTRFS_SEND_FLAG_COMPRESSED)struct btrfs_ioctl_send_args {__s64 send_fd;/* in */
@@ -666,7 +680,8 @@ struct btrfs_ioctl_send_args {__u64 __user *clone_sources;/* in */ __u64 parent_root;/* in */ __u64 flags;/* in */ -__u64 reserved[4 ];/* in */ +__u32 version;/* in */ +__u8 reserved[28 ];/* in */ };/* * Size of structure depends on pointer width, was not caught in the early
kernel-shared/send.h +1 -1
@@ -31,7 +31,7 @@ extern "C" {#endif #define BTRFS_SEND_STREAM_MAGIC "btrfs-stream" -#define BTRFS_SEND_STREAM_VERSION 1 +#define BTRFS_SEND_STREAM_VERSION 2/* * In send stream v1, no command is larger than 64k. In send stream v2, no limit
Signature · 1 line
2.35.1
Re: [PATCH v13 09/10] btrfs-progs: send: stream v2 ioctl flags
Quoted 5 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
First, add a --proto option to allow specifying the desired send protocol version. It defaults to zero, which tells the kernel to pick the latest version. This is based on Dave Sterba's patch.
Oops, this paragraph should be updated to read:
First, add a --proto option to allow specifying the desired send protocol version. It defaults to one, the original version. In a couple of releases once people are aware that protocol revisions are happening, we can change it to default to zero, which means the latest version supported by the kernel. This is based on Dave Sterba's patch.
PATCH v13 10/10 btrfs-progs: receive: add tests for basic encoded_write send/receive
In reply to Omar SandovalFrom: Boris Burkov <[email protected]>
Adapt the existing send/receive tests by passing '-o compress-force' to the mount commands in a new test. After writing a few files in the various compression formats, send/receive them with and without --force-decompress to test both the encoded_write path and the fallback to decode+write.
--- .../052-receive-write-encoded/test.sh | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 tests/misc-tests/052-receive-write-encoded/test.sh
tests/misc-tests/052-receive-write-encoded/test.sh new file +114 -0
@@ -0,0 +1,114 @@ +#!/bin/bash +# +# test that we can send and receive encoded writes for three modes of +# transparent compression: zlib, lzo, and zstd. + +source "$TEST_TOP/common" + +check_prereq mkfs.btrfs +check_prereq btrfs + +setup_root_helper +prepare_test_dev + +here=`pwd` + +# assumes the filesystem exists, and does mount, write, snapshot, send, unmount +# for the specified encoding option +send_one() { + local str + local subv + local snap + + algorithm="$1" + shift + str="$1" + shift + + subv="subv-$algorithm" + snap="snap-$algorithm" + + run_check_mount_test_dev "-o" "compress-force=$algorithm" + cd "$TEST_MNT" || _fail "cannot chdir to TEST_MNT" + + run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$subv" + run_check $SUDO_HELPER dd if=/dev/zero of="$subv/file1" bs=1M count=1 + run_check $SUDO_HELPER dd if=/dev/zero of="$subv/file2" bs=500K count=1 + run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot -r "$subv" "$snap" + run_check $SUDO_HELPER "$TOP/btrfs" send -f "$str" "$snap" "$@" + + cd "$here" || _fail "cannot chdir back to test directory" + run_check_umount_test_dev +} + +receive_one() { + local str + str="$1" + shift + + run_check_mkfs_test_dev + run_check_mount_test_dev + run_check $SUDO_HELPER "$TOP/btrfs" receive "$@" -v -f "$str" "$TEST_MNT" + run_check_umount_test_dev + run_check rm -f -- "$str" +} + +test_one_write_encoded() { + local str + local algorithm + algorithm="$1" + shift + str="$here/stream-$algorithm.stream" + + run_check_mkfs_test_dev + send_one "$algorithm" "$str" --compressed-data + receive_one "$str" "$@" +} + +test_one_stream_v1() { + local str + local algorithm + algorithm="$1" + shift + str="$here/stream-$algorithm.stream" + + run_check_mkfs_test_dev + send_one "$algorithm" "$str" --proto 1 + receive_one "$str" "$@" +} + +test_mix_write_encoded() { + local strzlib + local strlzo + local strzstd + strzlib="$here/stream-zlib.stream" + strlzo="$here/stream-lzo.stream" + strzstd="$here/stream-zstd.stream" + + run_check_mkfs_test_dev + + send_one "zlib" "$strzlib" --compressed-data + send_one "lzo" "$strlzo" --compressed-data + send_one "zstd" "$strzstd" --compressed-data + + receive_one "$strzlib" + receive_one "$strlzo" + receive_one "$strzstd" +} + +test_one_write_encoded "zlib" +test_one_write_encoded "lzo" +test_one_write_encoded "zstd" + +# with decompression forced +test_one_write_encoded "zlib" "--force-decompress" +test_one_write_encoded "lzo" "--force-decompress" +test_one_write_encoded "zstd" "--force-decompress" + +# send stream v1 +test_one_stream_v1 "zlib" +test_one_stream_v1 "lzo" +test_one_stream_v1 "zstd" + +# files use a mix of compression algorithms +test_mix_write_encoded
Signature · 1 line
2.35.1
Quoted 100 lines · Omar Sandoval
From: Omar Sandoval <[email protected]>
This series has three parts: new Btrfs ioctls for reading/writing compressed data, support for sending compressed data via Btrfs send, and btrfs-progs support for sending/receiving compressed data and writing it with the new ioctl.
Patches 1 and 2 are VFS changes exporting a couple of helpers for checks needed by reads and writes. Patches 3-8 are preparatory Btrfs changes for compressed reads and writes. Patch 6 is a cleanup. Patch 9 adds the compressed read ioctl and patch 10 adds the compressed write ioctl.
The main use-case for this interface is Btrfs send/receive. Currently, when sending data from one compressed filesystem to another, the sending side decompresses the data and the receiving side recompresses it before writing it out. This is wasteful and can be avoided if we can just send and write compressed extents.
Patches 11 and 12 are cleanups for Btrfs send. Patches 13-17 add the Btrfs send support. See the previous posting for more details and benchmarks [1]. Patches 13-15 prepare some protocol changes for send stream v2. Patch 16 implements compressed send. Patch 17 enables send stream v2 and compressed send in the send ioctl when requested.
These patches are based on Dave Sterba's Btrfs misc-next branch [2], which is in turn currently based on v5.17-rc3. Test cases are here [3].
Changes since v12 [4]:
- Changed some LZO code to use filesystem sector size instead of page size.
- Changed to usebtrfs_inode_lock()instead ofinode_lock().
- Added explanation of why we don't need to hold mmap lock for encoded reads to commit message.
- Got rid ofGFP_HIGHMEMallocations.
- Changed send protocol definitions to use explicit numbers.
- Changed btrfs send patch to default to protocol v1 for now.
- Style cleanups.1: https://lore.kernel.org/linux-btrfs/[email protected]/
2: https://github.com/kdave/btrfs-devel/tree/misc-next
3: https://github.com/osandov/xfstests/tree/btrfs-encoded-io
4: https://lore.kernel.org/linux-btrfs/[email protected]/Omar Sandoval (17):
fs: exportrw_verify_area()
fs: export variant ofgeneric_write_checkswithoutiov_iter
btrfs: don't advance offset for compressed bios in
btrfs_csum_one_bio()
btrfs: addram_bytesand offset tobtrfs_ordered_extent
btrfs: support different disk extent size for delalloc
btrfs: clean upcow_file_range_inline()
btrfs: optionally extendi_sizeincow_file_range_inline()
btrfs: add definitions + documentation for encoded I/O ioctls
btrfs: addBTRFS_IOC_ENCODED_READ
btrfs: addBTRFS_IOC_ENCODED_WRITE
btrfs: send: remove unused send_ctx::{total,cmd}_send_size
btrfs: send: explicitly number commands and attributes
btrfs: add send stream v2 definitions
btrfs: send: write larger chunks when using stream v2
btrfs: send: allocate send buffer withalloc_page()andvmap()for v2
btrfs: send: send compressed extents with encoded writes
btrfs: send: enable support for stream v2 and compressed writesfs/btrfs/compression.c | 10 +- fs/btrfs/compression.h | 6 +- fs/btrfs/ctree.h | 19 +- fs/btrfs/delalloc-space.c | 18 +- fs/btrfs/file-item.c | 32 +- fs/btrfs/file.c | 68 ++- fs/btrfs/inode.c | 930 +++++++++++++++++++++++++++++++++---- fs/btrfs/ioctl.c | 208 +++++++++ fs/btrfs/ordered-data.c | 131 ++---- fs/btrfs/ordered-data.h | 25 +- fs/btrfs/relocation.c | 2 +- fs/btrfs/send.c | 324 +++++++++++-- fs/btrfs/send.h | 142 +++--- fs/internal.h | 5 - fs/read_write.c | 34 +- include/linux/fs.h | 2 + include/uapi/linux/btrfs.h | 142 +++++- 17 files changed, 1760 insertions(+), 338 deletions(-)The btrfs-progs patches were written by Boris Burkov with some updates from me. Patches 1-4 are preparation. Patch 5 implements encoded writes. Patch 6 implements the fallback to decompressing. Patches 7 and 8 implement the other commands. Patch 9 adds the new
btrfs sendoptions. Patch 10 adds a test case.Boris Burkov (8):
btrfs-progs: receive: support v2 send stream largertlv_len
btrfs-progs: receive: dynamically allocatesctx->read_buf
btrfs-progs: receive: support v2 send stream DATA tlv format
btrfs-progs: receive: processencoded_writecommands
btrfs-progs: receive:encoded_writefallback to explicit decode and
write
btrfs-progs: receive: process fallocate commands
btrfs-progs: receive: process setflags ioctl commands
btrfs-progs: receive: add tests for basicencoded_writesend/receive
Quoted 3 lines
Omar Sandoval (2):
btrfs-progs: receive: add send stream v2 cmds and attrs tosend.h
btrfs-progs: send: stream v2 ioctl flags
Patches 1-10 (the encoded ioctls) are now in misc-next.