]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
btrfs: prepare zstd to support bs > ps cases
authorQu Wenruo <wqu@suse.com>
Mon, 8 Sep 2025 06:58:16 +0000 (16:28 +0930)
committerDavid Sterba <dsterba@suse.com>
Tue, 23 Sep 2025 06:49:24 +0000 (08:49 +0200)
This involves converting the following functions to use proper folio
sizes/shifts:

- zstd_compress_folios()
- zstd_decompress_bio()

The function zstd_decompress() is already using block size correctly
without using page size, thus it needs no modification.

And since zstd compression is calling kmap_local_folio(), the existing
code cannot handle large folios with HIGHMEM, as kmap_local_folio()
requires us to handle one page range each time.

I do not really think it's worth to spend time on some feature that will
be deprecated eventually.  So here just add an extra explicit rejection
for bs > ps with HIGHMEM feature enabled kernels.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/fs.c
fs/btrfs/zstd.c

index 014fb8b12f96b90e4dbb63745a8d9b3b6256880f..29ad1c8591944b21776b02875c6ac9d471214ad4 100644 (file)
@@ -79,6 +79,24 @@ bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize)
        if (blocksize == PAGE_SIZE || blocksize == SZ_4K || blocksize == BTRFS_MIN_BLOCKSIZE)
                return true;
 #ifdef CONFIG_BTRFS_EXPERIMENTAL
+       /*
+        * For bs > ps support it's done by specifying a minimal folio order
+        * for filemap, thus implying large data folios.
+        * For HIGHMEM systems, we can not always access the content of a (large)
+        * folio in one go, but go through them page by page.
+        *
+        * A lot of features don't implement a proper PAGE sized loop for large
+        * folios, this includes:
+        *
+        * - compression
+        * - verity
+        * - encoded write
+        *
+        * Considering HIGHMEM is such a pain to deal with and it's going
+        * to be deprecated eventually, just reject HIGHMEM && bs > ps cases.
+        */
+       if (IS_ENABLED(CONFIG_HIGHMEM) && blocksize > PAGE_SIZE)
+               return false;
        if (blocksize <= PAGE_SIZE)
                return true;
 #endif
index 28e2e99a24639b0ca7924b3d59e23f09640d33c7..8f7c8d27c9ca8de01d2c27754221655c23af1b0c 100644 (file)
@@ -414,7 +414,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
        const unsigned long nr_dest_folios = *out_folios;
        const u64 orig_end = start + len;
        const u32 blocksize = fs_info->sectorsize;
-       unsigned long max_out = nr_dest_folios * PAGE_SIZE;
+       const u32 min_folio_size = btrfs_min_folio_size(fs_info);
+       unsigned long max_out = nr_dest_folios * min_folio_size;
        unsigned int cur_len;
 
        workspace->params = zstd_get_btrfs_parameters(workspace->req_level, len);
@@ -452,7 +453,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
        folios[nr_folios++] = out_folio;
        workspace->out_buf.dst = folio_address(out_folio);
        workspace->out_buf.pos = 0;
-       workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
+       workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
 
        while (1) {
                size_t ret2;
@@ -486,8 +487,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
 
                /* Check if we need more output space */
                if (workspace->out_buf.pos == workspace->out_buf.size) {
-                       tot_out += PAGE_SIZE;
-                       max_out -= PAGE_SIZE;
+                       tot_out += min_folio_size;
+                       max_out -= min_folio_size;
                        if (nr_folios == nr_dest_folios) {
                                ret = -E2BIG;
                                goto out;
@@ -500,8 +501,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
                        folios[nr_folios++] = out_folio;
                        workspace->out_buf.dst = folio_address(out_folio);
                        workspace->out_buf.pos = 0;
-                       workspace->out_buf.size = min_t(size_t, max_out,
-                                                       PAGE_SIZE);
+                       workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
                }
 
                /* We've reached the end of the input */
@@ -551,8 +551,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
                        goto out;
                }
 
-               tot_out += PAGE_SIZE;
-               max_out -= PAGE_SIZE;
+               tot_out += min_folio_size;
+               max_out -= min_folio_size;
                if (nr_folios == nr_dest_folios) {
                        ret = -E2BIG;
                        goto out;
@@ -565,7 +565,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
                folios[nr_folios++] = out_folio;
                workspace->out_buf.dst = folio_address(out_folio);
                workspace->out_buf.pos = 0;
-               workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
+               workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
        }
 
        if (tot_out >= tot_in) {
@@ -587,14 +587,16 @@ out:
 
 int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 {
+       struct btrfs_fs_info *fs_info = cb_to_fs_info(cb);
        struct workspace *workspace = list_entry(ws, struct workspace, list);
        struct folio **folios_in = cb->compressed_folios;
        size_t srclen = cb->compressed_len;
        zstd_dstream *stream;
        int ret = 0;
-       const u32 blocksize = cb_to_fs_info(cb)->sectorsize;
+       const u32 blocksize = fs_info->sectorsize;
+       const unsigned int min_folio_size = btrfs_min_folio_size(fs_info);
        unsigned long folio_in_index = 0;
-       unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
+       unsigned long total_folios_in = DIV_ROUND_UP(srclen, min_folio_size);
        unsigned long buf_start;
        unsigned long total_out = 0;
 
@@ -612,7 +614,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 
        workspace->in_buf.src = kmap_local_folio(folios_in[folio_in_index], 0);
        workspace->in_buf.pos = 0;
-       workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
+       workspace->in_buf.size = min_t(size_t, srclen, min_folio_size);
 
        workspace->out_buf.dst = workspace->buf;
        workspace->out_buf.pos = 0;
@@ -657,11 +659,11 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
                                ret = -EIO;
                                goto done;
                        }
-                       srclen -= PAGE_SIZE;
+                       srclen -= min_folio_size;
                        workspace->in_buf.src =
                                kmap_local_folio(folios_in[folio_in_index], 0);
                        workspace->in_buf.pos = 0;
-                       workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
+                       workspace->in_buf.size = min_t(size_t, srclen, min_folio_size);
                }
        }
        ret = 0;