zlib.c 10.2 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
static struct workspace_manager wsm;

32
static struct list_head *zlib_get_workspace(unsigned int level)
33
{
34 35 36 37 38 39
	struct list_head *ws = btrfs_get_workspace(&wsm, level);
	struct workspace *workspace = list_entry(ws, struct workspace, list);

	workspace->level = level;

	return ws;
40 41 42 43 44 45 46
}

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

47 48 49
static void zlib_free_workspace(struct list_head *ws)
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
50

51
	kvfree(workspace->strm.workspace);
52 53 54 55
	kfree(workspace->buf);
	kfree(workspace);
}

56
static struct list_head *zlib_alloc_workspace(unsigned int level)
C
Chris Mason 已提交
57 58
{
	struct workspace *workspace;
59
	int workspacesize;
L
Li Zefan 已提交
60

61
	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
62 63
	if (!workspace)
		return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
64

65 66
	workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
			zlib_inflate_workspacesize());
67
	workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL);
68
	workspace->level = level;
69
	workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
70
	if (!workspace->strm.workspace || !workspace->buf)
71
		goto fail;
C
Chris Mason 已提交
72

73
	INIT_LIST_HEAD(&workspace->list);
C
Chris Mason 已提交
74

75 76 77 78
	return &workspace->list;
fail:
	zlib_free_workspace(&workspace->list);
	return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
79 80
}

81 82 83
int zlib_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)
C
Chris Mason 已提交
84
{
85
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
86 87 88 89 90 91 92
	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;
93
	unsigned long len = *total_out;
94
	unsigned long nr_dest_pages = *out_pages;
95
	const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
C
Chris Mason 已提交
96 97 98 99 100

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

101
	if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
102
		pr_warn("BTRFS: deflateInit failed\n");
103
		ret = -EIO;
C
Chris Mason 已提交
104 105 106
		goto out;
	}

107 108
	workspace->strm.total_in = 0;
	workspace->strm.total_out = 0;
C
Chris Mason 已提交
109

110
	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
C
Chris Mason 已提交
111 112 113
	data_in = kmap(in_page);

	out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
L
Li Zefan 已提交
114
	if (out_page == NULL) {
115
		ret = -ENOMEM;
L
Li Zefan 已提交
116 117
		goto out;
	}
C
Chris Mason 已提交
118 119 120 121
	cpage_out = kmap(out_page);
	pages[0] = out_page;
	nr_pages = 1;

122 123
	workspace->strm.next_in = data_in;
	workspace->strm.next_out = cpage_out;
124 125
	workspace->strm.avail_out = PAGE_SIZE;
	workspace->strm.avail_in = min(len, PAGE_SIZE);
C
Chris Mason 已提交
126

127 128
	while (workspace->strm.total_in < len) {
		ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
C
Chris Mason 已提交
129
		if (ret != Z_OK) {
130
			pr_debug("BTRFS: deflate in loop returned %d\n",
C
Chris Mason 已提交
131
			       ret);
132
			zlib_deflateEnd(&workspace->strm);
133
			ret = -EIO;
C
Chris Mason 已提交
134 135 136 137
			goto out;
		}

		/* we're making it bigger, give up */
138 139 140
		if (workspace->strm.total_in > 8192 &&
		    workspace->strm.total_in <
		    workspace->strm.total_out) {
141
			ret = -E2BIG;
C
Chris Mason 已提交
142 143 144 145 146 147
			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
		 */
148
		if (workspace->strm.avail_out == 0) {
C
Chris Mason 已提交
149 150 151
			kunmap(out_page);
			if (nr_pages == nr_dest_pages) {
				out_page = NULL;
152
				ret = -E2BIG;
C
Chris Mason 已提交
153 154 155
				goto out;
			}
			out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
L
Li Zefan 已提交
156
			if (out_page == NULL) {
157
				ret = -ENOMEM;
L
Li Zefan 已提交
158 159
				goto out;
			}
C
Chris Mason 已提交
160 161 162
			cpage_out = kmap(out_page);
			pages[nr_pages] = out_page;
			nr_pages++;
163
			workspace->strm.avail_out = PAGE_SIZE;
164
			workspace->strm.next_out = cpage_out;
C
Chris Mason 已提交
165 166
		}
		/* we're all done */
167
		if (workspace->strm.total_in >= len)
C
Chris Mason 已提交
168 169 170
			break;

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

175
			bytes_left = len - workspace->strm.total_in;
C
Chris Mason 已提交
176
			kunmap(in_page);
177
			put_page(in_page);
C
Chris Mason 已提交
178

179
			start += PAGE_SIZE;
C
Chris Mason 已提交
180
			in_page = find_get_page(mapping,
181
						start >> PAGE_SHIFT);
C
Chris Mason 已提交
182
			data_in = kmap(in_page);
183
			workspace->strm.avail_in = min(bytes_left,
184
							   PAGE_SIZE);
185
			workspace->strm.next_in = data_in;
C
Chris Mason 已提交
186 187
		}
	}
