zlib.c 10.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
C
Chris Mason 已提交
2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (C) 2008 Oracle.  All rights reserved.
 *
 * Based on jffs2 zlib code:
 * Copyright © 2001-2007 Red Hat, Inc.
 * Created by David Woodhouse <dwmw2@infradead.org>
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/zlib.h>
#include <linux/zutil.h>
14
#include <linux/mm.h>
C
Chris Mason 已提交
15 16 17 18 19
#include <linux/init.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/pagemap.h>
#include <linux/bio.h>
20
#include <linux/refcount.h>
21
#include "compression.h"
C
Chris Mason 已提交
22 23

struct workspace {
24
	z_stream strm;
C
Chris Mason 已提交
25 26
	char *buf;
	struct list_head list;
27
	int level;
C
Chris Mason 已提交
28 29
};

30 31 32 33 34 35 36 37 38 39 40 41
static struct workspace_manager wsm;

static void zlib_init_workspace_manager(void)
{
	btrfs_init_workspace_manager(&wsm, &btrfs_zlib_compress);
}

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

42
static struct list_head *zlib_get_workspace(unsigned int level)
43
{
44 45 46 47 48 49
	struct list_head *ws = btrfs_get_workspace(&wsm, level);
	struct workspace *workspace = list_entry(ws, struct workspace, list);

	workspace->level = level;

	return ws;
50 51 52 53 54 55 56
}

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

57 58 59
static void zlib_free_workspace(struct list_head *ws)
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
60

61
	kvfree(workspace->strm.workspace);
62 63 64 65
	kfree(workspace->buf);
	kfree(workspace);
}

66
static struct list_head *zlib_alloc_workspace(unsigned int level)
C
Chris Mason 已提交
67 68
{
	struct workspace *workspace;
69
	int workspacesize;
L
Li Zefan 已提交
70

71
	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
72 73
	if (!workspace)
		return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
74

75 76
	workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
			zlib_inflate_workspacesize());
77
	workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL);
78
	workspace->level = level;
79
	workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
80
	if (!workspace->strm.workspace || !workspace->buf)
81
		goto fail;
C
Chris Mason 已提交
82

83
	INIT_LIST_HEAD(&workspace->list);
C
Chris Mason 已提交
84

85 86 87 88
	return &workspace->list;
fail:
	zlib_free_workspace(&workspace->list);
	return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
89 90
}

91 92
static int zlib_compress_pages(struct list_head *ws,
			       struct address_space *mapping,
93
			       u64 start,
94 95 96
			       struct page **pages,
			       unsigned long *out_pages,
			       unsigned long *total_in,
97
			       unsigned long *total_out)
C
Chris Mason 已提交
98
{
99
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
100 101 102 103 104 105 106
	int ret;
	char *data_in;
	char *cpage_out;
	int nr_pages = 0;
	struct page *in_page = NULL;
	struct page *out_page = NULL;
	unsigned long bytes_left;
107
	unsigned long len = *total_out;
108
	unsigned long nr_dest_pages = *out_pages;
109
	const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
C
Chris Mason 已提交
110 111 112 113 114

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

115
	if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
116
		pr_warn("BTRFS: deflateInit failed\n");
117
		ret = -EIO;
C
Chris Mason 已提交
118 119 120
		goto out;
	}

121 122
	workspace->strm.total_in = 0;
	workspace->strm.total_out = 0;
C
Chris Mason 已提交
123

124
	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
C
Chris Mason 已提交
125 126 127
	data_in = kmap(in_page);

	out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
L
Li Zefan 已提交
128
	if (out_page == NULL) {
129
		ret = -ENOMEM;
L
Li Zefan 已提交
130 131
		goto out;
	}
C
Chris Mason 已提交
132 133 134 135
	cpage_out = kmap(out_page);
	pages[0] = out_page;
	nr_pages = 1;

136 137
	workspace->strm.next_in = data_in;
	workspace->strm.next_out = cpage_out;
138 139
	workspace->strm.avail_out = PAGE_SIZE;
	workspace->strm.avail_in = min(len, PAGE_SIZE);
