1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/zlib.h>
11 #include <linux/zstd.h>
26 static struct bbuf __bounce_alloc(struct bch_fs *c, unsigned size, int rw)
30 BUG_ON(size > c->sb.encoded_extent_max << 9);
32 b = kmalloc(size, GFP_NOIO|__GFP_NOWARN);
34 return (struct bbuf) { .b = b, .type = BB_KMALLOC, .rw = rw };
36 b = mempool_alloc(&c->compression_bounce[rw], GFP_NOWAIT);
37 b = b ? page_address(b) : NULL;
39 return (struct bbuf) { .b = b, .type = BB_MEMPOOL, .rw = rw };
43 return (struct bbuf) { .b = b, .type = BB_VMALLOC, .rw = rw };
45 b = mempool_alloc(&c->compression_bounce[rw], GFP_NOIO);
46 b = b ? page_address(b) : NULL;
48 return (struct bbuf) { .b = b, .type = BB_MEMPOOL, .rw = rw };
53 static struct bbuf __bio_map_or_bounce(struct bch_fs *c, struct bio *bio,
54 struct bvec_iter start, int rw)
58 struct bvec_iter iter;
59 unsigned nr_pages = 0;
60 struct page *stack_pages[16];
61 struct page **pages = NULL;
63 unsigned prev_end = PAGE_SIZE;
66 BUG_ON(bvec_iter_sectors(start) > c->sb.encoded_extent_max);
68 #ifndef CONFIG_HIGHMEM
69 __bio_for_each_bvec(bv, bio, iter, start) {
70 if (bv.bv_len == start.bi_size)
71 return (struct bbuf) {
72 .b = page_address(bv.bv_page) + bv.bv_offset,
73 .type = BB_NONE, .rw = rw
77 __bio_for_each_segment(bv, bio, iter, start) {
78 if ((!first && bv.bv_offset) ||
79 prev_end != PAGE_SIZE)
82 prev_end = bv.bv_offset + bv.bv_len;
86 BUG_ON(DIV_ROUND_UP(start.bi_size, PAGE_SIZE) > nr_pages);
88 pages = nr_pages > ARRAY_SIZE(stack_pages)
89 ? kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOIO)
95 __bio_for_each_segment(bv, bio, iter, start)
96 pages[nr_pages++] = bv.bv_page;
98 data = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
99 if (pages != stack_pages)
103 return (struct bbuf) {
104 .b = data + bio_iter_offset(bio, start),
105 .type = BB_VMAP, .rw = rw
108 ret = __bounce_alloc(c, start.bi_size, rw);
111 memcpy_from_bio(ret.b, bio, start);
116 static struct bbuf bio_map_or_bounce(struct bch_fs *c, struct bio *bio, int rw)
118 return __bio_map_or_bounce(c, bio, bio->bi_iter, rw);
121 static void bio_unmap_or_unbounce(struct bch_fs *c, struct bbuf buf)
127 vunmap((void *) ((unsigned long) buf.b & PAGE_MASK));
136 mempool_free(virt_to_page(buf.b),
137 &c->compression_bounce[buf.rw]);
142 static inline void zlib_set_workspace(z_stream *strm, void *workspace)
145 strm->workspace = workspace;
149 static int __bio_uncompress(struct bch_fs *c, struct bio *src,
150 void *dst_data, struct bch_extent_crc_unpacked crc)
152 struct bbuf src_data = { NULL };
153 size_t src_len = src->bi_iter.bi_size;
154 size_t dst_len = crc.uncompressed_size << 9;
158 src_data = bio_map_or_bounce(c, src, READ);
160 switch (crc.compression_type) {
161 case BCH_COMPRESSION_TYPE_lz4_old:
162 case BCH_COMPRESSION_TYPE_lz4:
163 ret = LZ4_decompress_safe_partial(src_data.b, dst_data,
164 src_len, dst_len, dst_len);
168 case BCH_COMPRESSION_TYPE_gzip: {
170 .next_in = src_data.b,
172 .next_out = dst_data,
173 .avail_out = dst_len,
176 workspace = mempool_alloc(&c->decompress_workspace, GFP_NOIO);
178 zlib_set_workspace(&strm, workspace);
179 zlib_inflateInit2(&strm, -MAX_WBITS);
180 ret = zlib_inflate(&strm, Z_FINISH);
182 mempool_free(workspace, &c->decompress_workspace);
184 if (ret != Z_STREAM_END)
188 case BCH_COMPRESSION_TYPE_zstd: {
192 workspace = mempool_alloc(&c->decompress_workspace, GFP_NOIO);
193 ctx = ZSTD_initDCtx(workspace, ZSTD_DCtxWorkspaceBound());
195 src_len = le32_to_cpup(src_data.b);
197 len = ZSTD_decompressDCtx(ctx,
199 src_data.b + 4, src_len);
201 mempool_free(workspace, &c->decompress_workspace);
212 bio_unmap_or_unbounce(c, src_data);
219 int bch2_bio_uncompress_inplace(struct bch_fs *c, struct bio *bio,
220 struct bch_extent_crc_unpacked *crc)
222 struct bbuf data = { NULL };
223 size_t dst_len = crc->uncompressed_size << 9;
225 /* bio must own its pages: */
226 BUG_ON(!bio->bi_vcnt);
227 BUG_ON(DIV_ROUND_UP(crc->live_size, PAGE_SECTORS) > bio->bi_max_vecs);
229 if (crc->uncompressed_size > c->sb.encoded_extent_max ||
230 crc->compressed_size > c->sb.encoded_extent_max) {
231 bch_err(c, "error rewriting existing data: extent too big");
235 data = __bounce_alloc(c, dst_len, WRITE);
237 if (__bio_uncompress(c, bio, data.b, *crc)) {
238 bch_err(c, "error rewriting existing data: decompression error");
239 bio_unmap_or_unbounce(c, data);
244 * XXX: don't have a good way to assert that the bio was allocated with
245 * enough space, we depend on bch2_move_extent doing the right thing
247 bio->bi_iter.bi_size = crc->live_size << 9;
249 memcpy_to_bio(bio, bio->bi_iter, data.b + (crc->offset << 9));
252 crc->compression_type = 0;
253 crc->compressed_size = crc->live_size;
254 crc->uncompressed_size = crc->live_size;
256 crc->csum = (struct bch_csum) { 0, 0 };
258 bio_unmap_or_unbounce(c, data);
262 int bch2_bio_uncompress(struct bch_fs *c, struct bio *src,
263 struct bio *dst, struct bvec_iter dst_iter,
264 struct bch_extent_crc_unpacked crc)
266 struct bbuf dst_data = { NULL };
267 size_t dst_len = crc.uncompressed_size << 9;
270 if (crc.uncompressed_size > c->sb.encoded_extent_max ||
271 crc.compressed_size > c->sb.encoded_extent_max)
274 dst_data = dst_len == dst_iter.bi_size
275 ? __bio_map_or_bounce(c, dst, dst_iter, WRITE)
276 : __bounce_alloc(c, dst_len, WRITE);
278 ret = __bio_uncompress(c, src, dst_data.b, crc);
282 if (dst_data.type != BB_NONE)
283 memcpy_to_bio(dst, dst_iter, dst_data.b + (crc.offset << 9));
285 bio_unmap_or_unbounce(c, dst_data);
289 static int attempt_compress(struct bch_fs *c,
291 void *dst, size_t dst_len,
292 void *src, size_t src_len,
293 enum bch_compression_type compression_type)
295 switch (compression_type) {
296 case BCH_COMPRESSION_TYPE_lz4: {
298 int ret = LZ4_compress_destSize(
308 case BCH_COMPRESSION_TYPE_gzip: {
313 .avail_out = dst_len,
316 zlib_set_workspace(&strm, workspace);
317 zlib_deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
318 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL,
321 if (zlib_deflate(&strm, Z_FINISH) != Z_STREAM_END)
324 if (zlib_deflateEnd(&strm) != Z_OK)
327 return strm.total_out;
329 case BCH_COMPRESSION_TYPE_zstd: {
330 ZSTD_CCtx *ctx = ZSTD_initCCtx(workspace,
331 ZSTD_CCtxWorkspaceBound(c->zstd_params.cParams));
333 size_t len = ZSTD_compressCCtx(ctx,
334 dst + 4, dst_len - 4,
337 if (ZSTD_isError(len))
340 *((__le32 *) dst) = cpu_to_le32(len);
348 static unsigned __bio_compress(struct bch_fs *c,
349 struct bio *dst, size_t *dst_len,
350 struct bio *src, size_t *src_len,
351 enum bch_compression_type compression_type)
353 struct bbuf src_data = { NULL }, dst_data = { NULL };
358 BUG_ON(compression_type >= BCH_COMPRESSION_TYPE_NR);
359 BUG_ON(!mempool_initialized(&c->compress_workspace[compression_type]));
361 /* If it's only one block, don't bother trying to compress: */
362 if (bio_sectors(src) <= c->opts.block_size)
365 dst_data = bio_map_or_bounce(c, dst, WRITE);
366 src_data = bio_map_or_bounce(c, src, READ);
368 workspace = mempool_alloc(&c->compress_workspace[compression_type], GFP_NOIO);
370 *src_len = src->bi_iter.bi_size;
371 *dst_len = dst->bi_iter.bi_size;
374 * XXX: this algorithm sucks when the compression code doesn't tell us
375 * how much would fit, like LZ4 does:
378 if (*src_len <= block_bytes(c)) {
383 ret = attempt_compress(c, workspace,
384 dst_data.b, *dst_len,
385 src_data.b, *src_len,
393 /* Didn't fit: should we retry with a smaller amount? */
394 if (*src_len <= *dst_len) {
400 * If ret is negative, it's a hint as to how much data would fit
402 BUG_ON(-ret >= *src_len);
407 *src_len -= (*src_len - *dst_len) / 2;
408 *src_len = round_down(*src_len, block_bytes(c));
411 mempool_free(workspace, &c->compress_workspace[compression_type]);
416 /* Didn't get smaller: */
417 if (round_up(*dst_len, block_bytes(c)) >= *src_len)
420 pad = round_up(*dst_len, block_bytes(c)) - *dst_len;
422 memset(dst_data.b + *dst_len, 0, pad);
425 if (dst_data.type != BB_NONE)
426 memcpy_to_bio(dst, dst->bi_iter, dst_data.b);
428 BUG_ON(!*dst_len || *dst_len > dst->bi_iter.bi_size);
429 BUG_ON(!*src_len || *src_len > src->bi_iter.bi_size);
430 BUG_ON(*dst_len & (block_bytes(c) - 1));
431 BUG_ON(*src_len & (block_bytes(c) - 1));
433 bio_unmap_or_unbounce(c, src_data);
434 bio_unmap_or_unbounce(c, dst_data);
435 return compression_type;
437 compression_type = BCH_COMPRESSION_TYPE_incompressible;
441 unsigned bch2_bio_compress(struct bch_fs *c,
442 struct bio *dst, size_t *dst_len,
443 struct bio *src, size_t *src_len,
444 unsigned compression_type)
446 unsigned orig_dst = dst->bi_iter.bi_size;
447 unsigned orig_src = src->bi_iter.bi_size;
449 /* Don't consume more than BCH_ENCODED_EXTENT_MAX from @src: */
450 src->bi_iter.bi_size = min_t(unsigned, src->bi_iter.bi_size,
451 c->sb.encoded_extent_max << 9);
452 /* Don't generate a bigger output than input: */
453 dst->bi_iter.bi_size = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
455 if (compression_type == BCH_COMPRESSION_TYPE_lz4_old)
456 compression_type = BCH_COMPRESSION_TYPE_lz4;
459 __bio_compress(c, dst, dst_len, src, src_len, compression_type);
461 dst->bi_iter.bi_size = orig_dst;
462 src->bi_iter.bi_size = orig_src;
463 return compression_type;
466 static int __bch2_fs_compress_init(struct bch_fs *, u64);
468 #define BCH_FEATURE_none 0
470 static const unsigned bch2_compression_opt_to_feature[] = {
471 #define x(t, n) [BCH_COMPRESSION_OPT_##t] = BCH_FEATURE_##t,
472 BCH_COMPRESSION_OPTS()
476 #undef BCH_FEATURE_none
478 static int __bch2_check_set_has_compressed_data(struct bch_fs *c, u64 f)
482 if ((c->sb.features & f) == f)
485 mutex_lock(&c->sb_lock);
487 if ((c->sb.features & f) == f) {
488 mutex_unlock(&c->sb_lock);
492 ret = __bch2_fs_compress_init(c, c->sb.features|f);
494 mutex_unlock(&c->sb_lock);
498 c->disk_sb.sb->features[0] |= cpu_to_le64(f);
500 mutex_unlock(&c->sb_lock);
505 int bch2_check_set_has_compressed_data(struct bch_fs *c,
506 unsigned compression_type)
508 BUG_ON(compression_type >= ARRAY_SIZE(bch2_compression_opt_to_feature));
510 return compression_type
511 ? __bch2_check_set_has_compressed_data(c,
512 1ULL << bch2_compression_opt_to_feature[compression_type])
516 void bch2_fs_compress_exit(struct bch_fs *c)
520 mempool_exit(&c->decompress_workspace);
521 for (i = 0; i < ARRAY_SIZE(c->compress_workspace); i++)
522 mempool_exit(&c->compress_workspace[i]);
523 mempool_exit(&c->compression_bounce[WRITE]);
524 mempool_exit(&c->compression_bounce[READ]);
527 static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)
529 size_t max_extent = c->sb.encoded_extent_max << 9;
530 size_t order = get_order(max_extent);
531 size_t decompress_workspace_size = 0;
532 bool decompress_workspace_needed;
533 ZSTD_parameters params = ZSTD_getParams(0, max_extent, 0);
537 size_t compress_workspace;
538 size_t decompress_workspace;
539 } compression_types[] = {
540 { BCH_FEATURE_lz4, BCH_COMPRESSION_TYPE_lz4, LZ4_MEM_COMPRESS, 0 },
541 { BCH_FEATURE_gzip, BCH_COMPRESSION_TYPE_gzip,
542 zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL),
543 zlib_inflate_workspacesize(), },
544 { BCH_FEATURE_zstd, BCH_COMPRESSION_TYPE_zstd,
545 ZSTD_CCtxWorkspaceBound(params.cParams),
546 ZSTD_DCtxWorkspaceBound() },
550 pr_verbose_init(c->opts, "");
552 c->zstd_params = params;
554 for (i = compression_types;
555 i < compression_types + ARRAY_SIZE(compression_types);
557 if (features & (1 << i->feature))
558 goto have_compressed;
563 if (!mempool_initialized(&c->compression_bounce[READ])) {
564 ret = mempool_init_page_pool(&c->compression_bounce[READ],
570 if (!mempool_initialized(&c->compression_bounce[WRITE])) {
571 ret = mempool_init_page_pool(&c->compression_bounce[WRITE],
577 for (i = compression_types;
578 i < compression_types + ARRAY_SIZE(compression_types);
580 decompress_workspace_size =
581 max(decompress_workspace_size, i->decompress_workspace);
583 if (!(features & (1 << i->feature)))
586 if (i->decompress_workspace)
587 decompress_workspace_needed = true;
589 if (mempool_initialized(&c->compress_workspace[i->type]))
592 ret = mempool_init_kvpmalloc_pool(
593 &c->compress_workspace[i->type],
594 1, i->compress_workspace);
599 if (!mempool_initialized(&c->decompress_workspace)) {
600 ret = mempool_init_kmalloc_pool(
601 &c->decompress_workspace,
602 1, decompress_workspace_size);
607 pr_verbose_init(c->opts, "ret %i", ret);
611 int bch2_fs_compress_init(struct bch_fs *c)
613 u64 f = c->sb.features;
615 if (c->opts.compression)
616 f |= 1ULL << bch2_compression_opt_to_feature[c->opts.compression];
618 if (c->opts.background_compression)
619 f |= 1ULL << bch2_compression_opt_to_feature[c->opts.background_compression];
621 return __bch2_fs_compress_init(c, f);