188 189 190
	workspace->strm.avail_in = 0;
	ret = zlib_deflate(&workspace->strm, Z_FINISH);
	zlib_deflateEnd(&workspace->strm);
C
Chris Mason 已提交
191 192

	if (ret != Z_STREAM_END) {
193
		ret = -EIO;
C
Chris Mason 已提交
194 195 196
		goto out;
	}

197
	if (workspace->strm.total_out >= workspace->strm.total_in) {
198
		ret = -E2BIG;
C
Chris Mason 已提交
199 200 201 202
		goto out;
	}

	ret = 0;
203 204
	*total_out = workspace->strm.total_out;
	*total_in = workspace->strm.total_in;
C
Chris Mason 已提交
205 206 207 208 209 210 211
out:
	*out_pages = nr_pages;
	if (out_page)
		kunmap(out_page);

	if (in_page) {
		kunmap(in_page);
212
		put_page(in_page);
C
Chris Mason 已提交
213 214 215 216
	}
	return ret;
}

217
int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
C
Chris Mason 已提交
218
{
219
	struct workspace *workspace = list_entry(ws, struct workspace, list);
220
	int ret = 0, ret2;
C
Chris Mason 已提交
221 222 223 224
	int wbits = MAX_WBITS;
	char *data_in;
	size_t total_out = 0;
	unsigned long page_in_index = 0;
225
	size_t srclen = cb->compressed_len;
226
	unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
C
Chris Mason 已提交
227
	unsigned long buf_start;
228 229 230
	struct page **pages_in = cb->compressed_pages;
	u64 disk_start = cb->start;
	struct bio *orig_bio = cb->orig_bio;
C
Chris Mason 已提交
231 232

	data_in = kmap(pages_in[page_in_index]);
233
	workspace->strm.next_in = data_in;
234
	workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
235
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
236

237 238
	workspace->strm.total_out = 0;
	workspace->strm.next_out = workspace->buf;
239
	workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
240 241 242 243 244 245 246 247

	/* 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);
248 249
		workspace->strm.next_in += 2;
		workspace->strm.avail_in -= 2;
C
Chris Mason 已提交
250 251
	}

252
	if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
253
		pr_warn("BTRFS: inflateInit failed\n");
254
		kunmap(pages_in[page_in_index]);
255
		return -EIO;
C
Chris Mason 已提交
256
	}
257 258
	while (workspace->strm.total_in < srclen) {
		ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
C
Chris Mason 已提交
259
		if (ret != Z_OK && ret != Z_STREAM_END)
C
Chris Mason 已提交
260 261
			break;

262
		buf_start = total_out;
263
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
264

265 266
		/* we didn't make progress in this inflate call, we're done */
		if (buf_start == total_out)
C
Chris Mason 已提交
267 268
			break;

269 270
		ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
						 total_out, disk_start,
271
						 orig_bio);
272 273 274
		if (ret2 == 0) {
			ret = 0;
			goto done;
C
Chris Mason 已提交
275
		}
276

277
		workspace->strm.next_out = workspace->buf;
278
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
279

280
		if (workspace->strm.avail_in == 0) {
C
Chris Mason 已提交
281 282 283 284 285 286 287 288
			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]);
289 290 291
			workspace->strm.next_in = data_in;
			tmp = srclen - workspace->strm.total_in;
			workspace->strm.avail_in = min(tmp,
292
							   PAGE_SIZE);
