Skip to contentSkip to conversation

dm: add era target

PATCH In review Mike Snitzer 3 participants 5 messages Mar 7, 2014 – Mar 11, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 dm: add era target

From: Joe Thornber <[email protected]>

dm-era is a target that behaves similar to the linear target. In addition it keeps track of which blocks were written within a user defined period of time called an 'era'. Each era target instance maintains the current era as a monotonically increasing 32-bit counter.

Use cases include tracking changed blocks for backup software, and partially invalidating the contents of a cache to restore cache coherency after rolling back a vendor snapshot.

dm-era is primarily expected to be paired with the dm-cache target.

---
 Documentation/device-mapper/era.txt |  107 +++
 drivers/md/Kconfig                  |   11 +
 drivers/md/Makefile                 |    2 +
 drivers/md/dm-era-target.c          | 1667 +++++++++++++++++++++++++++++++++++
 4 files changed, 1787 insertions(+)
 create mode 100644 Documentation/device-mapper/era.txt
 create mode 100644 drivers/md/dm-era-target.c
Documentation/device-mapper/era.txt new file +107 -0
new file mode 100644
index 0000000..f1173ed
@@ -0,0 +1,107 @@
+Introduction
+============
+
+dm-era is a target that behaves similar to the linear target.  In
+addition it keeps track of which blocks were written within a user
+defined period of time called an 'era'.  Each era target instance
+maintains the current era as a monotonically increasing 32-bit
+counter.
+
+Use cases include tracking changed blocks for backup software, and
+partially invalidating the contents of a cache to restore cache
+coherency after rolling back a vendor snapshot.
+
+Constructor
+===========
+
+ era <metadata dev> <origin dev> <block size>
+
+ metadata dev    : fast device holding the persistent metadata
+ origin dev	 : device holding data blocks that may change
+ block size      : block size of origin data device
+
+Messages
+========
+
+None of the dm messages take any arguments.
+
+checkpoint
+----------
+
+Possibly move to a new era.  You shouldn't assume the era has
+incremented.  After sending this message, you should check the
+current era via the status line.
+
+take_metadata_snap
+------------------
+
+Create a clone of the metadata, to allow a userland process to read it.
+
+drop_metadata_snap
+------------------
+
+Drop the metadata snapshot.
+
+Status
+======
+
+<metadata block size> <#used metadata blocks>/<#total metadata blocks>
+<current era> <held metadata root | '-'>
+
+metadata block size	 : Fixed block size for each metadata block in
+			     sectors
+#used metadata blocks	 : Number of metadata blocks used
+#total metadata blocks	 : Total number of metadata blocks
+current era		 : The current era
+held metadata root	 : The location, in blocks, of the metadata root
+			     that has been 'held' for userspace read
+			     access. '-' indicates there is no held root
+
+Detailed use case
+=================
+
+The scenario of invalidating a cache when rolling back a vendor
+snapshot was the primary use case when developing this target:
+
+Taking a vendor snapshot
+------------------------
+
+- Send a checkpoint message to the era target
+- Make a note of the current era in its status line
+- Take vendor snapshot (the era and snapshot should be forever
+  associated now).
+
+Rolling back to an vendor snapshot
+----------------------------------
+
+- Cache enters passthrough mode (see: dm-cache's docs in cache.txt)
+- Rollback vendor storage
+- Take metadata snapshot
+- Ascertain which blocks have been written since the snapshot was taken
+  by checking each block's era
+- Invalidate those blocks in the caching software
+- Cache returns to writeback/writethrough mode
+
+Memory usage
+============
+
+The target uses a bitset to record writes in the current era.  It also
+has a spare bitset ready for switching over to a new era.  Other than
+that it uses a few 4k blocks for updating metadata.
+
+   (4 * nr_blocks) bytes + buffers
+
+Resilience
+==========
+
+Metadata is updated on disk before a write to a previously unwritten
+block is performed.  As such dm-era should not be effected by a hard
+crash such as power failure.
+
+Userland tools
+==============
+
+Userland tools are found in the increasingly poorly named
+thin-provisioning-tools project:
+
+    https://github.com/jthornber/thin-provisioning-tools
drivers/md/Kconfig +11 -0
index 95ad936..5bdedf6 100644
@@ -285,6 +285,17 @@ config DM_CACHE_CLEANER
          A simple cache policy that writes back all data to the
          origin.  Used when decommissioning a dm-cache.
 
+config DM_ERA
+       tristate "Era target (EXPERIMENTAL)"
+       depends on BLK_DEV_DM
+       default n
+       select DM_PERSISTENT_DATA
+       select DM_BIO_PRISON
+       ---help---
+         dm-era tracks which parts of a block device are written to
+         over time.  Useful for maintaining cache coherency when using
+         vendor snapshots.
+
 config DM_MIRROR
        tristate "Mirror target"
        depends on BLK_DEV_DM
drivers/md/Makefile +2 -0
index f26d832..a2da532 100644
@@ -14,6 +14,7 @@ dm-thin-pool-y	+= dm-thin.o dm-thin-metadata.o
 dm-cache-y	+= dm-cache-target.o dm-cache-metadata.o dm-cache-policy.o
 dm-cache-mq-y   += dm-cache-policy-mq.o
 dm-cache-cleaner-y += dm-cache-policy-cleaner.o
+dm-era-y	+= dm-era-target.o
 md-mod-y	+= md.o bitmap.o
 raid456-y	+= raid5.o
 
@@ -53,6 +54,7 @@ obj-$(CONFIG_DM_VERITY)		+= dm-verity.o
 obj-$(CONFIG_DM_CACHE)		+= dm-cache.o
 obj-$(CONFIG_DM_CACHE_MQ)	+= dm-cache-mq.o
 obj-$(CONFIG_DM_CACHE_CLEANER)	+= dm-cache-cleaner.o
+obj-$(CONFIG_DM_ERA)		+= dm-era.o
 
 ifeq ($(CONFIG_DM_UEVENT),y)
 dm-mod-objs			+= dm-uevent.o
drivers/md/dm-era-target.c new file +1667 -0

1,667 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 2/3 dm era: support non power-of-2 blocksize

The dm-era target is expected to be paired with the dm-cache target which has support for non power-of-2 data blocksize. So dm-era should also support an origin data device blocksize that is non power-of-2.

---
 drivers/md/dm-era-target.c | 42 +++++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 13 deletions(-)
drivers/md/dm-era-target.c +29 -13
index f772351..d7bd7b3 100644
@@ -1080,9 +1080,9 @@ struct era {
 	struct dm_dev *metadata_dev;
 	struct dm_dev *origin_dev;
 
-	uint32_t block_size;
 	dm_block_t nr_blocks;
-	unsigned sectors_per_block_shift;
+	uint32_t sectors_per_block;
+	int sectors_per_block_shift;
 	struct era_metadata *md;
 
 	struct workqueue_struct *wq;
@@ -1113,9 +1113,21 @@ struct rpc {
 /*----------------------------------------------------------------
  * Remapping.
  *---------------------------------------------------------------*/
+static bool block_size_is_power_of_two(struct era *era)
+{
+	return era->sectors_per_block_shift >= 0;
+}
+
 static dm_block_t get_block(struct era *era, struct bio *bio)
 {
-	return bio->bi_iter.bi_sector >> era->sectors_per_block_shift;
+	dm_block_t block_nr = bio->bi_iter.bi_sector;
+
+	if (!block_size_is_power_of_two(era))
+		(void) sector_div(block_nr, era->sectors_per_block);
+	else
+		block_nr >>= era->sectors_per_block_shift;
+
+	return block_nr;
 }
 
 static void remap_to_origin(struct era *era, struct bio *bio)
@@ -1336,7 +1348,7 @@ static void era_destroy(struct era *era)
 
 static dm_block_t calc_nr_blocks(struct era *era)
 {
-	return dm_sector_div_up(era->ti->len, era->block_size);
+	return dm_sector_div_up(era->ti->len, era->sectors_per_block);
 }
 
 /*
@@ -1376,22 +1388,26 @@ static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
 		return -EINVAL;
 	}
 
-	r = sscanf(argv[2], "%u%c", &era->block_size, &dummy);
+	r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
 	if (r != 1) {
 		ti->error = "Error parsing block size";
 		era_destroy(era);
 		return -EINVAL;
 	}
-	era->sectors_per_block_shift = __ffs(era->block_size);
 
-	r = dm_set_target_max_io_len(ti, era->block_size);
+	r = dm_set_target_max_io_len(ti, era->sectors_per_block);
 	if (r) {
 		ti->error = "could not set max io len";
 		era_destroy(era);
 		return -EINVAL;
 	}
 
-	md = metadata_open(era->metadata_dev->bdev, era->block_size, true);
+	if (era->sectors_per_block & (era->sectors_per_block - 1))
+		era->sectors_per_block_shift = -1;
+	else
+		era->sectors_per_block_shift = __ffs(era->sectors_per_block);
+
+	md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
 	if (IS_ERR(md)) {
 		ti->error = "Error reading metadata";
 		era_destroy(era);
@@ -1549,7 +1565,7 @@ static void era_status(struct dm_target *ti, status_type_t type,
 		format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
 		DMEMIT("%s ", buf);
 		format_dev_t(buf, era->origin_dev->bdev->bd_dev);
-		DMEMIT("%s %u", buf, era->block_size);
+		DMEMIT("%s %u", buf, era->sectors_per_block);
 		break;
 	}
 
@@ -1614,12 +1630,12 @@ static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
 
 	/*
 	 * If the system-determined stacked limits are compatible with the
-	 * cache's blocksize (io_opt is a factor) do not override them.
+	 * era device's blocksize (io_opt is a factor) do not override them.
 	 */
-	if (io_opt_sectors < era->block_size ||
-	    do_div(io_opt_sectors, era->block_size)) {
+	if (io_opt_sectors < era->sectors_per_block ||
+	    do_div(io_opt_sectors, era->sectors_per_block)) {
 		blk_limits_io_min(limits, 0);
-		blk_limits_io_opt(limits, era->block_size << SECTOR_SHIFT);
+		blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
 	}
 }
 
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 3/3 dm bitset: only flush the current word if it has been dirtied

In reply to Mike Snitzer

From: Joe Thornber <[email protected]>

This change offers a big performance boost for dm-era.

---
 drivers/md/persistent-data/dm-bitset.c | 10 +++++++++-
 drivers/md/persistent-data/dm-bitset.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)
drivers/md/persistent-data/dm-bitset.c +9 -1
index cd9a86d..36f7cc2 100644
@@ -65,7 +65,7 @@ int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
 	int r;
 	__le64 value;
 
-	if (!info->current_index_set)
+	if (!info->current_index_set || !info->dirty)
 		return 0;
 
 	value = cpu_to_le64(info->current_bits);
@@ -77,6 +77,8 @@ int dm_bitset_flush(struct dm_disk_bitset *info, dm_block_t root,
 		return r;
 
 	info->current_index_set = false;
+	info->dirty = false;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(dm_bitset_flush);
@@ -94,6 +96,8 @@ static int read_bits(struct dm_disk_bitset *info, dm_block_t root,
 	info->current_bits = le64_to_cpu(value);
 	info->current_index_set = true;
 	info->current_index = array_index;
+	info->dirty = false;
+
 	return 0;
 }
 
@@ -126,6 +130,8 @@ int dm_bitset_set_bit(struct dm_disk_bitset *info, dm_block_t root,
 		return r;
 
 	set_bit(b, (unsigned long *) &info->current_bits);
+	info->dirty = true;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(dm_bitset_set_bit);
@@ -141,6 +147,8 @@ int dm_bitset_clear_bit(struct dm_disk_bitset *info, dm_block_t root,
 		return r;
 
 	clear_bit(b, (unsigned long *) &info->current_bits);
+	info->dirty = true;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(dm_bitset_clear_bit);
drivers/md/persistent-data/dm-bitset.h +1 -0
index e1b9bea..c2287d6 100644
@@ -71,6 +71,7 @@ struct dm_disk_bitset {
 	uint64_t current_bits;
 
 	bool current_index_set:1;
+	bool dirty:1;
 };
 
 /*
Signature · 1 line
1.8.1.4
maintainer
#3 lore raw
In reply to Mike Snitzer
Quoted 8 lines · Mike Snitzer

From: Joe Thornber <[email protected]>

dm-era is a target that behaves similar to the linear target. In addition it keeps track of which blocks were written within a user defined period of time called an 'era'. Each era target instance maintains the current era as a monotonically increasing 32-bit counter.

Quoted 5 lines

Use cases include tracking changed blocks for backup software, and partially invalidating the contents of a cache to restore cache coherency after rolling back a vendor snapshot.

dm-era is primarily expected to be paired with the dm-cache target.

Hi

I reviewed dm-era, here is a possible list of bugs:

There is no tracking of in-progress writes. Suppose that this happens:
1) a write is received, era_map sees that metadata_current_marked(era->md, block) is true, so it lets the write through
2) the user sends the checkpoint message
3) the write received in step 1) hits the disk and modifies it
4) the user sends metadata_take_snap message, the write is not in the snapshot, although it modified the disk between steps 2) and 4)
You can use the functions dm_internal_suspend and dm_internal_resume to clear in-progress bios - this is lightweight suspend/resume callable only from the kernel.

era_destroy calls metadata_close(era->md); without checking that era->md is not NULL. This can cause crash if era_destroy is called early in the constructor.

era->sectors_per_block should be validated that it is not zero.

There is no write memory barrier between the setting of rpc->result and rpc->complete:

                if (r)
                        list_for_each_entry_safe(rpc, tmp, &calls, list)
                                rpc->result = r;
        }

        list_for_each_entry_safe(rpc, tmp, &calls, list) {
                atomic_set(&rpc->complete, 1);
And there is no read memory barrier between the reading of rpc->complete 
and rpc->result:
        wait_event(rpc->wait, atomic_read(&rpc->complete));

        return rpc->result;

You should use completion (include/linux/completion.h) instead of rpc->complete and rpc->wait, it simplifies the code and takes care of memory barriers.

metadata_space_map_root is not aligned on 64-bit boundary, but it is accessed as 64-bit numbers in sm_ll_open_metadata.

Mixing 32-bit and 64-bit current_era (I'm not sure it this is a bug or not).

Missing read and write memory barriers around accesses to archived_writesets (I'm not sure if this is a bug, but it looks strange).

create_fresh_metadata doesn't free md->tm and md->sm on errors.

format_metadata doesn't undo the effect of create_fresh_metadata if write_superblock fails.

superblock_all_zeroes checks the full block for zeroes, although lvm zeroes only zeroes 4k at the beginning of a new logical volume.

test_bit and test_and_set_bit take signed integer as a parameter (on some architectures the argument is long, on some int), so you must restrict the maximum number of blocks to 2^31-1. Or, alternatively, do not use test_bit and test_and_set_bit and implement these functions on your own - this will allow you to have 64-bit block numbers.

The argument to bitset_size may overflow, it is not checked. If you allow 64-bit argument to bitset_size, you must also check that the conversion to size_t does not overflow.

era_status: /* FIXME: do we need to protect this? */ dm_sm_get_nr_free - on 32-bit architectures you need to lock it because 64-bit accesses are nonatomic. You can see the implementation of i_size_read and i_size_write and do something similar.

era_map doesn't distinguish REQ_FLUSH bios, it should just pass them through without accessing bio->bi_iter.bi_sector.

Possible improvements:

atomic64_inc(&md->current_era); - there is no concurrent access to current_era, so you can use this to avoid the interlocked instruction:
atomic64_set(&md->current_era, atomic64_read(&md->current_era) + 1)

process_deferred_bios: deferred_lock is never taken from interrupt, so you can replace spin_lock_irqsave/spin_unlock_irqrestore with spin_lock/spin_unlock.

writeset_test_and_set: test_and_set_bit is interlocked instruction - here, no concurrent access happens, so use __test_and_set_bit, which is not interlocked. Alternatively - reimplement it if you want 2^31 or more blocks - see above.

Mikulas

#4 lore raw

Hi Mikulas,

Thanks for taking the time to look through this. I think you're right on all counts.

- Joe

Quoted 92 lines · Mikulas Patocka

Hi

I reviewed dm-era, here is a possible list of bugs:

There is no tracking of in-progress writes. Suppose that this happens:
1) a write is received, era_map sees that metadata_current_marked(era->md, block) is true, so it lets the write through
2) the user sends the checkpoint message
3) the write received in step 1) hits the disk and modifies it
4) the user sends metadata_take_snap message, the write is not in the snapshot, although it modified the disk between steps 2) and 4)
You can use the functions dm_internal_suspend and dm_internal_resume to clear in-progress bios - this is lightweight suspend/resume callable only from the kernel.

