From: Jun'ichi Nomura <[email-masked]>

This patch fixes a panic on shrinking a DM device if there is
outstanding I/O to the part of the device that is being removed.
(Normally this doesn't happen - a filesystem would be resized first,
for example.)

The bug is that __clone_and_map() assumes dm_table_find_target()
always returns a valid pointer.  It may fail if a bio arrives from the
block layer but its target sector is no longer included in the DM
btree.

This patch tidies up dm_table_find_target() to return NULL instead of
a bogus pointer in the failure case.  Then __clone_and_map() can check
the return value and make the BIO return with -EIO.

target_message() in dm-ioctl.c, the only other user of
dm_table_find_target(), is also modified.

Sample test script to trigger oops:
--------------------------------------------------------------------------
#!/bin/bash

FILE=$(mktemp)
LODEV=$(losetup -f)
MAP=$(basename ${FILE})
SIZE=4M

dd if=/dev/zero of=${FILE} bs=${SIZE} count=1
losetup ${LODEV} ${FILE}

echo "0 $(blockdev --getsz ${LODEV}) linear ${LODEV} 0" |dmsetup create ${MAP}
dmsetup suspend ${MAP}
echo "0 1 linear ${LODEV} 0" |dmsetup load ${MAP}
dd if=/dev/zero of=/dev/mapper/${MAP} bs=${SIZE} count=1 &
echo "Wait til dd push some I/O"
sleep 5
dmsetup resume ${MAP}
--------------------------------------------------------------------------

Signed-off-by: Jun'ichi Nomura <[email-masked]>
Signed-off-by: Alasdair G Kergon <[email-masked]>

---
[AGK FIXME For performance reasons, replace the size check with an
extra element at the end of the btree.]

 drivers/md/dm-ioctl.c |   15 +++++----------
 drivers/md/dm-table.c |    3 +++
 drivers/md/dm.c       |   24 ++++++++++++++++++------
 3 files changed, 26 insertions(+), 16 deletions(-)

Index: linux-2.6.24-rc1/drivers/md/dm-ioctl.c
===================================================================
--- linux-2.6.24-rc1.orig/drivers/md/dm-ioctl.c	2007-10-31 15:23:49.000000000 +0000
+++ linux-2.6.24-rc1/drivers/md/dm-ioctl.c	2007-10-31 16:09:30.000000000 +0000
@@ -1250,21 +1250,16 @@ static int target_message(struct dm_ioct
 	if (!table)
 		goto out_argv;
 
-	if (tmsg->sector >= dm_table_get_size(table)) {
+	ti = dm_table_find_target(table, tmsg->sector);
+	if (!ti) {
 		DMWARN("Target message sector outside device.");
 		r = -EINVAL;
-		goto out_table;
-	}
-
-	ti = dm_table_find_target(table, tmsg->sector);
-	if (ti->type->message)
-		r = ti->type->message(ti, argc, argv);
-	else {
+	} else if (!ti->type->message) {
 		DMWARN("Target type does not support messages");
 		r = -EINVAL;
-	}
+	} else
+		r = ti->type->message(ti, argc, argv);
 
- out_table:
 	dm_table_put(table);
  out_argv:
 	kfree(argv);
Index: linux-2.6.24-rc1/drivers/md/dm-table.c
===================================================================
--- linux-2.6.24-rc1.orig/drivers/md/dm-table.c	2007-10-31 15:23:12.000000000 +0000
+++ linux-2.6.24-rc1/drivers/md/dm-table.c	2007-10-31 15:24:12.000000000 +0000
@@ -867,6 +867,9 @@ struct dm_target *dm_table_find_target(s
 	unsigned int l, n = 0, k = 0;
 	sector_t *node;
 
+	if (sector >= dm_table_get_size(t))
+		return NULL;
+
 	for (l = 0; l < t->depth; l++) {
 		n = get_child(n, k);
 		node = get_node(t, l, n);
Index: linux-2.6.24-rc1/drivers/md/dm.c
===================================================================
--- linux-2.6.24-rc1.orig/drivers/md/dm.c	2007-10-31 15:23:51.000000000 +0000
+++ linux-2.6.24-rc1/drivers/md/dm.c	2007-10-31 16:03:19.000000000 +0000
@@ -672,13 +672,19 @@ static struct bio *clone_bio(struct bio 
 	return clone;
 }
 
-static void __clone_and_map(struct clone_info *ci)
+static int __clone_and_map(struct clone_info *ci)
 {
 	struct bio *clone, *bio = ci->bio;
-	struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
-	sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
+	struct dm_target *ti;
+	sector_t len = 0, max;
 	struct dm_target_io *tio;
 
+	ti = dm_table_find_target(ci->map, ci->sector);
+	if (!ti)
+		return -EIO;
+
+	max = max_io_len(ci->md, ci->sector, ti);
+
 	/*
 	 * Allocate a target io object.
 	 */
@@ -736,6 +742,9 @@ static void __clone_and_map(struct clone
 		do {
 			if (offset) {
 				ti = dm_table_find_target(ci->map, ci->sector);
+				if (!ti)
+					return -EIO;
+
 				max = max_io_len(ci->md, ci->sector, ti);
 
 				tio = alloc_tio(ci->md);
@@ -759,6 +768,8 @@ static void __clone_and_map(struct clone
 
 		ci->idx++;
 	}
+
+	return 0;
 }
 
 /*
@@ -767,6 +778,7 @@ static void __clone_and_map(struct clone
 static int __split_bio(struct mapped_device *md, struct bio *bio)
 {
 	struct clone_info ci;
+	int error = 0;
 
 	ci.map = dm_get_table(md);
 	if (unlikely(!ci.map))
@@ -784,11 +796,11 @@ static int __split_bio(struct mapped_dev
 	ci.idx = bio->bi_idx;
 
 	start_io_acct(ci.io);
-	while (ci.sector_count)
-		__clone_and_map(&ci);
+	while (ci.sector_count && !error)
+		error = __clone_and_map(&ci);
 
 	/* drop the extra reference count */
-	dec_pending(ci.io, 0);
+	dec_pending(ci.io, error);
 	dm_table_put(ci.map);
 
 	return 0;