C
Chris Mason 已提交
293 294
		}
	}
C
Chris Mason 已提交
295
	if (ret != Z_STREAM_END)
296
		ret = -EIO;
C
Chris Mason 已提交
297
	else
C
Chris Mason 已提交
298 299
		ret = 0;
done:
300
	zlib_inflateEnd(&workspace->strm);
C
Chris Mason 已提交
301 302
	if (data_in)
		kunmap(pages_in[page_in_index]);
303
	if (!ret)
304
		zero_fill_bio(orig_bio);
C
Chris Mason 已提交
305 306 307
	return ret;
}

308 309 310
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 已提交
311
{
312
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
313 314
	int ret = 0;
	int wbits = MAX_WBITS;
315
	unsigned long bytes_left;
C
Chris Mason 已提交
316
	unsigned long total_out = 0;
317
	unsigned long pg_offset = 0;
C
Chris Mason 已提交
318 319
	char *kaddr;

320 321 322
	destlen = min_t(unsigned long, destlen, PAGE_SIZE);
	bytes_left = destlen;

323 324 325
	workspace->strm.next_in = data_in;
	workspace->strm.avail_in = srclen;
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
326

327
	workspace->strm.next_out = workspace->buf;
328
	workspace->strm.avail_out = PAGE_SIZE;
329
	workspace->strm.total_out = 0;
C
Chris Mason 已提交
330 331 332 333 334 335 336
	/* 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);
337 338
		workspace->strm.next_in += 2;
		workspace->strm.avail_in -= 2;
C
Chris Mason 已提交
339 340
	}

341
	if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
342
		pr_warn("BTRFS: inflateInit failed\n");
343
		return -EIO;
C
Chris Mason 已提交
344 345
	}

C
Chris Mason 已提交
346
	while (bytes_left > 0) {
C
Chris Mason 已提交
347 348 349 350
		unsigned long buf_start;
		unsigned long buf_offset;
		unsigned long bytes;

351
		ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
C
Chris Mason 已提交
352
		if (ret != Z_OK && ret != Z_STREAM_END)
C
Chris Mason 已提交
353 354 355
			break;

		buf_start = total_out;
356
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
357 358

		if (total_out == buf_start) {
359
			ret = -EIO;
C
Chris Mason 已提交
360 361 362
			break;
		}

C
Chris Mason 已提交
363
		if (total_out <= start_byte)
C
Chris Mason 已提交
364 365
			goto next;

C
Chris Mason 已提交
366
		if (total_out > start_byte && buf_start < start_byte)
C
Chris Mason 已提交
367
			buf_offset = start_byte - buf_start;
C
Chris Mason 已提交
368
		else
C
Chris Mason 已提交
369 370
			buf_offset = 0;

371 372
		bytes = min(PAGE_SIZE - pg_offset,
			    PAGE_SIZE - buf_offset);
C
Chris Mason 已提交
373 374
		bytes = min(bytes, bytes_left);

375
		kaddr = kmap_atomic(dest_page);
C
Chris Mason 已提交
376
		memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
377
		kunmap_atomic(kaddr);
C
Chris Mason 已提交
378 379 380 381

		pg_offset += bytes;
		bytes_left -= bytes;
next:
382
		workspace->strm.next_out = workspace->buf;
383
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
384
	}
C
Chris Mason 已提交
385 386

	if (ret != Z_STREAM_END && bytes_left != 0)
387
		ret = -EIO;
C
Chris Mason 已提交
388
	else
C
Chris Mason 已提交
389
		ret = 0;
C
Chris Mason 已提交
390

391
	zlib_inflateEnd(&workspace->strm);
392 393 394 395 396 397 398 399 400 401 402

	/*
	 * 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 已提交
403 404 405
	return ret;
}

406
const struct btrfs_compress_op btrfs_zlib_compress = {
407
	.workspace_manager	= &wsm,
408 409
	.get_workspace		= zlib_get_workspace,
	.put_workspace		= zlib_put_workspace,
410 411
	.alloc_workspace	= zlib_alloc_workspace,
	.free_workspace		= zlib_free_workspace,
412 413
	.max_level		= 9,
	.default_level		= BTRFS_ZLIB_DEFAULT_LEVEL,
414
};