Commit fc5c0c58 authored by Daniel Vacek's avatar Daniel Vacek Committed by David Sterba
Browse files

btrfs: defrag: extend ioctl to accept compression levels



The zstd and zlib compression types support setting compression levels.
Enhance the defrag interface to specify the levels as well. For zstd the
negative (realtime) levels are also accepted.

Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarDaniel Vacek <neelx@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 08f34076
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -145,6 +145,7 @@ struct btrfs_inode {
	 * different from prop_compress and takes precedence if set.
	 */
	u8 defrag_compress;
	s8 defrag_compress_level;

	/*
	 * Lock for counters and all fields used to determine if the inode is in
+10 −0
Original line number Diff line number Diff line
@@ -980,6 +980,16 @@ static int btrfs_compress_set_level(unsigned int type, int level)
	return level;
}

/*
 * Check whether the @level is within the valid range for the given type.
 */
bool btrfs_compress_level_valid(unsigned int type, int level)
{
	const struct btrfs_compress_op *ops = btrfs_compress_op[type];

	return ops->min_level <= level && level <= ops->max_level;
}

/* Wrapper around find_get_page(), with extra error message. */
int btrfs_compress_filemap_get_folio(struct address_space *mapping, u64 start,
				     struct folio **in_folio_ret)
+1 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ static inline u32 btrfs_calc_input_length(u64 range_end, u64 cur)
int __init btrfs_init_compress(void);
void __cold btrfs_exit_compress(void);

bool btrfs_compress_level_valid(unsigned int type, int level);
int btrfs_compress_folios(unsigned int type, int level, struct address_space *mapping,
			  u64 start, struct folio **folios, unsigned long *out_folios,
			 unsigned long *total_in, unsigned long *total_out);
+19 −5
Original line number Diff line number Diff line
@@ -1363,6 +1363,7 @@ int btrfs_defrag_file(struct btrfs_inode *inode, struct file_ra_state *ra,
	u64 last_byte;
	bool do_compress = (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS);
	int compress_type = BTRFS_COMPRESS_ZLIB;
	int compress_level = 0;
	int ret = 0;
	u32 extent_thresh = range->extent_thresh;
	pgoff_t start_index;
@@ -1376,11 +1377,22 @@ int btrfs_defrag_file(struct btrfs_inode *inode, struct file_ra_state *ra,
		return -EINVAL;

	if (do_compress) {
		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL) {
			if (range->compress.type >= BTRFS_NR_COMPRESS_TYPES)
				return -EINVAL;
			if (range->compress.type) {
				compress_type  = range->compress.type;
				compress_level = range->compress.level;
				if (!btrfs_compress_level_valid(compress_type, compress_level))
					return -EINVAL;
			}
		} else {
			if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
				return -EINVAL;
			if (range->compress_type)
				compress_type = range->compress_type;
		}
	}

	if (extent_thresh == 0)
		extent_thresh = SZ_256K;
@@ -1430,8 +1442,10 @@ int btrfs_defrag_file(struct btrfs_inode *inode, struct file_ra_state *ra,
			btrfs_inode_unlock(inode, 0);
			break;
		}
		if (do_compress)
		if (do_compress) {
			inode->defrag_compress = compress_type;
			inode->defrag_compress_level = compress_level;
		}
		ret = defrag_one_cluster(inode, ra, cur,
				cluster_end + 1 - cur, extent_thresh,
				newer_than, do_compress, &sectors_defragged,
+1 −1
Original line number Diff line number Diff line
@@ -497,7 +497,7 @@ struct btrfs_fs_info {
	u64 last_trans_log_full_commit;
	unsigned long long mount_opt;

	unsigned long compress_type:4;
	int compress_type;
	int compress_level;
	u32 commit_interval;
	/*
Loading