1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2a6fa6faeSLi Zefan /*
3a6fa6faeSLi Zefan * Copyright (C) 2008 Oracle. All rights reserved.
4a6fa6faeSLi Zefan */
5a6fa6faeSLi Zefan
6a6fa6faeSLi Zefan #include <linux/kernel.h>
7a6fa6faeSLi Zefan #include <linux/slab.h>
86acafd1eSDavid Sterba #include <linux/mm.h>
9a6fa6faeSLi Zefan #include <linux/init.h>
10a6fa6faeSLi Zefan #include <linux/err.h>
11a6fa6faeSLi Zefan #include <linux/sched.h>
12a6fa6faeSLi Zefan #include <linux/pagemap.h>
13a6fa6faeSLi Zefan #include <linux/bio.h>
14a6fa6faeSLi Zefan #include <linux/lzo.h>
15e1ddce71SAnand Jain #include <linux/refcount.h>
169b569ea0SJosef Bacik #include "messages.h"
17a6fa6faeSLi Zefan #include "compression.h"
18a6e66e6fSQu Wenruo #include "ctree.h"
197f0add25SJosef Bacik #include "super.h"
20544fe4a9SChristoph Hellwig #include "btrfs_inode.h"
21a6fa6faeSLi Zefan
22a6fa6faeSLi Zefan #define LZO_LEN 4
23a6fa6faeSLi Zefan
242a1f7c0cSQu Wenruo /*
252a1f7c0cSQu Wenruo * Btrfs LZO compression format
262a1f7c0cSQu Wenruo *
272a1f7c0cSQu Wenruo * Regular and inlined LZO compressed data extents consist of:
282a1f7c0cSQu Wenruo *
292a1f7c0cSQu Wenruo * 1. Header
302a1f7c0cSQu Wenruo * Fixed size. LZO_LEN (4) bytes long, LE32.
312a1f7c0cSQu Wenruo * Records the total size (including the header) of compressed data.
322a1f7c0cSQu Wenruo *
332a1f7c0cSQu Wenruo * 2. Segment(s)
3452042d8eSAndrea Gelmini * Variable size. Each segment includes one segment header, followed by data
352a1f7c0cSQu Wenruo * payload.
362a1f7c0cSQu Wenruo * One regular LZO compressed extent can have one or more segments.
372a1f7c0cSQu Wenruo * For inlined LZO compressed extent, only one segment is allowed.
38d4088803SQu Wenruo * One segment represents at most one sector of uncompressed data.
392a1f7c0cSQu Wenruo *
402a1f7c0cSQu Wenruo * 2.1 Segment header
412a1f7c0cSQu Wenruo * Fixed size. LZO_LEN (4) bytes long, LE32.
422a1f7c0cSQu Wenruo * Records the total size of the segment (not including the header).
43d4088803SQu Wenruo * Segment header never crosses sector boundary, thus it's possible to
44d4088803SQu Wenruo * have at most 3 padding zeros at the end of the sector.
452a1f7c0cSQu Wenruo *
462a1f7c0cSQu Wenruo * 2.2 Data Payload
47d4088803SQu Wenruo * Variable size. Size up limit should be lzo1x_worst_compress(sectorsize)
48d4088803SQu Wenruo * which is 4419 for a 4KiB sectorsize.
492a1f7c0cSQu Wenruo *
50d4088803SQu Wenruo * Example with 4K sectorsize:
512a1f7c0cSQu Wenruo * Page 1:
522a1f7c0cSQu Wenruo * 0 0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10
532a1f7c0cSQu Wenruo * 0x0000 | Header | SegHdr 01 | Data payload 01 ... |
542a1f7c0cSQu Wenruo * ...
552a1f7c0cSQu Wenruo * 0x0ff0 | SegHdr N | Data payload N ... |00|
562a1f7c0cSQu Wenruo * ^^ padding zeros
572a1f7c0cSQu Wenruo * Page 2:
582a1f7c0cSQu Wenruo * 0x1000 | SegHdr N+1| Data payload N+1 ... |
592a1f7c0cSQu Wenruo */
602a1f7c0cSQu Wenruo
61dc4a4bdbSDāvis Mosāns #define WORKSPACE_BUF_LENGTH (lzo1x_worst_compress(PAGE_SIZE))
62dc4a4bdbSDāvis Mosāns #define WORKSPACE_CBUF_LENGTH (lzo1x_worst_compress(PAGE_SIZE))
63dc4a4bdbSDāvis Mosāns
64a6fa6faeSLi Zefan struct workspace {
65a6fa6faeSLi Zefan void *mem;
663fb40375SJie Liu void *buf; /* where decompressed data goes */
673fb40375SJie Liu void *cbuf; /* where compressed data goes */
68a6fa6faeSLi Zefan struct list_head list;
69a6fa6faeSLi Zefan };
70a6fa6faeSLi Zefan
7192ee5530SDennis Zhou static struct workspace_manager wsm;
7292ee5530SDennis Zhou
lzo_free_workspace(struct list_head * ws)73d20f395fSDavid Sterba void lzo_free_workspace(struct list_head *ws)
74a6fa6faeSLi Zefan {
75a6fa6faeSLi Zefan struct workspace *workspace = list_entry(ws, struct workspace, list);
76a6fa6faeSLi Zefan
776acafd1eSDavid Sterba kvfree(workspace->buf);
786acafd1eSDavid Sterba kvfree(workspace->cbuf);
796acafd1eSDavid Sterba kvfree(workspace->mem);
80a6fa6faeSLi Zefan kfree(workspace);
81a6fa6faeSLi Zefan }
82a6fa6faeSLi Zefan
lzo_alloc_workspace(unsigned int level)83d20f395fSDavid Sterba struct list_head *lzo_alloc_workspace(unsigned int level)
84a6fa6faeSLi Zefan {
85a6fa6faeSLi Zefan struct workspace *workspace;
86a6fa6faeSLi Zefan
87389a6cfcSDavid Sterba workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
88a6fa6faeSLi Zefan if (!workspace)
89a6fa6faeSLi Zefan return ERR_PTR(-ENOMEM);
90a6fa6faeSLi Zefan
916acafd1eSDavid Sterba workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN);
92dc4a4bdbSDāvis Mosāns workspace->buf = kvmalloc(WORKSPACE_BUF_LENGTH, GFP_KERNEL | __GFP_NOWARN);
93dc4a4bdbSDāvis Mosāns workspace->cbuf = kvmalloc(WORKSPACE_CBUF_LENGTH, GFP_KERNEL | __GFP_NOWARN);
94a6fa6faeSLi Zefan if (!workspace->mem || !workspace->buf || !workspace->cbuf)
95a6fa6faeSLi Zefan goto fail;
96a6fa6faeSLi Zefan
97a6fa6faeSLi Zefan INIT_LIST_HEAD(&workspace->list);
98a6fa6faeSLi Zefan
99a6fa6faeSLi Zefan return &workspace->list;
100a6fa6faeSLi Zefan fail:
101a6fa6faeSLi Zefan lzo_free_workspace(&workspace->list);
102a6fa6faeSLi Zefan return ERR_PTR(-ENOMEM);
103a6fa6faeSLi Zefan }
104a6fa6faeSLi Zefan
write_compress_length(char * buf,size_t len)105a6fa6faeSLi Zefan static inline void write_compress_length(char *buf, size_t len)
106a6fa6faeSLi Zefan {
107a6fa6faeSLi Zefan __le32 dlen;
108a6fa6faeSLi Zefan
109a6fa6faeSLi Zefan dlen = cpu_to_le32(len);
110a6fa6faeSLi Zefan memcpy(buf, &dlen, LZO_LEN);
111a6fa6faeSLi Zefan }
112a6fa6faeSLi Zefan
read_compress_length(const char * buf)11314a3357bSDavid Sterba static inline size_t read_compress_length(const char *buf)
114a6fa6faeSLi Zefan {
115a6fa6faeSLi Zefan __le32 dlen;
116a6fa6faeSLi Zefan
117a6fa6faeSLi Zefan memcpy(&dlen, buf, LZO_LEN);
118a6fa6faeSLi Zefan return le32_to_cpu(dlen);
119a6fa6faeSLi Zefan }
120a6fa6faeSLi Zefan
121d4088803SQu Wenruo /*
122d4088803SQu Wenruo * Will do:
123d4088803SQu Wenruo *
124d4088803SQu Wenruo * - Write a segment header into the destination
125d4088803SQu Wenruo * - Copy the compressed buffer into the destination
126d4088803SQu Wenruo * - Make sure we have enough space in the last sector to fit a segment header
127d4088803SQu Wenruo * If not, we will pad at most (LZO_LEN (4)) - 1 bytes of zeros.
128d4088803SQu Wenruo *
129d4088803SQu Wenruo * Will allocate new pages when needed.
130d4088803SQu Wenruo */
copy_compressed_data_to_page(char * compressed_data,size_t compressed_size,struct page ** out_pages,unsigned long max_nr_page,u32 * cur_out,const u32 sectorsize)131d4088803SQu Wenruo static int copy_compressed_data_to_page(char *compressed_data,
132d4088803SQu Wenruo size_t compressed_size,
133d4088803SQu Wenruo struct page **out_pages,
1346f019c0eSQu Wenruo unsigned long max_nr_page,
135d4088803SQu Wenruo u32 *cur_out,
136d4088803SQu Wenruo const u32 sectorsize)
137d4088803SQu Wenruo {
138d4088803SQu Wenruo u32 sector_bytes_left;
139d4088803SQu Wenruo u32 orig_out;
140d4088803SQu Wenruo struct page *cur_page;
141037c50bfSLinus Torvalds char *kaddr;
142d4088803SQu Wenruo
1436f019c0eSQu Wenruo if ((*cur_out / PAGE_SIZE) >= max_nr_page)
1446f019c0eSQu Wenruo return -E2BIG;
1456f019c0eSQu Wenruo
146d4088803SQu Wenruo /*
147d4088803SQu Wenruo * We never allow a segment header crossing sector boundary, previous
148d4088803SQu Wenruo * run should ensure we have enough space left inside the sector.
149d4088803SQu Wenruo */
150d4088803SQu Wenruo ASSERT((*cur_out / sectorsize) == (*cur_out + LZO_LEN - 1) / sectorsize);
151d4088803SQu Wenruo
152d4088803SQu Wenruo cur_page = out_pages[*cur_out / PAGE_SIZE];
153d4088803SQu Wenruo /* Allocate a new page */
154d4088803SQu Wenruo if (!cur_page) {
155d4088803SQu Wenruo cur_page = alloc_page(GFP_NOFS);
156d4088803SQu Wenruo if (!cur_page)
157d4088803SQu Wenruo return -ENOMEM;
158d4088803SQu Wenruo out_pages[*cur_out / PAGE_SIZE] = cur_page;
159d4088803SQu Wenruo }
160d4088803SQu Wenruo
16151c0674aSFabio M. De Francesco kaddr = kmap_local_page(cur_page);
162037c50bfSLinus Torvalds write_compress_length(kaddr + offset_in_page(*cur_out),
163d4088803SQu Wenruo compressed_size);
164d4088803SQu Wenruo *cur_out += LZO_LEN;
165d4088803SQu Wenruo
166d4088803SQu Wenruo orig_out = *cur_out;
167d4088803SQu Wenruo
168d4088803SQu Wenruo /* Copy compressed data */
169d4088803SQu Wenruo while (*cur_out - orig_out < compressed_size) {
170d4088803SQu Wenruo u32 copy_len = min_t(u32, sectorsize - *cur_out % sectorsize,
171d4088803SQu Wenruo orig_out + compressed_size - *cur_out);
172d4088803SQu Wenruo
17351c0674aSFabio M. De Francesco kunmap_local(kaddr);
1746fdf8864SLinus Torvalds
1756f019c0eSQu Wenruo if ((*cur_out / PAGE_SIZE) >= max_nr_page)
1766f019c0eSQu Wenruo return -E2BIG;
1776f019c0eSQu Wenruo
178d4088803SQu Wenruo cur_page = out_pages[*cur_out / PAGE_SIZE];
179d4088803SQu Wenruo /* Allocate a new page */
180d4088803SQu Wenruo if (!cur_page) {
181d4088803SQu Wenruo cur_page = alloc_page(GFP_NOFS);
182d4088803SQu Wenruo if (!cur_page)
183d4088803SQu Wenruo return -ENOMEM;
184d4088803SQu Wenruo out_pages[*cur_out / PAGE_SIZE] = cur_page;
185d4088803SQu Wenruo }
18651c0674aSFabio M. De Francesco kaddr = kmap_local_page(cur_page);
187d4088803SQu Wenruo
188037c50bfSLinus Torvalds memcpy(kaddr + offset_in_page(*cur_out),
189d4088803SQu Wenruo compressed_data + *cur_out - orig_out, copy_len);
190d4088803SQu Wenruo
191d4088803SQu Wenruo *cur_out += copy_len;
192d4088803SQu Wenruo }
193d4088803SQu Wenruo
194d4088803SQu Wenruo /*
195d4088803SQu Wenruo * Check if we can fit the next segment header into the remaining space
196d4088803SQu Wenruo * of the sector.
197d4088803SQu Wenruo */
198d4088803SQu Wenruo sector_bytes_left = round_up(*cur_out, sectorsize) - *cur_out;
199d4088803SQu Wenruo if (sector_bytes_left >= LZO_LEN || sector_bytes_left == 0)
200037c50bfSLinus Torvalds goto out;
201d4088803SQu Wenruo
202d4088803SQu Wenruo /* The remaining size is not enough, pad it with zeros */
203037c50bfSLinus Torvalds memset(kaddr + offset_in_page(*cur_out), 0,
204d4088803SQu Wenruo sector_bytes_left);
205d4088803SQu Wenruo *cur_out += sector_bytes_left;
206037c50bfSLinus Torvalds
207037c50bfSLinus Torvalds out:
20851c0674aSFabio M. De Francesco kunmap_local(kaddr);
209d4088803SQu Wenruo return 0;
210d4088803SQu Wenruo }
211d4088803SQu Wenruo
lzo_compress_pages(struct list_head * ws,struct address_space * mapping,u64 start,struct page ** pages,unsigned long * out_pages,unsigned long * total_in,unsigned long * total_out)212c4bf665aSDavid Sterba int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
213c4bf665aSDavid Sterba u64 start, struct page **pages, unsigned long *out_pages,
214c4bf665aSDavid Sterba unsigned long *total_in, unsigned long *total_out)
215a6fa6faeSLi Zefan {
216a6fa6faeSLi Zefan struct workspace *workspace = list_entry(ws, struct workspace, list);
217d4088803SQu Wenruo const u32 sectorsize = btrfs_sb(mapping->host->i_sb)->sectorsize;
218d4088803SQu Wenruo struct page *page_in = NULL;
219037c50bfSLinus Torvalds char *sizes_ptr;
2206f019c0eSQu Wenruo const unsigned long max_nr_page = *out_pages;
221a6fa6faeSLi Zefan int ret = 0;
222d4088803SQu Wenruo /* Points to the file offset of input data */
223d4088803SQu Wenruo u64 cur_in = start;
224d4088803SQu Wenruo /* Points to the current output byte */
225d4088803SQu Wenruo u32 cur_out = 0;
226d4088803SQu Wenruo u32 len = *total_out;
227a6fa6faeSLi Zefan
2286f019c0eSQu Wenruo ASSERT(max_nr_page > 0);
229a6fa6faeSLi Zefan *out_pages = 0;
230a6fa6faeSLi Zefan *total_out = 0;
231a6fa6faeSLi Zefan *total_in = 0;
232a6fa6faeSLi Zefan
233a6fa6faeSLi Zefan /*
234d4088803SQu Wenruo * Skip the header for now, we will later come back and write the total
235d4088803SQu Wenruo * compressed size
236a6fa6faeSLi Zefan */
237d4088803SQu Wenruo cur_out += LZO_LEN;
238d4088803SQu Wenruo while (cur_in < start + len) {
239037c50bfSLinus Torvalds char *data_in;
240d4088803SQu Wenruo const u32 sectorsize_mask = sectorsize - 1;
241d4088803SQu Wenruo u32 sector_off = (cur_in - start) & sectorsize_mask;
242d4088803SQu Wenruo u32 in_len;
243d4088803SQu Wenruo size_t out_len;
244a6fa6faeSLi Zefan
245d4088803SQu Wenruo /* Get the input page first */
246d4088803SQu Wenruo if (!page_in) {
247d4088803SQu Wenruo page_in = find_get_page(mapping, cur_in >> PAGE_SHIFT);
248d4088803SQu Wenruo ASSERT(page_in);
249d4088803SQu Wenruo }
250d4088803SQu Wenruo
251d4088803SQu Wenruo /* Compress at most one sector of data each time */
252d4088803SQu Wenruo in_len = min_t(u32, start + len - cur_in, sectorsize - sector_off);
253d4088803SQu Wenruo ASSERT(in_len);
25451c0674aSFabio M. De Francesco data_in = kmap_local_page(page_in);
255037c50bfSLinus Torvalds ret = lzo1x_1_compress(data_in +
256d4088803SQu Wenruo offset_in_page(cur_in), in_len,
257d4088803SQu Wenruo workspace->cbuf, &out_len,
258d4088803SQu Wenruo workspace->mem);
25951c0674aSFabio M. De Francesco kunmap_local(data_in);
260d4088803SQu Wenruo if (ret < 0) {
261d4088803SQu Wenruo pr_debug("BTRFS: lzo in loop returned %d\n", ret);
26260e1975aSZach Brown ret = -EIO;
263a6fa6faeSLi Zefan goto out;
264a6fa6faeSLi Zefan }
265a6fa6faeSLi Zefan
266d4088803SQu Wenruo ret = copy_compressed_data_to_page(workspace->cbuf, out_len,
2676f019c0eSQu Wenruo pages, max_nr_page,
2686f019c0eSQu Wenruo &cur_out, sectorsize);
269d4088803SQu Wenruo if (ret < 0)
270d4088803SQu Wenruo goto out;
271a6fa6faeSLi Zefan
272d4088803SQu Wenruo cur_in += in_len;
273a6fa6faeSLi Zefan
274a6fa6faeSLi Zefan /*
275d4088803SQu Wenruo * Check if we're making it bigger after two sectors. And if
276d4088803SQu Wenruo * it is so, give up.
277a6fa6faeSLi Zefan */
278d4088803SQu Wenruo if (cur_in - start > sectorsize * 2 && cur_in - start < cur_out) {
27960e1975aSZach Brown ret = -E2BIG;
280a6fa6faeSLi Zefan goto out;
281a6fa6faeSLi Zefan }
282a6fa6faeSLi Zefan
283d4088803SQu Wenruo /* Check if we have reached page boundary */
284ce394a7fSYushan Zhou if (PAGE_ALIGNED(cur_in)) {
285d4088803SQu Wenruo put_page(page_in);
286d4088803SQu Wenruo page_in = NULL;
287a6fa6faeSLi Zefan }
288a6fa6faeSLi Zefan }
289a6fa6faeSLi Zefan
290d4088803SQu Wenruo /* Store the size of all chunks of compressed data */
291ccaa66c8SDavid Sterba sizes_ptr = kmap_local_page(pages[0]);
292037c50bfSLinus Torvalds write_compress_length(sizes_ptr, cur_out);
293ccaa66c8SDavid Sterba kunmap_local(sizes_ptr);
294a6fa6faeSLi Zefan
295a6fa6faeSLi Zefan ret = 0;
296d4088803SQu Wenruo *total_out = cur_out;
297d4088803SQu Wenruo *total_in = cur_in - start;
298a6fa6faeSLi Zefan out:
299daf87e95SQu Wenruo if (page_in)
300daf87e95SQu Wenruo put_page(page_in);
301d4088803SQu Wenruo *out_pages = DIV_ROUND_UP(cur_out, PAGE_SIZE);
302a6fa6faeSLi Zefan return ret;
303a6fa6faeSLi Zefan }
304a6fa6faeSLi Zefan
305a6e66e6fSQu Wenruo /*
306a6e66e6fSQu Wenruo * Copy the compressed segment payload into @dest.
307a6e66e6fSQu Wenruo *
308a6e66e6fSQu Wenruo * For the payload there will be no padding, just need to do page switching.
309a6e66e6fSQu Wenruo */
copy_compressed_segment(struct compressed_bio * cb,char * dest,u32 len,u32 * cur_in)310a6e66e6fSQu Wenruo static void copy_compressed_segment(struct compressed_bio *cb,
311a6e66e6fSQu Wenruo char *dest, u32 len, u32 *cur_in)
312a6e66e6fSQu Wenruo {
313a6e66e6fSQu Wenruo u32 orig_in = *cur_in;
314a6e66e6fSQu Wenruo
315a6e66e6fSQu Wenruo while (*cur_in < orig_in + len) {
316a6e66e6fSQu Wenruo struct page *cur_page;
317a6e66e6fSQu Wenruo u32 copy_len = min_t(u32, PAGE_SIZE - offset_in_page(*cur_in),
318a6e66e6fSQu Wenruo orig_in + len - *cur_in);
319a6e66e6fSQu Wenruo
320a6e66e6fSQu Wenruo ASSERT(copy_len);
321a6e66e6fSQu Wenruo cur_page = cb->compressed_pages[*cur_in / PAGE_SIZE];
322a6e66e6fSQu Wenruo
32351c0674aSFabio M. De Francesco memcpy_from_page(dest + *cur_in - orig_in, cur_page,
32451c0674aSFabio M. De Francesco offset_in_page(*cur_in), copy_len);
325a6e66e6fSQu Wenruo
326a6e66e6fSQu Wenruo *cur_in += copy_len;
327a6e66e6fSQu Wenruo }
328a6e66e6fSQu Wenruo }
329a6e66e6fSQu Wenruo
lzo_decompress_bio(struct list_head * ws,struct compressed_bio * cb)330c4bf665aSDavid Sterba int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
331a6fa6faeSLi Zefan {
332a6fa6faeSLi Zefan struct workspace *workspace = list_entry(ws, struct workspace, list);
333544fe4a9SChristoph Hellwig const struct btrfs_fs_info *fs_info = cb->bbio.inode->root->fs_info;
334a6e66e6fSQu Wenruo const u32 sectorsize = fs_info->sectorsize;
335ccaa66c8SDavid Sterba char *kaddr;
336a6e66e6fSQu Wenruo int ret;
337a6e66e6fSQu Wenruo /* Compressed data length, can be unaligned */
338a6e66e6fSQu Wenruo u32 len_in;
339a6e66e6fSQu Wenruo /* Offset inside the compressed data */
340a6e66e6fSQu Wenruo u32 cur_in = 0;
341a6e66e6fSQu Wenruo /* Bytes decompressed so far */
342a6e66e6fSQu Wenruo u32 cur_out = 0;
343a6fa6faeSLi Zefan
34451c0674aSFabio M. De Francesco kaddr = kmap_local_page(cb->compressed_pages[0]);
345ccaa66c8SDavid Sterba len_in = read_compress_length(kaddr);
34651c0674aSFabio M. De Francesco kunmap_local(kaddr);
347a6e66e6fSQu Wenruo cur_in += LZO_LEN;
348a6fa6faeSLi Zefan
349314bfa47SQu Wenruo /*
350a6e66e6fSQu Wenruo * LZO header length check
351314bfa47SQu Wenruo *
352a6e66e6fSQu Wenruo * The total length should not exceed the maximum extent length,
353a6e66e6fSQu Wenruo * and all sectors should be used.
354a6e66e6fSQu Wenruo * If this happens, it means the compressed extent is corrupted.
355314bfa47SQu Wenruo */
356a6e66e6fSQu Wenruo if (len_in > min_t(size_t, BTRFS_MAX_COMPRESSED, cb->compressed_len) ||
357a6e66e6fSQu Wenruo round_up(len_in, sectorsize) < cb->compressed_len) {
358a6e66e6fSQu Wenruo btrfs_err(fs_info,
359a6e66e6fSQu Wenruo "invalid lzo header, lzo len %u compressed len %u",
360a6e66e6fSQu Wenruo len_in, cb->compressed_len);
361a6e66e6fSQu Wenruo return -EUCLEAN;
362314bfa47SQu Wenruo }
363314bfa47SQu Wenruo
364a6e66e6fSQu Wenruo /* Go through each lzo segment */
365a6e66e6fSQu Wenruo while (cur_in < len_in) {
366a6e66e6fSQu Wenruo struct page *cur_page;
367a6e66e6fSQu Wenruo /* Length of the compressed segment */
368a6e66e6fSQu Wenruo u32 seg_len;
369a6e66e6fSQu Wenruo u32 sector_bytes_left;
370a6e66e6fSQu Wenruo size_t out_len = lzo1x_worst_compress(sectorsize);
371a6fa6faeSLi Zefan
372a6e66e6fSQu Wenruo /*
373a6e66e6fSQu Wenruo * We should always have enough space for one segment header
374a6e66e6fSQu Wenruo * inside current sector.
375a6e66e6fSQu Wenruo */
376a6e66e6fSQu Wenruo ASSERT(cur_in / sectorsize ==
377a6e66e6fSQu Wenruo (cur_in + LZO_LEN - 1) / sectorsize);
378a6e66e6fSQu Wenruo cur_page = cb->compressed_pages[cur_in / PAGE_SIZE];
379a6e66e6fSQu Wenruo ASSERT(cur_page);
38051c0674aSFabio M. De Francesco kaddr = kmap_local_page(cur_page);
381ccaa66c8SDavid Sterba seg_len = read_compress_length(kaddr + offset_in_page(cur_in));
38251c0674aSFabio M. De Francesco kunmap_local(kaddr);
383a6e66e6fSQu Wenruo cur_in += LZO_LEN;
384a6fa6faeSLi Zefan
385dc4a4bdbSDāvis Mosāns if (seg_len > WORKSPACE_CBUF_LENGTH) {
386741b23a9SDāvis Mosāns /*
387741b23a9SDāvis Mosāns * seg_len shouldn't be larger than we have allocated
388741b23a9SDāvis Mosāns * for workspace->cbuf
389741b23a9SDāvis Mosāns */
390741b23a9SDāvis Mosāns btrfs_err(fs_info, "unexpectedly large lzo segment len %u",
391741b23a9SDāvis Mosāns seg_len);
392*7edb9a3eSChristoph Hellwig return -EIO;
393741b23a9SDāvis Mosāns }
394741b23a9SDāvis Mosāns
395a6e66e6fSQu Wenruo /* Copy the compressed segment payload into workspace */
396a6e66e6fSQu Wenruo copy_compressed_segment(cb, workspace->cbuf, seg_len, &cur_in);
397a6fa6faeSLi Zefan
398a6e66e6fSQu Wenruo /* Decompress the data */
399a6e66e6fSQu Wenruo ret = lzo1x_decompress_safe(workspace->cbuf, seg_len,
400a6e66e6fSQu Wenruo workspace->buf, &out_len);
401a6fa6faeSLi Zefan if (ret != LZO_E_OK) {
402a6e66e6fSQu Wenruo btrfs_err(fs_info, "failed to decompress");
403*7edb9a3eSChristoph Hellwig return -EIO;
404a6fa6faeSLi Zefan }
405a6fa6faeSLi Zefan
406a6e66e6fSQu Wenruo /* Copy the data into inode pages */
407a6e66e6fSQu Wenruo ret = btrfs_decompress_buf2page(workspace->buf, out_len, cb, cur_out);
408a6e66e6fSQu Wenruo cur_out += out_len;
409a6fa6faeSLi Zefan
410a6e66e6fSQu Wenruo /* All data read, exit */
411a6e66e6fSQu Wenruo if (ret == 0)
412*7edb9a3eSChristoph Hellwig return 0;
413a6e66e6fSQu Wenruo ret = 0;
414a6e66e6fSQu Wenruo
415a6e66e6fSQu Wenruo /* Check if the sector has enough space for a segment header */
416a6e66e6fSQu Wenruo sector_bytes_left = sectorsize - (cur_in % sectorsize);
417a6e66e6fSQu Wenruo if (sector_bytes_left >= LZO_LEN)
418a6e66e6fSQu Wenruo continue;
419a6e66e6fSQu Wenruo
420a6e66e6fSQu Wenruo /* Skip the padding zeros */
421a6e66e6fSQu Wenruo cur_in += sector_bytes_left;
422a6fa6faeSLi Zefan }
423*7edb9a3eSChristoph Hellwig
424*7edb9a3eSChristoph Hellwig return 0;
425a6fa6faeSLi Zefan }
426a6fa6faeSLi Zefan
lzo_decompress(struct list_head * ws,const u8 * data_in,struct page * dest_page,unsigned long start_byte,size_t srclen,size_t destlen)4273e09b5b2SDavid Sterba int lzo_decompress(struct list_head *ws, const u8 *data_in,
428c4bf665aSDavid Sterba struct page *dest_page, unsigned long start_byte, size_t srclen,
429c4bf665aSDavid Sterba size_t destlen)
430a6fa6faeSLi Zefan {
431a6fa6faeSLi Zefan struct workspace *workspace = list_entry(ws, struct workspace, list);
432a6fa6faeSLi Zefan size_t in_len;
433a6fa6faeSLi Zefan size_t out_len;
434dc4a4bdbSDāvis Mosāns size_t max_segment_len = WORKSPACE_BUF_LENGTH;
435a6fa6faeSLi Zefan int ret = 0;
436a6fa6faeSLi Zefan char *kaddr;
437a6fa6faeSLi Zefan unsigned long bytes;
438a6fa6faeSLi Zefan
439de885e3eSQu Wenruo if (srclen < LZO_LEN || srclen > max_segment_len + LZO_LEN * 2)
440de885e3eSQu Wenruo return -EUCLEAN;
441a6fa6faeSLi Zefan
442de885e3eSQu Wenruo in_len = read_compress_length(data_in);
443de885e3eSQu Wenruo if (in_len != srclen)
444de885e3eSQu Wenruo return -EUCLEAN;
445a6fa6faeSLi Zefan data_in += LZO_LEN;
446a6fa6faeSLi Zefan
447a6fa6faeSLi Zefan in_len = read_compress_length(data_in);
448de885e3eSQu Wenruo if (in_len != srclen - LZO_LEN * 2) {
449de885e3eSQu Wenruo ret = -EUCLEAN;
450de885e3eSQu Wenruo goto out;
451de885e3eSQu Wenruo }
452a6fa6faeSLi Zefan data_in += LZO_LEN;
453a6fa6faeSLi Zefan
45409cbfeafSKirill A. Shutemov out_len = PAGE_SIZE;
455a6fa6faeSLi Zefan ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
456a6fa6faeSLi Zefan if (ret != LZO_E_OK) {
45762e85577SJeff Mahoney pr_warn("BTRFS: decompress failed!\n");
45860e1975aSZach Brown ret = -EIO;
459a6fa6faeSLi Zefan goto out;
460a6fa6faeSLi Zefan }
461a6fa6faeSLi Zefan
462a6fa6faeSLi Zefan if (out_len < start_byte) {
46360e1975aSZach Brown ret = -EIO;
464a6fa6faeSLi Zefan goto out;
465a6fa6faeSLi Zefan }
466a6fa6faeSLi Zefan
4672f19cad9SChris Mason /*
4682f19cad9SChris Mason * the caller is already checking against PAGE_SIZE, but lets
4692f19cad9SChris Mason * move this check closer to the memcpy/memset
4702f19cad9SChris Mason */
4712f19cad9SChris Mason destlen = min_t(unsigned long, destlen, PAGE_SIZE);
472a6fa6faeSLi Zefan bytes = min_t(unsigned long, destlen, out_len - start_byte);
473a6fa6faeSLi Zefan
474ccaa66c8SDavid Sterba kaddr = kmap_local_page(dest_page);
475a6fa6faeSLi Zefan memcpy(kaddr, workspace->buf + start_byte, bytes);
4762f19cad9SChris Mason
4772f19cad9SChris Mason /*
4782f19cad9SChris Mason * btrfs_getblock is doing a zero on the tail of the page too,
4792f19cad9SChris Mason * but this will cover anything missing from the decompressed
4802f19cad9SChris Mason * data.
4812f19cad9SChris Mason */
4822f19cad9SChris Mason if (bytes < destlen)
4832f19cad9SChris Mason memset(kaddr+bytes, 0, destlen-bytes);
484ccaa66c8SDavid Sterba kunmap_local(kaddr);
485a6fa6faeSLi Zefan out:
486a6fa6faeSLi Zefan return ret;
487a6fa6faeSLi Zefan }
488a6fa6faeSLi Zefan
489e8c9f186SDavid Sterba const struct btrfs_compress_op btrfs_lzo_compress = {
490be951045SDavid Sterba .workspace_manager = &wsm,
491e18333a7SDavid Sterba .max_level = 1,
492e18333a7SDavid Sterba .default_level = 1,
493a6fa6faeSLi Zefan };
494