Skip to content

Commit e01a83e

Browse files
committed
Revert "btrfs: zstd: fix and simplify the inline extent decompression"
This reverts commit 1e7f6de. It causes my machine to not even boot, and Klara Modin reports that the cause is that small zstd-compressed files return garbage when read. Reported-by: Klara Modin <[email protected]> Link: https://lore.kernel.org/linux-btrfs/CABq1_vj4GpUeZpVG49OHCo-3sdbe2-2ROcu_xDvUG-6-5zPRXg@mail.gmail.com/ Reported-and-bisected-by: Linus Torvalds <[email protected]> Acked-by: David Sterba <[email protected]> Cc: Qu Wenruo <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5d9248e commit e01a83e

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

fs/btrfs/compression.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
169169
unsigned long *total_in, unsigned long *total_out);
170170
int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
171171
int zstd_decompress(struct list_head *ws, const u8 *data_in,
172-
struct page *dest_page, unsigned long dest_pgoff, size_t srclen,
172+
struct page *dest_page, unsigned long start_byte, size_t srclen,
173173
size_t destlen);
174174
void zstd_init_workspace_manager(void);
175175
void zstd_cleanup_workspace_manager(void);

fs/btrfs/zstd.c

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "misc.h"
2121
#include "compression.h"
2222
#include "ctree.h"
23-
#include "super.h"
2423

2524
#define ZSTD_BTRFS_MAX_WINDOWLOG 17
2625
#define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
@@ -619,48 +618,80 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
619618
}
620619

621620
int zstd_decompress(struct list_head *ws, const u8 *data_in,
622-
struct page *dest_page, unsigned long dest_pgoff, size_t srclen,
621+
struct page *dest_page, unsigned long start_byte, size_t srclen,
623622
size_t destlen)
624623
{
625624
struct workspace *workspace = list_entry(ws, struct workspace, list);
626-
struct btrfs_fs_info *fs_info = btrfs_sb(dest_page->mapping->host->i_sb);
627-
const u32 sectorsize = fs_info->sectorsize;
628625
zstd_dstream *stream;
629626
int ret = 0;
630-
unsigned long to_copy = 0;
627+
size_t ret2;
628+
unsigned long total_out = 0;
629+
unsigned long pg_offset = 0;
631630

632631
stream = zstd_init_dstream(
633632
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
634633
if (!stream) {
635634
pr_warn("BTRFS: zstd_init_dstream failed\n");
635+
ret = -EIO;
636636
goto finish;
637637
}
638638

639+
destlen = min_t(size_t, destlen, PAGE_SIZE);
640+
639641
workspace->in_buf.src = data_in;
640642
workspace->in_buf.pos = 0;
641643
workspace->in_buf.size = srclen;
642644

643645
workspace->out_buf.dst = workspace->buf;
644646
workspace->out_buf.pos = 0;
645-
workspace->out_buf.size = sectorsize;
646-
647-
/*
648-
* Since both input and output buffers should not exceed one sector,
649-
* one call should end the decompression.
650-
*/
651-
ret = zstd_decompress_stream(stream, &workspace->out_buf, &workspace->in_buf);
652-
if (zstd_is_error(ret)) {
653-
pr_warn_ratelimited("BTRFS: zstd_decompress_stream return %d\n",
654-
zstd_get_error_code(ret));
655-
goto finish;
647+
workspace->out_buf.size = PAGE_SIZE;
648+
649+
ret2 = 1;
650+
while (pg_offset < destlen
651+
&& workspace->in_buf.pos < workspace->in_buf.size) {
652+
unsigned long buf_start;
653+
unsigned long buf_offset;
654+
unsigned long bytes;
655+
656+
/* Check if the frame is over and we still need more input */
657+
if (ret2 == 0) {
658+
pr_debug("BTRFS: zstd_decompress_stream ended early\n");
659+
ret = -EIO;
660+
goto finish;
661+
}
662+
ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
663+
&workspace->in_buf);
664+
if (zstd_is_error(ret2)) {
665+
pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
666+
zstd_get_error_code(ret2));
667+
ret = -EIO;
668+
goto finish;
669+
}
670+
671+
buf_start = total_out;
672+
total_out += workspace->out_buf.pos;
673+
workspace->out_buf.pos = 0;
674+
675+
if (total_out <= start_byte)
676+
continue;
677+
678+
if (total_out > start_byte && buf_start < start_byte)
679+
buf_offset = start_byte - buf_start;
680+
else
681+
buf_offset = 0;
682+
683+
bytes = min_t(unsigned long, destlen - pg_offset,
684+
workspace->out_buf.size - buf_offset);
685+
686+
memcpy_to_page(dest_page, pg_offset,
687+
workspace->out_buf.dst + buf_offset, bytes);
688+
689+
pg_offset += bytes;
656690
}
657-
to_copy = workspace->out_buf.pos;
658-
memcpy_to_page(dest_page, dest_pgoff + to_copy, workspace->out_buf.dst, to_copy);
691+
ret = 0;
659692
finish:
660-
/* Error or early end. */
661-
if (unlikely(to_copy < destlen)) {
662-
ret = -EIO;
663-
memzero_page(dest_page, dest_pgoff + to_copy, destlen - to_copy);
693+
if (pg_offset < destlen) {
694+
memzero_page(dest_page, pg_offset, destlen - pg_offset);
664695
}
665696
return ret;
666697
}

0 commit comments

Comments
 (0)