lzo.c 12.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Li Zefan 已提交
2 3 4 5 6 7
/*
 * Copyright (C) 2008 Oracle.  All rights reserved.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
8
#include <linux/mm.h>
L
Li Zefan 已提交
9 10 11 12 13 14
#include <linux/init.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/pagemap.h>
#include <linux/bio.h>
#include <linux/lzo.h>
15
#include <linux/refcount.h>
L
Li Zefan 已提交
16 17 18 19
#include "compression.h"

#define LZO_LEN	4

20 21 22 23 24 25 26 27 28 29
/*
 * 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)
30
 *     Variable size. Each segment includes one segment header, followed by data
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 *     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 page 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 page boundary, thus it's possible to
 *     have at most 3 padding zeros at the end of the page.
 *
 * 2.2 Data Payload
 *     Variable size. Size up limit should be lzo1x_worst_compress(PAGE_SIZE)
 *     which is 4419 for a 4KiB page.
 *
 * Example:
 * 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 ...                |
 */

L
Li Zefan 已提交
57 58
struct workspace {
	void *mem;
59 60
	void *buf;	/* where decompressed data goes */
	void *cbuf;	/* where compressed data goes */
L
Li Zefan 已提交
61 62 63
	struct list_head list;
};

64 65 66 67 68 69 70 71 72 73 74 75
static struct workspace_manager wsm;

static void lzo_init_workspace_manager(void)
{
	btrfs_init_workspace_manager(&wsm, &btrfs_lzo_compress);
}

static void lzo_cleanup_workspace_manager(void)
{
	btrfs_cleanup_workspace_manager(&wsm);
}

76
static struct list_head *lzo_get_workspace(unsigned int level)
77
{
78
	return btrfs_get_workspace(&wsm, level);
79 80 81 82 83 84 85
}

static void lzo_put_workspace(struct list_head *ws)
{
	btrfs_put_workspace(&wsm, ws);
}

L
Li Zefan 已提交
86 87 88 89
static void lzo_free_workspace(struct list_head *ws)
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);

90 91 92
	kvfree(workspace->buf);
	kvfree(workspace->cbuf);
	kvfree(workspace->mem);
L
Li Zefan 已提交
93 94 95
	kfree(workspace);
}

96
static struct list_head *lzo_alloc_workspace(unsigned int level)
L
Li Zefan 已提交
97 98 99
{
	struct workspace *workspace;

100
	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
L
Li Zefan 已提交
101 102 103
	if (!workspace)
		return ERR_PTR(-ENOMEM);

104 105 106
	workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
	workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
	workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
L
Li Zefan 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	if (!workspace->mem || !workspace->buf || !workspace->cbuf)
		goto fail;

	INIT_LIST_HEAD(&workspace->list);

	return &workspace->list;
fail:
	lzo_free_workspace(&workspace->list);
	return ERR_PTR(-ENOMEM);
}

static inline void write_compress_length(char *buf, size_t len)
{
	__le32 dlen;

	dlen = cpu_to_le32(len);
	memcpy(buf, &dlen, LZO_LEN);
}

126
static inline size_t read_compress_length(const char *buf)
L
Li Zefan 已提交
127 128 129 130 131 132 133 134 135
{
	__le32 dlen;

	memcpy(&dlen, buf, LZO_LEN);
	return le32_to_cpu(dlen);
}

static int lzo_compress_pages(struct list_head *ws,
			      struct address_space *mapping,
136
			      u64 start,
L
Li Zefan 已提交
137 138 139
			      struct page **pages,
			      unsigned long *out_pages,
			      unsigned long *total_in,
140
			      unsigned long *total_out)
L
Li Zefan 已提交
141 142 143 144 145 146 147 148 149
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
	int ret = 0;
	char *data_in;
	char *cpage_out;
	int nr_pages = 0;
	struct page *in_page = NULL;
	struct page *out_page = NULL;
	unsigned long bytes_left;
150
	unsigned long len = *total_out;
151
	unsigned long nr_dest_pages = *out_pages;
