zlib.c 10.4 KB
Newer Older
C
Chris Mason 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (C) 2008 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 *
 * 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>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/pagemap.h>
#include <linux/bio.h>
33
#include <linux/refcount.h>
34
#include "compression.h"
C
Chris Mason 已提交
35 36

struct workspace {
37
	z_stream strm;
C
Chris Mason 已提交
38 39 40 41
	char *buf;
	struct list_head list;
};

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

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

static struct list_head *zlib_alloc_workspace(void)
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 62
	workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
			zlib_inflate_workspacesize());
	workspace->strm.workspace = vmalloc(workspacesize);
63
	workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
64
	if (!workspace->strm.workspace || !workspace->buf)
65
		goto fail;
C
Chris Mason 已提交
66

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

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

75 76
static int zlib_compress_pages(struct list_head *ws,
			       struct address_space *mapping,
77
			       u64 start,
78 79 80
			       struct page **pages,
			       unsigned long *out_pages,
			       unsigned long *total_in,
81
			       unsigned long *total_out)
C
Chris Mason 已提交
82
{
83
	struct workspace *workspace = list_entry(ws, struct workspace, list);
C
Chris Mason 已提交
84 85 86 87 88 89 90
	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;
91
	unsigned long len = *total_out;
92
	unsigned long nr_dest_pages = *out_pages;
93
	const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
C
Chris Mason 已提交
94 95 96 97 98

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

405
const struct btrfs_compress_op btrfs_zlib_compress = {
406 407 408
	.alloc_workspace	= zlib_alloc_workspace,
	.free_workspace		= zlib_free_workspace,
	.compress_pages		= zlib_compress_pages,
409
	.decompress_bio		= zlib_decompress_bio,
410 411
	.decompress		= zlib_decompress,
};