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"
23 /* workspace buffer size for s390 zlib hardware support */
24 #define ZLIB_DFLTCC_BUF_SIZE (4 * PAGE_SIZE)
29 unsigned int buf_size;
30 struct list_head list;
34 static struct workspace_manager wsm;
36 struct list_head *zlib_get_workspace(unsigned int level)
38 struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
39 struct workspace *workspace = list_entry(ws, struct workspace, list);
41 workspace->level = level;
46 void zlib_free_workspace(struct list_head *ws)
48 struct workspace *workspace = list_entry(ws, struct workspace, list);
50 kvfree(workspace->strm.workspace);
51 kfree(workspace->buf);
55 struct list_head *zlib_alloc_workspace(unsigned int level)
57 struct workspace *workspace;
60 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
62 return ERR_PTR(-ENOMEM);
64 workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
65 zlib_inflate_workspacesize());
66 workspace->strm.workspace = kvzalloc(workspacesize, GFP_KERNEL | __GFP_NOWARN);
67 workspace->level = level;
68 workspace->buf = NULL;
70 * In case of s390 zlib hardware support, allocate lager workspace
71 * buffer. If allocator fails, fall back to a single page buffer.
73 if (zlib_deflate_dfltcc_enabled()) {
74 workspace->buf = kmalloc(ZLIB_DFLTCC_BUF_SIZE,
75 __GFP_NOMEMALLOC | __GFP_NORETRY |
76 __GFP_NOWARN | GFP_NOIO);
77 workspace->buf_size = ZLIB_DFLTCC_BUF_SIZE;
79 if (!workspace->buf) {
80 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
81 workspace->buf_size = PAGE_SIZE;
83 if (!workspace->strm.workspace || !workspace->buf)
86 INIT_LIST_HEAD(&workspace->list);
88 return &workspace->list;
90 zlib_free_workspace(&workspace->list);
91 return ERR_PTR(-ENOMEM);
94 int zlib_compress_folios(struct list_head *ws, struct address_space *mapping,
95 u64 start, struct folio **folios, unsigned long *out_folios,
96 unsigned long *total_in, unsigned long *total_out)
98 struct workspace *workspace = list_entry(ws, struct workspace, list);
100 char *data_in = NULL;
103 struct folio *in_folio = NULL;
104 struct folio *out_folio = NULL;
105 unsigned long bytes_left;
106 unsigned int in_buf_folios;
107 unsigned long len = *total_out;
108 unsigned long nr_dest_folios = *out_folios;
109 const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
115 if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
116 pr_warn("BTRFS: deflateInit failed\n");
121 workspace->strm.total_in = 0;
122 workspace->strm.total_out = 0;
124 out_folio = btrfs_alloc_compr_folio();
125 if (out_folio == NULL) {
129 cfolio_out = folio_address(out_folio);
130 folios[0] = out_folio;
133 workspace->strm.next_in = workspace->buf;
134 workspace->strm.avail_in = 0;
135 workspace->strm.next_out = cfolio_out;
136 workspace->strm.avail_out = PAGE_SIZE;
138 while (workspace->strm.total_in < len) {
140 * Get next input pages and copy the contents to
141 * the workspace buffer if required.
143 if (workspace->strm.avail_in == 0) {
144 bytes_left = len - workspace->strm.total_in;
145 in_buf_folios = min(DIV_ROUND_UP(bytes_left, PAGE_SIZE),
146 workspace->buf_size / PAGE_SIZE);
147 if (in_buf_folios > 1) {
150 for (i = 0; i < in_buf_folios; i++) {
152 kunmap_local(data_in);
156 ret = btrfs_compress_filemap_get_folio(mapping,
160 data_in = kmap_local_folio(in_folio, 0);
161 copy_page(workspace->buf + i * PAGE_SIZE,
165 workspace->strm.next_in = workspace->buf;
168 kunmap_local(data_in);
172 ret = btrfs_compress_filemap_get_folio(mapping,
176 data_in = kmap_local_folio(in_folio, 0);
178 workspace->strm.next_in = data_in;
180 workspace->strm.avail_in = min(bytes_left,
181 (unsigned long) workspace->buf_size);
184 ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
186 pr_debug("BTRFS: deflate in loop returned %d\n",
188 zlib_deflateEnd(&workspace->strm);
193 /* we're making it bigger, give up */
194 if (workspace->strm.total_in > 8192 &&
195 workspace->strm.total_in <
196 workspace->strm.total_out) {
200 /* we need another page for writing out. Test this
201 * before the total_in so we will pull in a new page for
202 * the stream end if required
204 if (workspace->strm.avail_out == 0) {
205 if (nr_folios == nr_dest_folios) {
209 out_folio = btrfs_alloc_compr_folio();
210 if (out_folio == NULL) {
214 cfolio_out = folio_address(out_folio);
215 folios[nr_folios] = out_folio;
217 workspace->strm.avail_out = PAGE_SIZE;
218 workspace->strm.next_out = cfolio_out;
221 if (workspace->strm.total_in >= len)
223 if (workspace->strm.total_out > max_out)
226 workspace->strm.avail_in = 0;
228 * Call deflate with Z_FINISH flush parameter providing more output
229 * space but no more input data, until it returns with Z_STREAM_END.
231 while (ret != Z_STREAM_END) {
232 ret = zlib_deflate(&workspace->strm, Z_FINISH);
233 if (ret == Z_STREAM_END)
235 if (ret != Z_OK && ret != Z_BUF_ERROR) {
236 zlib_deflateEnd(&workspace->strm);
239 } else if (workspace->strm.avail_out == 0) {
240 /* Get another folio for the stream end. */
241 if (nr_folios == nr_dest_folios) {
245 out_folio = btrfs_alloc_compr_folio();
246 if (out_folio == NULL) {
250 cfolio_out = folio_address(out_folio);
251 folios[nr_folios] = out_folio;
253 workspace->strm.avail_out = PAGE_SIZE;
254 workspace->strm.next_out = cfolio_out;
257 zlib_deflateEnd(&workspace->strm);
259 if (workspace->strm.total_out >= workspace->strm.total_in) {
265 *total_out = workspace->strm.total_out;
266 *total_in = workspace->strm.total_in;
268 *out_folios = nr_folios;
270 kunmap_local(data_in);
277 int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
279 struct workspace *workspace = list_entry(ws, struct workspace, list);
281 int wbits = MAX_WBITS;
283 size_t total_out = 0;
284 unsigned long folio_in_index = 0;
285 size_t srclen = cb->compressed_len;
286 unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
287 unsigned long buf_start;
288 struct folio **folios_in = cb->compressed_folios;
290 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
291 workspace->strm.next_in = data_in;
292 workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
293 workspace->strm.total_in = 0;
295 workspace->strm.total_out = 0;
296 workspace->strm.next_out = workspace->buf;
297 workspace->strm.avail_out = workspace->buf_size;
299 /* If it's deflate, and it's got no preset dictionary, then
300 we can tell zlib to skip the adler32 check. */
301 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
302 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
303 !(((data_in[0]<<8) + data_in[1]) % 31)) {
305 wbits = -((data_in[0] >> 4) + 8);
306 workspace->strm.next_in += 2;
307 workspace->strm.avail_in -= 2;
310 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
311 pr_warn("BTRFS: inflateInit failed\n");
312 kunmap_local(data_in);
315 while (workspace->strm.total_in < srclen) {
316 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
317 if (ret != Z_OK && ret != Z_STREAM_END)
320 buf_start = total_out;
321 total_out = workspace->strm.total_out;
323 /* we didn't make progress in this inflate call, we're done */
324 if (buf_start == total_out)
327 ret2 = btrfs_decompress_buf2page(workspace->buf,
328 total_out - buf_start, cb, buf_start);
334 workspace->strm.next_out = workspace->buf;
335 workspace->strm.avail_out = workspace->buf_size;
337 if (workspace->strm.avail_in == 0) {
339 kunmap_local(data_in);
341 if (folio_in_index >= total_folios_in) {
345 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
346 workspace->strm.next_in = data_in;
347 tmp = srclen - workspace->strm.total_in;
348 workspace->strm.avail_in = min(tmp, PAGE_SIZE);
351 if (ret != Z_STREAM_END)
356 zlib_inflateEnd(&workspace->strm);
358 kunmap_local(data_in);
362 int zlib_decompress(struct list_head *ws, const u8 *data_in,
363 struct page *dest_page, unsigned long dest_pgoff, size_t srclen,
366 struct workspace *workspace = list_entry(ws, struct workspace, list);
368 int wbits = MAX_WBITS;
369 unsigned long to_copy;
371 workspace->strm.next_in = data_in;
372 workspace->strm.avail_in = srclen;
373 workspace->strm.total_in = 0;
375 workspace->strm.next_out = workspace->buf;
376 workspace->strm.avail_out = workspace->buf_size;
377 workspace->strm.total_out = 0;
378 /* If it's deflate, and it's got no preset dictionary, then
379 we can tell zlib to skip the adler32 check. */
380 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
381 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
382 !(((data_in[0]<<8) + data_in[1]) % 31)) {
384 wbits = -((data_in[0] >> 4) + 8);
385 workspace->strm.next_in += 2;
386 workspace->strm.avail_in -= 2;
389 if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
390 pr_warn("BTRFS: inflateInit failed\n");
395 * Everything (in/out buf) should be at most one sector, there should
396 * be no need to switch any input/output buffer.
398 ret = zlib_inflate(&workspace->strm, Z_FINISH);
399 to_copy = min(workspace->strm.total_out, destlen);
400 if (ret != Z_STREAM_END)
403 memcpy_to_page(dest_page, dest_pgoff, workspace->buf, to_copy);
406 if (unlikely(to_copy != destlen)) {
407 pr_warn_ratelimited("BTRFS: inflate failed, decompressed=%lu expected=%zu\n",
414 zlib_inflateEnd(&workspace->strm);
416 if (unlikely(to_copy < destlen))
417 memzero_page(dest_page, dest_pgoff + to_copy, destlen - to_copy);
421 const struct btrfs_compress_op btrfs_zlib_compress = {
422 .workspace_manager = &wsm,
424 .default_level = BTRFS_ZLIB_DEFAULT_LEVEL,