era_destroy calls metadata_close(era->md); without checking that era->md is not NULL. This can cause crash if era_destroy is called early in the constructor.

era->sectors_per_block should be validated that it is not zero.

There is no write memory barrier between the setting of rpc->result and rpc->complete:

                if (r)
                        list_for_each_entry_safe(rpc, tmp, &calls, list)
                                rpc->result = r;
        }

        list_for_each_entry_safe(rpc, tmp, &calls, list) {
                atomic_set(&rpc->complete, 1);
And there is no read memory barrier between the reading of rpc->complete 
and rpc->result:
        wait_event(rpc->wait, atomic_read(&rpc->complete));

        return rpc->result;

You should use completion (include/linux/completion.h) instead of rpc->complete and rpc->wait, it simplifies the code and takes care of memory barriers.

metadata_space_map_root is not aligned on 64-bit boundary, but it is accessed as 64-bit numbers in sm_ll_open_metadata.

Mixing 32-bit and 64-bit current_era (I'm not sure it this is a bug or not).

Missing read and write memory barriers around accesses to archived_writesets (I'm not sure if this is a bug, but it looks strange).

create_fresh_metadata doesn't free md->tm and md->sm on errors.

format_metadata doesn't undo the effect of create_fresh_metadata if write_superblock fails.

superblock_all_zeroes checks the full block for zeroes, although lvm zeroes only zeroes 4k at the beginning of a new logical volume.

test_bit and test_and_set_bit take signed integer as a parameter (on some architectures the argument is long, on some int), so you must restrict the maximum number of blocks to 2^31-1. Or, alternatively, do not use test_bit and test_and_set_bit and implement these functions on your own - this will allow you to have 64-bit block numbers.

The argument to bitset_size may overflow, it is not checked. If you allow 64-bit argument to bitset_size, you must also check that the conversion to size_t does not overflow.

era_status: /* FIXME: do we need to protect this? */ dm_sm_get_nr_free - on 32-bit architectures you need to lock it because 64-bit accesses are nonatomic. You can see the implementation of i_size_read and i_size_write and do something similar.

era_map doesn't distinguish REQ_FLUSH bios, it should just pass them through without accessing bio->bi_iter.bi_sector.

Possible improvements:

atomic64_inc(&md->current_era); - there is no concurrent access to current_era, so you can use this to avoid the interlocked instruction:
atomic64_set(&md->current_era, atomic64_read(&md->current_era) + 1)

process_deferred_bios: deferred_lock is never taken from interrupt, so you can replace spin_lock_irqsave/spin_unlock_irqrestore with spin_lock/spin_unlock.

writeset_test_and_set: test_and_set_bit is interlocked instruction - here, no concurrent access happens, so use __test_and_set_bit, which is not interlocked. Alternatively - reimplement it if you want 2^31 or more blocks - see above.

Mikulas