152
	const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
L
Li Zefan 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
	size_t in_len;
	size_t out_len;
	char *buf;
	unsigned long tot_in = 0;
	unsigned long tot_out = 0;
	unsigned long pg_bytes_left;
	unsigned long out_offset;
	unsigned long bytes;

	*out_pages = 0;
	*total_out = 0;
	*total_in = 0;

166
	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
L
Li Zefan 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	data_in = kmap(in_page);

	/*
	 * store the size of all chunks of compressed data in
	 * the first 4 bytes
	 */
	out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
	if (out_page == NULL) {
		ret = -ENOMEM;
		goto out;
	}
	cpage_out = kmap(out_page);
	out_offset = LZO_LEN;
	tot_out = LZO_LEN;
	pages[0] = out_page;
	nr_pages = 1;
183
	pg_bytes_left = PAGE_SIZE - LZO_LEN;
L
Li Zefan 已提交
184 185

	/* compress at most one page of data each time */
186
	in_len = min(len, PAGE_SIZE);
L
Li Zefan 已提交
187 188 189 190
	while (tot_in < len) {
		ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
				       &out_len, workspace->mem);
		if (ret != LZO_E_OK) {
191
			pr_debug("BTRFS: lzo in loop returned %d\n",
L
Li Zefan 已提交
192
			       ret);
193
			ret = -EIO;
L
Li Zefan 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			goto out;
		}

		/* store the size of this chunk of compressed data */
		write_compress_length(cpage_out + out_offset, out_len);
		tot_out += LZO_LEN;
		out_offset += LZO_LEN;
		pg_bytes_left -= LZO_LEN;

		tot_in += in_len;
		tot_out += out_len;

		/* copy bytes from the working buffer into the pages */
		buf = workspace->cbuf;
		while (out_len) {
			bytes = min_t(unsigned long, pg_bytes_left, out_len);

			memcpy(cpage_out + out_offset, buf, bytes);

			out_len -= bytes;
			pg_bytes_left -= bytes;
			buf += bytes;
			out_offset += bytes;

			/*
			 * we need another page for writing out.
			 *
			 * Note if there's less than 4 bytes left, we just
			 * skip to a new page.
			 */
			if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
			    pg_bytes_left == 0) {
				if (pg_bytes_left) {
					memset(cpage_out + out_offset, 0,
					       pg_bytes_left);
					tot_out += pg_bytes_left;
				}

				/* we're done, don't allocate new page */
				if (out_len == 0 && tot_in >= len)
					break;

				kunmap(out_page);
				if (nr_pages == nr_dest_pages) {
					out_page = NULL;
239
					ret = -E2BIG;
L
Li Zefan 已提交
240 241 242 243 244 245 246 247 248 249 250
					goto out;
				}

				out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
				if (out_page == NULL) {
					ret = -ENOMEM;
					goto out;
				}
				cpage_out = kmap(out_page);
				pages[nr_pages++] = out_page;

251
				pg_bytes_left = PAGE_SIZE;
L
Li Zefan 已提交
252 253 254 255 256
				out_offset = 0;
			}
		}

		/* we're making it bigger, give up */
257
		if (tot_in > 8192 && tot_in < tot_out) {
258
			ret = -E2BIG;
L
Li Zefan 已提交
259
			goto out;
260
		}
L
Li Zefan 已提交
261 262 263 264 265 266 267 268 269 270

		/* we're all done */
		if (tot_in >= len)
			break;

		if (tot_out > max_out)
			break;

		bytes_left = len - tot_in;
		kunmap(in_page);
271
		put_page(in_page);
L
Li Zefan 已提交
272

273 274
		start += PAGE_SIZE;
		in_page = find_get_page(mapping, start >> PAGE_SHIFT);
L
Li Zefan 已提交
275
		data_in = kmap(in_page);
276
		in_len = min(bytes_left, PAGE_SIZE);
L
Li Zefan 已提交
277 278
	}

279 280
	if (tot_out >= tot_in) {
		ret = -E2BIG;
L
Li Zefan 已提交
281
		goto out;
282
	}
