1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/zlib.h>
11 #include <linux/zstd.h>
25 static struct bbuf __bounce_alloc(struct bch_fs *c, unsigned size, int rw)
29 BUG_ON(size > c->sb.encoded_extent_max << 9);
31 b = kmalloc(size, GFP_NOIO|__GFP_NOWARN);
33 return (struct bbuf) { .b = b, .type = BB_KMALLOC, .rw = rw };
35 b = mempool_alloc(&c->compression_bounce[rw], GFP_NOIO);
37 return (struct bbuf) { .b = b, .type = BB_MEMPOOL, .rw = rw };
42 static bool bio_phys_contig(struct bio *bio, struct bvec_iter start)
45 struct bvec_iter iter;
46 void *expected_start = NULL;
48 __bio_for_each_bvec(bv, bio, iter, start) {
50 expected_start != page_address(bv.bv_page) + bv.bv_offset)
53 expected_start = page_address(bv.bv_page) +
54 bv.bv_offset + bv.bv_len;
60 static struct bbuf __bio_map_or_bounce(struct bch_fs *c, struct bio *bio,
61 struct bvec_iter start, int rw)
65 struct bvec_iter iter;
66 unsigned nr_pages = 0;
67 struct page *stack_pages[16];
68 struct page **pages = NULL;
71 BUG_ON(bvec_iter_sectors(start) > c->sb.encoded_extent_max);
73 if (!IS_ENABLED(CONFIG_HIGHMEM) &&
74 bio_phys_contig(bio, start))
75 return (struct bbuf) {
76 .b = page_address(bio_iter_page(bio, start)) +
77 bio_iter_offset(bio, start),
78 .type = BB_NONE, .rw = rw
81 /* check if we can map the pages contiguously: */
82 __bio_for_each_segment(bv, bio, iter, start) {
83 if (iter.bi_size != start.bi_size &&
87 if (bv.bv_len < iter.bi_size &&
88 bv.bv_offset + bv.bv_len < PAGE_SIZE)
94 BUG_ON(DIV_ROUND_UP(start.bi_size, PAGE_SIZE) > nr_pages);
96 pages = nr_pages > ARRAY_SIZE(stack_pages)
97 ? kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOIO)
103 __bio_for_each_segment(bv, bio, iter, start)
104 pages[nr_pages++] = bv.bv_page;
106 data = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
107 if (pages != stack_pages)
111 return (struct bbuf) {
112 .b = data + bio_iter_offset(bio, start),
113 .type = BB_VMAP, .rw = rw
116 ret = __bounce_alloc(c, start.bi_size, rw);
119 memcpy_from_bio(ret.b, bio, start);
124 static struct bbuf bio_map_or_bounce(struct bch_fs *c, struct bio *bio, int rw)
126 return __bio_map_or_bounce(c, bio, bio->bi_iter, rw);
129 static void bio_unmap_or_unbounce(struct bch_fs *c, struct bbuf buf)
135 vunmap((void *) ((unsigned long) buf.b & PAGE_MASK));
141 mempool_free(buf.b, &c->compression_bounce[buf.rw]);
146 static inline void zlib_set_workspace(z_stream *strm, void *workspace)
149 strm->workspace = workspace;
153 static int __bio_uncompress(struct bch_fs *c, struct bio *src,
154 void *dst_data, struct bch_extent_crc_unpacked crc)
156 struct bbuf src_data = { NULL };
157 size_t src_len = src->bi_iter.bi_size;
158 size_t dst_len = crc.uncompressed_size << 9;
162 src_data = bio_map_or_bounce(c, src, READ);
164 switch (crc.compression_type) {
165 case BCH_COMPRESSION_TYPE_lz4_old:
166 case BCH_COMPRESSION_TYPE_lz4:
167 ret = LZ4_decompress_safe_partial(src_data.b, dst_data,
168 src_len, dst_len, dst_len);
172 case BCH_COMPRESSION_TYPE_gzip: {
174 .next_in = src_data.b,
176 .next_out = dst_data,
177 .avail_out = dst_len,
180 workspace = mempool_alloc(&c->decompress_workspace, GFP_NOIO);
182 zlib_set_workspace(&strm, workspace);
183 zlib_inflateInit2(&strm, -MAX_WBITS);
184 ret = zlib_inflate(&strm, Z_FINISH);
186 mempool_free(workspace, &c->decompress_workspace);
188 if (ret != Z_STREAM_END)
192 case BCH_COMPRESSION_TYPE_zstd: {
194 size_t real_src_len = le32_to_cpup(src_data.b);
196 if (real_src_len > src_len - 4)
199 workspace = mempool_alloc(&c->decompress_workspace, GFP_NOIO);
200 ctx = ZSTD_initDCtx(workspace, ZSTD_DCtxWorkspaceBound());
202 ret = ZSTD_decompressDCtx(ctx,
204 src_data.b + 4, real_src_len);
206 mempool_free(workspace, &c->decompress_workspace);
217 bio_unmap_or_unbounce(c, src_data);
224 int bch2_bio_uncompress_inplace(struct bch_fs *c, struct bio *bio,
225 struct bch_extent_crc_unpacked *crc)
227 struct bbuf data = { NULL };
228 size_t dst_len = crc->uncompressed_size << 9;
230 /* bio must own its pages: */
231 BUG_ON(!bio->bi_vcnt);
232 BUG_ON(DIV_ROUND_UP(crc->live_size, PAGE_SECTORS) > bio->bi_max_vecs);
234 if (crc->uncompressed_size > c->sb.encoded_extent_max ||
235 crc->compressed_size > c->sb.encoded_extent_max) {
236 bch_err(c, "error rewriting existing data: extent too big");
240 data = __bounce_alloc(c, dst_len, WRITE);
242 if (__bio_uncompress(c, bio, data.b, *crc)) {
243 bch_err(c, "error rewriting existing data: decompression error");
244 bio_unmap_or_unbounce(c, data);
249 * XXX: don't have a good way to assert that the bio was allocated with
250 * enough space, we depend on bch2_move_extent doing the right thing
252 bio->bi_iter.bi_size = crc->live_size << 9;
254 memcpy_to_bio(bio, bio->bi_iter, data.b + (crc->offset << 9));
257 crc->compression_type = 0;
258 crc->compressed_size = crc->live_size;
259 crc->uncompressed_size = crc->live_size;
261 crc->csum = (struct bch_csum) { 0, 0 };
263 bio_unmap_or_unbounce(c, data);
267 int bch2_bio_uncompress(struct bch_fs *c, struct bio *src,
268 struct bio *dst, struct bvec_iter dst_iter,
269 struct bch_extent_crc_unpacked crc)
271 struct bbuf dst_data = { NULL };
272 size_t dst_len = crc.uncompressed_size << 9;
275 if (crc.uncompressed_size > c->sb.encoded_extent_max ||
276 crc.compressed_size > c->sb.encoded_extent_max)
279 dst_data = dst_len == dst_iter.bi_size
280 ? __bio_map_or_bounce(c, dst, dst_iter, WRITE)
281 : __bounce_alloc(c, dst_len, WRITE);
283 ret = __bio_uncompress(c, src, dst_data.b, crc);
287 if (dst_data.type != BB_NONE &&
288 dst_data.type != BB_VMAP)
289 memcpy_to_bio(dst, dst_iter, dst_data.b + (crc.offset << 9));
291 bio_unmap_or_unbounce(c, dst_data);
295 static int attempt_compress(struct bch_fs *c,
297 void *dst, size_t dst_len,
298 void *src, size_t src_len,
299 enum bch_compression_type compression_type)
301 switch (compression_type) {
302 case BCH_COMPRESSION_TYPE_lz4: {
304 int ret = LZ4_compress_destSize(
314 case BCH_COMPRESSION_TYPE_gzip: {
319 .avail_out = dst_len,
322 zlib_set_workspace(&strm, workspace);
323 zlib_deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
324 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL,
327 if (zlib_deflate(&strm, Z_FINISH) != Z_STREAM_END)
330 if (zlib_deflateEnd(&strm) != Z_OK)
333 return strm.total_out;
335 case BCH_COMPRESSION_TYPE_zstd: {
336 ZSTD_CCtx *ctx = ZSTD_initCCtx(workspace,
337 ZSTD_CCtxWorkspaceBound(c->zstd_params.cParams));
339 size_t len = ZSTD_compressCCtx(ctx,
340 dst + 4, dst_len - 4,
343 if (ZSTD_isError(len))
346 *((__le32 *) dst) = cpu_to_le32(len);
354 static unsigned __bio_compress(struct bch_fs *c,
355 struct bio *dst, size_t *dst_len,
356 struct bio *src, size_t *src_len,
357 enum bch_compression_type compression_type)
359 struct bbuf src_data = { NULL }, dst_data = { NULL };
364 BUG_ON(compression_type >= BCH_COMPRESSION_TYPE_NR);
365 BUG_ON(!mempool_initialized(&c->compress_workspace[compression_type]));
367 /* If it's only one block, don't bother trying to compress: */
368 if (bio_sectors(src) <= c->opts.block_size)
371 dst_data = bio_map_or_bounce(c, dst, WRITE);
372 src_data = bio_map_or_bounce(c, src, READ);
374 workspace = mempool_alloc(&c->compress_workspace[compression_type], GFP_NOIO);
376 *src_len = src->bi_iter.bi_size;
377 *dst_len = dst->bi_iter.bi_size;
380 * XXX: this algorithm sucks when the compression code doesn't tell us
381 * how much would fit, like LZ4 does:
384 if (*src_len <= block_bytes(c)) {
389 ret = attempt_compress(c, workspace,
390 dst_data.b, *dst_len,
391 src_data.b, *src_len,
399 /* Didn't fit: should we retry with a smaller amount? */
400 if (*src_len <= *dst_len) {
406 * If ret is negative, it's a hint as to how much data would fit
408 BUG_ON(-ret >= *src_len);
413 *src_len -= (*src_len - *dst_len) / 2;
414 *src_len = round_down(*src_len, block_bytes(c));
417 mempool_free(workspace, &c->compress_workspace[compression_type]);
422 /* Didn't get smaller: */
423 if (round_up(*dst_len, block_bytes(c)) >= *src_len)
426 pad = round_up(*dst_len, block_bytes(c)) - *dst_len;
428 memset(dst_data.b + *dst_len, 0, pad);
431 if (dst_data.type != BB_NONE &&
432 dst_data.type != BB_VMAP)
433 memcpy_to_bio(dst, dst->bi_iter, dst_data.b);
435 BUG_ON(!*dst_len || *dst_len > dst->bi_iter.bi_size);
436 BUG_ON(!*src_len || *src_len > src->bi_iter.bi_size);
437 BUG_ON(*dst_len & (block_bytes(c) - 1));
438 BUG_ON(*src_len & (block_bytes(c) - 1));
440 bio_unmap_or_unbounce(c, src_data);
441 bio_unmap_or_unbounce(c, dst_data);
442 return compression_type;
444 compression_type = BCH_COMPRESSION_TYPE_incompressible;
448 unsigned bch2_bio_compress(struct bch_fs *c,
449 struct bio *dst, size_t *dst_len,
450 struct bio *src, size_t *src_len,
451 unsigned compression_type)
453 unsigned orig_dst = dst->bi_iter.bi_size;
454 unsigned orig_src = src->bi_iter.bi_size;
456 /* Don't consume more than BCH_ENCODED_EXTENT_MAX from @src: */
457 src->bi_iter.bi_size = min_t(unsigned, src->bi_iter.bi_size,
458 c->sb.encoded_extent_max << 9);
459 /* Don't generate a bigger output than input: */
460 dst->bi_iter.bi_size = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
462 if (compression_type == BCH_COMPRESSION_TYPE_lz4_old)
463 compression_type = BCH_COMPRESSION_TYPE_lz4;
466 __bio_compress(c, dst, dst_len, src, src_len, compression_type);
468 dst->bi_iter.bi_size = orig_dst;
469 src->bi_iter.bi_size = orig_src;
470 return compression_type;
473 static int __bch2_fs_compress_init(struct bch_fs *, u64);
475 #define BCH_FEATURE_none 0
477 static const unsigned bch2_compression_opt_to_feature[] = {
478 #define x(t, n) [BCH_COMPRESSION_OPT_##t] = BCH_FEATURE_##t,
479 BCH_COMPRESSION_OPTS()
483 #undef BCH_FEATURE_none
485 static int __bch2_check_set_has_compressed_data(struct bch_fs *c, u64 f)
489 if ((c->sb.features & f) == f)
492 mutex_lock(&c->sb_lock);
494 if ((c->sb.features & f) == f) {
495 mutex_unlock(&c->sb_lock);
499 ret = __bch2_fs_compress_init(c, c->sb.features|f);
501 mutex_unlock(&c->sb_lock);
505 c->disk_sb.sb->features[0] |= cpu_to_le64(f);
507 mutex_unlock(&c->sb_lock);
512 int bch2_check_set_has_compressed_data(struct bch_fs *c,
513 unsigned compression_type)
515 BUG_ON(compression_type >= ARRAY_SIZE(bch2_compression_opt_to_feature));
517 return compression_type
518 ? __bch2_check_set_has_compressed_data(c,
519 1ULL << bch2_compression_opt_to_feature[compression_type])
523 void bch2_fs_compress_exit(struct bch_fs *c)
527 mempool_exit(&c->decompress_workspace);
528 for (i = 0; i < ARRAY_SIZE(c->compress_workspace); i++)
529 mempool_exit(&c->compress_workspace[i]);
530 mempool_exit(&c->compression_bounce[WRITE]);
531 mempool_exit(&c->compression_bounce[READ]);
534 static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)
536 size_t max_extent = c->sb.encoded_extent_max << 9;
537 size_t decompress_workspace_size = 0;
538 bool decompress_workspace_needed;
539 ZSTD_parameters params = ZSTD_getParams(0, max_extent, 0);
543 size_t compress_workspace;
544 size_t decompress_workspace;
545 } compression_types[] = {
546 { BCH_FEATURE_lz4, BCH_COMPRESSION_TYPE_lz4, LZ4_MEM_COMPRESS, 0 },
547 { BCH_FEATURE_gzip, BCH_COMPRESSION_TYPE_gzip,
548 zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL),
549 zlib_inflate_workspacesize(), },
550 { BCH_FEATURE_zstd, BCH_COMPRESSION_TYPE_zstd,
551 ZSTD_CCtxWorkspaceBound(params.cParams),
552 ZSTD_DCtxWorkspaceBound() },
556 pr_verbose_init(c->opts, "");
558 c->zstd_params = params;
560 for (i = compression_types;
561 i < compression_types + ARRAY_SIZE(compression_types);
563 if (features & (1 << i->feature))
564 goto have_compressed;
569 if (!mempool_initialized(&c->compression_bounce[READ])) {
570 ret = mempool_init_kvpmalloc_pool(&c->compression_bounce[READ],
576 if (!mempool_initialized(&c->compression_bounce[WRITE])) {
577 ret = mempool_init_kvpmalloc_pool(&c->compression_bounce[WRITE],
583 for (i = compression_types;
584 i < compression_types + ARRAY_SIZE(compression_types);
586 decompress_workspace_size =
587 max(decompress_workspace_size, i->decompress_workspace);
589 if (!(features & (1 << i->feature)))
592 if (i->decompress_workspace)
593 decompress_workspace_needed = true;
595 if (mempool_initialized(&c->compress_workspace[i->type]))
598 ret = mempool_init_kvpmalloc_pool(
599 &c->compress_workspace[i->type],
600 1, i->compress_workspace);
605 if (!mempool_initialized(&c->decompress_workspace)) {
606 ret = mempool_init_kvpmalloc_pool(
607 &c->decompress_workspace,
608 1, decompress_workspace_size);
613 pr_verbose_init(c->opts, "ret %i", ret);
617 int bch2_fs_compress_init(struct bch_fs *c)
619 u64 f = c->sb.features;
621 if (c->opts.compression)
622 f |= 1ULL << bch2_compression_opt_to_feature[c->opts.compression];
624 if (c->opts.background_compression)
625 f |= 1ULL << bch2_compression_opt_to_feature[c->opts.background_compression];
627 return __bch2_fs_compress_init(c, f);