xref: /linux/fs/btrfs/lzo.c (revision 37cb8e1f8e10c6e9bd2a1b95cdda0620a21b0551)
1 /*
2  * Copyright (C) 2008 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/sched.h>
25 #include <linux/pagemap.h>
26 #include <linux/bio.h>
27 #include <linux/lzo.h>
28 #include <linux/refcount.h>
29 #include "compression.h"
30 
31 #define LZO_LEN	4
32 
33 struct workspace {
34 	void *mem;
35 	void *buf;	/* where decompressed data goes */
36 	void *cbuf;	/* where compressed data goes */
37 	struct list_head list;
38 };
39 
40 static void lzo_free_workspace(struct list_head *ws)
41 {
42 	struct workspace *workspace = list_entry(ws, struct workspace, list);
43 
44 	kvfree(workspace->buf);
45 	kvfree(workspace->cbuf);
46 	kvfree(workspace->mem);
47 	kfree(workspace);
48 }
49 
50 static struct list_head *lzo_alloc_workspace(void)
51 {
52 	struct workspace *workspace;
53 
54 	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
55 	if (!workspace)
56 		return ERR_PTR(-ENOMEM);
57 
58 	workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
59 	workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
60 	workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
61 	if (!workspace->mem || !workspace->buf || !workspace->cbuf)
62 		goto fail;
63 
64 	INIT_LIST_HEAD(&workspace->list);
65 
66 	return &workspace->list;
67 fail:
68 	lzo_free_workspace(&workspace->list);
69 	return ERR_PTR(-ENOMEM);
70 }
71 
72 static inline void write_compress_length(char *buf, size_t len)
73 {
74 	__le32 dlen;
75 
76 	dlen = cpu_to_le32(len);
77 	memcpy(buf, &dlen, LZO_LEN);
78 }
79 
80 static inline size_t read_compress_length(const char *buf)
81 {
82 	__le32 dlen;
83 
84 	memcpy(&dlen, buf, LZO_LEN);
85 	return le32_to_cpu(dlen);
86 }
87 
88 static int lzo_compress_pages(struct list_head *ws,
89 			      struct address_space *mapping,
90 			      u64 start,
91 			      struct page **pages,
92 			      unsigned long *out_pages,
93 			      unsigned long *total_in,
94 			      unsigned long *total_out)
95 {
96 	struct workspace *workspace = list_entry(ws, struct workspace, list);
97 	int ret = 0;
98 	char *data_in;
99 	char *cpage_out;
100 	int nr_pages = 0;
101 	struct page *in_page = NULL;
102 	struct page *out_page = NULL;
103 	unsigned long bytes_left;
104 	unsigned long len = *total_out;
105 	unsigned long nr_dest_pages = *out_pages;
106 	const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
107 	size_t in_len;
108 	size_t out_len;
109 	char *buf;
110 	unsigned long tot_in = 0;
111 	unsigned long tot_out = 0;
112 	unsigned long pg_bytes_left;
113 	unsigned long out_offset;
114 	unsigned long bytes;
115 
116 	*out_pages = 0;
117 	*total_out = 0;
118 	*total_in = 0;
119 
120 	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
121 	data_in = kmap(in_page);
122 
123 	/*
124 	 * store the size of all chunks of compressed data in
125 	 * the first 4 bytes
126 	 */
127 	out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
128 	if (out_page == NULL) {
129 		ret = -ENOMEM;
130 		goto out;
131 	}
132 	cpage_out = kmap(out_page);
133 	out_offset = LZO_LEN;
134 	tot_out = LZO_LEN;
135 	pages[0] = out_page;
136 	nr_pages = 1;
137 	pg_bytes_left = PAGE_SIZE - LZO_LEN;
138 
139 	/* compress at most one page of data each time */
140 	in_len = min(len, PAGE_SIZE);
141 	while (tot_in < len) {
142 		ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
143 				       &out_len, workspace->mem);
144 		if (ret != LZO_E_OK) {
145 			pr_debug("BTRFS: lzo in loop returned %d\n",
146 			       ret);
147 			ret = -EIO;
148 			goto out;
149 		}
150 
151 		/* store the size of this chunk of compressed data */
152 		write_compress_length(cpage_out + out_offset, out_len);
153 		tot_out += LZO_LEN;
154 		out_offset += LZO_LEN;
155 		pg_bytes_left -= LZO_LEN;
156 
157 		tot_in += in_len;
158 		tot_out += out_len;
159 
160 		/* copy bytes from the working buffer into the pages */
161 		buf = workspace->cbuf;
162 		while (out_len) {
163 			bytes = min_t(unsigned long, pg_bytes_left, out_len);
164 
165 			memcpy(cpage_out + out_offset, buf, bytes);
166 
167 			out_len -= bytes;
168 			pg_bytes_left -= bytes;
169 			buf += bytes;
170 			out_offset += bytes;
171 
172 			/*
173 			 * we need another page for writing out.
174 			 *
175 			 * Note if there's less than 4 bytes left, we just
176 			 * skip to a new page.
177 			 */
178 			if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
179 			    pg_bytes_left == 0) {
180 				if (pg_bytes_left) {
181 					memset(cpage_out + out_offset, 0,
182 					       pg_bytes_left);
183 					tot_out += pg_bytes_left;
184 				}
185 
186 				/* we're done, don't allocate new page */
187 				if (out_len == 0 && tot_in >= len)
188 					break;
189 
190 				kunmap(out_page);
191 				if (nr_pages == nr_dest_pages) {
192 					out_page = NULL;
193 					ret = -E2BIG;
194 					goto out;
195 				}
196 
197 				out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
198 				if (out_page == NULL) {
199 					ret = -ENOMEM;
200 					goto out;
201 				}
202 				cpage_out = kmap(out_page);
203 				pages[nr_pages++] = out_page;
204 
205 				pg_bytes_left = PAGE_SIZE;
206 				out_offset = 0;
207 			}
208 		}
209 
210 		/* we're making it bigger, give up */
211 		if (tot_in > 8192 && tot_in < tot_out) {
212 			ret = -E2BIG;
213 			goto out;
214 		}
215 
216 		/* we're all done */
217 		if (tot_in >= len)
218 			break;
219 
220 		if (tot_out > max_out)
221 			break;
222 
223 		bytes_left = len - tot_in;
224 		kunmap(in_page);
225 		put_page(in_page);
226 
227 		start += PAGE_SIZE;
228 		in_page = find_get_page(mapping, start >> PAGE_SHIFT);
229 		data_in = kmap(in_page);
230 		in_len = min(bytes_left, PAGE_SIZE);
231 	}
232 
233 	if (tot_out >= tot_in) {
234 		ret = -E2BIG;
235 		goto out;
236 	}
237 
238 	/* store the size of all chunks of compressed data */
239 	cpage_out = kmap(pages[0]);
240 	write_compress_length(cpage_out, tot_out);
241 
242 	kunmap(pages[0]);
243 
244 	ret = 0;
245 	*total_out = tot_out;
246 	*total_in = tot_in;
247 out:
248 	*out_pages = nr_pages;
249 	if (out_page)
250 		kunmap(out_page);
251 
252 	if (in_page) {
253 		kunmap(in_page);
254 		put_page(in_page);
255 	}
256 
257 	return ret;
258 }
259 
260 static int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
261 {
262 	struct workspace *workspace = list_entry(ws, struct workspace, list);
263 	int ret = 0, ret2;
264 	char *data_in;
265 	unsigned long page_in_index = 0;
266 	size_t srclen = cb->compressed_len;
267 	unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
268 	unsigned long buf_start;
269 	unsigned long buf_offset = 0;
270 	unsigned long bytes;
271 	unsigned long working_bytes;
272 	size_t in_len;
273 	size_t out_len;
274 	unsigned long in_offset;
275 	unsigned long in_page_bytes_left;
276 	unsigned long tot_in;
277 	unsigned long tot_out;
278 	unsigned long tot_len;
279 	char *buf;
280 	bool may_late_unmap, need_unmap;
281 	struct page **pages_in = cb->compressed_pages;
282 	u64 disk_start = cb->start;
283 	struct bio *orig_bio = cb->orig_bio;
284 
285 	data_in = kmap(pages_in[0]);
286 	tot_len = read_compress_length(data_in);
287 
288 	tot_in = LZO_LEN;
289 	in_offset = LZO_LEN;
290 	tot_len = min_t(size_t, srclen, tot_len);
291 	in_page_bytes_left = PAGE_SIZE - LZO_LEN;
292 
293 	tot_out = 0;
294 
295 	while (tot_in < tot_len) {
296 		in_len = read_compress_length(data_in + in_offset);
297 		in_page_bytes_left -= LZO_LEN;
298 		in_offset += LZO_LEN;
299 		tot_in += LZO_LEN;
300 
301 		tot_in += in_len;
302 		working_bytes = in_len;
303 		may_late_unmap = need_unmap = false;
304 
305 		/* fast path: avoid using the working buffer */
306 		if (in_page_bytes_left >= in_len) {
307 			buf = data_in + in_offset;
308 			bytes = in_len;
309 			may_late_unmap = true;
310 			goto cont;
311 		}
312 
313 		/* copy bytes from the pages into the working buffer */
314 		buf = workspace->cbuf;
315 		buf_offset = 0;
316 		while (working_bytes) {
317 			bytes = min(working_bytes, in_page_bytes_left);
318 
319 			memcpy(buf + buf_offset, data_in + in_offset, bytes);
320 			buf_offset += bytes;
321 cont:
322 			working_bytes -= bytes;
323 			in_page_bytes_left -= bytes;
324 			in_offset += bytes;
325 
326 			/* check if we need to pick another page */
327 			if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
328 			    || in_page_bytes_left == 0) {
329 				tot_in += in_page_bytes_left;
330 
331 				if (working_bytes == 0 && tot_in >= tot_len)
332 					break;
333 
334 				if (page_in_index + 1 >= total_pages_in) {
335 					ret = -EIO;
336 					goto done;
337 				}
338 
339 				if (may_late_unmap)
340 					need_unmap = true;
341 				else
342 					kunmap(pages_in[page_in_index]);
343 
344 				data_in = kmap(pages_in[++page_in_index]);
345 
346 				in_page_bytes_left = PAGE_SIZE;
347 				in_offset = 0;
348 			}
349 		}
350 
351 		out_len = lzo1x_worst_compress(PAGE_SIZE);
352 		ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
353 					    &out_len);
354 		if (need_unmap)
355 			kunmap(pages_in[page_in_index - 1]);
356 		if (ret != LZO_E_OK) {
357 			pr_warn("BTRFS: decompress failed\n");
358 			ret = -EIO;
359 			break;
360 		}
361 
362 		buf_start = tot_out;
363 		tot_out += out_len;
364 
365 		ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
366 						 tot_out, disk_start, orig_bio);
367 		if (ret2 == 0)
368 			break;
369 	}
370 done:
371 	kunmap(pages_in[page_in_index]);
372 	if (!ret)
373 		zero_fill_bio(orig_bio);
374 	return ret;
375 }
376 
377 static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
378 			  struct page *dest_page,
379 			  unsigned long start_byte,
380 			  size_t srclen, size_t destlen)
381 {
382 	struct workspace *workspace = list_entry(ws, struct workspace, list);
383 	size_t in_len;
384 	size_t out_len;
385 	size_t tot_len;
386 	int ret = 0;
387 	char *kaddr;
388 	unsigned long bytes;
389 
390 	BUG_ON(srclen < LZO_LEN);
391 
392 	tot_len = read_compress_length(data_in);
393 	data_in += LZO_LEN;
394 
395 	in_len = read_compress_length(data_in);
396 	data_in += LZO_LEN;
397 
398 	out_len = PAGE_SIZE;
399 	ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
400 	if (ret != LZO_E_OK) {
401 		pr_warn("BTRFS: decompress failed!\n");
402 		ret = -EIO;
403 		goto out;
404 	}
405 
406 	if (out_len < start_byte) {
407 		ret = -EIO;
408 		goto out;
409 	}
410 
411 	/*
412 	 * the caller is already checking against PAGE_SIZE, but lets
413 	 * move this check closer to the memcpy/memset
414 	 */
415 	destlen = min_t(unsigned long, destlen, PAGE_SIZE);
416 	bytes = min_t(unsigned long, destlen, out_len - start_byte);
417 
418 	kaddr = kmap_atomic(dest_page);
419 	memcpy(kaddr, workspace->buf + start_byte, bytes);
420 
421 	/*
422 	 * btrfs_getblock is doing a zero on the tail of the page too,
423 	 * but this will cover anything missing from the decompressed
424 	 * data.
425 	 */
426 	if (bytes < destlen)
427 		memset(kaddr+bytes, 0, destlen-bytes);
428 	kunmap_atomic(kaddr);
429 out:
430 	return ret;
431 }
432 
433 static void lzo_set_level(struct list_head *ws, unsigned int type)
434 {
435 }
436 
437 const struct btrfs_compress_op btrfs_lzo_compress = {
438 	.alloc_workspace	= lzo_alloc_workspace,
439 	.free_workspace		= lzo_free_workspace,
440 	.compress_pages		= lzo_compress_pages,
441 	.decompress_bio		= lzo_decompress_bio,
442 	.decompress		= lzo_decompress,
443 	.set_level		= lzo_set_level,
444 };
445