* [PATCH] dm-compress: introduce the dm-compress target
@ 2026-11-23 21:07 Mikulas Patocka
2026-11-23 21:35 ` sashiko-bot
2026-11-23 22:27 ` Eric Biggers
0 siblings, 2 replies; 3+ messages in thread
From: Mikulas Patocka @ 2026-11-23 21:07 UTC (permalink / raw)
To: Alasdair Kergon, Mike Snitzer, Benjamin Marzinski, Ming-Hung Tsai,
Zdenek Kabelac, Filip Suba, dm-devel
This commit introduces a new target dm-compress that provides transparent
compression and decompression of block devices.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
---
Documentation/admin-guide/device-mapper/dm-compress.rst | 61 +
drivers/md/Kconfig | 2
drivers/md/Makefile | 1
drivers/md/dm-bufio.c | 16
drivers/md/dm-compress/Kconfig | 17
drivers/md/dm-compress/Makefile | 10
drivers/md/dm-compress/alloc.c | 480 ++++++++
drivers/md/dm-compress/cc.c | 251 ++++
drivers/md/dm-compress/chunk.c | 440 +++++++
drivers/md/dm-compress/dm-compress.h | 217 +++
drivers/md/dm-compress/io.c | 652 ++++++++++
drivers/md/dm-compress/struct.h | 128 ++
drivers/md/dm-compress/target.c | 959 ++++++++++++++++
include/linux/dm-bufio.h | 14
14 files changed, 3248 insertions(+)
Index: linux-2.6/drivers/md/Kconfig
===================================================================
--- linux-2.6.orig/drivers/md/Kconfig
+++ linux-2.6/drivers/md/Kconfig
@@ -719,4 +719,6 @@ source "drivers/md/dm-vdo/Kconfig"
source "drivers/md/dm-pcache/Kconfig"
+source "drivers/md/dm-compress/Kconfig"
+
endif # MD
Index: linux-2.6/drivers/md/dm-compress/Kconfig
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/Kconfig
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# dm-compress configuration
+#
+
+config DM_COMPRESS
+ tristate "Compression target"
+ depends on BLK_DEV_DM
+ select DM_BUFIO
+ select CRYPTO_DEFLATE
+ select CRYPTO_LZO
+ select CRYPTO_842
+ select CRYPTO_LZ4
+ select CRYPTO_LZ4HC
+ select CRYPTO_ZSTD
+ help
+ This target stores compressed data.
Index: linux-2.6/drivers/md/dm-compress/dm-compress.h
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/dm-compress.h
@@ -0,0 +1,217 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include <linux/device-mapper.h>
+#include <linux/dm-io.h>
+#include <linux/dm-bufio.h>
+#include <linux/vmalloc.h>
+#include <crypto/acompress.h>
+
+//#define TEST_SPLIT_CHUNKS
+//#define TEST_SPLIT_CHUNK_CHAINS
+//#define TEST_UNCOMPRESSED_DATA
+//#define TEST_FUA
+//#define TEST_MALFORMED_CONTENT
+
+#define DM_MSG_PREFIX "compress"
+
+#define METADATA_WORKQUEUE_MAX_ACTIVE 128
+#define N_CACHED_CHUNKS 128
+#define DEFAULT_AUTOCOMMIT_MSEC 60000
+
+#include "struct.h"
+
+struct algorithm {
+ const char *name;
+ struct crypto_acomp *cc;
+};
+
+struct block_group_descriptor {
+ struct mutex mutex;
+ sector_t free;
+} ____cacheline_aligned_in_smp;
+
+#define TAG_VALID 1
+#define TAG_WRITTEN 2
+
+struct cached_chunk {
+ struct rb_node node;
+ struct list_head lru_entry;
+ sector_t chunk;
+ struct dm_compress *c;
+ bool in_tree;
+ bool dirty;
+#define INTENT_IDLE 0
+#define INTENT_READ 1
+#define INTENT_WRITE 2
+#define INTENT_FREE 3
+ uint8_t intent;
+ uint8_t algorithm;
+ unsigned compressed_size;
+ int failed;
+ void *uncompressed_data;
+ struct scatterlist *uncompressed_sglist;
+ void *compressed_data;
+ struct scatterlist *compressed_sglist;
+ void *merging_data;
+ struct scatterlist *merging_sglist;
+ struct work_struct work;
+ struct acomp_req *req;
+ unsigned short bio_vectors;
+ struct completion completion;
+ struct bio *bio;
+ uint8_t sector_tags[];
+};
+
+struct dm_compress {
+ struct dm_target *ti;
+ sector_t start;
+ struct dm_dev *dev;
+ struct dm_dev *meta_dev;
+ unsigned char log2_sectors_per_block;
+ unsigned char log2_sectors_per_chunk;
+ unsigned char algorithm;
+ sector_t n_blocks;
+ sector_t n_chunks;
+ struct superblock *sb;
+ struct crash_count_table *cct;
+ __u64 cc_txc;
+
+ int failed;
+
+ struct workqueue_struct *wq;
+ struct workqueue_struct *compress_wq;
+ struct workqueue_struct *flush_wq;
+
+ struct dm_bufio_client *bufio;
+ struct dm_io_client *io;
+
+ sector_t *block_directories;
+ sector_t *chunk_directories;
+ struct block_group_descriptor *groups;
+ size_t n_groups;
+ sector_t last_allocated;
+
+ atomic64_t free_blocks;
+
+ unsigned long autocommit_jiffies;
+ unsigned autocommit_msec;
+ bool autocommit_set;
+ bool autocommit_active;
+ struct delayed_work autocommit_work;
+ wait_queue_head_t wait_idle;
+
+ struct list_head free_chunks;
+ struct list_head lru;
+ struct rb_root chunks;
+ struct mutex mutex;
+
+ struct list_head waiting_bio_lru;
+ struct rb_root waiting_bio_tree;
+
+ unsigned n_cached_chunks;
+
+ mempool_t block_io_buffers;
+ struct dm_bufio_client *split_bufio;
+ int split_bufio_dirty;
+};
+
+struct dm_compress_io {
+ struct dm_compress *c;
+ struct work_struct work;
+ sector_t chunk;
+ unsigned offset_in_chunk;
+ bool is_flush;
+
+ struct list_head waiting_list_entry;
+ struct rb_node tree_entry;
+};
+
+static inline bool CC_VALID(struct dm_compress *c, __le64 *cc_txc)
+{
+ __u64 ct = le64_to_cpup(cc_txc);
+ __u16 cc = ct;
+ return (__s64)(le64_to_cpu(c->cct->entries[cc]) - ct) >= 0;
+}
+
+static inline bool CC_CURRENT(struct dm_compress *c, __le64 *cc_txc)
+{
+ __u64 ct = le64_to_cpup(cc_txc);
+ return ((c->cc_txc ^ ct) & ~(1ULL << 63)) == 0;
+}
+
+static inline void CC_SET_CURRENT(struct dm_compress *c, __le64 *cc_txc)
+{
+ __u64 ct = le64_to_cpup(cc_txc);
+ __u16 cc = ct;
+ __u64 top_bit = ~(le64_to_cpu(c->cct->entries[cc]) - ct) & (1ULL << 63);
+ *cc_txc = cpu_to_le64(c->cc_txc | top_bit);
+}
+
+static inline void block_location(struct dm_compress *c, sector_t block, size_t *group_ptr, size_t *dir_ptr, unsigned *dir_offset, unsigned *bmp_offset)
+{
+ *bmp_offset = do_div(block, BITS_PER_BITMAP);
+ *group_ptr = block;
+ *dir_offset = do_div(block, DIRECTORY_ENTRIES);
+ *dir_ptr = block;
+}
+
+static inline void chunk_location(struct dm_compress *c, sector_t chunk, size_t *dir_ptr, unsigned *dir_offset, unsigned *chunk_offset)
+{
+ *chunk_offset = do_div(chunk, CHUNKS_PER_BUFFER);
+ *dir_offset = do_div(chunk, DIRECTORY_ENTRIES);
+ *dir_ptr = chunk;
+}
+
+/* target.c */
+
+extern struct algorithm dm_compress_algorithms[];
+extern const unsigned dm_compress_n_algorithms;
+extern unsigned dm_compress_max_request_size;
+
+void dm_compress_error(struct dm_compress *c, const char *msg, int err);
+void dm_compress_error_chunk(struct cached_chunk *cc, const char *msg, int err);
+int dm_compress_sync_rw_metadata(struct dm_compress *c, blk_opf_t opf, enum dm_io_mem_type type, sector_t sector, void *ptr, sector_t sectors);
+void dm_compress_sync(struct dm_compress *c);
+
+/* cc.c */
+
+struct cached_chunk *dm_compress_alloc_cached_chunk(struct dm_compress *c);
+void dm_compress_free_cached_chunk(struct cached_chunk *cc);
+struct cached_chunk *dm_compress_find_cached_chunk(struct dm_compress *c, sector_t chunk, struct cached_chunk *for_insert);
+struct cached_chunk *dm_compress_new_cached_chunk(struct dm_compress_io *dio);
+void dm_compress_release_cached_chunk(struct cached_chunk *cc);
+void dm_compress_wait_for_cached_chunk(struct dm_compress_io *dio);
+struct dm_compress_io *dm_compress_pick_dio_for_chunk(struct dm_compress *c, sector_t chunk);
+struct dm_compress_io *dm_compress_pick_oldest_dio(struct dm_compress *c);
+void dm_compress_unlink_dio(struct dm_compress_io *dio);
+bool dm_compress_is_something_in_progress(struct dm_compress *c);
+
+/* alloc.c */
+
+#define ALLOCATE_NO_SPACE ((sector_t)-1)
+#define ALLOCATE_ERROR ((sector_t)-2)
+
+sector_t dm_compress_allocate_space(struct dm_compress *c, sector_t preferred_block, unsigned n_blocks);
+void dm_compress_free_space(struct dm_compress *c, sector_t block, unsigned n_blocks);
+int dm_compress_get_free_space(struct dm_compress *c);
+struct chunk_ptr *dm_compress_prepare_chunk(struct dm_compress *c, sector_t chunk, struct dm_buffer **bp);
+int dm_compress_resize_bitmaps(struct dm_compress *c, sector_t current_size, sector_t new_size, bool *need_sync);
+
+/* chunk.c */
+
+bool dm_compress_read_chunk(struct cached_chunk *cc, struct chunk_ptr *cp);
+bool dm_compress_allocate_chunk(struct cached_chunk *cc, sector_t preferred_block, struct chunk_ptr *cp);
+void dm_compress_release_chunk(struct dm_compress *c, struct chunk_ptr *ptr);
+
+/* io.c */
+
+void dm_compress_enqueue_chunk(struct cached_chunk *cc, unsigned intent);
+int dm_compress_map(struct dm_target *ti, struct bio *bio);
+void dm_compress_work(struct work_struct *w);
+void dm_compress_autocommit(struct work_struct *w);
Index: linux-2.6/drivers/md/dm-compress/target.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/target.c
@@ -0,0 +1,959 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-compress.h"
+
+struct algorithm dm_compress_algorithms[] = {
+ { "deflate", NULL },
+ { "lzo", NULL },
+ { "842", NULL },
+ { "lz4", NULL },
+ { "lz4hc", NULL },
+ { "zstd", NULL },
+};
+
+const unsigned dm_compress_n_algorithms = ARRAY_SIZE(dm_compress_algorithms);
+
+unsigned dm_compress_max_request_size = 0;
+
+void dm_compress_error(struct dm_compress *c, const char *msg, int err)
+{
+ if (!cmpxchg(&c->failed, 0, err)) {
+ DMERR("%s: %d", msg, err);
+ DMERR("Disabling the device");
+ }
+}
+
+void dm_compress_error_chunk(struct cached_chunk *cc, const char *msg, int err)
+{
+ if (!cmpxchg(&cc->failed, 0, err)) {
+ DMERR("%s: %d", msg, err);
+ }
+}
+
+int dm_compress_sync_rw_metadata(struct dm_compress *c, blk_opf_t opf, enum dm_io_mem_type type, sector_t sector, void *ptr, sector_t sectors)
+{
+ struct dm_io_request io_req;
+ struct dm_io_region io_loc;
+ int r;
+
+ io_req.bi_opf = opf;
+ io_req.mem.type = type;
+ io_req.mem.ptr.addr = ptr;
+ io_req.notify.fn = NULL;
+ io_req.client = c->io;
+ io_loc.bdev = c->meta_dev ? c->meta_dev->bdev : c->dev->bdev;
+ io_loc.sector = c->start + sector;
+ io_loc.count = sectors;
+
+ r = dm_io(&io_req, 1, &io_loc, NULL, NULL, IOPRIO_DEFAULT);
+ if (unlikely(r))
+ return r;
+
+ return 0;
+}
+
+void dm_compress_sync(struct dm_compress *c)
+{
+ struct block_device *bdev = c->meta_dev ? c->meta_dev->bdev : c->dev->bdev;
+ __u16 cc;
+ unsigned granularity;
+ unsigned offset;
+ int r;
+
+ r = dm_bufio_write_dirty_buffers(c->bufio);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error flushing buffers", r);
+ goto skip_commit;
+ }
+
+ if (unlikely(c->split_bufio_dirty)) {
+ c->split_bufio_dirty = false;
+ r = dm_bufio_write_dirty_buffers(c->split_bufio);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error flushing split chunk buffers", r);
+ goto skip_commit;
+ }
+ goto skip_flush_dev;
+ }
+
+ if (c->meta_dev) {
+ r = blkdev_issue_flush(c->dev->bdev);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error flushing the data device", r);
+ goto skip_commit;
+ }
+ }
+skip_flush_dev:
+
+ if (unlikely(c->failed))
+ goto skip_commit;
+
+ granularity = __fls(bdev_logical_block_size(bdev));
+ if (granularity < 12)
+ granularity = 12;
+
+ cc = c->cc_txc;
+ offset = (cc * 8) >> granularity;
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_SYNC | REQ_FUA, DM_IO_VMA, le64_to_cpu(c->sb->cct) * (BUFFER_SIZE >> SECTOR_SHIFT) + (offset << granularity >> SECTOR_SHIFT), (__u8 *)c->cct + (offset << granularity), 1U << granularity >> SECTOR_SHIFT);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error updating crash count table", r);
+ goto skip_commit;
+ }
+
+ c->cct->entries[c->cc_txc & 0xffff] = cpu_to_le64(le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) + 0x10000);
+ c->cc_txc += 0x10000;
+
+skip_commit:
+ cancel_delayed_work(&c->autocommit_work);
+}
+
+static int initialize_device(struct dm_compress *c)
+{
+ int r;
+ struct superblock *sb = c->sb;
+ sector_t position;
+ sector_t meta_size;
+ sector_t data_blocks;
+ sector_t n_bitmap_blocks;
+ sector_t n_bitmap_directories;
+ sector_t n_chunk_blocks;
+ sector_t n_chunk_directories;
+ sector_t used_meta_size;
+ unsigned i;
+ sector_t s, dp;
+ struct dm_buffer *bp;
+ const unsigned log2_buffer_sectors = __ffs(BUFFER_SECTORS);
+
+ memcpy(sb->magic, SB_MAGIC, 8);
+ sb->version = SB_VERSION_1;
+ sb->log2_buffer_size = __ffs(BUFFER_SIZE);
+ sb->log2_sectors_per_block = c->log2_sectors_per_block;
+ sb->log2_sectors_per_chunk = c->log2_sectors_per_chunk;
+ position = 1;
+ sb->cct = cpu_to_le64(position);
+ sb->n_blocks[0] = cpu_to_le64(c->n_blocks);
+ sb->n_chunks[0] = cpu_to_le64(c->n_chunks);
+ position += dm_div_up(CCT_SIZE, BUFFER_SIZE);
+
+ data_blocks = c->n_blocks;
+ n_bitmap_blocks = dm_sector_div_up(data_blocks, BITS_PER_BITMAP);
+ n_bitmap_directories = dm_sector_div_up(n_bitmap_blocks, DIRECTORY_ENTRIES);
+
+ n_chunk_blocks = dm_sector_div_up(c->n_chunks, CHUNKS_PER_BUFFER);
+ n_chunk_directories = dm_sector_div_up(n_chunk_blocks, DIRECTORY_ENTRIES);
+
+ meta_size = bdev_nr_sectors(c->meta_dev ? c->meta_dev->bdev : c->dev->bdev);
+ meta_size = meta_size >= c->start ? meta_size - c->start : 0;
+ meta_size >>= log2_buffer_sectors;
+
+ used_meta_size = position + n_bitmap_blocks + n_bitmap_directories + n_chunk_blocks + n_chunk_directories;
+ if (c->meta_dev)
+ sb->n_used_metadata_blocks[0] = cpu_to_le64(used_meta_size);
+ if (used_meta_size > meta_size) {
+ if (c->meta_dev)
+ c->ti->error = "The metadata device is too small";
+ else
+ c->ti->error = "The device is too small";
+ return -ENOSPC;
+ }
+ if (used_meta_size > 1ULL << 48) {
+ c->ti->error = "The device is too big";
+ return -ENOSPC;
+ }
+
+ /*printk("pos: %llx\n", position);
+ printk("pos: %llx\n", n_bitmap_blocks);
+ printk("pos: %llx\n", n_bitmap_directories);
+ printk("pos: %llx\n", n_chunk_blocks);
+ printk("pos: %llx\n", n_chunk_directories);
+ printk("pos: %llx\n", used_meta_size);*/
+ sb->bitmap_directory = cpu_to_le64(position + n_bitmap_blocks);
+ sb->chunk_directory = cpu_to_le64(position + n_bitmap_blocks + n_bitmap_directories + n_chunk_blocks);
+
+ if (c->log2_sectors_per_block < log2_buffer_sectors)
+ used_meta_size <<= log2_buffer_sectors - c->log2_sectors_per_block;
+ else if (c->log2_sectors_per_block > log2_buffer_sectors)
+ used_meta_size = dm_sector_div_up(used_meta_size, 1U << (c->log2_sectors_per_block - log2_buffer_sectors));
+
+ dp = 0;
+ for (s = 0; s < n_bitmap_blocks; s++) {
+ struct bitmap *bmp = dm_bufio_new(c->bufio, position + s, &bp);
+ if (unlikely(IS_ERR(bmp))) {
+ c->ti->error = "Error allocating buffer";
+ return PTR_ERR(bmp);
+ }
+ memset(bmp, 0, BUFFER_SIZE);
+ bmp->magic = BITMAP_MAGIC;
+ for (i = 0; i < BITS_PER_BITMAP; i++, dp++) {
+ if (!c->meta_dev && dp < used_meta_size)
+ __set_bit_le(i, bmp->bmp2);
+ if (dp >= data_blocks)
+ __set_bit_le(i, bmp->bmp2);
+ }
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ }
+
+ dp = 0;
+ for (s = 0; s < n_bitmap_directories; s++) {
+ struct directory *dir = dm_bufio_new(c->bufio, position + n_bitmap_blocks + s, &bp);
+ if (unlikely(IS_ERR(dir))) {
+ c->ti->error = "Error allocating buffer";
+ return PTR_ERR(dir);
+ }
+ memset(dir, 0, BUFFER_SIZE);
+ dir->magic = BMP_DIR_MAGIC;
+ dir->index = cpu_to_le64(s);
+ for (i = 0; i < DIRECTORY_ENTRIES; i++, dp++) {
+ if (dp < n_bitmap_blocks) {
+ WRITE_PTR(&dir->ptr[i], position + dp);
+ }
+ }
+ if (s + 1 < n_bitmap_directories) {
+ WRITE_PTR(&dir->next, position + n_bitmap_blocks + s + 1);
+ }
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ }
+ position += n_bitmap_blocks + n_bitmap_directories;
+ for (s = 0; s < n_chunk_blocks; s++) {
+ struct chunk_entries *ch = dm_bufio_new(c->bufio, position + s, &bp);
+ if (unlikely(IS_ERR(ch))) {
+ c->ti->error = "Error allocating buffer";
+ return PTR_ERR(ch);
+ }
+ memset(ch, 0, BUFFER_SIZE);
+ ch->magic = CHUNKS_MAGIC;
+ for (i = 0; i < CHUNKS_PER_BUFFER; i++)
+ ch->chunk[i].ptr[1].algorithm = ALGORITHM_ZERO;
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ }
+ dp = 0;
+ for (s = 0; s < n_chunk_directories; s++) {
+ struct directory *dir = dm_bufio_new(c->bufio, position + n_chunk_blocks + s, &bp);
+ if (unlikely(IS_ERR(dir))) {
+ c->ti->error = "Error allocating buffer";
+ return PTR_ERR(dir);
+ }
+ memset(dir, 0, BUFFER_SIZE);
+ dir->magic = CHUNK_DIR_MAGIC;
+ dir->index = cpu_to_le64(s);
+ for (i = 0; i < DIRECTORY_ENTRIES; i++, dp++) {
+ if (dp < n_chunk_blocks) {
+ WRITE_PTR(&dir->ptr[i], position + dp);
+ }
+ }
+ if (s + 1 < n_chunk_directories) {
+ WRITE_PTR(&dir->next, position + n_chunk_blocks + s + 1);
+ }
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ }
+
+ r = dm_bufio_write_dirty_buffers(c->bufio);
+ if (unlikely(r)) {
+ c->ti->error = "Error writing buffers";
+ return r;
+ }
+
+ for (i = 0; i < CCT_ENTRIES; i++)
+ c->cct->entries[i] = cpu_to_le64(0xffff);
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE, DM_IO_VMA, le64_to_cpu(sb->cct) * (BUFFER_SIZE >> SECTOR_SHIFT), c->cct, CCT_SIZE >> SECTOR_SHIFT);
+ if (unlikely(r)) {
+ c->ti->error = "Error writing crash count table";
+ return r;
+ }
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_PREFLUSH, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (unlikely(r)) {
+ c->ti->error = "Error writing superblock";
+ return r;
+ }
+
+ return 0;
+}
+
+static int verify_device(struct dm_compress *c, char **error)
+{
+ struct superblock *sb = c->sb;
+
+ if (memcmp(sb->magic, SB_MAGIC, 8)) {
+ *error = "The device is not initialized";
+ return -EINVAL;
+ }
+ if (sb->version != SB_VERSION_1) {
+ *error = "Version mismatch";
+ return -EINVAL;
+ }
+ if (sb->log2_buffer_size != __ffs(BUFFER_SIZE)) {
+ *error = "Buffer size mismatch";
+ return -EINVAL;
+ }
+ if (sb->log2_sectors_per_block != c->log2_sectors_per_block) {
+ *error = "Block size mismatch";
+ return -EINVAL;
+ }
+ if (sb->log2_sectors_per_chunk != c->log2_sectors_per_chunk) {
+ *error = "Chunk size mismatch";
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static void dm_compress_dtr(struct dm_target *ti);
+
+/*
+ * Arguments:
+ * underlying device
+ * offset from the start of the device
+ * block size
+ * chunk size
+ * number of chunks
+ * algorithm
+ * number of optional arguments
+ */
+
+static int dm_compress_ctr(struct dm_target *ti, unsigned int argc, char **argv)
+{
+ struct dm_compress *c;
+ int r;
+ char dummy;
+ unsigned long long param;
+ unsigned i;
+ unsigned extra_args;
+ size_t dir_ptr;
+ unsigned dir_offset, offset;
+ struct dm_arg_set as;
+ static const struct dm_arg _args[] = {
+ {0, 3, "Invalid number of feature args"},
+ };
+
+ c = kzalloc(sizeof(struct dm_compress), GFP_KERNEL);
+ if (!c) {
+ ti->error = "Cannot allocate compress structure";
+ r = -ENOMEM;
+ goto bad;
+ }
+ ti->private = c;
+ c->ti = ti;
+ ti->per_io_data_size = sizeof(struct dm_compress_io);
+ ti->num_flush_bios = 1;
+ ti->flush_supported = true;
+
+ INIT_LIST_HEAD(&c->free_chunks);
+ INIT_LIST_HEAD(&c->lru);
+ c->chunks = RB_ROOT;
+ mutex_init(&c->mutex);
+ INIT_LIST_HEAD(&c->waiting_bio_lru);
+ c->waiting_bio_tree = RB_ROOT;
+ c->n_cached_chunks = N_CACHED_CHUNKS;
+ c->autocommit_msec = DEFAULT_AUTOCOMMIT_MSEC;
+ INIT_DELAYED_WORK(&c->autocommit_work, dm_compress_autocommit);
+ init_waitqueue_head(&c->wait_idle);
+
+#define DIRECT_ARGUMENTS 6
+
+ if (argc <= DIRECT_ARGUMENTS) {
+ ti->error = "Invalid argument count";
+ r = -EINVAL;
+ goto bad;
+ }
+
+ r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
+ if (r) {
+ ti->error = "Device lookup failed";
+ goto bad;
+ }
+
+ if (sscanf(argv[1], "%llu%c", ¶m, &dummy) != 1 || param != (sector_t)param) {
+ ti->error = "Invalid starting offset";
+ r = -EINVAL;
+ goto bad;
+ }
+ c->start = param;
+
+ if (sscanf(argv[2], "%llu%c", ¶m, &dummy) != 1 || param < 512 || param > MAX_BLOCK_SIZE || (param & (param - 1))) {
+ ti->error = "Invalid block size";
+ r = -EINVAL;
+ goto bad;
+ }
+ if (param < bdev_logical_block_size(c->dev->bdev)) {
+ ti->error = "Block size is smaller than logical block size";
+ r = -EINVAL;
+ goto bad;
+ }
+ c->log2_sectors_per_block = __ffs(param >> 9);
+
+ if (sscanf(argv[3], "%llu%c", ¶m, &dummy) != 1 || param < 512 || param > MAX_CHUNK_SIZE || (param & (param - 1))) {
+ ti->error = "Invalid chunk size";
+ r = -EINVAL;
+ goto bad;
+ }
+ c->log2_sectors_per_chunk = __ffs(param >> 9);
+
+ if (c->log2_sectors_per_block >= c->log2_sectors_per_chunk) {
+ ti->error = "Block size is not less than chunk size";
+ r = -EINVAL;
+ goto bad;
+ }
+ if (c->log2_sectors_per_block + 8 < c->log2_sectors_per_chunk) {
+ ti->error = "Block size must be greater or equal than chunk size divied by 256";
+ r = -EINVAL;
+ goto bad;
+ }
+
+ if (sscanf(argv[4], "%llu%c", ¶m, &dummy) != 1 || !param || param != (sector_t)param) {
+ ti->error = "Invalid number of chunks";
+ r = -EINVAL;
+ goto bad;
+ }
+ c->n_chunks = param;
+
+ for (i = 0; i < dm_compress_n_algorithms; i++) {
+ if (!strcmp(dm_compress_algorithms[i].name, argv[5])) {
+ c->algorithm = i;
+ goto found_alg;
+ }
+ }
+ ti->error = "Invalid compression algorithm";
+ r = -EINVAL;
+ goto bad;
+found_alg:
+
+ as.argc = argc - DIRECT_ARGUMENTS;
+ as.argv = argv + DIRECT_ARGUMENTS;
+ r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
+ if (r)
+ goto bad;
+ while (extra_args--) {
+ const char *opt_string = dm_shift_arg(&as);
+ if (!opt_string) {
+ ti->error = "Not enough feature arguments";
+ r = -EINVAL;
+ goto bad;
+ }
+ if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
+ if (c->meta_dev) {
+ ti->error = "Metadata device specified more than once";
+ r = -EINVAL;
+ goto bad;
+ }
+ r = dm_get_device(ti, strchr(opt_string, ':') + 1, dm_table_get_mode(ti->table), &c->meta_dev);
+ if (r) {
+ ti->error = "Device lookup failed";
+ goto bad;
+ }
+ } else if (sscanf(opt_string, "n_cached_chunks:%llu%c", ¶m, &dummy) == 1 && param >= 1 && param < 1048576) {
+ c->n_cached_chunks = param;
+ } else if (sscanf(opt_string, "commit_time:%llu%c", ¶m, &dummy) == 1 && param == (unsigned)param) {
+ c->autocommit_msec = param;
+ c->autocommit_set = true;
+ } else {
+ ti->error = "Invalid argument";
+ r = -EINVAL;
+ goto bad;
+ }
+ }
+ c->autocommit_jiffies = msecs_to_jiffies(c->autocommit_msec);
+
+ c->wq = alloc_workqueue("dm-compress", WQ_PERCPU | WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
+ if (unlikely(!c->wq)) {
+ ti->error = "Cannot allocate workqueue";
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ c->compress_wq = alloc_workqueue("dm-compress-compress", WQ_PERCPU | WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
+ if (unlikely(!c->compress_wq)) {
+ ti->error = "Cannot allocate workqueue";
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ c->flush_wq = alloc_workqueue("dm-compress-flush", WQ_PERCPU | WQ_MEM_RECLAIM, 1);
+ if (unlikely(!c->flush_wq)) {
+ ti->error = "Cannot allocate workqueue";
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ c->bufio = dm_bufio_client_create(c->meta_dev ? c->meta_dev->bdev : c->dev->bdev, BUFFER_SIZE, 1, 0, NULL, NULL, 0);
+ if (IS_ERR(c->bufio)) {
+ ti->error = "Cannot initialize dm-bufio";
+ r = PTR_ERR(c->bufio);
+ c->bufio = NULL;
+ goto bad;
+ }
+ dm_bufio_set_sector_offset(c->bufio, c->start);
+
+ c->io = dm_io_client_create();
+ if (IS_ERR(c->io)) {
+ ti->error = "Cannot allocate dm io";
+ r = PTR_ERR(c->io);
+ c->io = NULL;
+ goto bad;
+ }
+
+ c->sb = kmalloc(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
+ if (!c->sb) {
+ ti->error = "Cannot allocate superblock";
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ c->cct = vmalloc(sizeof(struct crash_count_table));
+ if (!c->cct) {
+ ti->error = "Cannot allocate crash count table";
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ r = mempool_init_kmalloc_pool(&c->block_io_buffers, 1, 512U << c->log2_sectors_per_block);
+ if (r) {
+ ti->error = "Cannot allocate io buffers";
+ goto bad;
+ }
+
+ c->split_bufio = dm_bufio_client_create(c->dev->bdev, 512U << c->log2_sectors_per_block, 1, 0, NULL, NULL, 0);
+ if (IS_ERR(c->split_bufio)) {
+ ti->error = "Cannot initialize dm-bufio";
+ r = PTR_ERR(c->split_bufio);
+ c->split_bufio = NULL;
+ goto bad;
+ }
+ if (!c->meta_dev)
+ dm_bufio_set_sector_offset(c->split_bufio, c->start);
+
+ c->n_blocks = bdev_nr_sectors(c->dev->bdev);
+ if (!c->meta_dev) {
+ if (c->n_blocks <= c->start) {
+ ti->error = "The device is too small";
+ r = -ENOSPC;
+ goto bad;
+ }
+ c->n_blocks -= c->start;
+ }
+ c->n_blocks >>= c->log2_sectors_per_block;
+ if (!c->n_blocks) {
+ ti->error = "The device is too small";
+ r = -ENOSPC;
+ goto bad;
+ }
+
+ for (i = 0; i < c->n_cached_chunks; i++) {
+ struct cached_chunk *cc = dm_compress_alloc_cached_chunk(c);
+ if (!cc) {
+ ti->error = "Error allocating cached chunk";
+ r = -ENOMEM;
+ goto bad;
+ }
+ list_add(&cc->lru_entry, &c->free_chunks);
+ }
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_READ, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (r) {
+ ti->error = "Error reading superblock";
+ goto bad;
+ }
+
+ if (mem_is_zero(c->sb, SB_SECTORS << SECTOR_SHIFT)) {
+ r = initialize_device(c);
+ if (r)
+ goto bad;
+ }
+
+ r = verify_device(c, &ti->error);
+ if (r)
+ goto bad;
+
+ block_location(c, c->n_blocks - 1, &c->n_groups, &dir_ptr, &dir_offset, &offset);
+ c->n_groups++;
+ dir_ptr++;
+ c->block_directories = vmalloc_array(dir_ptr, sizeof(sector_t));
+ if (!c->block_directories) {
+ r = -ENOMEM;
+ goto bad;
+ }
+ c->groups = vmalloc_array(c->n_groups, sizeof(struct block_group_descriptor));
+ if (!c->groups) {
+ r = -ENOMEM;
+ goto bad;
+ }
+ for (i = 0; i < c->n_groups; i++) {
+ mutex_init(&c->groups[i].mutex);
+ c->groups[i].free = -1;
+ }
+
+ chunk_location(c, c->n_chunks - 1, &dir_ptr, &dir_offset, &offset);
+ dir_ptr++;
+ c->chunk_directories = vmalloc_array(dir_ptr, sizeof(sector_t));
+ if (!c->chunk_directories) {
+ r = -ENOMEM;
+ goto bad;
+ }
+
+ r = dm_set_target_max_io_len(ti, (sector_t)1 << c->log2_sectors_per_chunk);
+ if (r)
+ goto bad;
+ ti->discards_supported = true;
+ ti->num_discard_bios = 1;
+ ti->max_discard_granularity = true;
+
+ return 0;
+
+bad:
+ dm_compress_dtr(ti);
+ return r;
+}
+
+static void dm_compress_dtr(struct dm_target *ti)
+{
+ struct dm_compress *c = ti->private;
+
+ if (!c)
+ return;
+
+ if (c->wq)
+ destroy_workqueue(c->wq);
+ if (c->compress_wq)
+ destroy_workqueue(c->compress_wq);
+ if (c->flush_wq)
+ destroy_workqueue(c->flush_wq);
+ mempool_exit(&c->block_io_buffers);
+ if (c->bufio)
+ dm_bufio_client_destroy(c->bufio);
+ if (c->split_bufio)
+ dm_bufio_client_destroy(c->split_bufio);
+ if (c->io)
+ dm_io_client_destroy(c->io);
+
+ if (c->dev)
+ dm_put_device(ti, c->dev);
+ if (c->meta_dev)
+ dm_put_device(ti, c->meta_dev);
+
+ while (!list_empty(&c->free_chunks)) {
+ struct cached_chunk *cc = list_entry(c->free_chunks.next, struct cached_chunk, lru_entry);
+ list_del(&cc->lru_entry);
+ dm_compress_free_cached_chunk(cc);
+ }
+ while (!RB_EMPTY_ROOT(&c->chunks)) {
+ struct cached_chunk *cc = rb_entry(rb_first(&c->chunks), struct cached_chunk, node);
+ list_del(&cc->lru_entry);
+ rb_erase(&cc->node, &c->chunks);
+ dm_compress_free_cached_chunk(cc);
+ }
+
+ kfree(c->sb);
+ vfree(c->cct);
+ vfree(c->block_directories);
+ vfree(c->chunk_directories);
+ if (c->groups) {
+ size_t i;
+ for (i = 0; i < c->n_groups; i++)
+ mutex_destroy(&c->groups[i].mutex);
+ vfree(c->groups);
+ }
+
+ mutex_destroy(&c->mutex);
+
+ kfree(c);
+}
+
+
+static int load_directories(struct dm_compress *c, sector_t block, sector_t *directory, size_t n, __be64 magic)
+{
+ size_t i = 0;
+read_next:
+ directory[i++] = block;
+ if (i < n) {
+ struct dm_buffer *bp;
+ struct directory *dir = dm_bufio_read(c->bufio, block, &bp);
+ if (IS_ERR(dir))
+ return PTR_ERR(dir);
+ if (unlikely(dir->magic != magic)) {
+ dm_compress_error(c, "Directory magic doesn't match", -EUCLEAN);
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ if (unlikely(le64_to_cpu(dir->index) != i - 1)) {
+ dm_compress_error(c, "Directory index doesn't match", -EUCLEAN);
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ block = READ_PTR(&dir->next);
+ dm_bufio_release(bp);
+ goto read_next;
+ }
+ return 0;
+}
+
+static int load_bitmap_directories(struct dm_compress *c, sector_t block)
+{
+ size_t group_ptr, dir_ptr;
+ unsigned dir_offset, offset;
+ int r;
+
+ if (!block) {
+ dm_compress_error(c, "The superblock is corrupted - zero blocks", -EINVAL);
+ return -EINVAL;
+ }
+
+ block_location(c, block - 1, &group_ptr, &dir_ptr, &dir_offset, &offset);
+ dir_ptr++;
+ r = load_directories(c, le64_to_cpu(c->sb->bitmap_directory), c->block_directories, dir_ptr, BMP_DIR_MAGIC);
+ if (r) {
+ dm_compress_error(c, "Unable to read bitmap directories", r);
+ return r;
+ }
+ return 0;
+}
+
+static int load_chunk_directories(struct dm_compress *c, sector_t chunk)
+{
+ size_t dir_ptr;
+ unsigned dir_offset, offset;
+ int r;
+
+ if (!chunk) {
+ dm_compress_error(c, "The superblock is corrupted - zero chunks", -EINVAL);
+ return -EINVAL;
+ }
+
+ chunk_location(c, chunk - 1, &dir_ptr, &dir_offset, &offset);
+ dir_ptr++;
+ r = load_directories(c, le64_to_cpu(c->sb->chunk_directory), c->chunk_directories, dir_ptr, CHUNK_DIR_MAGIC);
+ if (r) {
+ dm_compress_error(c, "Unable to read chunk directories", r);
+ return r;
+ }
+ return 0;
+}
+
+static int dm_compress_preresume(struct dm_target *ti)
+{
+ struct dm_compress *c = ti->private;
+ struct superblock *sb = c->sb;
+ bool need_sync = false;
+ bool b;
+ int r;
+ char *error;
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_READ, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (r) {
+ dm_compress_error(c, "Error reading superblock", r);
+ return r;
+ }
+
+ r = verify_device(c, &error);
+ if (r) {
+ dm_compress_error(c, error, r);
+ return r;
+ }
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_READ, DM_IO_VMA, le64_to_cpu(sb->cct) * (BUFFER_SIZE >> SECTOR_SHIFT), c->cct, CCT_SIZE >> SECTOR_SHIFT);
+ if (r) {
+ dm_compress_error(c, "Error reading crash count table", r);
+ return r;
+ }
+
+ c->cc_txc = le16_to_cpup(&c->sb->cc);
+ c->cc_txc |= le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) & ~0xffffULL;
+
+ c->sb->cc = cpu_to_le16(le16_to_cpup(&c->sb->cc) + 1);
+ c->cc_txc += 0x10000;
+ c->cct->entries[c->cc_txc & 0xffff] = cpu_to_le64(le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) + 0x10000);
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error writing superblock", r);
+ return r;
+ }
+
+ b = !CC_VALID(c, &sb->n_cc_txc);
+ if (le64_to_cpu(sb->n_blocks[b]) != c->n_blocks) {
+ if (le64_to_cpu(sb->n_blocks[b]) > c->n_blocks) {
+ DMERR("Shrinking is not yet supported");
+ return -EOPNOTSUPP;
+ }
+ r = load_bitmap_directories(c, le64_to_cpu(sb->n_blocks[b]));
+ if (unlikely(r))
+ return r;
+ r = dm_compress_resize_bitmaps(c, le64_to_cpu(sb->n_blocks[b]), c->n_blocks, &need_sync);
+ if (unlikely(r))
+ return r;
+ }
+ b = !CC_VALID(c, &sb->n_cc_txc);
+ if (le64_to_cpu(sb->n_chunks[b]) != c->n_chunks) {
+ DMERR("Resizing is not yet supported");
+ return -EOPNOTSUPP;
+ }
+
+ r = load_bitmap_directories(c, c->n_blocks);
+ if (r)
+ return r;
+
+ r = load_chunk_directories(c, c->n_chunks);
+ if (r)
+ return r;
+
+ r = dm_compress_get_free_space(c);
+ if (r)
+ return r;
+
+ if (need_sync)
+ dm_compress_sync(c);
+
+ return 0;
+}
+
+static void dm_compress_postsuspend(struct dm_target *ti)
+{
+ struct dm_compress *c = ti->private;
+ int r;
+
+check_again:
+ mutex_lock(&c->mutex);
+ if (dm_compress_is_something_in_progress(c)) {
+ mutex_unlock(&c->mutex);
+ schedule_timeout_uninterruptible(1);
+ goto check_again;
+ }
+ dm_compress_sync(c);
+ mutex_unlock(&c->mutex);
+
+ cancel_delayed_work_sync(&c->autocommit_work);
+
+ c->cct->entries[c->cc_txc & 0xffff] = cpu_to_le64(le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) - 0x10000);
+ c->sb->cc = cpu_to_le16(le16_to_cpup(&c->sb->cc) - 1);
+ c->cc_txc -= 0x10000;
+
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (unlikely(r)) {
+ dm_compress_error(c, "Error writing superblock", r);
+ return;
+ }
+}
+
+static void dm_compress_status(struct dm_target *ti, status_type_t type,
+ unsigned int status_flags, char *result, unsigned int maxlen)
+{
+ struct dm_compress *c = ti->private;
+ size_t sz = 0;
+
+ switch (type) {
+ case STATUSTYPE_INFO: {
+ DMEMIT("%llu %llu",
+ (unsigned long long)c->n_blocks,
+ (unsigned long long)atomic64_read(&c->free_blocks));
+ break;
+ }
+
+ case STATUSTYPE_TABLE: {
+ unsigned arg_count;
+ DMEMIT("%s", c->dev->name);
+ DMEMIT(" %llu", (unsigned long long)c->start);
+ DMEMIT(" %u", 512U << c->log2_sectors_per_block);
+ DMEMIT(" %u", 512U << c->log2_sectors_per_chunk);
+ DMEMIT(" %llu", (unsigned long long)c->n_chunks);
+ DMEMIT(" %s", dm_compress_algorithms[c->algorithm].name);
+ arg_count = 0;
+ arg_count += !!c->meta_dev;
+ arg_count += c->n_cached_chunks != N_CACHED_CHUNKS;
+ arg_count += c->autocommit_set;
+ DMEMIT(" %u", arg_count);
+ if (c->meta_dev)
+ DMEMIT(" meta_device:%s", c->meta_dev->name);
+ if (c->n_cached_chunks != N_CACHED_CHUNKS)
+ DMEMIT(" n_cached_chunks:%u", c->n_cached_chunks);
+ if (c->autocommit_set)
+ DMEMIT(" commit_time:%u", c->autocommit_msec);
+ break;
+ }
+
+ case STATUSTYPE_IMA:
+ break;
+ }
+}
+
+static void dm_compress_io_hints(struct dm_target *ti, struct queue_limits *limits)
+{
+ struct dm_compress *c = ti->private;
+ limits->io_min = 512U << c->log2_sectors_per_chunk;
+ limits->io_opt = 512U << c->log2_sectors_per_chunk;
+ limits->discard_granularity = 512U << c->log2_sectors_per_chunk;
+ limits->max_hw_discard_sectors = limits->discard_granularity >> SECTOR_SHIFT;
+}
+
+static struct target_type compress_target = {
+ .name = "compress",
+ .version = {1, 0, 0},
+ .module = THIS_MODULE,
+ .features = 0,
+ .ctr = dm_compress_ctr,
+ .dtr = dm_compress_dtr,
+ .map = dm_compress_map,
+ .preresume = dm_compress_preresume,
+ .postsuspend = dm_compress_postsuspend,
+ .status = dm_compress_status,
+ .io_hints = dm_compress_io_hints,
+};
+
+static int __init dm_compress_init(void)
+{
+ int r;
+ unsigned i;
+
+ for (i = 0; i < dm_compress_n_algorithms; i++) {
+ unsigned request_size;
+ dm_compress_algorithms[i].cc = crypto_alloc_acomp(dm_compress_algorithms[i].name, 0, 0);
+ if (IS_ERR(dm_compress_algorithms[i].cc)) {
+ r = PTR_ERR(dm_compress_algorithms[i].cc);
+ dm_compress_algorithms[i].cc = NULL;
+ DMERR("Could not initialize algorithm %s", dm_compress_algorithms[i].name);
+ goto bad;
+ }
+ request_size = ALIGN(sizeof(struct acomp_req) + crypto_acomp_reqsize(dm_compress_algorithms[i].cc), CRYPTO_MINALIGN);
+ dm_compress_max_request_size = max(dm_compress_max_request_size, request_size);
+ }
+
+ r = dm_register_target(&compress_target);
+ if (r < 0)
+ goto bad;
+
+ return 0;
+
+bad:
+ for (i = 0; i < dm_compress_n_algorithms; i++) {
+ if (dm_compress_algorithms[i].cc)
+ crypto_free_acomp(dm_compress_algorithms[i].cc);
+ }
+ return r;
+}
+
+static void __exit dm_compress_exit(void)
+{
+ unsigned i;
+
+ dm_unregister_target(&compress_target);
+
+ for (i = 0; i < dm_compress_n_algorithms; i++)
+ crypto_free_acomp(dm_compress_algorithms[i].cc);
+}
+
+module_init(dm_compress_init);
+module_exit(dm_compress_exit);
+
+MODULE_AUTHOR("Mikulas Patocka");
+MODULE_DESCRIPTION(DM_NAME " target for transparent data compression");
+MODULE_LICENSE("GPL");
Index: linux-2.6/drivers/md/Makefile
===================================================================
--- linux-2.6.orig/drivers/md/Makefile
+++ linux-2.6/drivers/md/Makefile
@@ -85,6 +85,7 @@ obj-$(CONFIG_DM_INTEGRITY) += dm-integri
obj-$(CONFIG_DM_ZONED) += dm-zoned.o
obj-$(CONFIG_DM_WRITECACHE) += dm-writecache.o
obj-$(CONFIG_SECURITY_LOADPIN_VERITY) += dm-verity-loadpin.o
+obj-$(CONFIG_DM_COMPRESS) += dm-compress/
ifeq ($(CONFIG_DM_INIT),y)
dm-mod-objs += dm-init.o
Index: linux-2.6/drivers/md/dm-compress/Makefile
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/Makefile
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_DM_COMPRESS) += dm-compress.o
+
+dm-compress-objs := \
+ alloc.o \
+ cc.o \
+ chunk.o \
+ io.o \
+ target.o
Index: linux-2.6/drivers/md/dm-compress/alloc.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/alloc.c
@@ -0,0 +1,480 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-compress.h"
+
+static int prepare_bitmap(struct dm_compress *c, sector_t bmp_block, __u64 **working_copy, __u64 **stable_copy, struct dm_buffer **bp)
+{
+ struct bitmap *bmp;
+ bool idx;
+
+ bmp = dm_bufio_read(c->bufio, bmp_block, bp);
+ if (IS_ERR(bmp)) {
+ dm_compress_error(c, "Error reading bitmap", PTR_ERR(bmp));
+ return PTR_ERR(bmp);
+ }
+ if (unlikely(bmp->magic != BITMAP_MAGIC)) {
+ dm_bufio_release(*bp);
+ dm_compress_error(c, "Bitmap magic doesn't match", -EUCLEAN);
+ return -EUCLEAN;
+ }
+
+ //printk("prepare_bitmap (%llx, %llx): %d, %d\n", bmp->cc_txc, c->cc_txc, !CC_VALID(c, &bmp->cc_txc), !CC_CURRENT(c, &bmp->cc_txc));
+ idx = !CC_VALID(c, &bmp->cc_txc);
+ if (unlikely(!CC_CURRENT(c, &bmp->cc_txc))) {
+ CC_SET_CURRENT(c, &bmp->cc_txc);
+ //WARN_ON(idx != CC_VALID(c, &bmp->cc_txc));
+ if (!idx)
+ memcpy(bmp->bmp1, bmp->bmp2, BITS_PER_BITMAP / 8);
+ else
+ memcpy(bmp->bmp2, bmp->bmp1, BITS_PER_BITMAP / 8);
+ idx = !idx;
+ dm_bufio_mark_buffer_dirty(*bp);
+ }
+ if (idx)
+ *working_copy = bmp->bmp1, *stable_copy = bmp->bmp2;
+ else
+ *working_copy = bmp->bmp2, *stable_copy = bmp->bmp1;
+
+ return 0;
+}
+
+static bool verify_bmp_dir_magic(struct dm_compress *c, struct directory *dir, sector_t index)
+{
+ if (unlikely(dir->magic != BMP_DIR_MAGIC)) {
+ dm_compress_error(c, "Bitmap directory magic doesn't match", -EUCLEAN);
+ return false;
+ }
+ if (unlikely(le64_to_cpu(dir->index) != index)) {
+ dm_compress_error(c, "Bitmap directory index doesn't match", -EUCLEAN);
+ return false;
+ }
+ return true;
+}
+
+sector_t dm_compress_allocate_space(struct dm_compress *c, sector_t preferred_block, unsigned n_blocks)
+{
+ struct dm_buffer *bp;
+ size_t group_ptr, dir_ptr;
+ unsigned dir_offset, bmp_offset;
+ sector_t dir_block, bmp_block;
+ struct block_group_descriptor *group;
+ struct directory *dir;
+ __u64 *working_copy, *stable_copy;
+ unsigned bit;
+ size_t n_retries = 0;
+
+ if (unlikely(n_blocks > BITS_PER_BITMAP)) {
+ dm_compress_error(c, "Attempting to allocate too much space", -EUCLEAN);
+ return ALLOCATE_ERROR;
+ }
+
+try_next_bitmap:
+ if (unlikely(preferred_block >= c->n_blocks))
+ preferred_block = 0;
+
+ block_location(c, preferred_block, &group_ptr, &dir_ptr, &dir_offset, &bmp_offset);
+ group = &c->groups[group_ptr];
+ dir_block = c->block_directories[dir_ptr];
+ preferred_block -= bmp_offset;
+
+ dir = dm_bufio_read(c->bufio, dir_block, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ return ALLOCATE_ERROR;
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr))) {
+ dm_bufio_release(bp);
+ return ALLOCATE_ERROR;
+ }
+ bmp_block = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(bp);
+
+ mutex_lock(&group->mutex);
+ if (unlikely(prepare_bitmap(c, bmp_block, &working_copy, &stable_copy, &bp))) {
+ mutex_unlock(&group->mutex);
+ return ALLOCATE_ERROR;
+ }
+
+find_again:
+ bit = find_next_zero_bit_le(working_copy, BITS_PER_BITMAP, bmp_offset);
+ if (bit <= BITS_PER_BITMAP - n_blocks) {
+ unsigned i;
+ for (i = n_blocks; i--; ) {
+ if (test_bit_le(bit + i, working_copy) || test_bit_le(bit + i, stable_copy)) {
+ bmp_offset = bit + i + 1;
+ goto find_again;
+ }
+ }
+ for (i = 0; i < n_blocks; i++) {
+ __set_bit_le(bit + i, working_copy);
+ }
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ mutex_unlock(&group->mutex);
+
+ atomic64_sub(n_blocks, &c->free_blocks);
+
+ return preferred_block + bit;
+ }
+
+ dm_bufio_release(bp);
+ mutex_unlock(&group->mutex);
+
+ if (++n_retries <= c->n_groups) {
+ preferred_block += BITS_PER_BITMAP;
+ goto try_next_bitmap;
+ }
+
+ return ALLOCATE_NO_SPACE;
+}
+
+void dm_compress_free_space(struct dm_compress *c, sector_t block, unsigned n_blocks)
+{
+ struct dm_buffer *bp;
+ size_t group_ptr, dir_ptr;
+ unsigned dir_offset, bmp_offset;
+ sector_t dir_block, bmp_block;
+ struct block_group_descriptor *group;
+ struct directory *dir;
+ __u64 *working_copy, *stable_copy;
+ sector_t start_block = block;
+
+ if (unlikely(block + n_blocks < block) ||
+ unlikely(block + n_blocks > c->n_blocks)) {
+ dm_compress_error(c, "Freeing invalid range", -EUCLEAN);
+ return;
+ }
+
+ while (n_blocks) {
+ block_location(c, block, &group_ptr, &dir_ptr, &dir_offset, &bmp_offset);
+ group = &c->groups[group_ptr];
+ dir_block = c->block_directories[dir_ptr];
+
+ dir = dm_bufio_read(c->bufio, dir_block, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ goto ret;
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr))) {
+ dm_bufio_release(bp);
+ goto ret;
+ }
+ bmp_block = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(bp);
+
+ mutex_lock(&group->mutex);
+ if (unlikely(prepare_bitmap(c, bmp_block, &working_copy, &stable_copy, &bp))) {
+ mutex_unlock(&group->mutex);
+ goto ret;
+ }
+ while (n_blocks && bmp_offset < BITS_PER_BITMAP) {
+ if (!__test_and_clear_bit_le(bmp_offset, working_copy)) {
+ dm_compress_error(c, "Bitmap bit is already cleared", -EUCLEAN);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ mutex_unlock(&group->mutex);
+ goto ret;
+ }
+ bmp_offset++;
+ block++;
+ n_blocks--;
+ }
+
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ mutex_unlock(&group->mutex);
+ }
+ret:
+ atomic64_add(block - start_block, &c->free_blocks);
+}
+
+int dm_compress_get_free_space(struct dm_compress *c)
+{
+ sector_t block = 0;
+ sector_t free_blocks = 0;
+ while (block < c->n_blocks) {
+ size_t group_ptr, dir_ptr;
+ unsigned dir_offset, bmp_offset;
+ sector_t dir_block, bmp_block;
+ struct dm_buffer *bp;
+ struct directory *dir;
+ struct bitmap *bmp;
+ bool idx;
+ __u64 *bmp_data;
+
+ block_location(c, block, &group_ptr, &dir_ptr, &dir_offset, &bmp_offset);
+ dir_block = c->block_directories[dir_ptr];
+
+ dir = dm_bufio_read(c->bufio, dir_block, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ return PTR_ERR(dir);
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr))) {
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ bmp_block = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(bp);
+
+ bmp = dm_bufio_read(c->bufio, bmp_block, &bp);
+ if (IS_ERR(bmp)) {
+ dm_compress_error(c, "Error reading bitmap", PTR_ERR(bmp));
+ return PTR_ERR(bmp);
+ }
+ if (unlikely(bmp->magic != BITMAP_MAGIC)) {
+ dm_bufio_release(bp);
+ dm_compress_error(c, "Bitmap magic doesn't match when calculating free space", -EUCLEAN);
+ return -EUCLEAN;
+ }
+ idx = !CC_VALID(c, &bmp->cc_txc);
+ if (idx)
+ bmp_data = bmp->bmp1;
+ else
+ bmp_data = bmp->bmp2;
+ free_blocks += BITS_PER_BITMAP - bitmap_weight((void *)bmp_data, BITS_PER_BITMAP);
+ dm_bufio_release(bp);
+
+ block += BITS_PER_BITMAP;
+ }
+
+ atomic64_set(&c->free_blocks, free_blocks);
+
+ return 0;
+}
+
+struct chunk_ptr *dm_compress_prepare_chunk(struct dm_compress *c, sector_t chunk, struct dm_buffer **bp)
+{
+ struct dm_buffer *dbp;
+ size_t dir_ptr;
+ unsigned dir_offset, chunk_offset;
+ sector_t dir_block, chunk_block;
+ struct directory *dir;
+ struct chunk_entries *chunks;
+ struct chunk_entry *ce;
+ bool idx;
+
+ chunk_location(c, chunk, &dir_ptr, &dir_offset, &chunk_offset);
+
+ dir_block = c->chunk_directories[dir_ptr];
+
+ dir = dm_bufio_read(c->bufio, dir_block, &dbp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading chunk directory", PTR_ERR(dir));
+ return NULL;
+ }
+ if (unlikely(dir->magic != CHUNK_DIR_MAGIC)) {
+ dm_bufio_release(dbp);
+ dm_compress_error(c, "Chunk directory magic doesn't match", -EUCLEAN);
+ return NULL;
+ }
+ chunk_block = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(dbp);
+
+ chunks = dm_bufio_read(c->bufio, chunk_block, bp);
+ if (IS_ERR(chunks)) {
+ dm_compress_error(c, "Error reading chunk block", PTR_ERR(chunks));
+ return NULL;
+ }
+ if (unlikely(chunks->magic != CHUNKS_MAGIC)) {
+ dm_bufio_release(*bp);
+ dm_compress_error(c, "Chunks magic doesn't match", -EUCLEAN);
+ return NULL;
+ }
+ ce = &chunks->chunk[chunk_offset];
+
+ idx = !CC_VALID(c, &ce->cc_txc);
+ if (unlikely(!CC_CURRENT(c, &ce->cc_txc))) {
+ CC_SET_CURRENT(c, &ce->cc_txc);
+ WARN_ON(idx != CC_VALID(c, &ce->cc_txc));
+ ce->ptr[idx] = ce->ptr[!idx];
+ idx = !idx;
+ dm_bufio_mark_buffer_dirty(*bp);
+ }
+ return &ce->ptr[!idx];
+}
+
+int dm_compress_resize_bitmaps(struct dm_compress *c, sector_t current_size, sector_t new_size, bool *need_sync)
+{
+ struct superblock *sb = c->sb;
+ int r;
+ sector_t allocated_sector = 0;
+ sector_t n_allocated_sectors = 0;
+ bool did_something = false;
+ if (c->meta_dev) {
+ sector_t meta_size;
+ bool b;
+ b = !CC_VALID(c, &sb->n_cc_txc);
+ allocated_sector = le64_to_cpu(sb->n_used_metadata_blocks[b]) * BUFFER_SECTORS;
+ meta_size = bdev_nr_sectors(c->meta_dev->bdev);
+ if (meta_size < c->start) {
+ dm_compress_error(c, "The metadata device is too small", -ENOSPC);
+ return -ENOSPC;
+ }
+ meta_size -= c->start;
+ if (meta_size < allocated_sector) {
+ dm_compress_error(c, "The metadata device is too small", -ENOSPC);
+ return -ENOSPC;
+ }
+ n_allocated_sectors = meta_size - allocated_sector;
+ }
+ while (current_size < new_size) {
+ struct dm_buffer *bp;
+ size_t group_ptr, dir_ptr;
+ unsigned dir_offset, bmp_offset;
+ sector_t dir_block, bmp_block;
+ struct directory *dir;
+ __u64 *working_copy, *stable_copy;
+ unsigned need_sectors;
+
+ block_location(c, current_size, &group_ptr, &dir_ptr, &dir_offset, &bmp_offset);
+
+ need_sectors = 0;
+ if (!bmp_offset && !dir_offset)
+ need_sectors = BUFFER_SECTORS * 2;
+ else if (!bmp_offset)
+ need_sectors = BUFFER_SECTORS;
+ if (n_allocated_sectors < need_sectors) {
+ if (c->meta_dev) {
+ dm_compress_error(c, "The metadata device is too small", -ENOSPC);
+ return -ENOSPC;
+ }
+align_allocated:
+ allocated_sector = current_size << c->log2_sectors_per_block;
+ if (allocated_sector & (BUFFER_SECTORS - 1)) {
+ if (current_size >= new_size)
+ goto finish;
+ current_size++;
+ goto align_allocated;
+ }
+ while (n_allocated_sectors < need_sectors) {
+ if (current_size >= new_size)
+ goto finish;
+ current_size++;
+ n_allocated_sectors += 1 << c->log2_sectors_per_block;
+ }
+ }
+
+ did_something = true;
+ if (!bmp_offset && !dir_offset) {
+ dir = dm_bufio_new(c->bufio, allocated_sector / BUFFER_SECTORS, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Unable to create a bitmap directory", PTR_ERR(dir));
+ return PTR_ERR(dir);
+ }
+ memset(dir, 0, BUFFER_SIZE);
+ dir->magic = BMP_DIR_MAGIC;
+ dir->index = cpu_to_le64(dir_ptr);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ c->block_directories[dir_ptr] = allocated_sector / BUFFER_SECTORS;
+ dir = dm_bufio_read(c->bufio, c->block_directories[dir_ptr - 1], &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ return PTR_ERR(dir);
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr - 1))) {
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ WRITE_PTR(&dir->next, allocated_sector / BUFFER_SECTORS);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ allocated_sector += BUFFER_SECTORS;
+ n_allocated_sectors -= BUFFER_SECTORS;
+ }
+ if (!bmp_offset) {
+ struct bitmap *bmp;
+ bmp = dm_bufio_new(c->bufio, allocated_sector / BUFFER_SECTORS, &bp);
+ if (IS_ERR(bmp)) {
+ dm_compress_error(c, "Unable to create a bitmap", PTR_ERR(bmp));
+ return PTR_ERR(bmp);
+ }
+ memset(bmp, 0xff, BUFFER_SIZE);
+ bmp->magic = BITMAP_MAGIC;
+ bmp->cc_txc = cpu_to_le64(0);
+ CC_SET_CURRENT(c, &bmp->cc_txc);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+
+ dir_block = c->block_directories[dir_ptr];
+ dir = dm_bufio_read(c->bufio, dir_block, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ return PTR_ERR(dir);
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr))) {
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ WRITE_PTR(&dir->ptr[dir_offset], allocated_sector / BUFFER_SECTORS);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ allocated_sector += BUFFER_SECTORS;
+ n_allocated_sectors -= BUFFER_SECTORS;
+ }
+
+ block_location(c, current_size, &group_ptr, &dir_ptr, &dir_offset, &bmp_offset);
+
+ dir_block = c->block_directories[dir_ptr];
+ dir = dm_bufio_read(c->bufio, dir_block, &bp);
+ if (IS_ERR(dir)) {
+ dm_compress_error(c, "Error reading bitmap directory", PTR_ERR(dir));
+ return PTR_ERR(dir);
+ }
+ if (unlikely(!verify_bmp_dir_magic(c, dir, dir_ptr))) {
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ bmp_block = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(bp);
+
+ r = prepare_bitmap(c, bmp_block, &working_copy, &stable_copy, &bp);
+ if (r)
+ return r;
+
+ while (current_size < new_size && bmp_offset < BITS_PER_BITMAP) {
+ if (!__test_and_clear_bit_le(bmp_offset, working_copy)) {
+ dm_compress_error(c, "Bitmap bit is already cleared", -EUCLEAN);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ return -EUCLEAN;
+ }
+ bmp_offset++;
+ current_size++;
+ }
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ }
+
+finish:
+ if (did_something) {
+ bool b;
+ *need_sync = true;
+ b = !CC_VALID(c, &sb->n_cc_txc);
+ if (unlikely(!CC_CURRENT(c, &sb->n_cc_txc))) {
+ CC_SET_CURRENT(c, &sb->n_cc_txc);
+ WARN_ON(b != CC_VALID(c, &sb->n_cc_txc));
+ sb->n_blocks[!b] = sb->n_blocks[b];
+ sb->n_chunks[!b] = sb->n_chunks[b];
+ sb->n_used_metadata_blocks[!b] = sb->n_used_metadata_blocks[b];
+ b = !b;
+ }
+ sb->n_blocks[b] = cpu_to_le64(current_size);
+ if (c->meta_dev)
+ sb->n_used_metadata_blocks[b] = cpu_to_le64(allocated_sector / BUFFER_SECTORS);
+ r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
+ if (r) {
+ dm_compress_error(c, "Error writing superblock", r);
+ return r;
+ }
+ c->n_blocks = current_size;
+ }
+ return 0;
+}
Index: linux-2.6/drivers/md/dm-compress/cc.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/cc.c
@@ -0,0 +1,251 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-compress.h"
+
+static struct scatterlist *dm_compress_alloc_sglist(void *data, unsigned len, unsigned offset)
+{
+ struct scatterlist *sg;
+ if (!is_vmalloc_addr(data)) {
+ sg = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
+ if (!sg)
+ return NULL;
+ sg_init_one(sg, data + offset, len - offset);
+ } else {
+ unsigned i;
+ unsigned n_entries = DIV_ROUND_UP(len, PAGE_SIZE);
+ size_t s = array_size(sizeof(struct scatterlist), n_entries);
+ sg = kmalloc(s, GFP_KERNEL);
+ if (!sg)
+ return NULL;
+ sg_init_table(sg, n_entries);
+ for (i = 0; i < n_entries; i++) {
+ struct page *pg = vmalloc_to_page(data);
+ unsigned pg_len = min(len, PAGE_SIZE);
+ sg_set_page(&sg[i], pg, pg_len - offset, offset);
+ data += PAGE_SIZE;
+ len -= PAGE_SIZE;
+ offset = 0;
+ }
+ }
+ return sg;
+}
+
+struct cached_chunk *dm_compress_alloc_cached_chunk(struct dm_compress *c)
+{
+ struct cached_chunk *cc;
+ unsigned short v;
+ struct bio_vec *bv;
+
+ cc = kzalloc(sizeof(struct cached_chunk) + (1U << c->log2_sectors_per_chunk), GFP_KERNEL);
+ if (!cc)
+ return NULL;
+
+ cc->c = c;
+
+ cc->uncompressed_data = __vmalloc(512UL << c->log2_sectors_per_chunk, GFP_KERNEL);
+ if (!cc->uncompressed_data)
+ goto free_fail;
+
+ cc->uncompressed_sglist = dm_compress_alloc_sglist(cc->uncompressed_data, 512UL << c->log2_sectors_per_chunk, 0);
+ if (!cc->uncompressed_sglist)
+ goto free_fail;
+
+ cc->compressed_data = __vmalloc(512UL << c->log2_sectors_per_chunk, GFP_KERNEL);
+ if (!cc->compressed_data)
+ goto free_fail;
+
+ cc->compressed_sglist = dm_compress_alloc_sglist(cc->compressed_data, 512UL << c->log2_sectors_per_chunk, 4);
+ if (!cc->compressed_sglist)
+ goto free_fail;
+
+ cc->merging_data = __vmalloc(512UL << c->log2_sectors_per_chunk, GFP_KERNEL);
+ if (!cc->merging_data)
+ goto free_fail;
+
+ cc->merging_sglist = dm_compress_alloc_sglist(cc->merging_data, 512UL << c->log2_sectors_per_chunk, 0);
+ if (!cc->merging_sglist)
+ goto free_fail;
+
+ cc->req = kmalloc(dm_compress_max_request_size, GFP_KERNEL);
+ if (!cc->req)
+ goto free_fail;
+
+ if (is_vmalloc_addr(cc->compressed_data)) {
+ cc->bio_vectors = DIV_ROUND_UP(512UL << c->log2_sectors_per_chunk, PAGE_SIZE);
+ cc->bio = bio_kmalloc(cc->bio_vectors, GFP_KERNEL);
+ if (!cc->bio)
+ goto free_fail;
+ bv = bio_inline_vecs(cc->bio);
+ for (v = 0; v < cc->bio_vectors; v++) {
+ bv[v].bv_page = vmalloc_to_page(cc->compressed_data + (size_t)v * PAGE_SIZE);
+ bv[v].bv_len = PAGE_SIZE;
+ bv[v].bv_offset = 0;
+ }
+ } else {
+ cc->bio_vectors = 1;
+ cc->bio = bio_kmalloc(cc->bio_vectors, GFP_KERNEL);
+ if (!cc->bio)
+ goto free_fail;
+ bv = bio_inline_vecs(cc->bio);
+ bv[0].bv_page = virt_to_page(cc->compressed_data);
+ bv[0].bv_len = 512UL << c->log2_sectors_per_chunk;
+ bv[0].bv_offset = 0;
+ }
+
+ return cc;
+
+free_fail:
+ dm_compress_free_cached_chunk(cc);
+ return NULL;
+}
+
+void dm_compress_free_cached_chunk(struct cached_chunk *cc)
+{
+ kvfree(cc->uncompressed_data);
+ kfree(cc->uncompressed_sglist);
+ kvfree(cc->compressed_data);
+ kfree(cc->compressed_sglist);
+ kvfree(cc->merging_data);
+ kfree(cc->merging_sglist);
+ kfree(cc->req);
+ kfree(cc->bio);
+ kfree(cc);
+}
+
+struct cached_chunk *dm_compress_find_cached_chunk(struct dm_compress *c, sector_t chunk, struct cached_chunk *for_insert)
+{
+ struct rb_node *parent = NULL;
+ struct rb_node **node = &c->chunks.rb_node;
+
+ while (*node) {
+ struct cached_chunk *cc = container_of(*node, struct cached_chunk, node);
+ if (cc->chunk == chunk) {
+ list_del(&cc->lru_entry);
+ list_add(&cc->lru_entry, &c->lru);
+ return cc;
+ }
+ parent = &cc->node;
+ if (cc->chunk > chunk)
+ node = &parent->rb_left;
+ else
+ node = &parent->rb_right;
+ }
+
+ if (for_insert) {
+ rb_link_node(&for_insert->node, parent, node);
+ rb_insert_color(&for_insert->node, &c->chunks);
+ list_add(&for_insert->lru_entry, &c->lru);
+ for_insert->in_tree = true;
+ }
+
+ return NULL;
+}
+
+struct cached_chunk *dm_compress_new_cached_chunk(struct dm_compress_io *dio)
+{
+ struct dm_compress *c = dio->c;
+ struct cached_chunk *cc;
+ if (!list_empty(&c->free_chunks)) {
+ cc = list_entry(c->free_chunks.next, struct cached_chunk, lru_entry);
+ list_del(&cc->lru_entry);
+ cc->chunk = dio->chunk;
+ cc->in_tree = false;
+ cc->dirty = false;
+ cc->intent = INTENT_IDLE;
+ cc->failed = 0;
+ memset(cc->sector_tags, 0, 1U << c->log2_sectors_per_chunk);
+ return cc;
+ }
+ return NULL;
+}
+
+void dm_compress_release_cached_chunk(struct cached_chunk *cc)
+{
+ struct dm_compress *c = cc->c;
+ if (cc->in_tree) {
+ rb_erase(&cc->node, &c->chunks);
+ list_del(&cc->lru_entry);
+ cc->in_tree = false;
+ }
+ list_add(&cc->lru_entry, &c->free_chunks);
+}
+
+void dm_compress_wait_for_cached_chunk(struct dm_compress_io *dio)
+{
+ struct dm_compress *c = dio->c;
+ struct rb_node *parent, **node;
+
+ list_add(&dio->waiting_list_entry, &c->waiting_bio_lru);
+
+ parent = NULL;
+ node = &c->waiting_bio_tree.rb_node;
+
+ while (*node) {
+ struct dm_compress_io *tdio = container_of(*node, struct dm_compress_io, tree_entry);
+ parent = &tdio->tree_entry;
+ if (tdio->chunk > dio->chunk) {
+ node = &parent->rb_left;
+ } else {
+ node = &parent->rb_right;
+ }
+ }
+
+ rb_link_node(&dio->tree_entry, parent, node);
+ rb_insert_color(&dio->tree_entry, &c->waiting_bio_tree);
+}
+
+struct dm_compress_io *dm_compress_pick_dio_for_chunk(struct dm_compress *c, sector_t chunk)
+{
+ struct rb_node *node = c->waiting_bio_tree.rb_node;
+
+ while (node) {
+ struct dm_compress_io *tdio = container_of(node, struct dm_compress_io, tree_entry);
+ if (tdio->chunk == chunk) {
+ return tdio;
+ }
+ if (tdio->chunk > chunk)
+ node = tdio->tree_entry.rb_left;
+ else
+ node = tdio->tree_entry.rb_right;
+ }
+
+ return NULL;
+}
+
+struct dm_compress_io *dm_compress_pick_oldest_dio(struct dm_compress *c)
+{
+ struct dm_compress_io *dio;
+
+ if (list_empty(&c->waiting_bio_lru))
+ return NULL;
+
+ dio = list_entry(c->waiting_bio_lru.prev, struct dm_compress_io, waiting_list_entry);
+
+ return dio;
+}
+
+void dm_compress_unlink_dio(struct dm_compress_io *dio)
+{
+ struct dm_compress *c = dio->c;
+ list_del(&dio->waiting_list_entry);
+ rb_erase(&dio->tree_entry, &c->waiting_bio_tree);
+}
+
+bool dm_compress_is_something_in_progress(struct dm_compress *c)
+{
+ bool ret = false;
+ struct cached_chunk *cc;
+ list_for_each_entry(cc, &c->lru, lru_entry) {
+ if (cc->dirty && !c->failed && !cc->failed)
+ dm_compress_enqueue_chunk(cc, INTENT_WRITE);
+ if (cc->intent > INTENT_IDLE)
+ ret = true;
+ }
+ return ret;
+}
Index: linux-2.6/drivers/md/dm-compress/io.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/io.c
@@ -0,0 +1,652 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-compress.h"
+
+static bool dm_compress_process_chunk(struct dm_compress_io *dio);
+
+static void dm_compress_chunk_work(struct work_struct *w);
+static void dm_compress_chunk_work_2(struct work_struct *w);
+static void dm_compress_process_pending_ios(struct dm_compress *c);
+
+static void dm_compress_read_and_decompress(struct cached_chunk *cc);
+static void dm_compress_chunk_decompress(struct work_struct *w);
+
+static void dm_compress_process_bios_on_chunk(struct dm_compress *c, sector_t chunk);
+
+static void dm_compress_kick_autocommit(struct dm_compress *c);
+
+static struct chunk_entry *map_chunk(struct dm_compress *c, sector_t chunk, struct dm_buffer **bp)
+{
+ size_t dir_ptr;
+ unsigned dir_offset, chunk_offset;
+ struct directory *dir;
+ sector_t ptr;
+ struct chunk_entries *chunks;
+
+ chunk_location(c, chunk, &dir_ptr, &dir_offset, &chunk_offset);
+
+ dir = dm_bufio_read(c->bufio, c->chunk_directories[dir_ptr], bp);
+ if (unlikely(IS_ERR(dir)))
+ return (void *)dir;
+ if (unlikely(dir->magic != CHUNK_DIR_MAGIC)) {
+ dm_bufio_release(*bp);
+ dm_compress_error(c, "Chunk directory magic doesn't match", -EUCLEAN);
+ return ERR_PTR(-EUCLEAN);
+ }
+ if (unlikely(le64_to_cpu(dir->index) != dir_ptr)) {
+ dm_bufio_release(*bp);
+ dm_compress_error(c, "Chunk directory index doesn't match", -EUCLEAN);
+ return ERR_PTR(-EUCLEAN);
+ }
+ ptr = READ_PTR(&dir->ptr[dir_offset]);
+ dm_bufio_release(*bp);
+
+ chunks = dm_bufio_read(c->bufio, ptr, bp);
+ if (unlikely(IS_ERR(chunks)))
+ return (void *)chunks;
+ if (unlikely(chunks->magic != CHUNKS_MAGIC)) {
+ dm_bufio_release(*bp);
+ dm_compress_error(c, "Chunks magic doesn't match", -EUCLEAN);
+ return ERR_PTR(-EUCLEAN);
+ }
+
+ return &chunks->chunk[chunk_offset];
+}
+
+void dm_compress_enqueue_chunk(struct cached_chunk *cc, unsigned intent)
+{
+ unsigned old_intent = cc->intent;
+ cc->intent = max(cc->intent, intent);
+ if (old_intent == INTENT_IDLE) {
+ INIT_WORK(&cc->work, dm_compress_chunk_work);
+ queue_work(cc->c->compress_wq, &cc->work);
+ }
+}
+
+static void dm_compress_setup_flush(struct dm_compress_io *dio)
+{
+ dio->is_flush = true;
+ dio->chunk = 0;
+ dio->offset_in_chunk = 0;
+}
+
+static void dm_compress_endio(struct bio *bio)
+{
+ blk_opf_t opf = bio->bi_opf;
+#ifdef TEST_FUA
+ if (get_random_u8() & 1)
+ opf |= REQ_FUA;
+#endif
+ if (unlikely(opf & REQ_FUA) &&
+ likely(bio->bi_status == BLK_STS_OK)) {
+ struct dm_compress_io *dio = dm_per_bio_data(bio, sizeof(struct dm_compress_io));
+ if (!dio->is_flush) {
+ dm_compress_setup_flush(dio);
+ INIT_WORK(&dio->work, dm_compress_work);
+ queue_work(dio->c->wq, &dio->work);
+ return;
+ }
+ }
+ bio_endio(bio);
+}
+
+int dm_compress_map(struct dm_target *ti, struct bio *bio)
+{
+ struct dm_compress *c = ti->private;
+ struct dm_compress_io *dio = dm_per_bio_data(bio, sizeof(struct dm_compress_io));
+ sector_t logical_sector;
+ dio->c = c;
+
+ if (unlikely(c->failed)) {
+ bio->bi_status = errno_to_blk_status(c->failed);
+ bio_endio(bio);
+ return DM_MAPIO_SUBMITTED;
+ }
+
+ if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
+ dm_compress_setup_flush(dio);
+ } else {
+ dio->is_flush = false;
+ logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
+ dio->chunk = logical_sector >> c->log2_sectors_per_chunk;
+ dio->offset_in_chunk = logical_sector & ((1U << c->log2_sectors_per_chunk) - 1);
+
+ if (dio->chunk >= c->n_chunks)
+ return DM_MAPIO_KILL;
+ }
+
+ if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
+ if (unlikely(WARN_ON(dio->offset_in_chunk + bio_sectors(bio) > 1U << c->log2_sectors_per_chunk))) {
+ return DM_MAPIO_KILL;
+ }
+ }
+
+ INIT_WORK(&dio->work, dm_compress_work);
+ queue_work(c->wq, &dio->work);
+ return DM_MAPIO_SUBMITTED;
+}
+
+void dm_compress_work(struct work_struct *w)
+{
+ struct dm_compress_io *dio = container_of(w, struct dm_compress_io, work);
+ struct dm_compress *c = dio->c;
+ struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ bool ret;
+
+ mutex_lock(&c->mutex);
+ ret = dm_compress_process_chunk(dio);
+ if (!ret)
+ dm_compress_wait_for_cached_chunk(dio);
+ mutex_unlock(&c->mutex);
+
+ if (ret)
+ dm_compress_endio(bio);
+}
+
+static bool dm_compress_process_chunk(struct dm_compress_io *dio)
+{
+ struct dm_compress *c = dio->c;
+ struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ struct cached_chunk *cc;
+ struct chunk_entry *ce;
+ struct dm_buffer *bp;
+ struct chunk_ptr *cp;
+
+ if (unlikely(c->failed)) {
+ bio->bi_status = errno_to_blk_status(c->failed);
+ return true;
+ }
+
+ if (unlikely(dio->is_flush)) {
+ if (dm_compress_is_something_in_progress(c))
+ return false;
+ dm_compress_sync(c);
+ if (unlikely(c->failed))
+ bio->bi_status = errno_to_blk_status(c->failed);
+ return true;
+ }
+
+ if (bio_op(bio) == REQ_OP_WRITE || bio_op(bio) == REQ_OP_DISCARD) {
+ struct dm_compress_io *oldest_dio;
+ if (c->autocommit_active)
+ return false;
+ /*
+ * Flush waits for all writes to complete. In order to avoid
+ * starvation of the flush bio, we must not issue new writes if
+ * a flush is enqueued.
+ */
+ oldest_dio = dm_compress_pick_oldest_dio(c);
+ if (oldest_dio) {
+ if (unlikely(oldest_dio->is_flush))
+ return false;
+ }
+ }
+
+ cc = dm_compress_find_cached_chunk(c, dio->chunk, NULL);
+ if (cc) {
+ char *data_ptr;
+ unsigned offset;
+ struct bvec_iter iter;
+ struct bio_vec bv;
+have_cached_chunk:
+ if (bio_op(bio) == REQ_OP_READ) {
+ if (unlikely(cc->failed)) {
+ bio->bi_status = errno_to_blk_status(cc->failed);
+ return true;
+ }
+ } else {
+ if (cc->intent > INTENT_IDLE)
+ return false;
+ if (unlikely(cc->failed)) {
+ cc->dirty = false;
+ cc->failed = 0;
+ memset(cc->sector_tags, 0, 1U << c->log2_sectors_per_chunk);
+ }
+ }
+
+ if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
+ data_ptr = cc->uncompressed_data + (dio->offset_in_chunk << SECTOR_SHIFT);
+ if (!memchr(cc->sector_tags + dio->offset_in_chunk, 0, bio_sectors(bio)) &&
+ mem_is_zero(data_ptr, bio->bi_iter.bi_size))
+ return true;
+ memset(data_ptr, 0, bio->bi_iter.bi_size);
+ memset(cc->sector_tags + dio->offset_in_chunk, TAG_WRITTEN, bio_sectors(bio));
+ cc->dirty = true;
+ goto enqueue_if_full;
+ }
+
+ data_ptr = cc->uncompressed_data + (dio->offset_in_chunk << SECTOR_SHIFT);
+ offset = dio->offset_in_chunk;
+ bio_for_each_segment(bv, bio, iter) {
+ char *mem = bvec_kmap_local(&bv);
+ if (bio_op(bio) == REQ_OP_READ) {
+ if (memchr(cc->sector_tags + offset, 0, bv.bv_len >> SECTOR_SHIFT)) {
+ kunmap_local(mem);
+ dm_compress_enqueue_chunk(cc, INTENT_READ);
+ return false;
+ }
+ memcpy(mem, data_ptr, bv.bv_len);
+ flush_dcache_page(bv.bv_page);
+ } else {
+ flush_dcache_page(bv.bv_page);
+ memcpy(data_ptr, mem, bv.bv_len);
+ memset(cc->sector_tags + offset, TAG_WRITTEN, bv.bv_len >> SECTOR_SHIFT);
+ cc->dirty = true;
+ }
+ kunmap_local(mem);
+ data_ptr += bv.bv_len;
+ offset += bv.bv_len >> SECTOR_SHIFT;
+ }
+ if (bio_op(bio) == REQ_OP_WRITE) {
+enqueue_if_full:
+ if (!memchr_inv(cc->sector_tags, TAG_WRITTEN, 1U << c->log2_sectors_per_chunk)) {
+ dm_compress_enqueue_chunk(cc, INTENT_FREE);
+ }
+ dm_compress_kick_autocommit(c);
+ }
+ return true;
+ }
+
+ ce = map_chunk(c, dio->chunk, &bp);
+ if (unlikely(IS_ERR(ce))) {
+ bio->bi_status = errno_to_blk_status(PTR_ERR(ce));
+ return true;
+ }
+ cp = &ce->ptr[CC_VALID(c, &ce->cc_txc)];
+ if (cp->algorithm == ALGORITHM_ZERO) {
+ if (bio_op(bio) == REQ_OP_READ) {
+ dm_bufio_release(bp);
+ zero_fill_bio(bio);
+ return true;
+ }
+ if (bio_op(bio) == REQ_OP_DISCARD) {
+ dm_bufio_release(bp);
+ return true;
+ }
+ }
+ dm_bufio_release(bp);
+
+ cc = dm_compress_new_cached_chunk(dio);
+ if (cc) {
+ struct cached_chunk *qc = dm_compress_find_cached_chunk(c, dio->chunk, cc);
+ if (unlikely(qc != NULL)) {
+ dm_compress_release_cached_chunk(cc);
+ cc = qc;
+ goto have_cached_chunk;
+ }
+ goto have_cached_chunk;
+ }
+
+ cc = list_entry(c->lru.prev, struct cached_chunk, lru_entry);
+ dm_compress_enqueue_chunk(cc, INTENT_FREE);
+ return false;
+}
+
+static void dm_compress_chunk_work(struct work_struct *w)
+{
+ struct cached_chunk *cc = container_of(w, struct cached_chunk, work);
+ struct dm_compress *c = cc->c;
+ struct acomp_req *req;
+ unsigned len_in, len_out, len_compressed, trailing_space;
+ DECLARE_CRYPTO_WAIT(wait);
+ int ret;
+ unsigned flags;
+ sector_t preferred_block;
+ struct dm_buffer *bp;
+ struct chunk_ptr *cp;
+ struct chunk_ptr old_cp, new_cp;
+
+ init_completion(&cc->completion);
+
+ if (memchr(cc->sector_tags, 0, 1U << c->log2_sectors_per_chunk)) {
+ dm_compress_read_and_decompress(cc);
+ return;
+ }
+
+ mutex_lock(&c->mutex);
+ if (cc->intent == INTENT_READ) {
+ cc->intent = INTENT_IDLE;
+ dm_compress_process_bios_on_chunk(c, cc->chunk);
+ return;
+ }
+ if (!cc->dirty) {
+ mutex_unlock(&c->mutex);
+ complete(&cc->completion);
+ dm_compress_chunk_work_2(&cc->work);
+ return;
+ }
+ cc->dirty = false;
+ if (unlikely(cc->failed)) {
+ dm_compress_error(c, "Could not write failed chunk", cc->failed);
+ mutex_unlock(&c->mutex);
+ complete(&cc->completion);
+ dm_compress_chunk_work_2(&cc->work);
+ return;
+ }
+ mutex_unlock(&c->mutex);
+
+ len_in = 512U << c->log2_sectors_per_chunk;
+ len_out = (512U << c->log2_sectors_per_chunk) - (512U << c->log2_sectors_per_block) - 4;
+
+ if (mem_is_zero(cc->uncompressed_data, len_in)) {
+ cc->algorithm = ALGORITHM_ZERO;
+ len_compressed = 0;
+ *(uint32_t *)cc->compressed_data = cpu_to_le32(0);
+ goto skip_compression;
+ }
+
+ cc->algorithm = c->algorithm;
+
+ req = cc->req;
+ memset(req, 0, dm_compress_max_request_size);
+ acomp_request_set_tfm(req, dm_compress_algorithms[cc->algorithm].cc);
+ acomp_request_set_params(req, cc->uncompressed_sglist, cc->compressed_sglist, len_in, len_out);
+ acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, crypto_req_done, &wait);
+
+ flags = memalloc_noio_save();
+ ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
+ memalloc_noio_restore(flags);
+
+ len_compressed = req->dlen;
+ /*printk("compressed: %u\n", len_compressed);*/
+#ifdef TEST_UNCOMPRESSED_DATA
+ if (get_random_u8() & 1)
+ ret = -EOPNOTSUPP;
+#endif
+ /* !!! FIXME: this condition is weird */
+ if (unlikely(ret) || unlikely(!len_compressed) || unlikely(len_compressed > len_out)) {
+ memcpy(cc->compressed_data, cc->uncompressed_data, len_in);
+ cc->algorithm = ALGORITHM_UNCOMPRESSED;
+ len_compressed = len_in;
+ } else {
+ *(uint32_t *)cc->compressed_data = cpu_to_le32(len_compressed);
+ len_compressed += 4;
+#ifdef TEST_MALFORMED_CONTENT
+ ((uint8_t *)cc->compressed_data)[4]++;
+#endif
+ }
+
+skip_compression:
+ mutex_lock(&c->mutex);
+
+ trailing_space = -len_compressed & ((512U << c->log2_sectors_per_block) - 1);
+ memset(cc->compressed_data + len_compressed, 0, trailing_space);
+
+ preferred_block = READ_ONCE(c->last_allocated);
+ mutex_unlock(&c->mutex);
+
+ INIT_WORK(&cc->work, dm_compress_chunk_work_2);
+ if (unlikely(!dm_compress_allocate_chunk(cc, preferred_block, &new_cp))) {
+ complete(&cc->completion);
+ dm_compress_chunk_work_2(&cc->work);
+ return;
+ }
+
+ cp = dm_compress_prepare_chunk(c, cc->chunk, &bp);
+ if (unlikely(!cp))
+ goto cont;
+
+ old_cp = *cp;
+ *cp = new_cp;
+
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+
+ dm_compress_release_chunk(c, &old_cp);
+
+cont:
+ complete(&cc->completion);
+}
+
+static void dm_compress_chunk_work_2(struct work_struct *w)
+{
+ struct cached_chunk *cc = container_of(w, struct cached_chunk, work);
+ struct dm_compress *c = cc->c;
+
+ wait_for_completion(&cc->completion);
+
+ mutex_lock(&c->mutex);
+ if (cc->intent >= INTENT_FREE) {
+ dm_compress_release_cached_chunk(cc);
+ } else {
+ cc->intent = INTENT_IDLE;
+ }
+ dm_compress_process_pending_ios(c);
+}
+
+static void dm_compress_process_pending_ios(struct dm_compress *c)
+{
+ struct bio *bio;
+ struct bio_list bio_list;
+ bio_list_init(&bio_list);
+
+ while (true) {
+ sector_t chunk;
+ struct dm_compress_io *dio = dm_compress_pick_oldest_dio(c);
+ if (!dio)
+ break;
+ chunk = dio->chunk;
+ if (!dm_compress_process_chunk(dio)) {
+ break;
+ }
+ dm_compress_unlink_dio(dio);
+ bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ bio_list_add(&bio_list, bio);
+ while ((dio = dm_compress_pick_dio_for_chunk(c, chunk))) {
+ /*printk("picked another dio for chunk %llx\n", chunk);*/
+ if (!dm_compress_process_chunk(dio)) {
+ break;
+ }
+ dm_compress_unlink_dio(dio);
+ bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ bio_list_add(&bio_list, bio);
+ }
+ }
+ wake_up(&c->wait_idle);
+ mutex_unlock(&c->mutex);
+
+ while ((bio = bio_list_pop(&bio_list)))
+ dm_compress_endio(bio);
+}
+
+static void dm_compress_read_failed(struct cached_chunk *cc)
+{
+ struct dm_compress *c = cc->c;
+ sector_t chunk = cc->chunk;
+ mutex_lock(&c->mutex);
+ if (unlikely(cc->dirty)) {
+ cc->dirty = false;
+ dm_compress_error(c, "Could not read a chunk with pending writes", -EIO);
+ }
+ if (cc->intent >= INTENT_FREE) {
+ dm_compress_release_cached_chunk(cc);
+ } else {
+ cc->intent = INTENT_IDLE;
+ }
+ dm_compress_process_bios_on_chunk(c, chunk);
+}
+
+static void dm_compress_read_and_decompress(struct cached_chunk *cc)
+{
+ struct dm_compress *c = cc->c;
+ struct dm_buffer *bp;
+ struct chunk_entry *ce;
+ struct chunk_ptr *cp;
+ struct chunk_ptr cp_copy;
+
+ if (unlikely(cc->failed)) {
+ dm_compress_read_failed(cc);
+ return;
+ }
+
+ ce = map_chunk(c, cc->chunk, &bp);
+ if (unlikely(IS_ERR(ce))) {
+ dm_compress_error_chunk(cc, "Unable to load chunk descriptor", PTR_ERR(ce));
+ dm_compress_read_failed(cc);
+ return;
+ }
+
+ cp = &ce->ptr[CC_VALID(c, &ce->cc_txc)];
+ cp_copy = *cp;
+ dm_bufio_release(bp);
+
+ INIT_WORK(&cc->work, dm_compress_chunk_decompress);
+ if (unlikely(!dm_compress_read_chunk(cc, &cp_copy))) {
+ dm_compress_read_failed(cc);
+ return;
+ }
+}
+
+static void dm_compress_chunk_decompress(struct work_struct *w)
+{
+ struct cached_chunk *cc = container_of(w, struct cached_chunk, work);
+ struct dm_compress *c = cc->c;
+ unsigned i;
+ unsigned intent;
+
+ if (cc->algorithm == ALGORITHM_ZERO) {
+ mutex_lock(&c->mutex);
+ for (i = 0; i < 1 << c->log2_sectors_per_chunk; i++) {
+ if (!cc->sector_tags[i]) {
+ unsigned offset = i << 9;
+ memset(cc->uncompressed_data + offset, 0, 512);
+ }
+ }
+ } else if (cc->algorithm == ALGORITHM_UNCOMPRESSED) {
+ if (unlikely(cc->compressed_size != 512U << c->log2_sectors_per_chunk)) {
+ dm_compress_error_chunk(cc, "Decompressed less data than requested", -EUCLEAN);
+ dm_compress_read_failed(cc);
+ return;
+ }
+ mutex_lock(&c->mutex);
+ for (i = 0; i < 1 << c->log2_sectors_per_chunk; i++) {
+ if (!cc->sector_tags[i]) {
+ unsigned offset = i << 9;
+ memcpy(cc->uncompressed_data + offset, cc->compressed_data + offset, 512);
+ }
+ }
+ } else if (cc->algorithm < dm_compress_n_algorithms) {
+ int ret;
+ unsigned flags;
+ struct acomp_req *req;
+ DECLARE_CRYPTO_WAIT(wait);
+ unsigned len_in, len_out;
+
+ len_out = 512U << c->log2_sectors_per_chunk;
+ len_in = le32_to_cpu(*(__le32 *)cc->compressed_data);
+ if (unlikely(len_in > cc->compressed_size - 4)) {
+ dm_compress_error_chunk(cc, "Invalid length", -EUCLEAN);
+ dm_compress_read_failed(cc);
+ return;
+ }
+
+ req = cc->req;
+ memset(req, 0, dm_compress_max_request_size);
+ acomp_request_set_tfm(req, dm_compress_algorithms[cc->algorithm].cc);
+ acomp_request_set_params(req, cc->compressed_sglist, cc->merging_sglist, len_in, len_out);
+ acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, crypto_req_done, &wait);
+
+ flags = memalloc_noio_save();
+ ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
+ memalloc_noio_restore(flags);
+ if (unlikely(ret)) {
+ dm_compress_error_chunk(cc, "Decompression failed", ret);
+ dm_compress_read_failed(cc);
+ return;
+ }
+ if (unlikely(req->dlen != len_out)) {
+ dm_compress_error_chunk(cc, "Decompressed less data than requested", -EUCLEAN);
+ dm_compress_read_failed(cc);
+ return;
+ }
+
+ mutex_lock(&c->mutex);
+ for (i = 0; i < 1 << c->log2_sectors_per_chunk; i++) {
+ if (!cc->sector_tags[i]) {
+ unsigned offset = i << 9;
+ memcpy(cc->uncompressed_data + offset, cc->merging_data + offset, 512);
+ }
+ }
+ } else {
+ dm_compress_error_chunk(cc, "Unsupported algorithm", -EOPNOTSUPP);
+ dm_compress_read_failed(cc);
+ return;
+ }
+
+ memset(cc->sector_tags, TAG_VALID, 1U << c->log2_sectors_per_chunk);
+
+ intent = cc->intent;
+ if (intent == INTENT_READ) {
+ cc->intent = INTENT_IDLE;
+ }
+
+ dm_compress_process_bios_on_chunk(c, cc->chunk);
+
+ if (intent >= INTENT_WRITE) {
+ dm_compress_chunk_work(&cc->work);
+ }
+}
+
+static void dm_compress_process_bios_on_chunk(struct dm_compress *c, sector_t chunk)
+{
+ struct bio_list bio_list;
+ struct dm_compress_io *dio;
+ struct bio *bio;
+
+ bio_list_init(&bio_list);
+
+ while ((dio = dm_compress_pick_dio_for_chunk(c, chunk))) {
+ if (!dm_compress_process_chunk(dio)) {
+ break;
+ }
+ dm_compress_unlink_dio(dio);
+ bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ bio_list_add(&bio_list, bio);
+ }
+ while ((dio = dm_compress_pick_oldest_dio(c))) {
+ if (!dm_compress_process_chunk(dio)) {
+ break;
+ }
+ dm_compress_unlink_dio(dio);
+ bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_compress_io));
+ bio_list_add(&bio_list, bio);
+ }
+
+ wake_up(&c->wait_idle);
+ mutex_unlock(&c->mutex);
+
+ while ((bio = bio_list_pop(&bio_list)))
+ dm_compress_endio(bio);
+}
+
+static void dm_compress_kick_autocommit(struct dm_compress *c)
+{
+ queue_delayed_work(c->flush_wq, &c->autocommit_work, c->autocommit_jiffies);
+}
+
+void dm_compress_autocommit(struct work_struct *w)
+{
+ struct dm_compress *c = container_of(w, struct dm_compress, autocommit_work.work);
+ mutex_lock(&c->mutex);
+ c->autocommit_active = true;
+ while (dm_compress_is_something_in_progress(c)) {
+ DECLARE_WAITQUEUE(wait, current);
+ add_wait_queue(&c->wait_idle, &wait);
+ __set_current_state(TASK_UNINTERRUPTIBLE);
+ mutex_unlock(&c->mutex);
+ io_schedule();
+ mutex_lock(&c->mutex);
+ remove_wait_queue(&c->wait_idle, &wait);
+ }
+
+ dm_compress_sync(c);
+
+ c->autocommit_active = false;
+ dm_compress_process_pending_ios(c);
+}
Index: linux-2.6/Documentation/admin-guide/device-mapper/dm-compress.rst
===================================================================
--- /dev/null
+++ linux-2.6/Documentation/admin-guide/device-mapper/dm-compress.rst
@@ -0,0 +1,61 @@
+===========
+dm-compress
+===========
+
+The dm-compress target provides transparent compression of data on a block
+device.
+
+The device is divided into blocks, a block is the basic allocation unit. Block
+size must be a power of two and it must be greater or equal than the logical
+block size of the underlying device. A smaller block size improves
+space-efficiency. SSDs and some hard disks perform read-modify-write cycle when
+writing data that is not aligned on 4k boundary, so a block size smaller than 4k
+may degrade performance.
+
+The device is managed in the units of chunks. Chunk size must be a power of two
+and it must be greater than block size. Every chunk is fully compressed using
+the selected algorithm and it is stored in one or more blocks (or no blocks, if
+it contains only zeros). Greater chunk size improves the compression ratio, but
+it also degrades performance because the full chunk must be decompressed when
+accessing any part of the chunk.
+
+When loading the target for the first time, the kernel driver will format
+the device. The 4k superblock (after the reserved area) must contain zeros. If
+the superblock is not zeroed, the device can't be formatted and the target
+constructor will report an error.
+
+
+Target arguments:
+
+1. the underlying block device
+
+2. the number of reserved sectors at the beginning of the device - dm-compress
+ won't read or write these sectors
+
+3. block size (a power of two in the range 512 - 524288)
+
+4. chunk size (a power of two in the range 1024 - 1048576)
+ the chunk size divided by the block size must be less or equal than 256
+
+5. the total number of chunks
+
+6. the compression algorithm - the supported algorithms are:
+ deflate, lzo, 842, lz4, lz4hc, zstd
+
+7. the number of additional arguments
+
+Additional arguments:
+
+meta_device:device
+ Store metadata on a separate device
+
+n_cached_chunks:number (default 128)
+ The number of cached decompressed chunks that is kept in memory. The
+ cached chunks are preallocated when the target is loaded. The memory
+ consumption of the target is n_cached_chunks*chunk_size*3.
+
+
+Status line:
+
+1. The total number of blocks
+2. The number of free blocks
Index: linux-2.6/drivers/md/dm-compress/chunk.c
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/chunk.c
@@ -0,0 +1,440 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-compress.h"
+
+static void dm_compress_read_endio(struct bio *bio);
+static void dm_compress_write_endio(struct bio *bio);
+
+static struct split_chunk *map_split_block(struct dm_compress *c, struct cached_chunk *cc, sector_t block)
+{
+ struct dm_buffer *bp;
+ struct split_chunk *split, *split_copy;
+
+ split = dm_bufio_read(c->split_bufio, block, &bp);
+ if (IS_ERR(split)) {
+ if (cc)
+ dm_compress_error_chunk(cc, "Error reading split block", PTR_ERR(split));
+ else
+ dm_compress_error(c, "Error reading split block", PTR_ERR(split));
+ return split;
+ }
+
+ split_copy = mempool_alloc(&c->block_io_buffers, GFP_NOIO);
+ memcpy(split_copy, split, 512U << c->log2_sectors_per_block);
+ dm_bufio_release(bp);
+
+ if (unlikely(split_copy->magic != SPLIT_BLOCK_MAGIC)) {
+ if (cc)
+ dm_compress_error_chunk(cc, "Split block magic doesn't match", -EUCLEAN);
+ else
+ dm_compress_error(c, "Split block magic doesn't match", -EUCLEAN);
+ mempool_free(split_copy, &c->block_io_buffers);
+ return ERR_PTR(-EUCLEAN);
+ }
+
+ if (unlikely(offsetof(struct split_chunk, extents[le16_to_cpu(split_copy->n_extents)]) > 512U << c->log2_sectors_per_block)) {
+ if (cc)
+ dm_compress_error_chunk(cc, "Split block has corrupted n_extents", -EUCLEAN);
+ else
+ dm_compress_error(c, "Split block has corrupted n_extents", -EUCLEAN);
+ mempool_free(split_copy, &c->block_io_buffers);
+ return ERR_PTR(-EUCLEAN);
+ }
+
+ return split_copy;
+}
+
+bool dm_compress_read_chunk(struct cached_chunk *cc, struct chunk_ptr *cp)
+{
+ struct dm_compress *c = cc->c;
+ sector_t blk;
+ struct bio *bio;
+ struct split_chunk *split;
+ char *chunk_ptr;
+ size_t chunk_remaining;
+ unsigned loops;
+ const unsigned block_size = 512U << c->log2_sectors_per_block;
+ const unsigned chunk_size = 512U << c->log2_sectors_per_chunk;
+
+ blk = READ_PTR(cp);
+ if (likely(cp->algorithm != ALGORITHM_SPLIT)) {
+ unsigned n_blocks;
+ cc->algorithm = cp->algorithm;
+ n_blocks = cc->algorithm == ALGORITHM_ZERO ? 0 : cp->length + 1;
+ cc->compressed_size = n_blocks << (9 + c->log2_sectors_per_block);
+
+ if (unlikely(n_blocks > 1 << (c->log2_sectors_per_chunk - c->log2_sectors_per_block))) {
+ dm_compress_error_chunk(cc, "The block counter is corrupted", -EUCLEAN);
+ return false;
+ }
+
+ if (cp->algorithm == ALGORITHM_ZERO) {
+ queue_work(c->compress_wq, &cc->work);
+ return true;
+ }
+
+ bio = cc->bio;
+ bio_init_inline(bio, c->dev->bdev, cc->bio_vectors, REQ_OP_READ);
+ bio->bi_iter.bi_sector = blk << c->log2_sectors_per_block;
+ if (!c->meta_dev)
+ bio->bi_iter.bi_sector += c->start;
+ bio->bi_iter.bi_size = cc->compressed_size;
+ bio->bi_vcnt = cc->bio_vectors;
+ bio->bi_end_io = dm_compress_read_endio;
+ bio->bi_private = cc;
+ submit_bio(bio);
+
+ return true;
+ }
+
+ split = map_split_block(c, cc, blk);
+ if (IS_ERR(split))
+ return false;
+
+ cc->algorithm = split->algorithm;
+
+ if (cc->algorithm != ALGORITHM_UNCOMPRESSED) {
+ chunk_ptr = cc->compressed_data + 4;
+ *(int32_t *)cc->compressed_data = split->compressed_size;
+ chunk_remaining = le32_to_cpu(split->compressed_size);
+ if (unlikely(chunk_remaining > chunk_size - 4)) {
+ mempool_free(split, &c->block_io_buffers);
+ dm_compress_error_chunk(cc, "Invalid compressed size", -EUCLEAN);
+ return false;
+ }
+ cc->compressed_size = chunk_remaining + 4;
+ } else {
+ chunk_ptr = cc->compressed_data;
+ chunk_remaining = chunk_size;
+ cc->compressed_size = chunk_remaining;
+ }
+ loops = 0;
+ while (true) {
+ size_t i;
+ unsigned end, size, this_step;
+ bool next_valid;
+ sector_t next_blk;
+ if (unlikely(loops++ >= MAX_SPLIT_CHUNK_CHAIN)) {
+ dm_compress_error_chunk(cc, "Too long split chunk chain", -EUCLEAN);
+ mempool_free(split, &c->block_io_buffers);
+ return false;
+ }
+ for (i = 0; i < le16_to_cpu(split->n_extents); i++) {
+ struct split_extent *sx = &split->extents[i];
+ sector_t ptr = READ_PTR(sx);
+ unsigned len = le16_to_cpu(sx->len);
+ dm_bufio_prefetch(c->split_bufio, ptr, len);
+ }
+ if (split->next_valid)
+ dm_bufio_prefetch(c->split_bufio, READ_PTR(split), 1);
+
+ for (i = 0; i < le16_to_cpu(split->n_extents); i++) {
+ struct split_extent *sx = &split->extents[i];
+ sector_t ptr = READ_PTR(sx);
+ unsigned len = le16_to_cpu(sx->len);
+ unsigned j;
+ for (j = 0; j < len; j++, ptr++) {
+ struct dm_buffer *bp;
+ char *data = dm_bufio_read(c->split_bufio, ptr, &bp);
+ if (IS_ERR(data)) {
+ dm_compress_error_chunk(cc, "Error reading split block data", PTR_ERR(data));
+ mempool_free(split, &c->block_io_buffers);
+ return false;
+ }
+ this_step = min(block_size, chunk_remaining);
+ memcpy(chunk_ptr, data, this_step);
+ chunk_ptr += this_step;
+ chunk_remaining -= this_step;
+ dm_bufio_release(bp);
+ }
+ }
+
+ end = offsetof(struct split_chunk, extents[le16_to_cpu(split->n_extents)]);
+ size = block_size - end;
+ this_step = min(size, chunk_remaining);
+ memcpy(chunk_ptr, &split->extents[le16_to_cpu(split->n_extents)], this_step);
+ chunk_ptr += this_step;
+ chunk_remaining -= this_step;
+
+ next_valid = split->next_valid;
+ next_blk = READ_PTR(split);
+ mempool_free(split, &c->block_io_buffers);
+
+ if (!next_valid)
+ break;
+ if (unlikely(!chunk_remaining)) {
+ dm_compress_error_chunk(cc, "Split chunk is too big", -EUCLEAN);
+ return false;
+ }
+
+ split = map_split_block(c, cc, next_blk);
+ if (IS_ERR(split))
+ return false;
+ }
+ if (unlikely(chunk_remaining != 0)) {
+ dm_compress_error_chunk(cc, "Split chunk is too small", -EUCLEAN);
+ return false;
+ }
+
+ queue_work(c->compress_wq, &cc->work);
+ return true;
+}
+
+static void dm_compress_read_endio(struct bio *bio)
+{
+ struct cached_chunk *cc = bio->bi_private;
+ struct dm_compress *c = cc->c;
+ if (unlikely(bio->bi_status != BLK_STS_OK))
+ dm_compress_error_chunk(cc, "Error reading compressed data", blk_status_to_errno(bio->bi_status));
+ bio_uninit(bio);
+ queue_work(c->compress_wq, &cc->work);
+}
+
+bool dm_compress_allocate_chunk(struct cached_chunk *cc, sector_t preferred_block, struct chunk_ptr *cp)
+{
+ struct dm_compress *c = cc->c;
+ const unsigned block_size = 512U << c->log2_sectors_per_block;
+ const unsigned chunk_size = 512U << c->log2_sectors_per_chunk;
+ unsigned len_compressed;
+ unsigned n_blocks;
+ char *src_ptr;
+ sector_t blk;
+ sector_t next_blk;
+ struct bio *bio;
+ struct split_chunk *split;
+ struct split_chunk *split_buf;
+ unsigned n_extents;
+ unsigned split_space;
+ unsigned this_step;
+ struct dm_buffer *bp;
+
+ if (cc->algorithm == ALGORITHM_ZERO) {
+ WRITE_PTR(cp, 0);
+ cp->length = 0;
+ cp->algorithm = ALGORITHM_ZERO;
+ queue_work(c->compress_wq, &cc->work);
+ return true;
+ } else if (cc->algorithm == ALGORITHM_UNCOMPRESSED) {
+ len_compressed = chunk_size;
+ src_ptr = cc->compressed_data;
+ n_blocks = chunk_size >> (9 + c->log2_sectors_per_block);
+ } else {
+ len_compressed = le32_to_cpu(*(uint32_t *)cc->compressed_data);
+ src_ptr = cc->compressed_data + 4;
+ n_blocks = (len_compressed + 4 + block_size - 1) >> (9 + c->log2_sectors_per_block);
+ }
+#ifdef TEST_SPLIT_CHUNKS
+ if (get_random_u8() & 1)
+ blk = ALLOCATE_NO_SPACE;
+ else
+#endif
+ blk = dm_compress_allocate_space(c, preferred_block, n_blocks);
+ if (unlikely(blk == ALLOCATE_ERROR))
+ return false;
+
+ if (likely(blk != ALLOCATE_NO_SPACE)) {
+ WRITE_ONCE(c->last_allocated, blk + n_blocks);
+
+ bio = cc->bio;
+ bio_init_inline(bio, c->dev->bdev, cc->bio_vectors, REQ_OP_WRITE);
+ bio->bi_iter.bi_sector = blk << c->log2_sectors_per_block;
+ if (!c->meta_dev)
+ bio->bi_iter.bi_sector += c->start;
+ bio->bi_iter.bi_size = n_blocks << (9 + c->log2_sectors_per_block);
+ bio->bi_vcnt = cc->bio_vectors;
+ bio->bi_end_io = dm_compress_write_endio;
+ bio->bi_private = cc;
+ submit_bio(bio);
+
+ WRITE_PTR(cp, blk);
+ cp->length = n_blocks - 1;
+ cp->algorithm = cc->algorithm;
+ return true;
+ }
+
+ blk = dm_compress_allocate_space(c, preferred_block, 1);
+ if (unlikely(blk == ALLOCATE_ERROR))
+ return false;
+ if (unlikely(blk == ALLOCATE_NO_SPACE)) {
+ dm_compress_error(c, "The device is out of space", -ENOSPC);
+ return false;
+ }
+ preferred_block = blk + 1;
+ WRITE_ONCE(c->last_allocated, preferred_block);
+ WRITE_PTR(cp, blk);
+ cp->length = 0;
+ cp->algorithm = ALGORITHM_SPLIT;
+
+ split = mempool_alloc(&c->block_io_buffers, GFP_NOIO);
+
+ memset(split, 0, block_size);
+ split->magic = SPLIT_BLOCK_MAGIC;
+ split->algorithm = cc->algorithm;
+ split->compressed_size = cpu_to_le32(len_compressed);
+ split->n_extents = cpu_to_le16(0);
+
+new_sub_block:
+ n_extents = le16_to_cpu(split->n_extents);
+ split_space = block_size - offsetof(struct split_chunk, extents[n_extents]);
+
+ if (len_compressed > split_space) {
+ if (split_space >= sizeof(struct split_extent)) {
+ void *sub;
+ sector_t subblk = dm_compress_allocate_space(c, preferred_block, 1);
+ if (unlikely(subblk == ALLOCATE_ERROR))
+ goto free_ret;
+ if (unlikely(subblk == ALLOCATE_NO_SPACE)) {
+ dm_compress_error(c, "The device is out of space", -ENOSPC);
+ goto free_ret;
+ }
+ preferred_block = subblk + 1;
+ WRITE_ONCE(c->last_allocated, preferred_block);
+#ifdef TEST_SPLIT_CHUNK_CHAINS
+ if (!get_random_u8())
+#endif
+ if (n_extents) {
+ sector_t last_run = le16_to_cpu(split->extents[n_extents - 1].len);
+ sector_t last = READ_PTR(&split->extents[n_extents - 1]);
+ if (subblk == last + last_run && last_run < 0xffff) {
+ last_run++;
+ split->extents[n_extents - 1].len = cpu_to_le16(last_run);
+ goto appended;
+ }
+ }
+ WRITE_PTR(&split->extents[n_extents], subblk);
+ split->extents[n_extents].len = cpu_to_le16(1);
+ n_extents++;
+ split->n_extents = cpu_to_le16(n_extents);
+ goto appended;
+appended:
+ sub = dm_bufio_new(c->split_bufio, subblk, &bp);
+ if (IS_ERR(sub)) {
+ dm_compress_error(c, "Unable to create a new buffer", PTR_ERR(sub));
+ goto free_ret;
+ }
+ this_step = min(block_size, len_compressed);
+ memcpy(sub, src_ptr, this_step);
+ src_ptr += this_step;
+ len_compressed -= this_step;
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ c->split_bufio_dirty = true;
+ goto new_sub_block;
+ }
+ }
+ this_step = min(split_space, len_compressed);
+ memcpy(&split->extents[n_extents], src_ptr, this_step);
+ src_ptr += this_step;
+ len_compressed -= this_step;
+
+ if (unlikely(len_compressed != 0)) {
+ next_blk = dm_compress_allocate_space(c, preferred_block, 1);
+ if (unlikely(next_blk == ALLOCATE_ERROR))
+ goto free_ret;
+ if (unlikely(next_blk == ALLOCATE_NO_SPACE)) {
+ dm_compress_error(c, "The device is out of space", -ENOSPC);
+ goto free_ret;
+ }
+ preferred_block = next_blk + 1;
+ WRITE_ONCE(c->last_allocated, preferred_block);
+
+ split->next_valid = 1;
+ WRITE_PTR(split, next_blk);
+ }
+
+ split_buf = dm_bufio_new(c->split_bufio, blk, &bp);
+ if (IS_ERR(split_buf)) {
+ dm_compress_error(c, "Unable to create a new buffer", PTR_ERR(split_buf));
+ goto free_ret;
+ }
+ memcpy(split_buf, split, block_size);
+ dm_bufio_mark_buffer_dirty(bp);
+ dm_bufio_release(bp);
+ c->split_bufio_dirty = true;
+
+ if (likely(!len_compressed)) {
+ mempool_free(split, &c->block_io_buffers);
+ queue_work(c->compress_wq, &cc->work);
+ return true;
+ }
+
+ blk = next_blk;
+
+ memset(split, 0, block_size);
+ split->magic = SPLIT_BLOCK_MAGIC;
+ split->n_extents = cpu_to_le16(0);
+ goto new_sub_block;
+
+free_ret:
+ mempool_free(split, &c->block_io_buffers);
+ return false;
+}
+
+static void dm_compress_write_endio(struct bio *bio)
+{
+ struct cached_chunk *cc = bio->bi_private;
+ struct dm_compress *c = cc->c;
+ if (unlikely(bio->bi_status != BLK_STS_OK))
+ dm_compress_error(c, "Error writing compressed data", blk_status_to_errno(bio->bi_status));
+ bio_uninit(bio);
+
+ queue_work(c->compress_wq, &cc->work);
+}
+
+void dm_compress_release_chunk(struct dm_compress *c, struct chunk_ptr *cp)
+{
+ sector_t existing_block;
+ unsigned existing_length;
+ struct split_chunk *split;
+ size_t i;
+ sector_t next;
+ unsigned loops;
+
+ existing_block = ((__u64)le16_to_cpu(cp->hi) << 32) | le32_to_cpu(cp->lo);
+
+ if (likely(cp->algorithm != ALGORITHM_SPLIT)) {
+ existing_length = cp->algorithm != ALGORITHM_ZERO ? cp->length + 1 : 0;
+
+ if (existing_length)
+ dm_compress_free_space(c, existing_block, existing_length);
+
+ return;
+ }
+
+ loops = 0;
+ do {
+ if (unlikely(loops++ >= MAX_SPLIT_CHUNK_CHAIN)) {
+ dm_compress_error(c, "Too long split chunk chain", -EUCLEAN);
+ return;
+ }
+
+ split = map_split_block(c, NULL, existing_block);
+ if (IS_ERR(split))
+ return;
+
+ for (i = 0; i < le16_to_cpu(split->n_extents); i++) {
+ struct split_extent *sx = &split->extents[i];
+ sector_t ptr = READ_PTR(sx);
+ sector_t len = le16_to_cpu(sx->len);
+ dm_bufio_forget_buffers_forced(c->split_bufio, ptr, len);
+ dm_compress_free_space(c, ptr, len);
+ }
+
+ next = (sector_t)-1;
+ if (split->next_valid)
+ next = READ_PTR(split);
+
+ mempool_free(split, &c->block_io_buffers);
+
+ dm_bufio_forget_buffers_forced(c->split_bufio, existing_block, 1);
+ dm_compress_free_space(c, existing_block, 1);
+
+ existing_block = next;
+ } while (existing_block != (sector_t)-1);
+}
Index: linux-2.6/drivers/md/dm-bufio.c
===================================================================
--- linux-2.6.orig/drivers/md/dm-bufio.c
+++ linux-2.6/drivers/md/dm-bufio.c
@@ -2278,6 +2278,22 @@ void dm_bufio_forget_buffers(struct dm_b
}
EXPORT_SYMBOL_GPL(dm_bufio_forget_buffers);
+static enum evict_result forced_evict(struct dm_buffer *b, void *context)
+{
+ wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
+ wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
+
+ return ER_EVICT;
+}
+
+void dm_bufio_forget_buffers_forced(struct dm_bufio_client *c, sector_t block, sector_t n_blocks)
+{
+ dm_bufio_lock(c);
+ cache_remove_range(&c->cache, block, block + n_blocks, forced_evict, __free_buffer_wake);
+ dm_bufio_unlock(c);
+}
+EXPORT_SYMBOL_GPL(dm_bufio_forget_buffers_forced);
+
void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n)
{
c->minimum_buffers = n;
Index: linux-2.6/include/linux/dm-bufio.h
===================================================================
--- linux-2.6.orig/include/linux/dm-bufio.h
+++ linux-2.6/include/linux/dm-bufio.h
@@ -154,6 +154,20 @@ void dm_bufio_forget(struct dm_bufio_cli
void dm_bufio_forget_buffers(struct dm_bufio_client *c, sector_t block, sector_t n_blocks);
/*
+ * Free the given range of buffers, discarding any dirty data that they hold
+ * without writing it back - the caller is expected to be releasing the
+ * underlying blocks.
+ *
+ * The caller must guarantee that nobody else uses the blocks in the range.
+ * When this function returns the buffers are gone and any i/o on them has
+ * completed.
+ *
+ * This function sleeps, so it must not be used on a client that was created
+ * with no_sleep set.
+ */
+void dm_bufio_forget_buffers_forced(struct dm_bufio_client *c, sector_t block, sector_t n_blocks);
+
+/*
* Set the minimum number of buffers before cleanup happens.
*/
void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned int n);
Index: linux-2.6/drivers/md/dm-compress/struct.h
===================================================================
--- /dev/null
+++ linux-2.6/drivers/md/dm-compress/struct.h
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2026 Mikulas Patocka
+ *
+ * This file is released under the GPL.
+ */
+
+#define BUFFER_SIZE 4096
+
+#define MAX_BLOCK_SIZE 1048576
+#define MAX_CHUNK_SIZE 1048576
+
+#define SB_MAGIC "dm-comp"
+
+#define SB_VERSION_1 1
+
+#define BUFFER_SECTORS (BUFFER_SIZE >> SECTOR_SHIFT)
+#define SB_SECTORS BUFFER_SECTORS
+
+struct superblock {
+ __u8 magic[8];
+ __u8 version;
+ __u8 log2_buffer_size; /* log2 of BUFFER_SIZE */
+ __u8 log2_sectors_per_block;
+ __u8 log2_sectors_per_chunk;
+ __le16 cc;
+ __le16 pad;
+ __le64 cct; /* ptr to struct crash_count_table */
+ __le64 n_cc_txc;
+ __le64 n_blocks[2];
+ __le64 n_chunks[2];
+ __le64 n_used_metadata_blocks[2];
+ __le64 bitmap_directory; /* ptr to struct directory */
+ __le64 chunk_directory; /* ptr to struct directory */
+};
+
+#define CCT_ENTRIES 65536
+
+struct crash_count_table {
+ __le64 entries[CCT_ENTRIES];
+};
+
+#define CCT_SIZE sizeof(struct crash_count_table)
+
+#define ALGORITHM_ZERO 0xff
+#define ALGORITHM_UNCOMPRESSED 0xfe
+#define ALGORITHM_SPLIT 0xfd
+
+struct chunk_ptr {
+ __le32 lo;
+ __le16 hi;
+ __u8 length;
+ __u8 algorithm;
+};
+
+struct chunk_entry {
+ __le64 cc_txc;
+ struct chunk_ptr ptr[2];
+};
+
+#define CHUNKS_PER_BUFFER (BUFFER_SIZE / sizeof(struct chunk_entry))
+
+#define CHUNKS_MAGIC (cpu_to_be64(0x4348554E4B53ULL))
+
+struct chunk_entries {
+ __be64 magic;
+ struct chunk_entry chunk[CHUNKS_PER_BUFFER];
+} __attribute__((__aligned__(BUFFER_SIZE)));
+
+#define BITS_PER_BITMAP ((BUFFER_SIZE / 2 - 8) * 8)
+
+#define BITMAP_MAGIC (cpu_to_be64(0x4249544D4150ULL))
+
+struct bitmap {
+ __be64 magic;
+ __u64 bmp1[BITS_PER_BITMAP / 64];
+ __le64 cc_txc;
+ __u64 bmp2[BITS_PER_BITMAP / 64];
+} __attribute__((__aligned__(BUFFER_SIZE)));
+
+struct ptr {
+ __le32 lo;
+ __le16 hi;
+} __attribute__((__packed__));
+
+#define READ_PTR(ptr) (le32_to_cpup(&(ptr)->lo) | ((__u64)le16_to_cpup(&(ptr)->hi) << 32))
+
+#define WRITE_PTR(ptr, value) \
+do { \
+ (ptr)->lo = cpu_to_le32(value); \
+ (ptr)->hi = cpu_to_le16((value) >> 31 >> 1); \
+} while (0)
+
+#define DIRECTORY_ENTRIES ((BUFFER_SIZE - 16 - sizeof(struct ptr)) / sizeof(struct ptr))
+
+#define BMP_DIR_MAGIC (cpu_to_be64(0x424D505F444952ULL))
+#define CHUNK_DIR_MAGIC (cpu_to_be64(0x4348554E4B444952ULL))
+
+struct directory {
+ __be64 magic;
+ __le64 index;
+ struct ptr ptr[DIRECTORY_ENTRIES]; /* ptr to struct chunk_entries or struct bitmap */
+ struct ptr next; /* ptr to next struct directory */
+} __attribute__((__aligned__(BUFFER_SIZE)));
+
+#define SPLIT_BLOCK_MAGIC (cpu_to_be32(0x53504C49U))
+
+struct split_extent {
+ __le32 lo;
+ __le16 hi;
+ __le16 len;
+};
+
+#define MAX_SPLIT_CHUNK_CHAIN 8
+
+struct split_chunk {
+ __be32 magic;
+ __le16 pad;
+ __u8 algorithm;
+ __u8 next_valid;
+ __le32 lo; /* ptr to next split_chunk */
+ __le16 hi;
+ __le16 n_extents;
+ __le32 compressed_size;
+ __le32 pad2;
+ struct split_extent extents[];
+};
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] dm-compress: introduce the dm-compress target
2026-11-23 21:07 [PATCH] dm-compress: introduce the dm-compress target Mikulas Patocka
@ 2026-11-23 21:35 ` sashiko-bot
2026-11-23 22:27 ` Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-23 21:35 UTC (permalink / raw)
To: Mikulas Patocka, Alasdair Kergon, Mike Snitzer,
Benjamin Marzinski, Ming-Hung Tsai, Zdenek Kabelac, Filip Suba,
dm-devel
Thank you for your contribution! Sashiko AI review found 11 potential
issues to consider:
- [Critical] dm-compress: uncommitted metadata flushed and sb->cc
rewound on error
- [High] dm-compress: scomp scratch page overflow via page-split
scatterlists
- [High] dm-compress: unallocated bitmap in dm_compress_resize_bitmaps()
- [High] dm-compress: stale n_allocated_sectors in
dm_compress_resize_bitmaps()
- [High] dm-compress: missing cc->failed check in
dm_compress_chunk_decompress()
- [High] dm-compress: premature chunk release in
dm_compress_read_failed()
- [High] dm-compress: bio reordering and blocking in
dm_compress_pick_dio_for_chunk()
- [High] dm-compress: active work_struct re-initialized in
dm_compress_chunk_work()
- [Medium] dm-compress: prepare_bitmap() dirties bitmaps before
allocation check
- [Medium] dm-compress: metadata size checked against raw sectors in
initialize_device()
- [Low] dm-compress: missing index.rst entry and commit_time
documentation
--
commit aeac9b542cd6769324ef487c13c874a11f0ef7b9
Author: Mikulas Patocka <mpatocka@redhat.com>
dm-compress: introduce the dm-compress target
This commit adds the dm-compress device-mapper target for transparent block
device compression and decompression, along with a forced buffer eviction
helper in dm-bufio.
> diff --git a/Documentation/admin-guide/device-mapper/dm-compress.rst b/Documentation/admin-guide/device-mapper/dm-compress.rst
> new file mode 100644
> index 0000000000000..606e93cc5b577
> --- /dev/null
> +++ b/Documentation/admin-guide/device-mapper/dm-compress.rst
[ ... ]
> +Additional arguments:
> +
> +meta_device:device
> + Store metadata on a separate device
> +
> +n_cached_chunks:number (default 128)
> + The number of cached decompressed chunks that is kept in memory. The
> + cached chunks are preallocated when the target is loaded. The memory
> + consumption of the target is n_cached_chunks*chunk_size*3.
[Severity: Low]
Should Documentation/admin-guide/device-mapper/dm-compress.rst be added to
Documentation/admin-guide/device-mapper/index.rst to avoid a Sphinx warning
during htmldocs builds, and should the optional commit_time:<msec> argument
parsed in dm_compress_ctr() also be documented here?
> diff --git a/drivers/md/dm-compress/alloc.c b/drivers/md/dm-compress/alloc.c
> new file mode 100644
> index 0000000000000..7a990124a3d11
> --- /dev/null
> +++ b/drivers/md/dm-compress/alloc.c
> @@ -0,0 +1,480 @@
[ ... ]
> +static int prepare_bitmap(struct dm_compress *c, sector_t bmp_block, __u64 **working_copy, __u64 **stable_copy, struct dm_buffer **bp)
> +{
[ ... ]
> + idx = !CC_VALID(c, &bmp->cc_txc);
> + if (unlikely(!CC_CURRENT(c, &bmp->cc_txc))) {
> + CC_SET_CURRENT(c, &bmp->cc_txc);
> + //WARN_ON(idx != CC_VALID(c, &bmp->cc_txc));
> + if (!idx)
> + memcpy(bmp->bmp1, bmp->bmp2, BITS_PER_BITMAP / 8);
> + else
> + memcpy(bmp->bmp2, bmp->bmp1, BITS_PER_BITMAP / 8);
> + idx = !idx;
> + dm_bufio_mark_buffer_dirty(*bp);
> + }
[ ... ]
> +}
[ ... ]
> +sector_t dm_compress_allocate_space(struct dm_compress *c, sector_t preferred_block, unsigned n_blocks)
> +{
[ ... ]
> + mutex_lock(&group->mutex);
> + if (unlikely(prepare_bitmap(c, bmp_block, &working_copy, &stable_copy, &bp))) {
> + mutex_unlock(&group->mutex);
> + return ALLOCATE_ERROR;
> + }
> +
> +find_again:
> + bit = find_next_zero_bit_le(working_copy, BITS_PER_BITMAP, bmp_offset);
[ ... ]
> + dm_bufio_release(bp);
> + mutex_unlock(&group->mutex);
> +
> + if (++n_retries <= c->n_groups) {
> + preferred_block += BITS_PER_BITMAP;
> + goto try_next_bitmap;
> + }
[Severity: Medium]
Does calling prepare_bitmap() before checking whether the bitmap has enough
free bits dirty every bitmap visited during an allocation scan?
When dm_compress_allocate_space() steps past full or fragmented bitmaps,
prepare_bitmap() updates bmp->cc_txc, copies 2040 bytes between bmp1 and
bmp2, and marks the buffer dirty even when no bits are allocated. In
addition, struct block_group_descriptor has a free field initialized to -1
in dm_compress_ctr() that is never maintained or consulted, so failed
contiguous allocations can linearly scan and dirty every bitmap on the
device before commit.
[ ... ]
> +int dm_compress_resize_bitmaps(struct dm_compress *c, sector_t current_size, sector_t new_size, bool *need_sync)
> +{
[ ... ]
> + need_sectors = 0;
> + if (!bmp_offset && !dir_offset)
> + need_sectors = BUFFER_SECTORS * 2;
> + else if (!bmp_offset)
> + need_sectors = BUFFER_SECTORS;
> + if (n_allocated_sectors < need_sectors) {
> + if (c->meta_dev) {
> + dm_compress_error(c, "The metadata device is too small", -ENOSPC);
> + return -ENOSPC;
> + }
> +align_allocated:
> + allocated_sector = current_size << c->log2_sectors_per_block;
> + if (allocated_sector & (BUFFER_SECTORS - 1)) {
> + if (current_size >= new_size)
> + goto finish;
> + current_size++;
> + goto align_allocated;
> + }
> + while (n_allocated_sectors < need_sectors) {
> + if (current_size >= new_size)
> + goto finish;
> + current_size++;
> + n_allocated_sectors += 1 << c->log2_sectors_per_block;
> + }
> + }
[Severity: High]
When align_allocated sets a new allocated_sector because
n_allocated_sectors < need_sectors, should n_allocated_sectors be reset to
0?
With !c->meta_dev and a block size larger than BUFFER_SIZE (e.g. 8 KiB
blocks, 16 sectors), reserving one block adds 16 sectors to
n_allocated_sectors while allocating a 4 KiB bitmap consumes 8 sectors,
leaving n_allocated_sectors == 8. Because DIRECTORY_ENTRIES (679) is odd,
reaching a directory boundary requires need_sectors == 16 while
n_allocated_sectors is 8. Jumping to align_allocated moves
allocated_sector to current_size without zeroing n_allocated_sectors, so
the loop only reserves one 8 KiB block instead of two, leaving
allocated_sector pointing at the next block while that block is marked free
in the bitmap and later overwritten by the next metadata bitmap.
> +
> + did_something = true;
[ ... ]
> +finish:
> + if (did_something) {
> + bool b;
> + *need_sync = true;
> + b = !CC_VALID(c, &sb->n_cc_txc);
> + if (unlikely(!CC_CURRENT(c, &sb->n_cc_txc))) {
> + CC_SET_CURRENT(c, &sb->n_cc_txc);
> + WARN_ON(b != CC_VALID(c, &sb->n_cc_txc));
> + sb->n_blocks[!b] = sb->n_blocks[b];
> + sb->n_chunks[!b] = sb->n_chunks[b];
> + sb->n_used_metadata_blocks[!b] = sb->n_used_metadata_blocks[b];
> + b = !b;
> + }
> + sb->n_blocks[b] = cpu_to_le64(current_size);
> + if (c->meta_dev)
> + sb->n_used_metadata_blocks[b] = cpu_to_le64(allocated_sector / BUFFER_SECTORS);
> + r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
> + if (r) {
> + dm_compress_error(c, "Error writing superblock", r);
> + return r;
> + }
> + c->n_blocks = current_size;
> + }
> + return 0;
> +}
[Severity: High]
What happens in dm_compress_resize_bitmaps() if current_size reaches
new_size inside the align_allocated or n_allocated_sectors < need_sectors
loops and jumps to finish?
Because current_size is incremented in place before checking whether enough
new blocks exist to satisfy need_sectors, jumping to finish leaves
current_size == new_size without allocating the new bitmap or directory.
If did_something is true from an earlier iteration, finish writes
sb->n_blocks[b] = new_size to the superblock; if did_something is false,
c->n_blocks still remains at new_size from dm_compress_ctr(). In both
cases dm_compress_preresume() then tries to load or scan the unallocated
bitmap/directory at block 0 (the superblock) and fails with -EUCLEAN.
> diff --git a/drivers/md/dm-compress/cc.c b/drivers/md/dm-compress/cc.c
> new file mode 100644
> index 0000000000000..bf9b940c0178e
> --- /dev/null
> +++ b/drivers/md/dm-compress/cc.c
> @@ -0,0 +1,251 @@
[ ... ]
> +static struct scatterlist *dm_compress_alloc_sglist(void *data, unsigned len, unsigned offset)
> +{
> + struct scatterlist *sg;
> + if (!is_vmalloc_addr(data)) {
> + sg = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
> + if (!sg)
> + return NULL;
> + sg_init_one(sg, data + offset, len - offset);
> + } else {
> + unsigned i;
> + unsigned n_entries = DIV_ROUND_UP(len, PAGE_SIZE);
> + size_t s = array_size(sizeof(struct scatterlist), n_entries);
> + sg = kmalloc(s, GFP_KERNEL);
> + if (!sg)
> + return NULL;
> + sg_init_table(sg, n_entries);
> + for (i = 0; i < n_entries; i++) {
> + struct page *pg = vmalloc_to_page(data);
> + unsigned pg_len = min(len, PAGE_SIZE);
> + sg_set_page(&sg[i], pg, pg_len - offset, offset);
> + data += PAGE_SIZE;
> + len -= PAGE_SIZE;
> + offset = 0;
> + }
> + }
> + return sg;
> +}
[Severity: High]
Can passing PAGE_SIZE-split scatterlists via acomp_request_set_params()
overflow scomp's order-0 scratch->src page or fail with -ENOSYS?
Four of the six supported algorithms (lzo, 842, lz4, lz4hc) are scomp
algorithms bridged through scomp_acomp_comp_decomp(). When virtual
pointers are not used, scomp_acomp_comp_decomp() requires dlen <=
req->dst->length (only the first scatterlist entry, which is PAGE_SIZE - 4
on compress and PAGE_SIZE on decompress) and copies linear source data into
scratch->src allocated as a single order-0 page by scomp_alloc_scratch():
crypto/scompress.c:scomp_acomp_comp_decomp() {
...
if (!dst_isvirt) {
if (dlen <= req->dst->length) {
...
} else
return -ENOSYS;
}
if (src_isvirt || slen <= req->src->length) {
...
} else {
memcpy_from_sglist(scratch->src, req->src, 0, slen);
src = scratch->src;
}
...
}
When chunk_size is 2 * PAGE_SIZE (8192) and block_size is PAGE_SIZE (4096),
len_out is 4092 == cc->compressed_sglist[0].length, so the destination
check passes and memcpy_from_sglist() copies slen (8192 bytes) into the
4096-byte scratch->src page, overflowing it by 4096 bytes. For larger
chunk sizes, compression fails with -ENOSYS and silently falls back to
ALGORITHM_UNCOMPRESSED, and decompression of any scomp-compressed chunk
with chunk_size > PAGE_SIZE fails with -ENOSYS. Since the buffers come
from __vmalloc(), passing them via acomp_request_set_src_nondma() and
acomp_request_set_dst_nondma() would avoid the scatterlist path entirely.
[ ... ]
> +void dm_compress_wait_for_cached_chunk(struct dm_compress_io *dio)
> +{
> + struct dm_compress *c = dio->c;
> + struct rb_node *parent, **node;
> +
> + list_add(&dio->waiting_list_entry, &c->waiting_bio_lru);
> +
> + parent = NULL;
> + node = &c->waiting_bio_tree.rb_node;
> +
> + while (*node) {
> + struct dm_compress_io *tdio = container_of(*node, struct dm_compress_io, tree_entry);
> + parent = &tdio->tree_entry;
> + if (tdio->chunk > dio->chunk) {
> + node = &parent->rb_left;
> + } else {
> + node = &parent->rb_right;
> + }
> + }
> +
> + rb_link_node(&dio->tree_entry, parent, node);
> + rb_insert_color(&dio->tree_entry, &c->waiting_bio_tree);
> +}
> +
> +struct dm_compress_io *dm_compress_pick_dio_for_chunk(struct dm_compress *c, sector_t chunk)
> +{
> + struct rb_node *node = c->waiting_bio_tree.rb_node;
> +
> + while (node) {
> + struct dm_compress_io *tdio = container_of(node, struct dm_compress_io, tree_entry);
> + if (tdio->chunk == chunk) {
> + return tdio;
> + }
> + if (tdio->chunk > chunk)
> + node = tdio->tree_entry.rb_left;
> + else
> + node = tdio->tree_entry.rb_right;
> + }
> +
> + return NULL;
> +}
[Severity: High]
Can inserting duplicate chunk keys into c->waiting_bio_tree reorder or
block waiting bios in dm_compress_pick_dio_for_chunk()?
When multiple bios wait on the same chunk, rb_insert_color() and
rb_erase() rotations can place a newer dio above an older dio with the same
chunk key. dm_compress_pick_dio_for_chunk() returns the first matching
node closest to the root, so waiting writes to the same chunk can execute
out of FIFO order and overwrite newer data with older data.
In addition, if the topmost node matching chunk cannot run yet (for
example, a write bio when cc->intent >= INTENT_WRITE in
dm_compress_chunk_decompress(), or a waiting flush bio which
dm_compress_setup_flush() inserts with fake key dio->chunk = 0),
dm_compress_process_bios_on_chunk() stops immediately without serving
runnable read bios for that chunk located lower in the tree.
> diff --git a/drivers/md/dm-compress/io.c b/drivers/md/dm-compress/io.c
> new file mode 100644
> index 0000000000000..64f3e4d58d67e
> --- /dev/null
> +++ b/drivers/md/dm-compress/io.c
> @@ -0,0 +1,652 @@
[ ... ]
> +static void dm_compress_chunk_work(struct work_struct *w)
> +{
[ ... ]
> + INIT_WORK(&cc->work, dm_compress_chunk_work_2);
> + if (unlikely(!dm_compress_allocate_chunk(cc, preferred_block, &new_cp))) {
> + complete(&cc->completion);
> + dm_compress_chunk_work_2(&cc->work);
> + return;
> + }
> +
> + cp = dm_compress_prepare_chunk(c, cc->chunk, &bp);
> + if (unlikely(!cp))
> + goto cont;
> +
> + old_cp = *cp;
> + *cp = new_cp;
> +
> + dm_bufio_mark_buffer_dirty(bp);
> + dm_bufio_release(bp);
> +
> + dm_compress_release_chunk(c, &old_cp);
> +
> +cont:
> + complete(&cc->completion);
> +}
> +
> +static void dm_compress_chunk_work_2(struct work_struct *w)
> +{
> + struct cached_chunk *cc = container_of(w, struct cached_chunk, work);
> + struct dm_compress *c = cc->c;
> +
> + wait_for_completion(&cc->completion);
> +
> + mutex_lock(&c->mutex);
> + if (cc->intent >= INTENT_FREE) {
> + dm_compress_release_cached_chunk(cc);
> + } else {
> + cc->intent = INTENT_IDLE;
> + }
> + dm_compress_process_pending_ios(c);
> +}
[Severity: High]
Is it safe to call INIT_WORK(&cc->work, ...) and queue &cc->work while a
work callback on &cc->work is still running?
kworker/0 kworker/1
process_one_work(&cc->work)
dm_compress_chunk_work(&cc->work)
INIT_WORK(&cc->work,
dm_compress_chunk_work_2)
dm_compress_allocate_chunk()
...
queue_work(..., &cc->work)
process_one_work(&cc->work)
dm_compress_chunk_work_2()
wait_for_completion()
dm_compress_prepare_chunk()
dm_compress_release_chunk()
complete(&cc->completion)
cc->intent = INTENT_IDLE
dm_compress_process_pending_ios()
dm_compress_enqueue_chunk()
INIT_WORK(&cc->work, ...)
Calling INIT_WORK() inside dm_compress_chunk_work() (and similarly in
dm_compress_read_and_decompress() and via dm_compress_enqueue_chunk()
before the worker returns) overwrites cc->work.data and cc->work.lockdep_map
while process_one_work() is still tracking the work item, breaking
workqueue non-reentrancy tracking and allowing concurrent execution on the
same work_struct.
[ ... ]
> +static void dm_compress_read_failed(struct cached_chunk *cc)
> +{
> + struct dm_compress *c = cc->c;
> + sector_t chunk = cc->chunk;
> + mutex_lock(&c->mutex);
> + if (unlikely(cc->dirty)) {
> + cc->dirty = false;
> + dm_compress_error(c, "Could not read a chunk with pending writes", -EIO);
> + }
> + if (cc->intent >= INTENT_FREE) {
> + dm_compress_release_cached_chunk(cc);
> + } else {
> + cc->intent = INTENT_IDLE;
> + }
> + dm_compress_process_bios_on_chunk(c, chunk);
> +}
[Severity: High]
If cc->intent was upgraded to INTENT_FREE while a read was in flight, does
releasing cc before dm_compress_process_bios_on_chunk() prevent waiting
read bios from seeing cc->failed?
When c->free_chunks is empty, another bio can select cc from c->lru.prev
and upgrade cc->intent from INTENT_READ to INTENT_FREE. If the read or
decompression then fails, dm_compress_read_failed() calls
dm_compress_release_cached_chunk(cc) before
dm_compress_process_bios_on_chunk(c, chunk), removing cc from c->chunks.
When dm_compress_process_bios_on_chunk() then runs the waiting read bio for
chunk, dm_compress_find_cached_chunk() returns NULL instead of finding
cc->failed, and dm_compress_new_cached_chunk() re-allocates cc with
cc->failed reset to 0, retrying the failed read in an unbounded loop.
> +
> +static void dm_compress_read_and_decompress(struct cached_chunk *cc)
[ ... ]
> +static void dm_compress_chunk_decompress(struct work_struct *w)
> +{
> + struct cached_chunk *cc = container_of(w, struct cached_chunk, work);
> + struct dm_compress *c = cc->c;
> + unsigned i;
> + unsigned intent;
> +
> + if (cc->algorithm == ALGORITHM_ZERO) {
[Severity: High]
Should dm_compress_chunk_decompress() check cc->failed at entry before
copying or decompressing cc->compressed_data?
When a non-split read bio fails, dm_compress_read_endio() records the error
in cc->failed via dm_compress_error_chunk() and queues cc->work, which runs
dm_compress_chunk_decompress(). Because dm_compress_chunk_decompress()
does not check cc->failed, it proceeds to copy or decompress stale data
from cc->compressed_data into cc->uncompressed_data, marks all
cc->sector_tags as TAG_VALID, and skips dm_compress_read_failed(cc). If
cc->dirty was set from an earlier partial write, skipping
dm_compress_read_failed(cc) also skips disabling the device and allows a
subsequent write to reset cc->dirty and cc->sector_tags, silently losing
the earlier write.
> diff --git a/drivers/md/dm-compress/target.c b/drivers/md/dm-compress/target.c
> new file mode 100644
> index 0000000000000..6f71b41e3e44d
> --- /dev/null
> +++ b/drivers/md/dm-compress/target.c
> @@ -0,0 +1,959 @@
[ ... ]
> +void dm_compress_sync(struct dm_compress *c)
> +{
> + struct block_device *bdev = c->meta_dev ? c->meta_dev->bdev : c->dev->bdev;
> + __u16 cc;
> + unsigned granularity;
> + unsigned offset;
> + int r;
> +
> + r = dm_bufio_write_dirty_buffers(c->bufio);
> + if (unlikely(r)) {
> + dm_compress_error(c, "Error flushing buffers", r);
> + goto skip_commit;
> + }
> +
> + if (unlikely(c->split_bufio_dirty)) {
> + c->split_bufio_dirty = false;
> + r = dm_bufio_write_dirty_buffers(c->split_bufio);
> + if (unlikely(r)) {
> + dm_compress_error(c, "Error flushing split chunk buffers", r);
> + goto skip_commit;
> + }
> + goto skip_flush_dev;
> + }
> +
> + if (c->meta_dev) {
> + r = blkdev_issue_flush(c->dev->bdev);
> + if (unlikely(r)) {
> + dm_compress_error(c, "Error flushing the data device", r);
> + goto skip_commit;
> + }
> + }
> +skip_flush_dev:
> +
> + if (unlikely(c->failed))
> + goto skip_commit;
[Severity: Critical]
When c->failed is set, does flushing dirty buffers in dm_compress_sync()
and rewinding c->sb->cc in dm_compress_postsuspend() cause uncommitted
metadata from a failed transaction to be treated as valid on the next
resume?
In dm_compress_preresume(), c->sb->cc is incremented on disk from K to
K + 1 and c->cc_txc is advanced to transaction T + 0x10000, while on-disk
c->cct->entries[K] remains at T until commit. If a runtime error (such as
-ENOSPC mid-allocation in dm_compress_allocate_chunk() or a write error)
sets c->failed, dirty metadata buffers in c->bufio are already stamped with
(K, T + 0x10000). When dm_compress_postsuspend() runs:
drivers/md/dm-compress/target.c:dm_compress_postsuspend() {
...
dm_compress_sync(c);
mutex_unlock(&c->mutex);
cancel_delayed_work_sync(&c->autocommit_work);
c->cct->entries[c->cc_txc & 0xffff] = cpu_to_le64(le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) - 0x10000);
c->sb->cc = cpu_to_le16(le16_to_cpup(&c->sb->cc) - 1);
c->cc_txc -= 0x10000;
r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
...
}
dm_compress_sync() writes the dirty metadata buffers to disk before
checking c->failed and skipping the c->cct update, and
dm_compress_postsuspend() then unconditionally decrements c->sb->cc back to
K on disk. On the next resume, dm_compress_preresume() reads sb->cc == K
and cct->entries[K] == T, starting the new session with the same
(K, T + 0x10000) stamp as the aborted transaction so CC_VALID() treats the
partially updated metadata as committed instead of rolling it back.
[ ... ]
> +static int initialize_device(struct dm_compress *c)
> +{
[ ... ]
> + meta_size = bdev_nr_sectors(c->meta_dev ? c->meta_dev->bdev : c->dev->bdev);
> + meta_size = meta_size >= c->start ? meta_size - c->start : 0;
> + meta_size >>= log2_buffer_sectors;
> +
> + used_meta_size = position + n_bitmap_blocks + n_bitmap_directories + n_chunk_blocks + n_chunk_directories;
> + if (c->meta_dev)
> + sb->n_used_metadata_blocks[0] = cpu_to_le64(used_meta_size);
> + if (used_meta_size > meta_size) {
> + if (c->meta_dev)
> + c->ti->error = "The metadata device is too small";
> + else
> + c->ti->error = "The device is too small";
> + return -ENOSPC;
> + }
[ ... ]
> + if (c->log2_sectors_per_block < log2_buffer_sectors)
> + used_meta_size <<= log2_buffer_sectors - c->log2_sectors_per_block;
> + else if (c->log2_sectors_per_block > log2_buffer_sectors)
> + used_meta_size = dm_sector_div_up(used_meta_size, 1U << (c->log2_sectors_per_block - log2_buffer_sectors));
[Severity: Medium]
When !c->meta_dev, should initialize_device() verify that used_meta_size
(after converting to data blocks) is strictly less than data_blocks?
dm_compress_ctr() computes c->n_blocks (data_blocks) by shifting the device
sector count right by c->log2_sectors_per_block, which truncates any
trailing sectors that do not form a full data block. Because
initialize_device() checks used_meta_size against meta_size in 4 KiB buffer
units before converting used_meta_size to data block units, a device with
block_size > 4096 and trailing sectors can place metadata buffers in the
partial block at index data_blocks (or format with 0 free data blocks). If
the device is later grown, dm_compress_resize_bitmaps() clears the bitmap
bit starting at current_size == data_blocks, freeing the block holding
those metadata buffers for data allocation.
--
Sashiko AI review · https://sashiko.dev/#/patchset/d3fbdf3d-c7d8-0e4f-31f4-b187eb52ec30@redhat.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] dm-compress: introduce the dm-compress target
2026-11-23 21:07 [PATCH] dm-compress: introduce the dm-compress target Mikulas Patocka
2026-11-23 21:35 ` sashiko-bot
@ 2026-11-23 22:27 ` Eric Biggers
1 sibling, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-11-23 22:27 UTC (permalink / raw)
To: Mikulas Patocka
Cc: Alasdair Kergon, Mike Snitzer, Benjamin Marzinski, Ming-Hung Tsai,
Zdenek Kabelac, Filip Suba, dm-devel
On Mon, Nov 23, 2026 at 10:07:01PM +0100, Mikulas Patocka wrote:
> +static int __init dm_compress_init(void)
> +{
> + int r;
> + unsigned i;
> +
> + for (i = 0; i < dm_compress_n_algorithms; i++) {
> + unsigned request_size;
> + dm_compress_algorithms[i].cc = crypto_alloc_acomp(dm_compress_algorithms[i].name, 0, 0);
> + if (IS_ERR(dm_compress_algorithms[i].cc)) {
> + r = PTR_ERR(dm_compress_algorithms[i].cc);
> + dm_compress_algorithms[i].cc = NULL;
> + DMERR("Could not initialize algorithm %s", dm_compress_algorithms[i].name);
> + goto bad;
> + }
> + request_size = ALIGN(sizeof(struct acomp_req) + crypto_acomp_reqsize(dm_compress_algorithms[i].cc), CRYPTO_MINALIGN);
> + dm_compress_max_request_size = max(dm_compress_max_request_size, request_size);
> + }
I would strongly suggest avoiding crypto_acomp for now. Instead, it
would be better to simplify the set of supported algorithms to ZSTD and
LZ4 (the ones that are well known to do the best on the space/time
tradeoff curve) and call those library APIs directly.
That avoids all the scatterlist nonsense and other crypto_acomp issues,
especially when it seems the caller in this module has linear buffers
already and waits synchronously for each one to complete. (acomp does
now support virtual addresses too, but it's still an extra layer that
provides no benefit for a synchronous software-only user like this.)
As-is, it looks like it also doesn't work correctly with the scomp-based
algorithms (lz4, lz4hc, lzo, 842) when chunk_size > PAGE_SIZE. In that
case scomp either returns -ENOSYS (making dm-compress silently store
everything uncompressed) or overflows its internal scratch buffer.
There's plenty of precedent for just using the compression libraries
directly: zram, dm-vdo, btrfs, f2fs, etc. If hardware offload is ever
wanted, it can be added later as an optional path, like erofs does.
- Eric
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 22:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 21:07 [PATCH] dm-compress: introduce the dm-compress target Mikulas Patocka
2026-11-23 21:35 ` sashiko-bot
2026-11-23 22:27 ` Eric Biggers
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.