C
Chris Mason 已提交
140

141 142
	while (workspace->strm.total_in < len) {
		ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
C
Chris Mason 已提交
143
		if (ret != Z_OK) {
144
			pr_debug("BTRFS: deflate in loop returned %d\n",
C
Chris Mason 已提交
145
			       ret);
146
			zlib_deflateEnd(&workspace->strm);
147
			ret = -EIO;
C
Chris Mason 已提交
148 149 150 151
			goto out;
		}

		/* we're making it bigger, give up */
152 153 154
		if (workspace->strm.total_in > 8192 &&
		    workspace->strm.total_in <
		    workspace->strm.total_out) {
155
			ret = -E2BIG;
C
Chris Mason 已提交
156 157 158 159 160 161
			goto out;
		}
		/* we need another page for writing out.  Test this
		 * before the total_in so we will pull in a new page for
		 * the stream end if required
		 */
162
		if (workspace->strm.avail_out == 0) {
C
Chris Mason 已提交
163 164 165
			kunmap(out_page);
			if (nr_pages == nr_dest_pages) {
				out_page = NULL;
166
				ret = -E2BIG;
C
Chris Mason 已提交
167 168 169
				goto out;
			}
			out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
L
Li Zefan 已提交
170
			if (out_page == NULL) {
171
				ret = -ENOMEM;
L
Li Zefan 已提交
172 173
				goto out;
			}
C
Chris Mason 已提交
174 175 176
			cpage_out = kmap(out_page);
			pages[nr_pages] = out_page;
			nr_pages++;
177
			workspace->strm.avail_out = PAGE_SIZE;
178
			workspace->strm.next_out = cpage_out;
C
Chris Mason 已提交
179 180
		}
		/* we're all done */
181
		if (workspace->strm.total_in >= len)
C
Chris Mason 已提交
182 183 184
			break;

		/* we've read in a full page, get a new one */
185 186
		if (workspace->strm.avail_in == 0) {
			if (workspace->strm.total_out > max_out)
C
Chris Mason 已提交
187 188
				break;

189
			bytes_left = len - workspace->strm.total_in;
C
Chris Mason 已提交
190
			kunmap(in_page);
191
			put_page(in_page);
C
Chris Mason 已提交
192

193
			start += PAGE_SIZE;
C
Chris Mason 已提交
194
			in_page = find_get_page(mapping,
195
						start >> PAGE_SHIFT);
C
Chris Mason 已提交
196
			data_in = kmap(in_page);
197
			workspace->strm.avail_in = min(bytes_left,
198
							   PAGE_SIZE);
199
			workspace->strm.next_in = data_in;
C
Chris Mason 已提交
200 201
		}
	}
202 203 204
	workspace->strm.avail_in = 0;
	ret = zlib_deflate(&workspace->strm, Z_FINISH);
	zlib_deflateEnd(&workspace->strm);
C
Chris Mason 已提交
205 206

	if (ret != Z_STREAM_END) {
207
		ret = -EIO;
C
Chris Mason 已提交
208 209 210
		goto out;
	}

211
	if (workspace->strm.total_out >= workspace->strm.total_in) {
212
		ret = -E2BIG;
C
Chris Mason 已提交
213 214 215 216
		goto out;
	}

	ret = 0;
217 218
	*total_out = workspace->strm.total_out;
	*total_in = workspace->strm.total_in;
C
Chris Mason 已提交
219 220 221 222 223 224 225
out:
	*out_pages = nr_pages;
	if (out_page)
		kunmap(out_page);

	if (in_page) {
		kunmap(in_page);
226
		put_page(in_page);
C
Chris Mason 已提交
227 228 229 230
	}
	return ret;
}