L
Li Zefan 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

	/* store the size of all chunks of compressed data */
	cpage_out = kmap(pages[0]);
	write_compress_length(cpage_out, tot_out);

	kunmap(pages[0]);

	ret = 0;
	*total_out = tot_out;
	*total_in = tot_in;
out:
	*out_pages = nr_pages;
	if (out_page)
		kunmap(out_page);

	if (in_page) {
		kunmap(in_page);
300
		put_page(in_page);
L
Li Zefan 已提交
301 302 303 304 305
	}

	return ret;
}

306
static int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
L
Li Zefan 已提交
307 308
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
309
	int ret = 0, ret2;
L
Li Zefan 已提交
310 311
	char *data_in;
	unsigned long page_in_index = 0;
312
	size_t srclen = cb->compressed_len;
313
	unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
L
Li Zefan 已提交
314 315 316 317 318 319
	unsigned long buf_start;
	unsigned long buf_offset = 0;
	unsigned long bytes;
	unsigned long working_bytes;
	size_t in_len;
	size_t out_len;
320
	const size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
L
Li Zefan 已提交
321 322 323 324 325 326
	unsigned long in_offset;
	unsigned long in_page_bytes_left;
	unsigned long tot_in;
	unsigned long tot_out;
	unsigned long tot_len;
	char *buf;
327
	bool may_late_unmap, need_unmap;
328 329 330
	struct page **pages_in = cb->compressed_pages;
	u64 disk_start = cb->start;
	struct bio *orig_bio = cb->orig_bio;
L
Li Zefan 已提交
331 332 333

	data_in = kmap(pages_in[0]);
	tot_len = read_compress_length(data_in);
334 335 336 337 338 339 340 341 342 343 344 345 346
	/*
	 * Compressed data header check.
	 *
	 * The real compressed size can't exceed the maximum extent length, and
	 * all pages should be used (whole unused page with just the segment
	 * header is not possible).  If this happens it means the compressed
	 * extent is corrupted.
	 */
	if (tot_len > min_t(size_t, BTRFS_MAX_COMPRESSED, srclen) ||
	    tot_len < srclen - PAGE_SIZE) {
		ret = -EUCLEAN;
		goto done;
	}
L
Li Zefan 已提交
347 348 349

	tot_in = LZO_LEN;
	in_offset = LZO_LEN;
350
	in_page_bytes_left = PAGE_SIZE - LZO_LEN;
