1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Oracle. All rights reserved.
5 * Based on jffs2 zlib code:
6 * Copyright © 2001-2007 Red Hat, Inc.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/zlib.h>
13 #include <linux/zutil.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/pagemap.h>
19 #include <linux/bio.h>
20 #include <linux/refcount.h>
21 #include "compression.h"
26 struct list_head list;
30 static struct workspace_manager wsm;
32 struct list_head *zlib_get_workspace(unsigned int level)
34 struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
35 struct workspace *workspace = list_entry(ws, struct workspace, list);
37 workspace->level = level;
42 void zlib_free_workspace(struct list_head *ws)
44 struct workspace *workspace = list_entry(ws, struct workspace, list);
46 kvfree(workspace->strm.workspace);
47 kfree(workspace->buf);
51 struct list_head *zlib_alloc_workspace(unsigned int level)
53 struct workspace *workspace;
56 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
58 return ERR_PTR(-ENOMEM);
60 workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
61 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)
68 INIT_LIST_HEAD(&workspace->list);
70 return &workspace->list;
72 zlib_free_workspace(&workspace->list);
73 return ERR_PTR(-ENOMEM);
76 int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
77 u64 start, struct page **pages, unsigned long *out_pages,
78 unsigned long *total_in, unsigned long *total_out)
80 struct workspace *workspace = list_entry(ws, struct workspace, list);
85 struct page *in_page = NULL;
86 struct page *out_page = NULL;
87 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;
96 if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
97 pr_warn("BTRFS: deflateInit failed\n");
102 workspace->strm.total_in = 0;
103 workspace->strm.total_out = 0;
105 in_page = find_get_page(mapping, start >> PAGE_SHIFT);
106 data_in = kmap(in_page);
108 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
109 if (out_page == NULL) {
113 cpage_out = kmap(out_page);
117 workspace->strm.next_in = data_in;
118 workspace->strm.next_out = cpage_out;
119 workspace->strm.avail_out = PAGE_SIZE;
120 workspace->strm.avail_in = min(len, PAGE_SIZE);
122 while (workspace->strm.total_in < len) {
123 ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
125 pr_debug("BTRFS: deflate in loop returned %d\n",
127 zlib_deflateEnd(&workspace->strm);
132 /* we're making it bigger, give up */
133 if (workspace->strm.total_in > 8192 &&
134 workspace->strm.total_in <
135 workspace->strm.total_out) {
139 /* we need another page for writing out. Test this
140 * before the total_in so we will pull in a new page for
141 * the stream end if required
143 if (workspace->strm.avail_out == 0) {
145 if (nr_pages == nr_dest_pages) {
150 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
151 if (out_page == NULL) {
155 cpage_out = kmap(out_page);
156 pages[nr_pages] = out_page;
158 workspace->strm.avail_out = PAGE_SIZE;
159 workspace->strm.next_out = cpage_out;
162 if (workspace->strm.total_in >= len)
165 /* we've read in a full page, get a new one */
166 if (workspace->strm.avail_in == 0) {
167 if (workspace->strm.total_out > max_out)
170 bytes_left = len - workspace->strm.total_in;
175 in_page = find_get_page(mapping,
176 start >> PAGE_SHIFT);
177 data_in = kmap(in_page);
178 workspace->strm.avail_in = min(bytes_left,
180 workspace->strm.next_in = data_in;
183 workspace->strm.avail_in = 0;
184 ret = zlib_deflate(&workspace->strm, Z_FINISH);
185 zlib_deflateEnd(&workspace->strm);
187 if (ret != Z_STREAM_END) {
192 if (workspace->strm.total_out >= workspace->strm.total_in) {
198 *total_out = workspace->strm.total_out;
199 *total_in = workspace->strm.total_in;
201 *out_pages = nr_pages;
212 int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
214 struct workspace *workspace = list_entry(ws, struct workspace, list);
216 int wbits = MAX_WBITS;
218 size_t total_out = 0;
219 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);
222 unsigned long buf_start;
223 struct page **pages_in = cb->compressed_pages;
224 u64 disk_start = cb->start;
225 struct bio *orig_bio = cb->orig_bio;
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;
232 workspace->strm.total_out = 0;
233 workspace->strm.next_out = workspace->buf;
234 workspace->strm.avail_out = PAGE_SIZE;
236 /* If it's deflate, and it's got no preset dictionary, then
237 we can tell zlib to skip the adler32 check. */
238 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
239 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
240 !(((data_in[0]<<8) + data_in[1]) % 31)) {
242 wbits = -((data_in[0] >> 4) + 8);
243 workspace->strm.next_in += 2;
244 workspace->strm.avail_in -= 2;
247 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
248 pr_warn("BTRFS: inflateInit failed\n");
249 kunmap(pages_in[page_in_index]);
252 while (workspace->strm.total_in < srclen) {
253 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
254 if (ret != Z_OK && ret != Z_STREAM_END)
257 buf_start = total_out;
258 total_out = workspace->strm.total_out;
260 /* we didn't make progress in this inflate call, we're done */
261 if (buf_start == total_out)
264 ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
265 total_out, disk_start,
272 workspace->strm.next_out = workspace->buf;
273 workspace->strm.avail_out = PAGE_SIZE;
275 if (workspace->strm.avail_in == 0) {
277 kunmap(pages_in[page_in_index]);
279 if (page_in_index >= total_pages_in) {
283 data_in = kmap(pages_in[page_in_index]);
284 workspace->strm.next_in = data_in;
285 tmp = srclen - workspace->strm.total_in;
286 workspace->strm.avail_in = min(tmp,
290 if (ret != Z_STREAM_END)
295 zlib_inflateEnd(&workspace->strm);
297 kunmap(pages_in[page_in_index]);
299 zero_fill_bio(orig_bio);
303 int zlib_decompress(struct list_head *ws, unsigned char *data_in,
304 struct page *dest_page, unsigned long start_byte, size_t srclen,
307 struct workspace *workspace = list_entry(ws, struct workspace, list);
309 int wbits = MAX_WBITS;
310 unsigned long bytes_left;
311 unsigned long total_out = 0;
312 unsigned long pg_offset = 0;
315 destlen = min_t(unsigned long, destlen, PAGE_SIZE);
316 bytes_left = destlen;
318 workspace->strm.next_in = data_in;
319 workspace->strm.avail_in = srclen;
320 workspace->strm.total_in = 0;
322 workspace->strm.next_out = workspace->buf;
323 workspace->strm.avail_out = PAGE_SIZE;
324 workspace->strm.total_out = 0;
325 /* If it's deflate, and it's got no preset dictionary, then
326 we can tell zlib to skip the adler32 check. */
327 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
328 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
329 !(((data_in[0]<<8) + data_in[1]) % 31)) {
331 wbits = -((data_in[0] >> 4) + 8);
332 workspace->strm.next_in += 2;
333 workspace->strm.avail_in -= 2;
336 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
337 pr_warn("BTRFS: inflateInit failed\n");
341 while (bytes_left > 0) {
342 unsigned long buf_start;
343 unsigned long buf_offset;
346 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
347 if (ret != Z_OK && ret != Z_STREAM_END)
350 buf_start = total_out;
351 total_out = workspace->strm.total_out;
353 if (total_out == buf_start) {
358 if (total_out <= start_byte)
361 if (total_out > start_byte && buf_start < start_byte)
362 buf_offset = start_byte - buf_start;
366 bytes = min(PAGE_SIZE - pg_offset,
367 PAGE_SIZE - buf_offset);
368 bytes = min(bytes, bytes_left);
370 kaddr = kmap_atomic(dest_page);
371 memcpy(kaddr + pg_offset, workspace->buf + buf_offset, bytes);
372 kunmap_atomic(kaddr);
377 workspace->strm.next_out = workspace->buf;
378 workspace->strm.avail_out = PAGE_SIZE;
381 if (ret != Z_STREAM_END && bytes_left != 0)
386 zlib_inflateEnd(&workspace->strm);
389 * this should only happen if zlib returned fewer bytes than we
390 * expected. btrfs_get_block is responsible for zeroing from the
391 * end of the inline extent (destlen) to the end of the page
393 if (pg_offset < destlen) {
394 kaddr = kmap_atomic(dest_page);
395 memset(kaddr + pg_offset, 0, destlen - pg_offset);
396 kunmap_atomic(kaddr);
401 const struct btrfs_compress_op btrfs_zlib_compress = {
402 .workspace_manager = &wsm,
404 .default_level = BTRFS_ZLIB_DEFAULT_LEVEL,