231
static int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
C
Chris Mason 已提交
232
{
233
	struct workspace *workspace = list_entry(ws, struct workspace, list);
234
	int ret = 0, ret2;
C
Chris Mason 已提交
235 236 237 238
	int wbits = MAX_WBITS;
	char *data_in;
	size_t total_out = 0;
	unsigned long page_in_index = 0;
239
	size_t srclen = cb->compressed_len;
240
	unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
C
Chris Mason 已提交
241
	unsigned long buf_start;
242 243 244
	struct page **pages_in = cb->compressed_pages;
	u64 disk_start = cb->start;
	struct bio *orig_bio = cb->orig_bio;
C
Chris Mason 已提交
245 246

	data_in = kmap(pages_in[page_in_index]);
247
	workspace->strm.next_in = data_in;
248
	workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
249
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
250

251 252
	workspace->strm.total_out = 0;
	workspace->strm.next_out = workspace->buf;
253
	workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
254 255 256 257 258 259 260 261

	/* If it's deflate, and it's got no preset dictionary, then
	   we can tell zlib to skip the adler32 check. */
	if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
	    ((data_in[0] & 0x0f) == Z_DEFLATED) &&
	    !(((data_in[0]<<8) + data_in[1]) % 31)) {

		wbits = -((data_in[0] >> 4) + 8);
262 263
		workspace->strm.next_in += 2;
		workspace->strm.avail_in -= 2;
C
Chris Mason 已提交
264 265
	}

266
	if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
267
		pr_warn("BTRFS: inflateInit failed\n");
268
		kunmap(pages_in[page_in_index]);
269
		return -EIO;
C
Chris Mason 已提交
270
	}
271 272
	while (workspace->strm.total_in < srclen) {
		ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
C
Chris Mason 已提交
273
		if (ret != Z_OK && ret != Z_STREAM_END)
C
Chris Mason 已提交
274 275
			break;

276
		buf_start = total_out;
277
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
278

279 280
		/* we didn't make progress in this inflate call, we're done */
		if (buf_start == total_out)
C
Chris Mason 已提交
281 282
			break;

283 284
		ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
						 total_out, disk_start,
285
						 orig_bio);
286 287 288
		if (ret2 == 0) {
			ret = 0;
			goto done;
C
Chris Mason 已提交
289
		}
290

291
		workspace->strm.next_out = workspace->buf;
292
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
293

294
		if (workspace->strm.avail_in == 0) {
C
Chris Mason 已提交
295 296 297 298 299 300 301 302
			unsigned long tmp;
			kunmap(pages_in[page_in_index]);
			page_in_index++;
			if (page_in_index >= total_pages_in) {
				data_in = NULL;
				break;
			}
			data_in = kmap(pages_in[page_in_index]);
303 304 305
			workspace->strm.next_in = data_in;
			tmp = srclen - workspace->strm.total_in;
			workspace->strm.avail_in = min(tmp,
306
							   PAGE_SIZE);
C
Chris Mason 已提交
307 308
		}
	}
C
Chris Mason 已提交
309
	if (ret != Z_STREAM_END)
310
		ret = -EIO;
C
Chris Mason 已提交
311
	else
C
Chris Mason 已提交
312 313
		ret = 0;
done:
314
	zlib_inflateEnd(&workspace->strm);
C
Chris Mason 已提交
315 316
	if (data_in)
		kunmap(pages_in[page_in_index]);
317
	if (!ret)
318
		zero_fill_bio(orig_bio);
C
Chris Mason 已提交
319 320 321
	return ret;
}

322 323 324 325
static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
			   struct page *dest_page,
			   unsigned long start_byte,
			   size_t srclen, size_t destlen)
