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.
7 * Created by David Woodhouse <dwmw2@infradead.org>
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 "btrfs_inode.h"
22 #include "compression.h"
26 /* workspace buffer size for s390 zlib hardware support */
27 #define ZLIB_DFLTCC_BUF_SIZE (4 * PAGE_SIZE)
32 unsigned int buf_size;
33 struct list_head list;
37 static struct workspace_manager wsm;
39 struct list_head *zlib_get_workspace(unsigned int level)
41 struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
42 struct workspace *workspace = list_entry(ws, struct workspace, list);
44 workspace->level = level;
49 void zlib_free_workspace(struct list_head *ws)
51 struct workspace *workspace = list_entry(ws, struct workspace, list);
53 kvfree(workspace->strm.workspace);
54 kfree(workspace->buf);
58 struct list_head *zlib_alloc_workspace(unsigned int level)
60 struct workspace *workspace;
63 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
65 return ERR_PTR(-ENOMEM);
67 workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
68 zlib_inflate_workspacesize());
69 workspace->strm.workspace = kvzalloc(workspacesize, GFP_KERNEL | __GFP_NOWARN);
70 workspace->level = level;
71 workspace->buf = NULL;
73 * In case of s390 zlib hardware support, allocate lager workspace
74 * buffer. If allocator fails, fall back to a single page buffer.
76 if (zlib_deflate_dfltcc_enabled()) {
77 workspace->buf = kmalloc(ZLIB_DFLTCC_BUF_SIZE,
78 __GFP_NOMEMALLOC | __GFP_NORETRY |
79 __GFP_NOWARN | GFP_NOIO);
80 workspace->buf_size = ZLIB_DFLTCC_BUF_SIZE;
82 if (!workspace->buf) {
83 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
84 workspace->buf_size = PAGE_SIZE;
86 if (!workspace->strm.workspace || !workspace->buf)
89 INIT_LIST_HEAD(&workspace->list);
91 return &workspace->list;
93 zlib_free_workspace(&workspace->list);
94 return ERR_PTR(-ENOMEM);
98 * Helper for S390x with hardware zlib compression support.
100 * That hardware acceleration requires a buffer size larger than a single page
101 * to get ideal performance, thus we need to do the memory copy rather than
102 * use the page cache directly as input buffer.
104 static int copy_data_into_buffer(struct address_space *mapping,
105 struct workspace *workspace, u64 filepos,
106 unsigned long length)
110 /* It's only for hardware accelerated zlib code. */
111 ASSERT(zlib_deflate_dfltcc_enabled());
113 while (cur < filepos + length) {
117 unsigned long copy_length;
120 ret = btrfs_compress_filemap_get_folio(mapping, cur, &folio);
124 offset = offset_in_folio(folio, cur);
125 copy_length = min(folio_size(folio) - offset,
126 filepos + length - cur);
128 data_in = kmap_local_folio(folio, offset);
129 memcpy(workspace->buf + cur - filepos, data_in, copy_length);
130 kunmap_local(data_in);
136 int zlib_compress_folios(struct list_head *ws, struct address_space *mapping,
137 u64 start, struct folio **folios, unsigned long *out_folios,
138 unsigned long *total_in, unsigned long *total_out)
140 struct workspace *workspace = list_entry(ws, struct workspace, list);
142 char *data_in = NULL;
145 struct folio *in_folio = NULL;
146 struct folio *out_folio = NULL;
147 unsigned long len = *total_out;
148 unsigned long nr_dest_folios = *out_folios;
149 const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
150 const u64 orig_end = start + len;
156 ret = zlib_deflateInit(&workspace->strm, workspace->level);
157 if (unlikely(ret != Z_OK)) {
158 struct btrfs_inode *inode = BTRFS_I(mapping->host);
160 btrfs_err(inode->root->fs_info,
161 "zlib compression init failed, error %d root %llu inode %llu offset %llu",
162 ret, btrfs_root_id(inode->root), btrfs_ino(inode), start);
167 workspace->strm.total_in = 0;
168 workspace->strm.total_out = 0;
170 out_folio = btrfs_alloc_compr_folio();
171 if (out_folio == NULL) {
175 cfolio_out = folio_address(out_folio);
176 folios[0] = out_folio;
179 workspace->strm.next_in = workspace->buf;
180 workspace->strm.avail_in = 0;
181 workspace->strm.next_out = cfolio_out;
182 workspace->strm.avail_out = PAGE_SIZE;
184 while (workspace->strm.total_in < len) {
186 * Get next input pages and copy the contents to
187 * the workspace buffer if required.
189 if (workspace->strm.avail_in == 0) {
190 unsigned long bytes_left = len - workspace->strm.total_in;
191 unsigned int copy_length = min(bytes_left, workspace->buf_size);
194 * This can only happen when hardware zlib compression is
197 if (copy_length > PAGE_SIZE) {
198 ret = copy_data_into_buffer(mapping, workspace,
202 start += copy_length;
203 workspace->strm.next_in = workspace->buf;
204 workspace->strm.avail_in = copy_length;
206 unsigned int cur_len;
209 kunmap_local(data_in);
213 ret = btrfs_compress_filemap_get_folio(mapping,
217 cur_len = btrfs_calc_input_length(in_folio, orig_end, start);
218 data_in = kmap_local_folio(in_folio,
219 offset_in_folio(in_folio, start));
221 workspace->strm.next_in = data_in;
222 workspace->strm.avail_in = cur_len;
226 ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
227 if (unlikely(ret != Z_OK)) {
228 struct btrfs_inode *inode = BTRFS_I(mapping->host);
230 btrfs_warn(inode->root->fs_info,
231 "zlib compression failed, error %d root %llu inode %llu offset %llu",
232 ret, btrfs_root_id(inode->root), btrfs_ino(inode),
234 zlib_deflateEnd(&workspace->strm);
239 /* we're making it bigger, give up */
240 if (workspace->strm.total_in > 8192 &&
241 workspace->strm.total_in <
242 workspace->strm.total_out) {
246 /* we need another page for writing out. Test this
247 * before the total_in so we will pull in a new page for
248 * the stream end if required
250 if (workspace->strm.avail_out == 0) {
251 if (nr_folios == nr_dest_folios) {
255 out_folio = btrfs_alloc_compr_folio();
256 if (out_folio == NULL) {
260 cfolio_out = folio_address(out_folio);
261 folios[nr_folios] = out_folio;
263 workspace->strm.avail_out = PAGE_SIZE;
264 workspace->strm.next_out = cfolio_out;
267 if (workspace->strm.total_in >= len)
269 if (workspace->strm.total_out > max_out)
272 workspace->strm.avail_in = 0;
274 * Call deflate with Z_FINISH flush parameter providing more output
275 * space but no more input data, until it returns with Z_STREAM_END.
277 while (ret != Z_STREAM_END) {
278 ret = zlib_deflate(&workspace->strm, Z_FINISH);
279 if (ret == Z_STREAM_END)
281 if (ret != Z_OK && ret != Z_BUF_ERROR) {
282 zlib_deflateEnd(&workspace->strm);
285 } else if (workspace->strm.avail_out == 0) {
286 /* Get another folio for the stream end. */
287 if (nr_folios == nr_dest_folios) {
291 out_folio = btrfs_alloc_compr_folio();
292 if (out_folio == NULL) {
296 cfolio_out = folio_address(out_folio);
297 folios[nr_folios] = out_folio;
299 workspace->strm.avail_out = PAGE_SIZE;
300 workspace->strm.next_out = cfolio_out;
303 zlib_deflateEnd(&workspace->strm);
305 if (workspace->strm.total_out >= workspace->strm.total_in) {
311 *total_out = workspace->strm.total_out;
312 *total_in = workspace->strm.total_in;
314 *out_folios = nr_folios;
316 kunmap_local(data_in);
323 int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
325 struct workspace *workspace = list_entry(ws, struct workspace, list);
327 int wbits = MAX_WBITS;
329 size_t total_out = 0;
330 unsigned long folio_in_index = 0;
331 size_t srclen = cb->compressed_len;
332 unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
333 unsigned long buf_start;
334 struct folio **folios_in = cb->compressed_folios;
336 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
337 workspace->strm.next_in = data_in;
338 workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
339 workspace->strm.total_in = 0;
341 workspace->strm.total_out = 0;
342 workspace->strm.next_out = workspace->buf;
343 workspace->strm.avail_out = workspace->buf_size;
345 /* If it's deflate, and it's got no preset dictionary, then
346 we can tell zlib to skip the adler32 check. */
347 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
348 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
349 !(((data_in[0]<<8) + data_in[1]) % 31)) {
351 wbits = -((data_in[0] >> 4) + 8);
352 workspace->strm.next_in += 2;
353 workspace->strm.avail_in -= 2;
356 ret = zlib_inflateInit2(&workspace->strm, wbits);
357 if (unlikely(ret != Z_OK)) {
358 struct btrfs_inode *inode = cb->bbio.inode;
360 kunmap_local(data_in);
361 btrfs_err(inode->root->fs_info,
362 "zlib decompression init failed, error %d root %llu inode %llu offset %llu",
363 ret, btrfs_root_id(inode->root), btrfs_ino(inode), cb->start);
366 while (workspace->strm.total_in < srclen) {
367 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
368 if (ret != Z_OK && ret != Z_STREAM_END)
371 buf_start = total_out;
372 total_out = workspace->strm.total_out;
374 /* we didn't make progress in this inflate call, we're done */
375 if (buf_start == total_out)
378 ret2 = btrfs_decompress_buf2page(workspace->buf,
379 total_out - buf_start, cb, buf_start);
385 workspace->strm.next_out = workspace->buf;
386 workspace->strm.avail_out = workspace->buf_size;
388 if (workspace->strm.avail_in == 0) {
390 kunmap_local(data_in);
392 if (folio_in_index >= total_folios_in) {
396 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
397 workspace->strm.next_in = data_in;
398 tmp = srclen - workspace->strm.total_in;
399 workspace->strm.avail_in = min(tmp, PAGE_SIZE);
402 if (unlikely(ret != Z_STREAM_END)) {
403 btrfs_err(cb->bbio.inode->root->fs_info,
404 "zlib decompression failed, error %d root %llu inode %llu offset %llu",
405 ret, btrfs_root_id(cb->bbio.inode->root),
406 btrfs_ino(cb->bbio.inode), cb->start);
412 zlib_inflateEnd(&workspace->strm);
414 kunmap_local(data_in);
418 int zlib_decompress(struct list_head *ws, const u8 *data_in,
419 struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
422 struct workspace *workspace = list_entry(ws, struct workspace, list);
424 int wbits = MAX_WBITS;
425 unsigned long to_copy;
427 workspace->strm.next_in = data_in;
428 workspace->strm.avail_in = srclen;
429 workspace->strm.total_in = 0;
431 workspace->strm.next_out = workspace->buf;
432 workspace->strm.avail_out = workspace->buf_size;
433 workspace->strm.total_out = 0;
434 /* If it's deflate, and it's got no preset dictionary, then
435 we can tell zlib to skip the adler32 check. */
436 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
437 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
438 !(((data_in[0]<<8) + data_in[1]) % 31)) {
440 wbits = -((data_in[0] >> 4) + 8);
441 workspace->strm.next_in += 2;
442 workspace->strm.avail_in -= 2;
445 ret = zlib_inflateInit2(&workspace->strm, wbits);
446 if (unlikely(ret != Z_OK)) {
447 struct btrfs_inode *inode = folio_to_inode(dest_folio);
449 btrfs_err(inode->root->fs_info,
450 "zlib decompression init failed, error %d root %llu inode %llu offset %llu",
451 ret, btrfs_root_id(inode->root), btrfs_ino(inode),
452 folio_pos(dest_folio));
457 * Everything (in/out buf) should be at most one sector, there should
458 * be no need to switch any input/output buffer.
460 ret = zlib_inflate(&workspace->strm, Z_FINISH);
461 to_copy = min(workspace->strm.total_out, destlen);
462 if (ret != Z_STREAM_END)
465 memcpy_to_folio(dest_folio, dest_pgoff, workspace->buf, to_copy);
468 if (unlikely(to_copy != destlen)) {
469 struct btrfs_inode *inode = folio_to_inode(dest_folio);
471 btrfs_err(inode->root->fs_info,
472 "zlib decompression failed, error %d root %llu inode %llu offset %llu decompressed %lu expected %zu",
473 ret, btrfs_root_id(inode->root), btrfs_ino(inode),
474 folio_pos(dest_folio), to_copy, destlen);
480 zlib_inflateEnd(&workspace->strm);
482 if (unlikely(to_copy < destlen))
483 folio_zero_range(dest_folio, dest_pgoff + to_copy, destlen - to_copy);
487 const struct btrfs_compress_op btrfs_zlib_compress = {
488 .workspace_manager = &wsm,
491 .default_level = BTRFS_ZLIB_DEFAULT_LEVEL,