From 55e07d6dd155269d4b8b8bdcb03716ca5df815cb Mon Sep 17 00:00:00 2001 Date: Tue, 29 Mar 2022 11:50:39 +0200 Subject: [PATCH] fs/btrfs: Allow for a per-extent compression level --- fs/btrfs/inode.c | 34 ++++++++++++++++++++++++---------- fs/btrfs/ioctl.c | 6 +++++- include/uapi/linux/btrfs.h | 9 ++++++++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index aa0a60ee26cb..9f751076a00e 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -597,6 +597,7 @@ static noinline int compress_file_range(struct async_chunk *async_chunk) int i; int will_compress; int compress_type = fs_info->compress_type; + int compress_level = 0; int compressed_extents = 0; int redirty = 0; @@ -675,8 +676,10 @@ static noinline int compress_file_range(struct async_chunk *async_chunk) goto cont; } - if (BTRFS_I(inode)->defrag_compress) - compress_type = BTRFS_I(inode)->defrag_compress; + if (BTRFS_I(inode)->defrag_compress){ + compress_type = BTRFS_I(inode)->defrag_compress & 0xF; + compress_level = BTRFS_I(inode)->defrag_compress >> 4; + } else if (BTRFS_I(inode)->prop_compress) compress_type = BTRFS_I(inode)->prop_compress; @@ -697,14 +700,25 @@ static noinline int compress_file_range(struct async_chunk *async_chunk) redirty = 1; } - /* Compression level is applied here and only here */ - ret = btrfs_compress_pages( - compress_type | (fs_info->compress_level << 4), - inode->i_mapping, start, - pages, - &nr_pages, - &total_in, - &total_compressed); + /* Compression level is applied here and only here + * Takes the compression level from the inode, if set + */ + if (compress_level) + ret = btrfs_compress_pages( + compress_type | compress_level << 4, + inode->i_mapping, start, + pages, + &nr_pages, + &total_in, + &total_compressed); + else + ret = btrfs_compress_pages( + compress_type | fs_info->compress_level << 4, + inode->i_mapping, start, + pages, + &nr_pages, + &total_in, + &total_compressed); if (!ret) { unsigned long offset = offset_in_page(total_compressed); diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 238cee5b5254..7534e13c487d 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1777,6 +1777,7 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; bool ra_allocated = false; int compress_type = BTRFS_COMPRESS_ZLIB; + int compress_level = 0; int ret = 0; u32 extent_thresh = range->extent_thresh; pgoff_t start_index; @@ -1792,6 +1793,8 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, return -EINVAL; if (range->compress_type) compress_type = range->compress_type; + if (range->compress_level) + compress_level = range->compress_level; } if (extent_thresh == 0) @@ -1855,7 +1858,8 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, break; } if (do_compress) - BTRFS_I(inode)->defrag_compress = compress_type; + BTRFS_I(inode)->defrag_compress = compress_type | + compress_level << 4; ret = defrag_one_cluster(BTRFS_I(inode), ra, cur, cluster_end + 1 - cur, extent_thresh, newer_than, do_compress, §ors_defragged, diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h index d956b2993970..b330d0896a5a 100644 --- a/include/uapi/linux/btrfs.h +++ b/include/uapi/linux/btrfs.h @@ -605,8 +605,15 @@ struct btrfs_ioctl_defrag_range_args { */ __u32 compress_type; + /* + * which compression strength to use if turning on compression + * for this defrag operation. If unspecified, the inode's value + * will be used + */ + __u32 compress_level; + /* spare for later */ - __u32 unused[4]; + __u32 unused[3]; }; -- 2.30.2