C
Chris Mason 已提交
326
{
327
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
328 329
	int ret = 0;
	int wbits = MAX_WBITS;
330
	unsigned long bytes_left;
C
Chris Mason 已提交
331
	unsigned long total_out = 0;
332
	unsigned long pg_offset = 0;
C
Chris Mason 已提交
333 334
	char *kaddr;

335 336 337
	destlen = min_t(unsigned long, destlen, PAGE_SIZE);
	bytes_left = destlen;

338 339 340
	workspace->strm.next_in = data_in;
	workspace->strm.avail_in = srclen;
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
341

342
	workspace->strm.next_out = workspace->buf;
343
	workspace->strm.avail_out = PAGE_SIZE;
344
	workspace->strm.total_out = 0;
C
Chris Mason 已提交
345 346 347 348 349 350 351
	/* If it's deflate, and it's got no preset dictionary, then
	   we can tell zlib to skip the adler32 check. */
	if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
	    ((data_in[0] & 0x0f) == Z_DEFLATED) &&
	    !(((data_in[0]<<8) + data_in[1]) % 31)) {

		wbits = -((data_in[0] >> 4) + 8);
352 353
		workspace->strm.next_in += 2;
		workspace->strm.avail_in -= 2;
C
Chris Mason 已提交
354 355
	}

356
	if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
357
		pr_warn("BTRFS: inflateInit failed\n");
358
		return -EIO;
C
Chris Mason 已提交
359 360
	}

C
Chris Mason 已提交
361
	while (bytes_left > 0) {
C
Chris Mason 已提交
362 363 364 365
		unsigned long buf_start;
		unsigned long buf_offset;
		unsigned long bytes;

366
		ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
C
Chris Mason 已提交
367
		if (ret != Z_OK && ret != Z_STREAM_END)
C
Chris Mason 已提交
368 369 370
			break;

		buf_start = total_out;
371
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
372 373

		if (total_out == buf_start) {
374
			ret = -EIO;
C
Chris Mason 已提交
375 376 377
			break;
		}

C
Chris Mason 已提交
378
		if (total_out <= start_byte)
C
Chris Mason 已提交
379 380
			goto next;

C
Chris Mason 已提交
381
		if (total_out > start_byte && buf_start < start_byte)
C
Chris Mason 已提交
382
			buf_offset = start_byte - buf_start;
C
Chris Mason 已提交
383
		else
C
Chris Mason 已提交
384 385
			buf_offset = 0;

386 387
		bytes = min(PAGE_SIZE - pg_offset,
			    PAGE_SIZE - buf_offset);
C
Chris Mason 已提交
388 389
		bytes = min(bytes, bytes_left);

390
		kaddr = kmap_atomic(dest_page);
C
Chris Mason 已提交
391
		memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
392
		kunmap_atomic(kaddr);
C
Chris Mason 已提交
393 394 395 396

		pg_offset += bytes;
		bytes_left -= bytes;
next:
397
		workspace->strm.next_out = workspace->buf;
398
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
399
	}
C
Chris Mason 已提交
400 401

	if (ret != Z_STREAM_END && bytes_left != 0)
402
		ret = -EIO;
C
Chris Mason 已提交
403
	else
C
Chris Mason 已提交
404
		ret = 0;
C
Chris Mason 已提交
405

406
	zlib_inflateEnd(&workspace->strm);
407 408 409 410 411 412 413 414 415 416 417

	/*
	 * this should only happen if zlib returned fewer bytes than we
	 * expected.  btrfs_get_block is responsible for zeroing from the
	 * end of the inline extent (destlen) to the end of the page
	 */
	if (pg_offset < destlen) {
		kaddr = kmap_atomic(dest_page);
		memset(kaddr + pg_offset, 0, destlen - pg_offset);
		kunmap_atomic(kaddr);
	}
C
Chris Mason 已提交
418 419 420
	return ret;
}

421
const struct btrfs_compress_op btrfs_zlib_compress = {
422 423 424 425
	.init_workspace_manager	= zlib_init_workspace_manager,
	.cleanup_workspace_manager = zlib_cleanup_workspace_manager,
	.get_workspace		= zlib_get_workspace,
	.put_workspace		= zlib_put_workspace,
426 427 428
	.alloc_workspace	= zlib_alloc_workspace,
	.free_workspace		= zlib_free_workspace,
	.compress_pages		= zlib_compress_pages,
429
	.decompress_bio		= zlib_decompress_bio,
430
	.decompress		= zlib_decompress,
431 432
	.max_level		= 9,
	.default_level		= BTRFS_ZLIB_DEFAULT_LEVEL,
433
};