/* * Btrfs LZO compression format * * Regular and inlined LZO compressed data extents consist of: * * 1. Header * Fixed size. LZO_LEN (4) bytes long, LE32. * Records the total size (including the header) of compressed data. * * 2. Segment(s) * Variable size. Each segment includes one segment header, followed by data * payload. * One regular LZO compressed extent can have one or more segments. * For inlined LZO compressed extent, only one segment is allowed. * One segment represents at most one sector of uncompressed data. * * 2.1 Segment header * Fixed size. LZO_LEN (4) bytes long, LE32. * Records the total size of the segment (not including the header). * Segment header never crosses sector boundary, thus it's possible to * have at most 3 padding zeros at the end of the sector. * * 2.2 Data Payload * Variable size. Size up limit should be lzo1x_worst_compress(sectorsize) * which is 4419 for a 4KiB sectorsize. * * Example with 4K sectorsize: * Page 1: * 0 0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10 * 0x0000 | Header | SegHdr 01 | Data payload 01 ... | * ... * 0x0ff0 | SegHdr N | Data payload N ... |00| * ^^ padding zeros * Page 2: * 0x1000 | SegHdr N+1| Data payload N+1 ... |
*/
/* * Will do: * * - Write a segment header into the destination * - Copy the compressed buffer into the destination * - Make sure we have enough space in the last sector to fit a segment header * If not, we will pad at most (LZO_LEN (4)) - 1 bytes of zeros. * * Will allocate new pages when needed.
*/ staticint copy_compressed_data_to_page(char *compressed_data,
size_t compressed_size, struct folio **out_folios, unsignedlong max_nr_folio,
u32 *cur_out, const u32 sectorsize)
{
u32 sector_bytes_left;
u32 orig_out; struct folio *cur_folio; char *kaddr;
if ((*cur_out / PAGE_SIZE) >= max_nr_folio) return -E2BIG;
/* * We never allow a segment header crossing sector boundary, previous * run should ensure we have enough space left inside the sector.
*/
ASSERT((*cur_out / sectorsize) == (*cur_out + LZO_LEN - 1) / sectorsize);
cur_folio = out_folios[*cur_out / PAGE_SIZE]; /* Allocate a new page */ if (!cur_folio) {
cur_folio = btrfs_alloc_compr_folio(); if (!cur_folio) return -ENOMEM;
out_folios[*cur_out / PAGE_SIZE] = cur_folio;
}
/* * Check if we can fit the next segment header into the remaining space * of the sector.
*/
sector_bytes_left = round_up(*cur_out, sectorsize) - *cur_out; if (sector_bytes_left >= LZO_LEN || sector_bytes_left == 0) goto out;
/* The remaining size is not enough, pad it with zeros */
memset(kaddr + offset_in_page(*cur_out), 0,
sector_bytes_left);
*cur_out += sector_bytes_left;
out:
kunmap_local(kaddr); return 0;
}
int lzo_compress_folios(struct list_head *ws, struct address_space *mapping,
u64 start, struct folio **folios, unsignedlong *out_folios, unsignedlong *total_in, unsignedlong *total_out)
{ struct workspace *workspace = list_entry(ws, struct workspace, list); const u32 sectorsize = inode_to_fs_info(mapping->host)->sectorsize; struct folio *folio_in = NULL; char *sizes_ptr; constunsignedlong max_nr_folio = *out_folios; int ret = 0; /* Points to the file offset of input data */
u64 cur_in = start; /* Points to the current output byte */
u32 cur_out = 0;
u32 len = *total_out;
/* * Skip the header for now, we will later come back and write the total * compressed size
*/
cur_out += LZO_LEN; while (cur_in < start + len) { char *data_in; const u32 sectorsize_mask = sectorsize - 1;
u32 sector_off = (cur_in - start) & sectorsize_mask;
u32 in_len;
size_t out_len;
/* Get the input page first */ if (!folio_in) {
ret = btrfs_compress_filemap_get_folio(mapping, cur_in, &folio_in); if (ret < 0) goto out;
}
/* Compress at most one sector of data each time */
in_len = min_t(u32, start + len - cur_in, sectorsize - sector_off);
ASSERT(in_len);
data_in = kmap_local_folio(folio_in, offset_in_folio(folio_in, cur_in));
ret = lzo1x_1_compress(data_in, in_len,
workspace->cbuf, &out_len,
workspace->mem);
kunmap_local(data_in); if (unlikely(ret < 0)) { /* lzo1x_1_compress never fails. */
ret = -EIO; goto out;
}
ret = copy_compressed_data_to_page(workspace->cbuf, out_len,
folios, max_nr_folio,
&cur_out, sectorsize); if (ret < 0) goto out;
cur_in += in_len;
/* * Check if we're making it bigger after two sectors. And if * it is so, give up.
*/ if (cur_in - start > sectorsize * 2 && cur_in - start < cur_out) {
ret = -E2BIG; goto out;
}
/* Check if we have reached page boundary */ if (PAGE_ALIGNED(cur_in)) {
folio_put(folio_in);
folio_in = NULL;
}
}
/* Store the size of all chunks of compressed data */
sizes_ptr = kmap_local_folio(folios[0], 0);
write_compress_length(sizes_ptr, cur_out);
kunmap_local(sizes_ptr);
/* * Copy the compressed segment payload into @dest. * * For the payload there will be no padding, just need to do page switching.
*/ staticvoid copy_compressed_segment(struct compressed_bio *cb, char *dest, u32 len, u32 *cur_in)
{
u32 orig_in = *cur_in;
/* * LZO header length check * * The total length should not exceed the maximum extent length, * and all sectors should be used. * If this happens, it means the compressed extent is corrupted.
*/ if (unlikely(len_in > min_t(size_t, BTRFS_MAX_COMPRESSED, cb->compressed_len) ||
round_up(len_in, sectorsize) < cb->compressed_len)) { struct btrfs_inode *inode = cb->bbio.inode;
/* Go through each lzo segment */ while (cur_in < len_in) { struct folio *cur_folio; /* Length of the compressed segment */
u32 seg_len;
u32 sector_bytes_left;
size_t out_len = lzo1x_worst_compress(sectorsize);
/* * We should always have enough space for one segment header * inside current sector.
*/
ASSERT(cur_in / sectorsize ==
(cur_in + LZO_LEN - 1) / sectorsize);
cur_folio = cb->compressed_folios[cur_in / PAGE_SIZE];
ASSERT(cur_folio);
kaddr = kmap_local_folio(cur_folio, 0);
seg_len = read_compress_length(kaddr + offset_in_page(cur_in));
kunmap_local(kaddr);
cur_in += LZO_LEN;
if (unlikely(seg_len > WORKSPACE_CBUF_LENGTH)) { struct btrfs_inode *inode = cb->bbio.inode;
/* * seg_len shouldn't be larger than we have allocated * for workspace->cbuf
*/
btrfs_err(fs_info, "lzo segment too big, root %llu inode %llu offset %llu len %u",
btrfs_root_id(inode->root), btrfs_ino(inode),
cb->start, seg_len); return -EIO;
}
/* Copy the compressed segment payload into workspace */
copy_compressed_segment(cb, workspace->cbuf, seg_len, &cur_in);
/* Decompress the data */
ret = lzo1x_decompress_safe(workspace->cbuf, seg_len,
workspace->buf, &out_len); if (unlikely(ret != LZO_E_OK)) { struct btrfs_inode *inode = cb->bbio.inode;
/* Copy the data into inode pages */
ret = btrfs_decompress_buf2page(workspace->buf, out_len, cb, cur_out);
cur_out += out_len;
/* All data read, exit */ if (ret == 0) return 0;
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;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.