zlib.c 10.0 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
struct list_head *zlib_get_workspace(unsigned int level)
33
{
34
	struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
35 36 37 38 39
	struct workspace *workspace = list_entry(ws, struct workspace, list);

	workspace->level = level;

	return ws;
40 41
}

42
void zlib_free_workspace(struct list_head *ws)
43 44
{
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
45

46
	kvfree(workspace->strm.workspace);
47 48 49 50
	kfree(workspace->buf);
	kfree(workspace);
}

51
struct list_head *zlib_alloc_workspace(unsigned int level)
C
Chris Mason 已提交
52 53
{
	struct workspace *workspace;
54
	int workspacesize;
L
Li Zefan 已提交
55

56
	workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
57 58
	if (!workspace)
		return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
59

60 61
	workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
			zlib_inflate_workspacesize());
62
	workspace->strm.workspace = kvmalloc(workspacesize, GFP_KERNEL);
63
	workspace->level = level;
64
	workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
65
	if (!workspace->strm.workspace || !workspace->buf)
66
		goto fail;
C
Chris Mason 已提交
67

68
	INIT_LIST_HEAD(&workspace->list);
C
Chris Mason 已提交
69

70 71 72 73
	return &workspace->list;
fail:
	zlib_free_workspace(&workspace->list);
	return ERR_PTR(-ENOMEM);
C
Chris Mason 已提交
74 75
}

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

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

96
	if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
97
		pr_warn("BTRFS: deflateInit failed\n");
98
		ret = -EIO;
C
Chris Mason 已提交
99 100 101
		goto out;
	}

102 103
	workspace->strm.total_in = 0;
	workspace->strm.total_out = 0;
C
Chris Mason 已提交
104

105
	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
C
Chris Mason 已提交
106 107 108
	data_in = kmap(in_page);

	out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
L
Li Zefan 已提交
109
	if (out_page == NULL) {
110
		ret = -ENOMEM;
L
Li Zefan 已提交
111 112
		goto out;
	}
C
Chris Mason 已提交
113 114 115 116
	cpage_out = kmap(out_page);
	pages[0] = out_page;
	nr_pages = 1;

117 118
	workspace->strm.next_in = data_in;
	workspace->strm.next_out = cpage_out;
119 120
	workspace->strm.avail_out = PAGE_SIZE;
	workspace->strm.avail_in = min(len, PAGE_SIZE);
C
Chris Mason 已提交
121

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

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

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

170
			bytes_left = len - workspace->strm.total_in;
C
Chris Mason 已提交
171
			kunmap(in_page);
172
			put_page(in_page);
C
Chris Mason 已提交
173

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

	if (ret != Z_STREAM_END) {
188
		ret = -EIO;
C
Chris Mason 已提交
189 190 191
		goto out;
	}

192
	if (workspace->strm.total_out >= workspace->strm.total_in) {
193
		ret = -E2BIG;
C
Chris Mason 已提交
194 195 196 197
		goto out;
	}

	ret = 0;
198 199
	*total_out = workspace->strm.total_out;
	*total_in = workspace->strm.total_in;
C
Chris Mason 已提交
200 201 202 203 204 205 206
out:
	*out_pages = nr_pages;
	if (out_page)
		kunmap(out_page);

	if (in_page) {
		kunmap(in_page);
207
		put_page(in_page);
C
Chris Mason 已提交
208 209 210 211
	}
	return ret;
}

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

	data_in = kmap(pages_in[page_in_index]);
228
	workspace->strm.next_in = data_in;
229
	workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
230
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
231

232 233
	workspace->strm.total_out = 0;
	workspace->strm.next_out = workspace->buf;
234
	workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
235 236 237 238 239 240 241 242

	/* 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);
243 244
		workspace->strm.next_in += 2;
		workspace->strm.avail_in -= 2;
C
Chris Mason 已提交
245 246
	}

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

257
		buf_start = total_out;
258
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
259

260 261
		/* we didn't make progress in this inflate call, we're done */
		if (buf_start == total_out)
C
Chris Mason 已提交
262 263
			break;

264 265
		ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
						 total_out, disk_start,
266
						 orig_bio);
267 268 269
		if (ret2 == 0) {
			ret = 0;
			goto done;
C
Chris Mason 已提交
270
		}
271

272
		workspace->strm.next_out = workspace->buf;
273
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
274

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

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

315 316 317
	destlen = min_t(unsigned long, destlen, PAGE_SIZE);
	bytes_left = destlen;

318 319 320
	workspace->strm.next_in = data_in;
	workspace->strm.avail_in = srclen;
	workspace->strm.total_in = 0;
C
Chris Mason 已提交
321

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

336
	if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
337
		pr_warn("BTRFS: inflateInit failed\n");
338
		return -EIO;
C
Chris Mason 已提交
339 340
	}

C
Chris Mason 已提交
341
	while (bytes_left > 0) {
C
Chris Mason 已提交
342 343 344 345
		unsigned long buf_start;
		unsigned long buf_offset;
		unsigned long bytes;

346
		ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
C
Chris Mason 已提交
347
		if (ret != Z_OK && ret != Z_STREAM_END)
C
Chris Mason 已提交
348 349 350
			break;

		buf_start = total_out;
351
		total_out = workspace->strm.total_out;
C
Chris Mason 已提交
352 353

		if (total_out == buf_start) {
354
			ret = -EIO;
C
Chris Mason 已提交
355 356 357
			break;
		}

C
Chris Mason 已提交
358
		if (total_out <= start_byte)
C
Chris Mason 已提交
359 360
			goto next;

C
Chris Mason 已提交
361
		if (total_out > start_byte && buf_start < start_byte)
C
Chris Mason 已提交
362
			buf_offset = start_byte - buf_start;
C
Chris Mason 已提交
363
		else
C
Chris Mason 已提交
364 365
			buf_offset = 0;

366 367
		bytes = min(PAGE_SIZE - pg_offset,
			    PAGE_SIZE - buf_offset);
C
Chris Mason 已提交
368 369
		bytes = min(bytes, bytes_left);

370
		kaddr = kmap_atomic(dest_page);
C
Chris Mason 已提交
371
		memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
372
		kunmap_atomic(kaddr);
C
Chris Mason 已提交
373 374 375 376

		pg_offset += bytes;
		bytes_left -= bytes;
next:
377
		workspace->strm.next_out = workspace->buf;
378
		workspace->strm.avail_out = PAGE_SIZE;
C
Chris Mason 已提交
379
	}
C
Chris Mason 已提交
380 381

	if (ret != Z_STREAM_END && bytes_left != 0)
382
		ret = -EIO;
C
Chris Mason 已提交
383
	else
C
Chris Mason 已提交
384
		ret = 0;
C
Chris Mason 已提交
385

386
	zlib_inflateEnd(&workspace->strm);
387 388 389 390 391 392 393 394 395 396 397

	/*
	 * 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 已提交
398 399 400
	return ret;
}

401
const struct btrfs_compress_op btrfs_zlib_compress = {
402
	.workspace_manager	= &wsm,
403 404
	.alloc_workspace	= zlib_alloc_workspace,
	.free_workspace		= zlib_free_workspace,
405 406
	.max_level		= 9,
	.default_level		= BTRFS_ZLIB_DEFAULT_LEVEL,
407
};