Skip to contentSkip to conversation

DM Block integrity support

PATCH In review Martin K. Petersen 2 participants 3 messages Jan 4, 2009 – Jan 8, 2009 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 DM Block integrity support

This patch provides support for data integrity passthrough in the device mapper.

- If one or more component devices support integrity an integrity profile is preallocated for the DM device.

- If all component devices have compatible profiles the DM device is flagged as capable.

- Handle integrity metadata when splitting and cloning bios.

---

drivers/md/dm-ioctl.c +21 -0
@@ -1048,6 +1048,19 @@ static int populate_table(struct dm_tabl
 	return dm_table_complete(table);
 }
 
+static int table_prealloc_integrity(struct dm_table *t, struct mapped_device *md)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *ddi;
+
+	list_for_each_entry(ddi, devices, list) {
+		if (bdev_get_integrity(ddi->dm_dev.bdev))
+			return blk_integrity_register(dm_disk(md), NULL);
+	}
+
+	return 0;
+}
+
 static int table_load(struct dm_ioctl *param, size_t param_size)
 {
 	int r;
@@ -1065,6 +1078,14 @@ static int table_load(struct dm_ioctl *p
 
 	r = populate_table(t, param, param_size);
 	if (r) {
+		dm_table_put(t);
+		goto out;
+	}
+
+	r = table_prealloc_integrity(t, md);
+	if (r) {
+		DMERR("could not register integrity profile for %s.",
+		      dm_disk(md)->disk_name);
 		dm_table_put(t);
 		goto out;
 	}
drivers/md/dm-table.c +36 -0
@@ -865,7 +865,43 @@ void dm_table_set_restrictions(struct dm
 		queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
 	else
 		queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
+}
 
+void dm_table_set_integrity(struct dm_table *t, struct mapped_device *md)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *prev, *cur;
+
+	/*
+	 * Run through all devices to ensure they have matching
+	 * integrity profile
+	 */
+	cur = prev = NULL;
+
+	list_for_each_entry(cur, devices, list) {
+
+		if (prev &&
+		    blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
+					  cur->dm_dev.bdev->bd_disk) < 0) {
+			printk(KERN_ERR "%s: %s %s integrity mismatch!\n",
+			       __func__, prev->dm_dev.bdev->bd_disk->disk_name,
+			       cur->dm_dev.bdev->bd_disk->disk_name);
+			return;
+		}
+		prev = cur;
+	}
+
+	if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
+		return;
+
+	/* Register dm device as being integrity capable */
+	if (blk_integrity_register(dm_disk(md),
+				   bdev_get_integrity(prev->dm_dev.bdev)))
+		printk(KERN_ERR "%s: %s Could not register integrity!\n",
+		       __func__, dm_disk(md)->disk_name);
+	else
+		printk(KERN_INFO "Enabling data integrity on %s\n",
+		       dm_disk(md)->disk_name);
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +16 -0
@@ -661,6 +661,12 @@ static struct bio *split_bvec(struct bio
 	clone->bi_io_vec->bv_len = clone->bi_size;
 	clone->bi_flags |= 1 << BIO_CLONED;
 
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+		bio_integrity_trim(clone,
+				   bio_sector_offset(bio, idx, offset), len);
+	}
+
 	return clone;
 }
 
@@ -681,6 +687,14 @@ static struct bio *clone_bio(struct bio 
 	clone->bi_vcnt = idx + bv_count;
 	clone->bi_size = to_bytes(len);
 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
+
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+
+		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
+			bio_integrity_trim(clone,
+					   bio_sector_offset(bio, idx, 0), len);
+	}
 
 	return clone;
 }
