From: Jeff Mahoney <[email-masked]>

The reference counting on dm-mod is zero if no mapped devices are
open.  This is incorrect, and can lead to an oops if the module
is unloaded while mapped devices exist.

This patch claims a reference to the module whenever a device is
created, and drops it again when the device is freed.

Devices must be removed before dm-mod is unloaded.

Signed-off-by: Jeff Mahoney <[email-masked]>
Signed-Off-By: Alasdair G Kergon <[email-masked]>

Index: linux-2.6.17-rc4/drivers/md/dm.c
===================================================================
--- linux-2.6.17-rc4.orig/drivers/md/dm.c	2006-06-09 17:17:22.000000000 +0100
+++ linux-2.6.17-rc4/drivers/md/dm.c	2006-06-09 17:17:25.000000000 +0100
@@ -852,6 +852,9 @@ static struct mapped_device *alloc_dev(u
 		return NULL;
 	}
 
+	if (!try_module_get(THIS_MODULE))
+		goto bad0;
+
 	/* get a minor number for the dev */
 	r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
 	if (r < 0)
@@ -918,6 +921,8 @@ static struct mapped_device *alloc_dev(u
 	blk_cleanup_queue(md->queue);
 	free_minor(minor);
  bad1:
+	module_put(THIS_MODULE);
+ bad0:
 	kfree(md);
 	return NULL;
 }
@@ -941,6 +946,7 @@ static void free_dev(struct mapped_devic
 
 	put_disk(md->disk);
 	blk_cleanup_queue(md->queue);
+	module_put(THIS_MODULE);
 	kfree(md);
 }