btrfs: use predefined limits for calculating maximum number of pages for compression
[linux-block.git] / fs / btrfs / zlib.c
CommitLineData
c8b97818
CM
1/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 *
18 * Based on jffs2 zlib code:
19 * Copyright © 2001-2007 Red Hat, Inc.
20 * Created by David Woodhouse <dwmw2@infradead.org>
21 */
22
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/zlib.h>
26#include <linux/zutil.h>
27#include <linux/vmalloc.h>
28#include <linux/init.h>
29#include <linux/err.h>
30#include <linux/sched.h>
31#include <linux/pagemap.h>
32#include <linux/bio.h>
b2950863 33#include "compression.h"
c8b97818 34
c8b97818 35struct workspace {
78809913 36 z_stream strm;
c8b97818
CM
37 char *buf;
38 struct list_head list;
39};
40
261507a0
LZ
41static void zlib_free_workspace(struct list_head *ws)
42{
43 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818 44
78809913 45 vfree(workspace->strm.workspace);
261507a0
LZ
46 kfree(workspace->buf);
47 kfree(workspace);
48}
49
50static struct list_head *zlib_alloc_workspace(void)
c8b97818
CM
51{
52 struct workspace *workspace;
78809913 53 int workspacesize;
8844355d 54
c8b97818 55 workspace = kzalloc(sizeof(*workspace), GFP_NOFS);
261507a0
LZ
56 if (!workspace)
57 return ERR_PTR(-ENOMEM);
c8b97818 58
78809913
SS
59 workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
60 zlib_inflate_workspacesize());
61 workspace->strm.workspace = vmalloc(workspacesize);
09cbfeaf 62 workspace->buf = kmalloc(PAGE_SIZE, GFP_NOFS);
78809913 63 if (!workspace->strm.workspace || !workspace->buf)
261507a0 64 goto fail;
c8b97818 65
261507a0 66 INIT_LIST_HEAD(&workspace->list);
c8b97818 67
261507a0
LZ
68 return &workspace->list;
69fail:
70 zlib_free_workspace(&workspace->list);
71 return ERR_PTR(-ENOMEM);
c8b97818
CM
72}
73
261507a0
LZ
74static int zlib_compress_pages(struct list_head *ws,
75 struct address_space *mapping,
38c31464 76 u64 start,
261507a0 77 struct page **pages,
261507a0
LZ
78 unsigned long *out_pages,
79 unsigned long *total_in,
80 unsigned long *total_out,
81 unsigned long max_out)
c8b97818 82{
261507a0 83 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818 84 int ret;
c8b97818
CM
85 char *data_in;
86 char *cpage_out;
87 int nr_pages = 0;
88 struct page *in_page = NULL;
89 struct page *out_page = NULL;
c8b97818 90 unsigned long bytes_left;
38c31464 91 unsigned long len = *total_out;
4d3a800e 92 unsigned long nr_dest_pages = *out_pages;
c8b97818
CM
93
94 *out_pages = 0;
95 *total_out = 0;
96 *total_in = 0;
97
78809913 98 if (Z_OK != zlib_deflateInit(&workspace->strm, 3)) {
62e85577 99 pr_warn("BTRFS: deflateInit failed\n");
60e1975a 100 ret = -EIO;
c8b97818
CM
101 goto out;
102 }
103
78809913
SS
104 workspace->strm.total_in = 0;
105 workspace->strm.total_out = 0;
c8b97818 106
09cbfeaf 107 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
c8b97818
CM
108 data_in = kmap(in_page);
109
110 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
4b72029d 111 if (out_page == NULL) {
60e1975a 112 ret = -ENOMEM;
4b72029d
LZ
113 goto out;
114 }
c8b97818
CM
115 cpage_out = kmap(out_page);
116 pages[0] = out_page;
117 nr_pages = 1;
118
78809913
SS
119 workspace->strm.next_in = data_in;
120 workspace->strm.next_out = cpage_out;
09cbfeaf
KS
121 workspace->strm.avail_out = PAGE_SIZE;
122 workspace->strm.avail_in = min(len, PAGE_SIZE);
c8b97818 123
78809913
SS
124 while (workspace->strm.total_in < len) {
125 ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
c8b97818 126 if (ret != Z_OK) {
62e85577 127 pr_debug("BTRFS: deflate in loop returned %d\n",
c8b97818 128 ret);
78809913 129 zlib_deflateEnd(&workspace->strm);
60e1975a 130 ret = -EIO;
c8b97818
CM
131 goto out;
132 }
133
134 /* we're making it bigger, give up */
78809913
SS
135 if (workspace->strm.total_in > 8192 &&
136 workspace->strm.total_in <
137 workspace->strm.total_out) {
130d5b41 138 ret = -E2BIG;
c8b97818
CM
139 goto out;
140 }
141 /* we need another page for writing out. Test this
142 * before the total_in so we will pull in a new page for
143 * the stream end if required
144 */
78809913 145 if (workspace->strm.avail_out == 0) {
c8b97818
CM
146 kunmap(out_page);
147 if (nr_pages == nr_dest_pages) {
148 out_page = NULL;
60e1975a 149 ret = -E2BIG;
c8b97818
CM
150 goto out;
151 }
152 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
4b72029d 153 if (out_page == NULL) {
60e1975a 154 ret = -ENOMEM;
4b72029d
LZ
155 goto out;
156 }
c8b97818
CM
157 cpage_out = kmap(out_page);
158 pages[nr_pages] = out_page;
159 nr_pages++;
09cbfeaf 160 workspace->strm.avail_out = PAGE_SIZE;
78809913 161 workspace->strm.next_out = cpage_out;
c8b97818
CM
162 }
163 /* we're all done */
78809913 164 if (workspace->strm.total_in >= len)
c8b97818
CM
165 break;
166
167 /* we've read in a full page, get a new one */
78809913
SS
168 if (workspace->strm.avail_in == 0) {
169 if (workspace->strm.total_out > max_out)
c8b97818
CM
170 break;
171
78809913 172 bytes_left = len - workspace->strm.total_in;
c8b97818 173 kunmap(in_page);
09cbfeaf 174 put_page(in_page);
c8b97818 175
09cbfeaf 176 start += PAGE_SIZE;
c8b97818 177 in_page = find_get_page(mapping,
09cbfeaf 178 start >> PAGE_SHIFT);
c8b97818 179 data_in = kmap(in_page);
78809913 180 workspace->strm.avail_in = min(bytes_left,
09cbfeaf 181 PAGE_SIZE);
78809913 182 workspace->strm.next_in = data_in;
c8b97818
CM
183 }
184 }
78809913
SS
185 workspace->strm.avail_in = 0;
186 ret = zlib_deflate(&workspace->strm, Z_FINISH);
187 zlib_deflateEnd(&workspace->strm);
c8b97818
CM
188
189 if (ret != Z_STREAM_END) {
60e1975a 190 ret = -EIO;
c8b97818
CM
191 goto out;
192 }
193
78809913 194 if (workspace->strm.total_out >= workspace->strm.total_in) {
60e1975a 195 ret = -E2BIG;
c8b97818
CM
196 goto out;
197 }
198
199 ret = 0;
78809913
SS
200 *total_out = workspace->strm.total_out;
201 *total_in = workspace->strm.total_in;
c8b97818
CM
202out:
203 *out_pages = nr_pages;
204 if (out_page)
205 kunmap(out_page);
206
207 if (in_page) {
208 kunmap(in_page);
09cbfeaf 209 put_page(in_page);
c8b97818 210 }
c8b97818
CM
211 return ret;
212}
213
974b1adc 214static int zlib_decompress_bio(struct list_head *ws, struct page **pages_in,
261507a0 215 u64 disk_start,
974b1adc 216 struct bio *orig_bio,
261507a0 217 size_t srclen)
c8b97818 218{
261507a0 219 struct workspace *workspace = list_entry(ws, struct workspace, list);
3a39c18d 220 int ret = 0, ret2;
c8b97818 221 int wbits = MAX_WBITS;
c8b97818
CM
222 char *data_in;
223 size_t total_out = 0;
c8b97818 224 unsigned long page_in_index = 0;
09cbfeaf 225 unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
c8b97818 226 unsigned long buf_start;
c8b97818 227
c8b97818 228 data_in = kmap(pages_in[page_in_index]);
78809913 229 workspace->strm.next_in = data_in;
09cbfeaf 230 workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
78809913 231 workspace->strm.total_in = 0;
c8b97818 232
78809913
SS
233 workspace->strm.total_out = 0;
234 workspace->strm.next_out = workspace->buf;
09cbfeaf 235 workspace->strm.avail_out = PAGE_SIZE;
c8b97818
CM
236
237 /* If it's deflate, and it's got no preset dictionary, then
238 we can tell zlib to skip the adler32 check. */
239 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
240 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
241 !(((data_in[0]<<8) + data_in[1]) % 31)) {
242
243 wbits = -((data_in[0] >> 4) + 8);
78809913
SS
244 workspace->strm.next_in += 2;
245 workspace->strm.avail_in -= 2;
c8b97818
CM
246 }
247
78809913 248 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
62e85577 249 pr_warn("BTRFS: inflateInit failed\n");
d1111a75 250 kunmap(pages_in[page_in_index]);
60e1975a 251 return -EIO;
c8b97818 252 }
78809913
SS
253 while (workspace->strm.total_in < srclen) {
254 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
d397712b 255 if (ret != Z_OK && ret != Z_STREAM_END)
c8b97818 256 break;
c8b97818 257
3a39c18d 258 buf_start = total_out;
78809913 259 total_out = workspace->strm.total_out;
c8b97818 260
3a39c18d
LZ
261 /* we didn't make progress in this inflate call, we're done */
262 if (buf_start == total_out)
c8b97818 263 break;
c8b97818 264
3a39c18d
LZ
265 ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
266 total_out, disk_start,
974b1adc 267 orig_bio);
3a39c18d
LZ
268 if (ret2 == 0) {
269 ret = 0;
270 goto done;
c8b97818 271 }
3a39c18d 272
78809913 273 workspace->strm.next_out = workspace->buf;
09cbfeaf 274 workspace->strm.avail_out = PAGE_SIZE;
c8b97818 275
78809913 276 if (workspace->strm.avail_in == 0) {
c8b97818
CM
277 unsigned long tmp;
278 kunmap(pages_in[page_in_index]);
279 page_in_index++;
280 if (page_in_index >= total_pages_in) {
281 data_in = NULL;
282 break;
283 }
284 data_in = kmap(pages_in[page_in_index]);
78809913
SS
285 workspace->strm.next_in = data_in;
286 tmp = srclen - workspace->strm.total_in;
287 workspace->strm.avail_in = min(tmp,
09cbfeaf 288 PAGE_SIZE);
c8b97818
CM
289 }
290 }
d397712b 291 if (ret != Z_STREAM_END)
60e1975a 292 ret = -EIO;
d397712b 293 else
c8b97818 294 ret = 0;
c8b97818 295done:
78809913 296 zlib_inflateEnd(&workspace->strm);
c8b97818
CM
297 if (data_in)
298 kunmap(pages_in[page_in_index]);
2f19cad9 299 if (!ret)
974b1adc 300 zero_fill_bio(orig_bio);
c8b97818
CM
301 return ret;
302}
303
261507a0
LZ
304static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
305 struct page *dest_page,
306 unsigned long start_byte,
307 size_t srclen, size_t destlen)
c8b97818 308{
261507a0 309 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818
CM
310 int ret = 0;
311 int wbits = MAX_WBITS;
2f19cad9 312 unsigned long bytes_left;
c8b97818 313 unsigned long total_out = 0;
2f19cad9 314 unsigned long pg_offset = 0;
c8b97818
CM
315 char *kaddr;
316
2f19cad9
CM
317 destlen = min_t(unsigned long, destlen, PAGE_SIZE);
318 bytes_left = destlen;
319
78809913
SS
320 workspace->strm.next_in = data_in;
321 workspace->strm.avail_in = srclen;
322 workspace->strm.total_in = 0;
c8b97818 323
78809913 324 workspace->strm.next_out = workspace->buf;
09cbfeaf 325 workspace->strm.avail_out = PAGE_SIZE;
78809913 326 workspace->strm.total_out = 0;
c8b97818
CM
327 /* If it's deflate, and it's got no preset dictionary, then
328 we can tell zlib to skip the adler32 check. */
329 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
330 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
331 !(((data_in[0]<<8) + data_in[1]) % 31)) {
332
333 wbits = -((data_in[0] >> 4) + 8);
78809913
SS
334 workspace->strm.next_in += 2;
335 workspace->strm.avail_in -= 2;
c8b97818
CM
336 }
337
78809913 338 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
62e85577 339 pr_warn("BTRFS: inflateInit failed\n");
60e1975a 340 return -EIO;
c8b97818
CM
341 }
342
d397712b 343 while (bytes_left > 0) {
c8b97818
CM
344 unsigned long buf_start;
345 unsigned long buf_offset;
346 unsigned long bytes;
c8b97818 347
78809913 348 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
d397712b 349 if (ret != Z_OK && ret != Z_STREAM_END)
c8b97818 350 break;
c8b97818
CM
351
352 buf_start = total_out;
78809913 353 total_out = workspace->strm.total_out;
c8b97818
CM
354
355 if (total_out == buf_start) {
60e1975a 356 ret = -EIO;
c8b97818
CM
357 break;
358 }
359
d397712b 360 if (total_out <= start_byte)
c8b97818 361 goto next;
c8b97818 362
d397712b 363 if (total_out > start_byte && buf_start < start_byte)
c8b97818 364 buf_offset = start_byte - buf_start;
d397712b 365 else
c8b97818 366 buf_offset = 0;
c8b97818 367
09cbfeaf
KS
368 bytes = min(PAGE_SIZE - pg_offset,
369 PAGE_SIZE - buf_offset);
c8b97818
CM
370 bytes = min(bytes, bytes_left);
371
7ac687d9 372 kaddr = kmap_atomic(dest_page);
c8b97818 373 memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
7ac687d9 374 kunmap_atomic(kaddr);
c8b97818
CM
375
376 pg_offset += bytes;
377 bytes_left -= bytes;
378next:
78809913 379 workspace->strm.next_out = workspace->buf;
09cbfeaf 380 workspace->strm.avail_out = PAGE_SIZE;
c8b97818 381 }
d397712b
CM
382
383 if (ret != Z_STREAM_END && bytes_left != 0)
60e1975a 384 ret = -EIO;
d397712b 385 else
c8b97818 386 ret = 0;
d397712b 387
78809913 388 zlib_inflateEnd(&workspace->strm);
2f19cad9
CM
389
390 /*
391 * this should only happen if zlib returned fewer bytes than we
392 * expected. btrfs_get_block is responsible for zeroing from the
393 * end of the inline extent (destlen) to the end of the page
394 */
395 if (pg_offset < destlen) {
396 kaddr = kmap_atomic(dest_page);
397 memset(kaddr + pg_offset, 0, destlen - pg_offset);
398 kunmap_atomic(kaddr);
399 }
c8b97818
CM
400 return ret;
401}
402
e8c9f186 403const struct btrfs_compress_op btrfs_zlib_compress = {
261507a0
LZ
404 .alloc_workspace = zlib_alloc_workspace,
405 .free_workspace = zlib_free_workspace,
406 .compress_pages = zlib_compress_pages,
974b1adc 407 .decompress_bio = zlib_decompress_bio,
261507a0
LZ
408 .decompress = zlib_decompress,
409};