@@ -1160,6 +1174,7 @@ static void free_dev(struct mapped_devic
 	mempool_destroy(md->tio_pool);
 	mempool_destroy(md->io_pool);
 	bioset_free(md->bs);
+	blk_integrity_unregister(md->disk);
 	del_gendisk(md->disk);
 	free_minor(minor);
 
@@ -1225,6 +1240,7 @@ static int __bind(struct mapped_device *
 	write_lock(&md->map_lock);
 	md->map = t;
 	dm_table_set_restrictions(t, q);
+	dm_table_set_integrity(t, md);
 	write_unlock(&md->map_lock);
 
 	return 0;
drivers/md/dm.h +1 -0
@@ -41,6 +41,7 @@ struct dm_target *dm_table_get_target(st
 struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index);
 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector);
 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q);
+void dm_table_set_integrity(struct dm_table *t, struct mapped_device *md);
 struct list_head *dm_table_get_devices(struct dm_table *t);
 void dm_table_presuspend_targets(struct dm_table *t);
 void dm_table_postsuspend_targets(struct dm_table *t);
#1 lore raw

New additions merged back into the original cleaned-up patch and unresolved header comments updated.

Alasdair.

From: Martin K. Petersen <[email protected]>

This patch provides support for data integrity passthrough in the device mapper.

- If one or more component devices support integrity an integrity profile is preallocated for the DM device.

