Commit a6e66e6f authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: rework lzo_decompress_bio() to make it subpage compatible

For the initial subpage support, although we won't support compressed
write, we still need to support compressed read.

But for lzo_decompress_bio() it has several problems:

- The abuse of PAGE_SIZE for boundary detection
  For subpage case, we should follow sectorsize to detect the padding
  zeros.
  Using PAGE_SIZE will cause subpage compress read to skip certain
  bytes, and causing read error.

- Too many helper variables
  There are half a dozen helper variables, which is only making things
  harder to read

This patch will rework lzo_decompress_bio() to make it work for subpage:

- Use sectorsize to do boundary check, while still use PAGE_SIZE for
  page switching
  This allows us to have the same on-disk format for 4K sectorsize fs,
  while take advantage of larger page size.

- Use two main cursors
  Only @cur_in and @cur_out is utilized as the main cursor.
  The helper variables will only be declared inside the loop, and only 2
  helper variables needed.

- Introduce a helper function to copy compressed segment payload
  Introduce a new helper, copy_compressed_segment(), to copy a
  compressed segment to workspace buffer.
  This function will handle the page switching.

Now the net result is, with all the excessive comments and new helper
function, the refactored code is still smaller, and easier to read.

For other decompression code, they have no special padding rule, thus no
need to bother for initial subpage support, but will be refactored to
the same style later.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 1c3dc173
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <linux/lzo.h> #include <linux/lzo.h>
#include <linux/refcount.h> #include <linux/refcount.h>
#include "compression.h" #include "compression.h"
#include "ctree.h"
#define LZO_LEN 4 #define LZO_LEN 4
...@@ -271,130 +272,113 @@ int lzo_compress_pages(struct list_head *ws, struct address_space *mapping, ...@@ -271,130 +272,113 @@ int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
return ret; return ret;
} }
/*
* Copy the compressed segment payload into @dest.
*
* For the payload there will be no padding, just need to do page switching.
*/
static void copy_compressed_segment(struct compressed_bio *cb,
char *dest, u32 len, u32 *cur_in)
{
u32 orig_in = *cur_in;
while (*cur_in < orig_in + len) {
struct page *cur_page;
u32 copy_len = min_t(u32, PAGE_SIZE - offset_in_page(*cur_in),
orig_in + len - *cur_in);
ASSERT(copy_len);
cur_page = cb->compressed_pages[*cur_in / PAGE_SIZE];
memcpy(dest + *cur_in - orig_in,
page_address(cur_page) + offset_in_page(*cur_in),
copy_len);
*cur_in += copy_len;
}
}
int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb) int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
{ {
struct workspace *workspace = list_entry(ws, struct workspace, list); struct workspace *workspace = list_entry(ws, struct workspace, list);
int ret = 0, ret2; const struct btrfs_fs_info *fs_info = btrfs_sb(cb->inode->i_sb);
char *data_in; const u32 sectorsize = fs_info->sectorsize;
unsigned long page_in_index = 0; int ret;
size_t srclen = cb->compressed_len; /* Compressed data length, can be unaligned */
unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE); u32 len_in;
unsigned long buf_start; /* Offset inside the compressed data */
unsigned long buf_offset = 0; u32 cur_in = 0;
unsigned long bytes; /* Bytes decompressed so far */
unsigned long working_bytes; u32 cur_out = 0;
size_t in_len;
size_t out_len; len_in = read_compress_length(page_address(cb->compressed_pages[0]));
const size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE); cur_in += LZO_LEN;
unsigned long in_offset;
unsigned long in_page_bytes_left;
unsigned long tot_in;
unsigned long tot_out;
unsigned long tot_len;
char *buf;
struct page **pages_in = cb->compressed_pages;
data_in = page_address(pages_in[0]);
tot_len = read_compress_length(data_in);
/* /*
* Compressed data header check. * LZO header length check
* *
* The real compressed size can't exceed the maximum extent length, and * The total length should not exceed the maximum extent length,
* all pages should be used (whole unused page with just the segment * and all sectors should be used.
* header is not possible). If this happens it means the compressed * If this happens, it means the compressed extent is corrupted.
* extent is corrupted.
*/ */
if (tot_len > min_t(size_t, BTRFS_MAX_COMPRESSED, srclen) || if (len_in > min_t(size_t, BTRFS_MAX_COMPRESSED, cb->compressed_len) ||
tot_len < srclen - PAGE_SIZE) { round_up(len_in, sectorsize) < cb->compressed_len) {
ret = -EUCLEAN; btrfs_err(fs_info,
goto done; "invalid lzo header, lzo len %u compressed len %u",
len_in, cb->compressed_len);
return -EUCLEAN;
} }
tot_in = LZO_LEN; /* Go through each lzo segment */
in_offset = LZO_LEN; while (cur_in < len_in) {
in_page_bytes_left = PAGE_SIZE - LZO_LEN; struct page *cur_page;
/* Length of the compressed segment */
tot_out = 0; u32 seg_len;
u32 sector_bytes_left;
while (tot_in < tot_len) { size_t out_len = lzo1x_worst_compress(sectorsize);
in_len = read_compress_length(data_in + in_offset);
in_page_bytes_left -= LZO_LEN;
in_offset += LZO_LEN;
tot_in += LZO_LEN;
/* /*
* Segment header check. * We should always have enough space for one segment header
* * inside current sector.
* The segment length must not exceed the maximum LZO
* compression size, nor the total compressed size.
*/ */
if (in_len > max_segment_len || tot_in + in_len > tot_len) { ASSERT(cur_in / sectorsize ==
ret = -EUCLEAN; (cur_in + LZO_LEN - 1) / sectorsize);
goto done; cur_page = cb->compressed_pages[cur_in / PAGE_SIZE];
} ASSERT(cur_page);
seg_len = read_compress_length(page_address(cur_page) +
tot_in += in_len; offset_in_page(cur_in));
working_bytes = in_len; cur_in += LZO_LEN;
/* fast path: avoid using the working buffer */ /* Copy the compressed segment payload into workspace */
if (in_page_bytes_left >= in_len) { copy_compressed_segment(cb, workspace->cbuf, seg_len, &cur_in);
buf = data_in + in_offset;
bytes = in_len; /* Decompress the data */
goto cont; ret = lzo1x_decompress_safe(workspace->cbuf, seg_len,
} workspace->buf, &out_len);
/* copy bytes from the pages into the working buffer */
buf = workspace->cbuf;
buf_offset = 0;
while (working_bytes) {
bytes = min(working_bytes, in_page_bytes_left);
memcpy(buf + buf_offset, data_in + in_offset, bytes);
buf_offset += bytes;
cont:
working_bytes -= bytes;
in_page_bytes_left -= bytes;
in_offset += bytes;
/* check if we need to pick another page */
if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
|| in_page_bytes_left == 0) {
tot_in += in_page_bytes_left;
if (working_bytes == 0 && tot_in >= tot_len)
break;
if (page_in_index + 1 >= total_pages_in) {
ret = -EIO;
goto done;
}
page_in_index++;
data_in = page_address(pages_in[page_in_index]);
in_page_bytes_left = PAGE_SIZE;
in_offset = 0;
}
}
out_len = max_segment_len;
ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
&out_len);
if (ret != LZO_E_OK) { if (ret != LZO_E_OK) {
pr_warn("BTRFS: decompress failed\n"); btrfs_err(fs_info, "failed to decompress");
ret = -EIO; ret = -EIO;
break; goto out;
} }
buf_start = tot_out; /* Copy the data into inode pages */
tot_out += out_len; ret = btrfs_decompress_buf2page(workspace->buf, out_len, cb, cur_out);
cur_out += out_len;
ret2 = btrfs_decompress_buf2page(workspace->buf, out_len, /* All data read, exit */
cb, buf_start); if (ret == 0)
if (ret2 == 0) goto out;
break; ret = 0;
/* Check if the sector has enough space for a segment header */
sector_bytes_left = sectorsize - (cur_in % sectorsize);
if (sector_bytes_left >= LZO_LEN)
continue;
/* Skip the padding zeros */
cur_in += sector_bytes_left;
} }
done: out:
if (!ret) if (!ret)
zero_fill_bio(cb->orig_bio); zero_fill_bio(cb->orig_bio);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment