Skip to content

Commit a6452b8

Browse files
adam900710kdave
authored andcommitted
btrfs: prepare zstd to support bs > ps cases
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>
1 parent c2ffb1e commit a6452b8

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

fs/btrfs/fs.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize)
7979
if (blocksize == PAGE_SIZE || blocksize == SZ_4K || blocksize == BTRFS_MIN_BLOCKSIZE)
8080
return true;
8181
#ifdef CONFIG_BTRFS_EXPERIMENTAL
82+
/*
83+
* For bs > ps support it's done by specifying a minimal folio order
84+
* for filemap, thus implying large data folios.
85+
* For HIGHMEM systems, we can not always access the content of a (large)
86+
* folio in one go, but go through them page by page.
87+
*
88+
* A lot of features don't implement a proper PAGE sized loop for large
89+
* folios, this includes:
90+
*
91+
* - compression
92+
* - verity
93+
* - encoded write
94+
*
95+
* Considering HIGHMEM is such a pain to deal with and it's going
96+
* to be deprecated eventually, just reject HIGHMEM && bs > ps cases.
97+
*/
98+
if (IS_ENABLED(CONFIG_HIGHMEM) && blocksize > PAGE_SIZE)
99+
return false;
82100
if (blocksize <= PAGE_SIZE)
83101
return true;
84102
#endif

fs/btrfs/zstd.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
414414
const unsigned long nr_dest_folios = *out_folios;
415415
const u64 orig_end = start + len;
416416
const u32 blocksize = fs_info->sectorsize;
417-
unsigned long max_out = nr_dest_folios * PAGE_SIZE;
417+
const u32 min_folio_size = btrfs_min_folio_size(fs_info);
418+
unsigned long max_out = nr_dest_folios * min_folio_size;
418419
unsigned int cur_len;
419420

420421
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,
452453
folios[nr_folios++] = out_folio;
453454
workspace->out_buf.dst = folio_address(out_folio);
454455
workspace->out_buf.pos = 0;
455-
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
456+
workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
456457

457458
while (1) {
458459
size_t ret2;
@@ -486,8 +487,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
486487

487488
/* Check if we need more output space */
488489
if (workspace->out_buf.pos == workspace->out_buf.size) {
489-
tot_out += PAGE_SIZE;
490-
max_out -= PAGE_SIZE;
490+
tot_out += min_folio_size;
491+
max_out -= min_folio_size;
491492
if (nr_folios == nr_dest_folios) {
492493
ret = -E2BIG;
493494
goto out;
@@ -500,8 +501,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
500501
folios[nr_folios++] = out_folio;
501502
workspace->out_buf.dst = folio_address(out_folio);
502503
workspace->out_buf.pos = 0;
503-
workspace->out_buf.size = min_t(size_t, max_out,
504-
PAGE_SIZE);
504+
workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
505505
}
506506

507507
/* We've reached the end of the input */
@@ -551,8 +551,8 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
551551
goto out;
552552
}
553553

554-
tot_out += PAGE_SIZE;
555-
max_out -= PAGE_SIZE;
554+
tot_out += min_folio_size;
555+
max_out -= min_folio_size;
556556
if (nr_folios == nr_dest_folios) {
557557
ret = -E2BIG;
558558
goto out;
@@ -565,7 +565,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
565565
folios[nr_folios++] = out_folio;
566566
workspace->out_buf.dst = folio_address(out_folio);
567567
workspace->out_buf.pos = 0;
568-
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
568+
workspace->out_buf.size = min_t(size_t, max_out, min_folio_size);
569569
}
570570

571571
if (tot_out >= tot_in) {
@@ -587,14 +587,16 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
587587

588588
int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
589589
{
590+
struct btrfs_fs_info *fs_info = cb_to_fs_info(cb);
590591
struct workspace *workspace = list_entry(ws, struct workspace, list);
591592
struct folio **folios_in = cb->compressed_folios;
592593
size_t srclen = cb->compressed_len;
593594
zstd_dstream *stream;
594595
int ret = 0;
595-
const u32 blocksize = cb_to_fs_info(cb)->sectorsize;
596+
const u32 blocksize = fs_info->sectorsize;
597+
const unsigned int min_folio_size = btrfs_min_folio_size(fs_info);
596598
unsigned long folio_in_index = 0;
597-
unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
599+
unsigned long total_folios_in = DIV_ROUND_UP(srclen, min_folio_size);
598600
unsigned long buf_start;
599601
unsigned long total_out = 0;
600602

@@ -612,7 +614,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
612614

613615
workspace->in_buf.src = kmap_local_folio(folios_in[folio_in_index], 0);
614616
workspace->in_buf.pos = 0;
615-
workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
617+
workspace->in_buf.size = min_t(size_t, srclen, min_folio_size);
616618

617619
workspace->out_buf.dst = workspace->buf;
618620
workspace->out_buf.pos = 0;
@@ -657,11 +659,11 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
657659
ret = -EIO;
658660
goto done;
659661
}
660-
srclen -= PAGE_SIZE;
662+
srclen -= min_folio_size;
661663
workspace->in_buf.src =
662664
kmap_local_folio(folios_in[folio_in_index], 0);
663665
workspace->in_buf.pos = 0;
664-
workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
666+
workspace->in_buf.size = min_t(size_t, srclen, min_folio_size);
665667
}
666668
}
667669
ret = 0;

0 commit comments

Comments
 (0)