[FIXME: Looks as if that will hit BUG_ON(template == NULL) - maybe there's an undocumented dependency on some other patch? Does this handle table swaps correctly? i.e. NULL must leave any existing (live) setting unchanged.]

- If all component devices have compatible profiles the DM device is flagged as capable.

- Handle integrity metadata when splitting and cloning bios.

[FIXME: Does this cope with integrity profile needing removing after a table swap?]

[FIXME: blk_integrity_register() allocating & manipulating kobjects needs moving outside __bind(). - Does the preallocation address this now, with the exception of some unimportant races we'll ignore but should document? E.g. If integrity profile appears on an underlying device between the reload and the table swap. Would it be better for the table swap to fail in that particular case?]

---
 drivers/md/dm-ioctl.c |   21 +++++++++++++++++++++
 drivers/md/dm-table.c |   39 +++++++++++++++++++++++++++++++++++++++
 drivers/md/dm.c       |   15 +++++++++++++++
 3 files changed, 75 insertions(+)
drivers/md/dm-ioctl.c +21 -0
Index: linux-2.6.28/drivers/md/dm-ioctl.c
===================================================================
@@ -1047,6 +1047,19 @@ static int populate_table(struct dm_tabl
 	return dm_table_complete(table);
 }
 
+static int table_prealloc_integrity(struct dm_table *t,
+				    struct mapped_device *md)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *dd;
+
+	list_for_each_entry(dd, devices, list)
+		if (bdev_get_integrity(dd->dm_dev.bdev))
+			return blk_integrity_register(dm_disk(md), NULL);
+
+	return 0;
+}
+
 static int table_load(struct dm_ioctl *param, size_t param_size)
 {
 	int r;
@@ -1068,6 +1081,14 @@ static int table_load(struct dm_ioctl *p
 		goto out;
 	}
 
+	r = table_prealloc_integrity(t, md);
+	if (r) {
+		DMERR("%s: could not register integrity profile.",
+		      dm_device_name(md));
+		dm_table_destroy(t);
+		goto out;
+	}
+
 	down_write(&_hash_lock);
 	hc = dm_get_mdptr(md);
 	if (!hc || hc->md != md) {
drivers/md/dm-table.c +39 -0
Index: linux-2.6.28/drivers/md/dm-table.c
===================================================================
@@ -877,6 +877,44 @@ struct dm_target *dm_table_find_target(s
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+/*
+ * Set the integrity profile for this device if all devices used have
+ * matching profiles.
+ */
+static void dm_table_set_integrity(struct dm_table *t)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *prev = NULL, *dd = NULL;
+	struct blk_integrity *bi;
+
+	list_for_each_entry(dd, devices, list) {
+		if (prev &&
+		    blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
+					  dd->dm_dev.bdev->bd_disk) < 0) {
+			DMWARN("%s: integrity not set: %s and %s mismatch",
+			       dm_device_name(t->md),
+			       prev->dm_dev.bdev->bd_disk->disk_name,
+			       dd->dm_dev.bdev->bd_disk->disk_name);
+			return;
+		}
+		prev = dd;
+	}
+
+	if (!prev)
+		return;
+
+	bi = bdev_get_integrity(prev->dm_dev.bdev);
+	if (!bi)
+		return;
+
+	/* Register dm device as being integrity capable */
+	if (blk_integrity_register(dm_disk(t->md), bi))
+		DMERR("%s: %s: blk_integrity_register failed.",
+		       __func__, dm_device_name(t->md));
+	else
+		DMDEBUG("%s: Enabling data integrity.", dm_device_name(t->md));
+}
+
 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
 {
 	/*
@@ -897,6 +935,7 @@ void dm_table_set_restrictions(struct dm
 	else
 		queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
 
+	dm_table_set_integrity(t);
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +15 -0
Index: linux-2.6.28/drivers/md/dm.c
===================================================================
@@ -703,6 +703,12 @@ static struct bio *split_bvec(struct bio
 	clone->bi_io_vec->bv_len = clone->bi_size;
 	clone->bi_flags |= 1 << BIO_CLONED;
 
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+		bio_integrity_trim(clone,
+				   bio_sector_offset(bio, idx, offset), len);
+	}
+
 	return clone;
 }
 
@@ -724,6 +730,14 @@ static struct bio *clone_bio(struct bio 
 	clone->bi_size = to_bytes(len);
 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
 
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+
+		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
+			bio_integrity_trim(clone,
+					   bio_sector_offset(bio, idx, 0), len);
+	}
+
 	return clone;
 }
 
@@ -1191,6 +1205,7 @@ static void free_dev(struct mapped_devic
 	mempool_destroy(md->tio_pool);
 	mempool_destroy(md->io_pool);
 	bioset_free(md->bs);
+	blk_integrity_unregister(md->disk);
 	del_gendisk(md->disk);
 	free_minor(minor);
 
#2 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Alasdair" == Alasdair G Kergon <[email protected]> writes:

Alasdair> [FIXME: Looks as if that will hit BUG_ON(template == NULL) - Alasdair> maybe there's an undocumented dependency on some other patch?

Sorry, yes. That note got lost in the noise when I sliced and diced the patch set. There's a patch in Jens' tree that uses NULL to indicate the default (empty) profile. Subsequent calls to register will simply update the already allocated profile.

Alasdair> [FIXME: Does this cope with integrity profile needing removing Alasdair> after a table swap?]

Massaged version below. I incorporated your changes.

Alasdair> [FIXME: blk_integrity_register() allocating & manipulating Alasdair> kobjects needs moving outside __bind(). - Does the Alasdair> preallocation address this now,

Yep.

Alasdair> with the exception of some unimportant races we'll ignore but Alasdair> should document? E.g. If integrity profile appears on an Alasdair> underlying device between the reload and the table swap. Alasdair> Would it be better for the table swap to fail in that Alasdair> particular case?]

An integrity profile doesn't just appear out of the blue. It takes several hours to reformat a SCSI disk to turn it on (and the drive is offline in the meantime). So I think this is mostly in the academic interest department. I guess a runtime state transition could occur if you have a DM device on top of a mirror with one drive with integrity enabled and one without. And you then swap the legacy disk out with an integrity device in causing the RAID1 to suddenly be flagged as capable. But at least from a physical storage device perspective the integrity capability is a persistent state.

There's another corner case, though. If you go from all legacy devices to all integrity ditto no profile will be registered. That's an unfortunate side-effect of doing preallocation. But also an unlikely scenario, I'd say.

dm: Add support for data integrity to DM

This patch provides support for data integrity passthrough in the device mapper.

- If one or more component devices support integrity an integrity profile is preallocated for the DM device.

- If all component devices have compatible profiles the DM device is flagged as capable.

- Handle integrity metadata when splitting and cloning bios.

---

drivers/md/dm-ioctl.c +21 -0
@@ -1046,6 +1046,19 @@ static int populate_table(struct dm_tabl
 	return dm_table_complete(table);
 }
 
+static int table_prealloc_integrity(struct dm_table *t,
+				    struct mapped_device *md)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *dd;
+
+	list_for_each_entry(dd, devices, list)
+		if (bdev_get_integrity(dd->dm_dev.bdev))
+			return blk_integrity_register(dm_disk(md), NULL);
+
+	return 0;
+}
+
 static int table_load(struct dm_ioctl *param, size_t param_size)
 {
 	int r;
@@ -1064,6 +1077,14 @@ static int table_load(struct dm_ioctl *p
 	r = populate_table(t, param, param_size);
 	if (r) {
 		dm_table_put(t);
+		goto out;
+	}
+
+	r = table_prealloc_integrity(t, md);
+	if (r) {
+		DMERR("%s: could not register integrity profile.",
+		      dm_device_name(md));
+		dm_table_destroy(t);
 		goto out;
 	}
 
drivers/md/dm-table.c +40 -0
@@ -877,6 +877,45 @@ struct dm_target *dm_table_find_target(s
 	return &t->targets[(KEYS_PER_NODE * n) + k];
 }
 
+/*
+ * Set the integrity profile for this device if all devices used have
+ * matching profiles.
+ */
+static void dm_table_set_integrity(struct dm_table *t)
+{
+	struct list_head *devices = dm_table_get_devices(t);
+	struct dm_dev_internal *prev = NULL, *dd = NULL;
+
+	if (!blk_get_integrity(dm_disk(t->md)))
+		return;
+
+	list_for_each_entry(dd, devices, list) {
+		if (prev &&
+		    blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
+					  dd->dm_dev.bdev->bd_disk) < 0) {
+			DMWARN("%s: integrity not set: %s and %s mismatch",
+			       dm_device_name(t->md),
+			       prev->dm_dev.bdev->bd_disk->disk_name,
+			       dd->dm_dev.bdev->bd_disk->disk_name);
+			goto no_integrity;
+		}
+		prev = dd;
+	}
+
+	if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
+		goto no_integrity;
+
+	blk_integrity_register(dm_disk(t->md),
+			       bdev_get_integrity(prev->dm_dev.bdev));
+
+	return;
+
+no_integrity:
+	blk_integrity_register(dm_disk(t->md), NULL);
+
+	return;
+}
+
 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
 {
 	/*
@@ -897,6 +936,7 @@ void dm_table_set_restrictions(struct dm
 	else
 		queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
 
+	dm_table_set_integrity(t);
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
drivers/md/dm.c +15 -0
@@ -703,6 +703,12 @@ static struct bio *split_bvec(struct bio
 	clone->bi_io_vec->bv_len = clone->bi_size;
 	clone->bi_flags |= 1 << BIO_CLONED;
 
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+		bio_integrity_trim(clone,
+				   bio_sector_offset(bio, idx, offset), len);
+	}
+
 	return clone;
 }
 
@@ -723,6 +729,14 @@ static struct bio *clone_bio(struct bio 
 	clone->bi_vcnt = idx + bv_count;
 	clone->bi_size = to_bytes(len);
 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
+
+	if (bio_integrity(bio)) {
+		bio_integrity_clone(clone, bio, bs);
+
+		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
+			bio_integrity_trim(clone,
+					   bio_sector_offset(bio, idx, 0), len);
+	}
 
 	return clone;
 }
@@ -1191,6 +1205,7 @@ static void free_dev(struct mapped_devic
 	mempool_destroy(md->tio_pool);
 	mempool_destroy(md->io_pool);
 	bioset_free(md->bs);
+	blk_integrity_unregister(md->disk);
 	del_gendisk(md->disk);
 	free_minor(minor);