L
Li Zefan 已提交
351 352 353 354 355 356 357 358 359

	tot_out = 0;

	while (tot_in < tot_len) {
		in_len = read_compress_length(data_in + in_offset);
		in_page_bytes_left -= LZO_LEN;
		in_offset += LZO_LEN;
		tot_in += LZO_LEN;

360 361 362 363 364 365 366 367 368 369 370
		/*
		 * Segment header check.
		 *
		 * 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) {
			ret = -EUCLEAN;
			goto done;
		}

L
Li Zefan 已提交
371 372
		tot_in += in_len;
		working_bytes = in_len;
373
		may_late_unmap = need_unmap = false;
L
Li Zefan 已提交
374 375 376 377 378

		/* fast path: avoid using the working buffer */
		if (in_page_bytes_left >= in_len) {
			buf = data_in + in_offset;
			bytes = in_len;
379
			may_late_unmap = true;
L
Li Zefan 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
			goto cont;
		}

		/* 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;

404
				if (page_in_index + 1 >= total_pages_in) {
405
					ret = -EIO;
L
Li Zefan 已提交
406 407
					goto done;
				}
408 409 410 411 412 413 414

				if (may_late_unmap)
					need_unmap = true;
				else
					kunmap(pages_in[page_in_index]);

				data_in = kmap(pages_in[++page_in_index]);
L
Li Zefan 已提交
415

416
				in_page_bytes_left = PAGE_SIZE;
L
Li Zefan 已提交
417 418 419 420
				in_offset = 0;
			}
		}

421
		out_len = max_segment_len;
L
Li Zefan 已提交
422 423
		ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
					    &out_len);
424 425
		if (need_unmap)
			kunmap(pages_in[page_in_index - 1]);
L
Li Zefan 已提交
426
		if (ret != LZO_E_OK) {
427
			pr_warn("BTRFS: decompress failed\n");
428
			ret = -EIO;
L
Li Zefan 已提交
429 430 431 432 433 434
			break;
		}

		buf_start = tot_out;
		tot_out += out_len;

435
		ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
436
						 tot_out, disk_start, orig_bio);
437
		if (ret2 == 0)
L
Li Zefan 已提交
438 439 440
			break;
	}
done:
441
	kunmap(pages_in[page_in_index]);
442
	if (!ret)
443
		zero_fill_bio(orig_bio);
L
Li Zefan 已提交
444 445 446 447 448 449 450 451 452 453 454
	return ret;
}

static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
			  struct page *dest_page,
			  unsigned long start_byte,
			  size_t srclen, size_t destlen)
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
	size_t in_len;
	size_t out_len;
455
	size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
L
Li Zefan 已提交
456 457 458 459
	int ret = 0;
	char *kaddr;
	unsigned long bytes;

460 461
	if (srclen < LZO_LEN || srclen > max_segment_len + LZO_LEN * 2)
		return -EUCLEAN;
L
Li Zefan 已提交
462

463 464 465
	in_len = read_compress_length(data_in);
	if (in_len != srclen)
		return -EUCLEAN;
L
Li Zefan 已提交
466 467 468
	data_in += LZO_LEN;

	in_len = read_compress_length(data_in);
469 470 471 472
	if (in_len != srclen - LZO_LEN * 2) {
		ret = -EUCLEAN;
		goto out;
	}
L
Li Zefan 已提交
473 474
	data_in += LZO_LEN;

475
	out_len = PAGE_SIZE;
L
Li Zefan 已提交
476 477
	ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
	if (ret != LZO_E_OK) {
478
		pr_warn("BTRFS: decompress failed!\n");
479
		ret = -EIO;
L
Li Zefan 已提交
480 481 482 483
		goto out;
	}

	if (out_len < start_byte) {
484
		ret = -EIO;
L
Li Zefan 已提交
485 486 487
		goto out;
	}

488 489 490 491 492
	/*
	 * the caller is already checking against PAGE_SIZE, but lets
	 * move this check closer to the memcpy/memset
	 */
	destlen = min_t(unsigned long, destlen, PAGE_SIZE);
L
Li Zefan 已提交
493 494
	bytes = min_t(unsigned long, destlen, out_len - start_byte);

495
	kaddr = kmap_atomic(dest_page);
L
Li Zefan 已提交
496
	memcpy(kaddr, workspace->buf + start_byte, bytes);
497 498 499 500 501 502 503 504

	/*
	 * btrfs_getblock is doing a zero on the tail of the page too,
	 * but this will cover anything missing from the decompressed
	 * data.
	 */
	if (bytes < destlen)
		memset(kaddr+bytes, 0, destlen-bytes);
505
	kunmap_atomic(kaddr);
L
Li Zefan 已提交
506 507 508 509
out:
	return ret;
}

510
const struct btrfs_compress_op btrfs_lzo_compress = {
511 512 513 514
	.init_workspace_manager	= lzo_init_workspace_manager,
	.cleanup_workspace_manager = lzo_cleanup_workspace_manager,
	.get_workspace		= lzo_get_workspace,
	.put_workspace		= lzo_put_workspace,
L
Li Zefan 已提交
515 516 517
	.alloc_workspace	= lzo_alloc_workspace,
	.free_workspace		= lzo_free_workspace,
	.compress_pages		= lzo_compress_pages,
518
	.decompress_bio		= lzo_decompress_bio,
L
Li Zefan 已提交
519
	.decompress		= lzo_decompress,
520 521
	.max_level		= 1,
	.default_level		= 1,
L
Li Zefan 已提交
522
};