Merge tag 'net-6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-block.git] / fs / btrfs / zlib.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
c8b97818
CM
2/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
4 *
c8b97818
CM
5 * Based on jffs2 zlib code:
6 * Copyright © 2001-2007 Red Hat, Inc.
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 */
9
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/zlib.h>
13#include <linux/zutil.h>
6acafd1e 14#include <linux/mm.h>
c8b97818
CM
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>
e1ddce71 20#include <linux/refcount.h>
be9438f0 21#include "btrfs_inode.h"
b2950863 22#include "compression.h"
fd1e75d0
QW
23#include "fs.h"
24#include "subpage.h"
c8b97818 25
3fd396af
MZ
26/* workspace buffer size for s390 zlib hardware support */
27#define ZLIB_DFLTCC_BUF_SIZE (4 * PAGE_SIZE)
28
c8b97818 29struct workspace {
78809913 30 z_stream strm;
c8b97818 31 char *buf;
3fd396af 32 unsigned int buf_size;
c8b97818 33 struct list_head list;
f51d2b59 34 int level;
c8b97818
CM
35};
36
92ee5530
DZ
37static struct workspace_manager wsm;
38
d20f395f 39struct list_head *zlib_get_workspace(unsigned int level)
92ee5530 40{
5907a9bb 41 struct list_head *ws = btrfs_get_workspace(BTRFS_COMPRESS_ZLIB, level);
d0ab62ce
DZ
42 struct workspace *workspace = list_entry(ws, struct workspace, list);
43
44 workspace->level = level;
45
46 return ws;
92ee5530
DZ
47}
48
d20f395f 49void zlib_free_workspace(struct list_head *ws)
261507a0
LZ
50{
51 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818 52
6acafd1e 53 kvfree(workspace->strm.workspace);
261507a0
LZ
54 kfree(workspace->buf);
55 kfree(workspace);
56}
57
d20f395f 58struct list_head *zlib_alloc_workspace(unsigned int level)
c8b97818
CM
59{
60 struct workspace *workspace;
78809913 61 int workspacesize;
8844355d 62
389a6cfc 63 workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
261507a0
LZ
64 if (!workspace)
65 return ERR_PTR(-ENOMEM);
c8b97818 66
78809913
SS
67 workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
68 zlib_inflate_workspacesize());
8ab546bb 69 workspace->strm.workspace = kvzalloc(workspacesize, GFP_KERNEL | __GFP_NOWARN);
7bf49943 70 workspace->level = level;
3fd396af
MZ
71 workspace->buf = NULL;
72 /*
73 * In case of s390 zlib hardware support, allocate lager workspace
74 * buffer. If allocator fails, fall back to a single page buffer.
75 */
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;
81 }
82 if (!workspace->buf) {
83 workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
84 workspace->buf_size = PAGE_SIZE;
85 }
78809913 86 if (!workspace->strm.workspace || !workspace->buf)
261507a0 87 goto fail;
c8b97818 88
261507a0 89 INIT_LIST_HEAD(&workspace->list);
c8b97818 90
261507a0
LZ
91 return &workspace->list;
92fail:
93 zlib_free_workspace(&workspace->list);
94 return ERR_PTR(-ENOMEM);
c8b97818
CM
95}
96
aa60fe12
QW
97/*
98 * Helper for S390x with hardware zlib compression support.
99 *
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.
103 */
104static int copy_data_into_buffer(struct address_space *mapping,
105 struct workspace *workspace, u64 filepos,
106 unsigned long length)
107{
108 u64 cur = filepos;
109
110 /* It's only for hardware accelerated zlib code. */
111 ASSERT(zlib_deflate_dfltcc_enabled());
112
113 while (cur < filepos + length) {
114 struct folio *folio;
115 void *data_in;
116 unsigned int offset;
117 unsigned long copy_length;
118 int ret;
119
120 ret = btrfs_compress_filemap_get_folio(mapping, cur, &folio);
121 if (ret < 0)
122 return ret;
aa60fe12
QW
123
124 offset = offset_in_folio(folio, cur);
125 copy_length = min(folio_size(folio) - offset,
126 filepos + length - cur);
127
128 data_in = kmap_local_folio(folio, offset);
129 memcpy(workspace->buf + cur - filepos, data_in, copy_length);
130 kunmap_local(data_in);
131 cur += copy_length;
132 }
133 return 0;
134}
135
400b172b
QW
136int 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)
c8b97818 139{
261507a0 140 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818 141 int ret;
718e5855 142 char *data_in = NULL;
400b172b
QW
143 char *cfolio_out;
144 int nr_folios = 0;
145 struct folio *in_folio = NULL;
146 struct folio *out_folio = NULL;
38c31464 147 unsigned long len = *total_out;
400b172b
QW
148 unsigned long nr_dest_folios = *out_folios;
149 const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
fd1e75d0 150 const u64 orig_end = start + len;
c8b97818 151
400b172b 152 *out_folios = 0;
c8b97818
CM
153 *total_out = 0;
154 *total_in = 0;
155
be9438f0
DS
156 ret = zlib_deflateInit(&workspace->strm, workspace->level);
157 if (unlikely(ret != Z_OK)) {
158 struct btrfs_inode *inode = BTRFS_I(mapping->host);
159
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);
60e1975a 163 ret = -EIO;
c8b97818
CM
164 goto out;
165 }
166
78809913
SS
167 workspace->strm.total_in = 0;
168 workspace->strm.total_out = 0;
c8b97818 169
400b172b
QW
170 out_folio = btrfs_alloc_compr_folio();
171 if (out_folio == NULL) {
60e1975a 172 ret = -ENOMEM;
4b72029d
LZ
173 goto out;
174 }
400b172b
QW
175 cfolio_out = folio_address(out_folio);
176 folios[0] = out_folio;
177 nr_folios = 1;
c8b97818 178
3fd396af
MZ
179 workspace->strm.next_in = workspace->buf;
180 workspace->strm.avail_in = 0;
400b172b 181 workspace->strm.next_out = cfolio_out;
09cbfeaf 182 workspace->strm.avail_out = PAGE_SIZE;
c8b97818 183
78809913 184 while (workspace->strm.total_in < len) {
3fd396af
MZ
185 /*
186 * Get next input pages and copy the contents to
187 * the workspace buffer if required.
188 */
189 if (workspace->strm.avail_in == 0) {
aa60fe12
QW
190 unsigned long bytes_left = len - workspace->strm.total_in;
191 unsigned int copy_length = min(bytes_left, workspace->buf_size);
192
193 /*
194 * This can only happen when hardware zlib compression is
195 * enabled.
196 */
197 if (copy_length > PAGE_SIZE) {
198 ret = copy_data_into_buffer(mapping, workspace,
199 start, copy_length);
200 if (ret < 0)
201 goto out;
202 start += copy_length;
3fd396af 203 workspace->strm.next_in = workspace->buf;
aa60fe12 204 workspace->strm.avail_in = copy_length;
3fd396af 205 } else {
fd1e75d0
QW
206 unsigned int cur_len;
207
718e5855
FDF
208 if (data_in) {
209 kunmap_local(data_in);
400b172b 210 folio_put(in_folio);
6de35954 211 data_in = NULL;
55276e14 212 }
400b172b
QW
213 ret = btrfs_compress_filemap_get_folio(mapping,
214 start, &in_folio);
6de35954
QW
215 if (ret < 0)
216 goto out;
7bf9bfa9
QW
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));
f6ebedb0 220 start += cur_len;
3fd396af 221 workspace->strm.next_in = data_in;
fd1e75d0 222 workspace->strm.avail_in = cur_len;
3fd396af 223 }
3fd396af
MZ
224 }
225
78809913 226 ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
be9438f0
DS
227 if (unlikely(ret != Z_OK)) {
228 struct btrfs_inode *inode = BTRFS_I(mapping->host);
229
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),
233 start);
78809913 234 zlib_deflateEnd(&workspace->strm);
60e1975a 235 ret = -EIO;
c8b97818
CM
236 goto out;
237 }
238
239 /* we're making it bigger, give up */
78809913
SS
240 if (workspace->strm.total_in > 8192 &&
241 workspace->strm.total_in <
242 workspace->strm.total_out) {
130d5b41 243 ret = -E2BIG;
c8b97818
CM
244 goto out;
245 }
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
249 */
78809913 250 if (workspace->strm.avail_out == 0) {
400b172b 251 if (nr_folios == nr_dest_folios) {
60e1975a 252 ret = -E2BIG;
c8b97818
CM
253 goto out;
254 }
400b172b
QW
255 out_folio = btrfs_alloc_compr_folio();
256 if (out_folio == NULL) {
60e1975a 257 ret = -ENOMEM;
4b72029d
LZ
258 goto out;
259 }
400b172b
QW
260 cfolio_out = folio_address(out_folio);
261 folios[nr_folios] = out_folio;
262 nr_folios++;
09cbfeaf 263 workspace->strm.avail_out = PAGE_SIZE;
400b172b 264 workspace->strm.next_out = cfolio_out;
c8b97818
CM
265 }
266 /* we're all done */
78809913 267 if (workspace->strm.total_in >= len)
c8b97818 268 break;
3fd396af
MZ
269 if (workspace->strm.total_out > max_out)
270 break;
c8b97818 271 }
78809913 272 workspace->strm.avail_in = 0;
3fd396af
MZ
273 /*
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.
276 */
277 while (ret != Z_STREAM_END) {
278 ret = zlib_deflate(&workspace->strm, Z_FINISH);
279 if (ret == Z_STREAM_END)
280 break;
281 if (ret != Z_OK && ret != Z_BUF_ERROR) {
282 zlib_deflateEnd(&workspace->strm);
283 ret = -EIO;
284 goto out;
285 } else if (workspace->strm.avail_out == 0) {
400b172b
QW
286 /* Get another folio for the stream end. */
287 if (nr_folios == nr_dest_folios) {
3fd396af
MZ
288 ret = -E2BIG;
289 goto out;
290 }
400b172b
QW
291 out_folio = btrfs_alloc_compr_folio();
292 if (out_folio == NULL) {
3fd396af
MZ
293 ret = -ENOMEM;
294 goto out;
295 }
400b172b
QW
296 cfolio_out = folio_address(out_folio);
297 folios[nr_folios] = out_folio;
298 nr_folios++;
3fd396af 299 workspace->strm.avail_out = PAGE_SIZE;
400b172b 300 workspace->strm.next_out = cfolio_out;
3fd396af 301 }
c8b97818 302 }
3fd396af 303 zlib_deflateEnd(&workspace->strm);
c8b97818 304
78809913 305 if (workspace->strm.total_out >= workspace->strm.total_in) {
60e1975a 306 ret = -E2BIG;
c8b97818
CM
307 goto out;
308 }
309
310 ret = 0;
78809913
SS
311 *total_out = workspace->strm.total_out;
312 *total_in = workspace->strm.total_in;
c8b97818 313out:
400b172b 314 *out_folios = nr_folios;
718e5855
FDF
315 if (data_in) {
316 kunmap_local(data_in);
400b172b 317 folio_put(in_folio);
55276e14 318 }
718e5855 319
c8b97818
CM
320 return ret;
321}
322
c4bf665a 323int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
c8b97818 324{
261507a0 325 struct workspace *workspace = list_entry(ws, struct workspace, list);
3a39c18d 326 int ret = 0, ret2;
c8b97818 327 int wbits = MAX_WBITS;
c8b97818
CM
328 char *data_in;
329 size_t total_out = 0;
400b172b 330 unsigned long folio_in_index = 0;
e1ddce71 331 size_t srclen = cb->compressed_len;
400b172b 332 unsigned long total_folios_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
c8b97818 333 unsigned long buf_start;
400b172b 334 struct folio **folios_in = cb->compressed_folios;
c8b97818 335
400b172b 336 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
78809913 337 workspace->strm.next_in = data_in;
09cbfeaf 338 workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
78809913 339 workspace->strm.total_in = 0;
c8b97818 340
78809913
SS
341 workspace->strm.total_out = 0;
342 workspace->strm.next_out = workspace->buf;
3fd396af 343 workspace->strm.avail_out = workspace->buf_size;
c8b97818
CM
344
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)) {
350
351 wbits = -((data_in[0] >> 4) + 8);
78809913
SS
352 workspace->strm.next_in += 2;
353 workspace->strm.avail_in -= 2;
c8b97818
CM
354 }
355
be9438f0
DS
356 ret = zlib_inflateInit2(&workspace->strm, wbits);
357 if (unlikely(ret != Z_OK)) {
358 struct btrfs_inode *inode = cb->bbio.inode;
359
5a6e6e7c 360 kunmap_local(data_in);
be9438f0
DS
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);
60e1975a 364 return -EIO;
c8b97818 365 }
78809913
SS
366 while (workspace->strm.total_in < srclen) {
367 ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
d397712b 368 if (ret != Z_OK && ret != Z_STREAM_END)
c8b97818 369 break;
c8b97818 370
3a39c18d 371 buf_start = total_out;
78809913 372 total_out = workspace->strm.total_out;
c8b97818 373
3a39c18d
LZ
374 /* we didn't make progress in this inflate call, we're done */
375 if (buf_start == total_out)
c8b97818 376 break;
c8b97818 377
1c3dc173
QW
378 ret2 = btrfs_decompress_buf2page(workspace->buf,
379 total_out - buf_start, cb, buf_start);
3a39c18d
LZ
380 if (ret2 == 0) {
381 ret = 0;
382 goto done;
c8b97818 383 }
3a39c18d 384
78809913 385 workspace->strm.next_out = workspace->buf;
3fd396af 386 workspace->strm.avail_out = workspace->buf_size;
c8b97818 387
78809913 388 if (workspace->strm.avail_in == 0) {
c8b97818 389 unsigned long tmp;
5a6e6e7c 390 kunmap_local(data_in);
400b172b
QW
391 folio_in_index++;
392 if (folio_in_index >= total_folios_in) {
c8b97818
CM
393 data_in = NULL;
394 break;
395 }
400b172b 396 data_in = kmap_local_folio(folios_in[folio_in_index], 0);
78809913
SS
397 workspace->strm.next_in = data_in;
398 tmp = srclen - workspace->strm.total_in;
1c3dc173 399 workspace->strm.avail_in = min(tmp, PAGE_SIZE);
c8b97818
CM
400 }
401 }
be9438f0
DS
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);
60e1975a 407 ret = -EIO;
be9438f0 408 } else {
c8b97818 409 ret = 0;
be9438f0 410 }
c8b97818 411done:
78809913 412 zlib_inflateEnd(&workspace->strm);
55276e14 413 if (data_in)
5a6e6e7c 414 kunmap_local(data_in);
c8b97818
CM
415 return ret;
416}
417
3e09b5b2 418int zlib_decompress(struct list_head *ws, const u8 *data_in,
54c78d49 419 struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
c4bf665a 420 size_t destlen)
c8b97818 421{
261507a0 422 struct workspace *workspace = list_entry(ws, struct workspace, list);
c8b97818
CM
423 int ret = 0;
424 int wbits = MAX_WBITS;
2c25716d 425 unsigned long to_copy;
2f19cad9 426
78809913
SS
427 workspace->strm.next_in = data_in;
428 workspace->strm.avail_in = srclen;
429 workspace->strm.total_in = 0;
c8b97818 430
78809913 431 workspace->strm.next_out = workspace->buf;
3fd396af 432 workspace->strm.avail_out = workspace->buf_size;
78809913 433 workspace->strm.total_out = 0;
c8b97818
CM
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)) {
439
440 wbits = -((data_in[0] >> 4) + 8);
78809913
SS
441 workspace->strm.next_in += 2;
442 workspace->strm.avail_in -= 2;
c8b97818
CM
443 }
444
be9438f0
DS
445 ret = zlib_inflateInit2(&workspace->strm, wbits);
446 if (unlikely(ret != Z_OK)) {
54c78d49 447 struct btrfs_inode *inode = folio_to_inode(dest_folio);
be9438f0
DS
448
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),
54c78d49 452 folio_pos(dest_folio));
60e1975a 453 return -EIO;
c8b97818
CM
454 }
455
2c25716d
QW
456 /*
457 * Everything (in/out buf) should be at most one sector, there should
458 * be no need to switch any input/output buffer.
459 */
460 ret = zlib_inflate(&workspace->strm, Z_FINISH);
461 to_copy = min(workspace->strm.total_out, destlen);
462 if (ret != Z_STREAM_END)
463 goto out;
c8b97818 464
54c78d49 465 memcpy_to_folio(dest_folio, dest_pgoff, workspace->buf, to_copy);
c8b97818 466
2c25716d
QW
467out:
468 if (unlikely(to_copy != destlen)) {
54c78d49 469 struct btrfs_inode *inode = folio_to_inode(dest_folio);
be9438f0
DS
470
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),
54c78d49 474 folio_pos(dest_folio), to_copy, destlen);
60e1975a 475 ret = -EIO;
2c25716d 476 } else {
c8b97818 477 ret = 0;
2c25716d 478 }
d397712b 479
78809913 480 zlib_inflateEnd(&workspace->strm);
2f19cad9 481
2c25716d 482 if (unlikely(to_copy < destlen))
54c78d49 483 folio_zero_range(dest_folio, dest_pgoff + to_copy, destlen - to_copy);
c8b97818
CM
484 return ret;
485}
486
e8c9f186 487const struct btrfs_compress_op btrfs_zlib_compress = {
be951045 488 .workspace_manager = &wsm,
da798fa5 489 .min_level = 1,
e18333a7
DS
490 .max_level = 9,
491 .default_level = BTRFS_ZLIB_DEFAULT_LEVEL,
261507a0 492};