Skip to content

Commit b10d8c8

Browse files
adam900710kdave
authored andcommitted
btrfs: use blocksize to check if compression is making things larger
[BEHAVIOR DIFFERENCE BETWEEN COMPRESSION ALGOS] Currently LZO compression algorithm will check if we're making the compressed data larger after compressing more than 2 blocks. But zlib and zstd do the same checks after compressing more than 8192 bytes. This is not a big deal, but since we're already supporting larger block size (e.g. 64K block size if page size is also 64K), this check is not suitable for all block sizes. For example, if our page and block size are both 16KiB, and after the first block compressed using zlib, the resulted compressed data is slightly larger than 16KiB, we will immediately abort the compression. This makes zstd and zlib compression algorithms to behave slightly different from LZO, which only aborts after compressing two blocks. [ENHANCEMENT] To unify the behavior, only abort the compression after compressing at least two blocks. Reviewed-by: Anand Jain <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 0f0f0ba commit b10d8c8

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

fs/btrfs/zlib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ int zlib_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
148148
unsigned long len = *total_out;
149149
unsigned long nr_dest_folios = *out_folios;
150150
const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
151+
const u32 blocksize = inode->root->fs_info->sectorsize;
151152
const u64 orig_end = start + len;
152153

153154
*out_folios = 0;
@@ -234,7 +235,7 @@ int zlib_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
234235
}
235236

236237
/* we're making it bigger, give up */
237-
if (workspace->strm.total_in > 8192 &&
238+
if (workspace->strm.total_in > blocksize * 2 &&
238239
workspace->strm.total_in <
239240
workspace->strm.total_out) {
240241
ret = -E2BIG;

fs/btrfs/zstd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
400400
unsigned long len = *total_out;
401401
const unsigned long nr_dest_folios = *out_folios;
402402
const u64 orig_end = start + len;
403+
const u32 blocksize = inode->root->fs_info->sectorsize;
403404
unsigned long max_out = nr_dest_folios * PAGE_SIZE;
404405
unsigned int cur_len;
405406

@@ -456,7 +457,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
456457
}
457458

458459
/* Check to see if we are making it bigger */
459-
if (tot_in + workspace->in_buf.pos > 8192 &&
460+
if (tot_in + workspace->in_buf.pos > blocksize * 2 &&
460461
tot_in + workspace->in_buf.pos <
461462
tot_out + workspace->out_buf.pos) {
462463
ret = -E2BIG;

0 commit comments

Comments
 (0)