8 #include <linux/zlib.h>
17 static void *__bounce_alloc(struct cache_set *c, unsigned size,
18 unsigned *bounced, int direction)
22 *bounced = BOUNCED_KMALLOCED;
23 data = kmalloc(size, GFP_NOIO|__GFP_NOWARN);
27 *bounced = BOUNCED_MEMPOOLED;
28 data = mempool_alloc(&c->compression_bounce[direction], GFP_NOWAIT);
30 return page_address(data);
32 *bounced = BOUNCED_VMALLOCED;
37 *bounced = BOUNCED_MEMPOOLED;
38 data = mempool_alloc(&c->compression_bounce[direction], GFP_NOIO);
39 return page_address(data);
42 static void *__bio_map_or_bounce(struct cache_set *c,
43 struct bio *bio, struct bvec_iter start,
44 unsigned *bounced, int direction)
47 struct bvec_iter iter;
48 unsigned nr_pages = 0;
49 struct page *stack_pages[16];
50 struct page **pages = NULL;
52 unsigned prev_end = PAGE_SIZE;
55 BUG_ON(bvec_iter_sectors(start) > BCH_ENCODED_EXTENT_MAX);
57 *bounced = BOUNCED_MAPPED;
59 __bio_for_each_segment(bv, bio, iter, start) {
60 if ((!first && bv.bv_offset) ||
61 prev_end != PAGE_SIZE)
64 prev_end = bv.bv_offset + bv.bv_len;
68 BUG_ON(DIV_ROUND_UP(start.bi_size, PAGE_SIZE) > nr_pages);
70 pages = nr_pages > ARRAY_SIZE(stack_pages)
71 ? kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOIO)
77 __bio_for_each_segment(bv, bio, iter, start)
78 pages[nr_pages++] = bv.bv_page;
80 data = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
81 if (pages != stack_pages)
84 return data + bio_iter_offset(bio, start);
86 data = __bounce_alloc(c, start.bi_size, bounced, direction);
88 if (direction == READ)
89 memcpy_from_bio(data, bio, start);
94 static void *bio_map_or_bounce(struct cache_set *c, struct bio *bio,
95 unsigned *bounced, int direction)
97 return __bio_map_or_bounce(c, bio, bio->bi_iter, bounced, direction);
100 static void bio_unmap_or_unbounce(struct cache_set *c, void *data,
101 unsigned bounced, int direction)
108 vunmap((void *) ((unsigned long) data & PAGE_MASK));
110 case BOUNCED_KMALLOCED:
113 case BOUNCED_VMALLOCED:
116 case BOUNCED_MEMPOOLED:
117 mempool_free(virt_to_page(data), &c->compression_bounce[direction]);
122 static int __bio_uncompress(struct cache_set *c, struct bio *src,
123 void *dst_data, struct bch_extent_crc128 crc)
125 void *src_data = NULL;
126 unsigned src_bounced;
127 size_t src_len = src->bi_iter.bi_size;
128 size_t dst_len = crc_uncompressed_size(NULL, &crc) << 9;
131 src_data = bio_map_or_bounce(c, src, &src_bounced, READ);
133 switch (crc.compression_type) {
134 case BCH_COMPRESSION_LZ4:
135 ret = lz4_decompress(src_data, &src_len,
142 case BCH_COMPRESSION_GZIP: {
146 workspace = kmalloc(zlib_inflate_workspacesize(),
147 GFP_NOIO|__GFP_NOWARN);
149 mutex_lock(&c->zlib_workspace_lock);
150 workspace = c->zlib_workspace;
153 strm.workspace = workspace;
154 strm.next_in = src_data;
155 strm.avail_in = src_len;
156 strm.next_out = dst_data;
157 strm.avail_out = dst_len;
158 zlib_inflateInit2(&strm, -MAX_WBITS);
160 ret = zlib_inflate(&strm, Z_FINISH);
162 if (workspace == c->zlib_workspace)
163 mutex_unlock(&c->zlib_workspace_lock);
167 if (ret != Z_STREAM_END) {
178 bio_unmap_or_unbounce(c, src_data, src_bounced, READ);
182 int bch_bio_uncompress_inplace(struct cache_set *c, struct bio *bio,
183 unsigned live_data_sectors,
184 struct bch_extent_crc128 crc)
186 void *dst_data = NULL;
187 size_t dst_len = crc_uncompressed_size(NULL, &crc) << 9;
190 BUG_ON(DIV_ROUND_UP(live_data_sectors, PAGE_SECTORS) > bio->bi_max_vecs);
193 dst_data = kmalloc(dst_len, GFP_NOIO|__GFP_NOWARN);
195 dst_data = vmalloc(dst_len);
200 ret = __bio_uncompress(c, bio, dst_data, crc);
204 while (bio->bi_vcnt < DIV_ROUND_UP(live_data_sectors, PAGE_SECTORS)) {
205 struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
207 bv->bv_page = alloc_page(GFP_NOIO);
211 bv->bv_len = PAGE_SIZE;
216 bio->bi_iter.bi_size = live_data_sectors << 9;
218 memcpy_to_bio(bio, bio->bi_iter, dst_data + (crc.offset << 9));
224 * We already allocated from mempool, we can't allocate from it again
225 * without freeing the pages we already allocated or else we could
229 bch_bio_free_pages_pool(c, bio);
230 bch_bio_alloc_pages_pool(c, bio, live_data_sectors << 9);
234 int bch_bio_uncompress(struct cache_set *c, struct bio *src,
235 struct bio *dst, struct bvec_iter dst_iter,
236 struct bch_extent_crc128 crc)
238 void *dst_data = NULL;
239 unsigned dst_bounced;
240 size_t dst_len = crc_uncompressed_size(NULL, &crc) << 9;
243 dst_data = dst_len == dst_iter.bi_size
244 ? __bio_map_or_bounce(c, dst, dst_iter, &dst_bounced, WRITE)
245 : __bounce_alloc(c, dst_len, &dst_bounced, WRITE);
247 ret = __bio_uncompress(c, src, dst_data, crc);
252 memcpy_to_bio(dst, dst_iter, dst_data + (crc.offset << 9));
254 bio_unmap_or_unbounce(c, dst_data, dst_bounced, WRITE);
258 static int __bio_compress(struct cache_set *c,
259 struct bio *dst, size_t *dst_len,
260 struct bio *src, size_t *src_len,
261 unsigned compression_type)
263 void *src_data = NULL, *dst_data = NULL;
264 unsigned src_bounced, dst_bounced, pad;
267 dst_data = bio_map_or_bounce(c, dst, &dst_bounced, WRITE);
268 src_data = bio_map_or_bounce(c, src, &src_bounced, READ);
270 switch (compression_type) {
271 case BCH_COMPRESSION_LZ4: {
274 *dst_len = dst->bi_iter.bi_size;
275 *src_len = src->bi_iter.bi_size;
277 workspace = mempool_alloc(&c->lz4_workspace_pool, GFP_NOIO);
279 while (*src_len > block_bytes(c) &&
280 (ret = lz4_compress(src_data, *src_len,
284 * On error, the compressed data was bigger than
285 * dst_len, and -ret is the amount of data we were able
286 * to compress - round down to nearest block and try
290 BUG_ON(-ret >= *src_len);
292 *src_len = round_down(-ret, block_bytes(c));
295 mempool_free(workspace, &c->lz4_workspace_pool);
301 case BCH_COMPRESSION_GZIP: {
305 workspace = kmalloc(zlib_deflate_workspacesize(MAX_WBITS,
307 GFP_NOIO|__GFP_NOWARN);
309 mutex_lock(&c->zlib_workspace_lock);
310 workspace = c->zlib_workspace;
313 strm.workspace = workspace;
314 strm.next_in = src_data;
315 strm.avail_in = min(src->bi_iter.bi_size,
316 dst->bi_iter.bi_size);
317 strm.next_out = dst_data;
318 strm.avail_out = dst->bi_iter.bi_size;
319 zlib_deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
320 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL,
323 ret = zlib_deflate(&strm, Z_FINISH);
324 if (ret != Z_STREAM_END) {
329 ret = zlib_deflateEnd(&strm);
337 if (workspace == c->zlib_workspace)
338 mutex_unlock(&c->zlib_workspace_lock);
345 *dst_len = strm.total_out;
346 *src_len = strm.total_in;
354 BUG_ON(*dst_len > dst->bi_iter.bi_size);
356 BUG_ON(*src_len & (block_bytes(c) - 1));
357 BUG_ON(*src_len > src->bi_iter.bi_size);
359 /* Didn't get smaller: */
360 if (round_up(*dst_len, block_bytes(c)) >= *src_len) {
365 pad = round_up(*dst_len, block_bytes(c)) - *dst_len;
367 memset(dst_data + *dst_len, 0, pad);
371 memcpy_to_bio(dst, dst->bi_iter, dst_data);
373 bio_unmap_or_unbounce(c, src_data, src_bounced, READ);
374 bio_unmap_or_unbounce(c, dst_data, dst_bounced, WRITE);
378 void bch_bio_compress(struct cache_set *c,
379 struct bio *dst, size_t *dst_len,
380 struct bio *src, size_t *src_len,
381 unsigned *compression_type)
383 unsigned orig_dst = dst->bi_iter.bi_size;
384 unsigned orig_src = src->bi_iter.bi_size;
386 /* Don't consume more than BCH_ENCODED_EXTENT_MAX from @src: */
387 src->bi_iter.bi_size =
388 min(src->bi_iter.bi_size, BCH_ENCODED_EXTENT_MAX << 9);
390 /* Don't generate a bigger output than input: */
391 dst->bi_iter.bi_size =
392 min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
394 /* If it's only one block, don't bother trying to compress: */
395 if (*compression_type != BCH_COMPRESSION_NONE &&
396 bio_sectors(src) > c->sb.block_size &&
397 !__bio_compress(c, dst, dst_len, src, src_len, *compression_type))
400 /* If compressing failed (didn't get smaller), just copy: */
401 *compression_type = BCH_COMPRESSION_NONE;
402 *dst_len = *src_len = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
403 bio_copy_data(dst, src);
405 dst->bi_iter.bi_size = orig_dst;
406 src->bi_iter.bi_size = orig_src;
409 /* doesn't write superblock: */
410 int bch_check_set_has_compressed_data(struct cache_set *c,
411 unsigned compression_type)
413 switch (compression_type) {
414 case BCH_COMPRESSION_NONE:
416 case BCH_COMPRESSION_LZ4:
417 if (bch_sb_test_feature(c->disk_sb, BCH_FEATURE_LZ4))
420 bch_sb_set_feature(c->disk_sb, BCH_FEATURE_LZ4);
422 case BCH_COMPRESSION_GZIP:
423 if (bch_sb_test_feature(c->disk_sb, BCH_FEATURE_GZIP))
426 bch_sb_set_feature(c->disk_sb, BCH_FEATURE_GZIP);
430 return bch_compress_init(c);
433 void bch_compress_free(struct cache_set *c)
435 vfree(c->zlib_workspace);
436 mempool_exit(&c->lz4_workspace_pool);
437 mempool_exit(&c->compression_bounce[WRITE]);
438 mempool_exit(&c->compression_bounce[READ]);
439 free_percpu(c->bio_decompress_worker);
442 #define COMPRESSION_WORKSPACE_SIZE \
443 max_t(size_t, zlib_inflate_workspacesize(), \
444 zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL))
446 int bch_compress_init(struct cache_set *c)
448 unsigned order = get_order(BCH_ENCODED_EXTENT_MAX << 9);
451 if (!bch_sb_test_feature(c->disk_sb, BCH_FEATURE_LZ4) &&
452 !bch_sb_test_feature(c->disk_sb, BCH_FEATURE_GZIP))
455 if (!c->bio_decompress_worker) {
456 c->bio_decompress_worker = alloc_percpu(*c->bio_decompress_worker);
457 if (!c->bio_decompress_worker)
460 for_each_possible_cpu(cpu) {
461 struct bio_decompress_worker *d =
462 per_cpu_ptr(c->bio_decompress_worker, cpu);
465 INIT_WORK(&d->work, bch_bio_decompress_work);
466 init_llist_head(&d->bio_list);
470 if (!mempool_initialized(&c->compression_bounce[READ])) {
471 ret = mempool_init_page_pool(&c->compression_bounce[READ],
477 if (!mempool_initialized(&c->compression_bounce[WRITE])) {
478 ret = mempool_init_page_pool(&c->compression_bounce[WRITE],
484 if (!mempool_initialized(&c->lz4_workspace_pool) &&
485 bch_sb_test_feature(c->disk_sb, BCH_FEATURE_LZ4)) {
486 ret = mempool_init_kmalloc_pool(&c->lz4_workspace_pool,
487 1, LZ4_MEM_COMPRESS);
492 if (!c->zlib_workspace &&
493 bch_sb_test_feature(c->disk_sb, BCH_FEATURE_GZIP)) {
494 c->zlib_workspace = vmalloc(COMPRESSION_WORKSPACE_SIZE);
495 if (!c->zlib_workspace)