From: Joe Thornber <joe@fib011235813.fsnet.co.uk>
To: Linus Torvalds <torvalds@transmeta.com>
Cc: Linux Mailing List <linux-kernel@vger.kernel.org>
Subject: [PATCH] dm update 3/3
Date: Tue, 29 Oct 2002 17:20:16 +0000 [thread overview]
Message-ID: <20021029172016.GC1779@fib011235813.fsnet.co.uk> (raw)
Keep track of allocated minors in a bitset rather than abusing
get_gendisk.
--- diff/drivers/md/dm.c 2002-10-29 16:22:36.000000000 +0000
+++ source/drivers/md/dm.c 2002-10-29 16:34:45.000000000 +0000
@@ -15,7 +15,7 @@
#include <linux/slab.h>
static const char *_name = DM_NAME;
-#define MAX_DEVICES 256
+#define MAX_DEVICES (1 << KDEV_MINOR_BITS)
#define SECTOR_SHIFT 9
static int major = 0;
@@ -483,13 +483,25 @@
return 0;
}
+/*-----------------------------------------------------------------
+ * A bitset is used to keep track of allocated minor numbers.
+ *---------------------------------------------------------------*/
+static spinlock_t _minor_lock = SPIN_LOCK_UNLOCKED;
+static unsigned long _minor_bits[MAX_DEVICES / BITS_PER_LONG];
+
+static void free_minor(int minor)
+{
+ spin_lock(&_minor_lock);
+ clear_bit(minor, _minor_bits);
+ spin_unlock(&_minor_lock);
+}
+
/*
* See if the device with a specific minor # is free.
*/
-static int specific_dev(int minor, struct mapped_device *md)
+static int specific_minor(int minor)
{
- struct gendisk *disk;
- int part;
+ int r = -EBUSY;
if (minor >= MAX_DEVICES) {
DMWARN("request for a mapped_device beyond MAX_DEVICES (%d)",
@@ -497,26 +509,32 @@
return -EINVAL;
}
- disk = get_gendisk(MKDEV(_major, minor), &part);
- if (disk) {
- put_disk(disk);
- return -EBUSY;
+ spin_lock(&_minor_lock);
+ if (!test_bit(minor, _minor_bits)) {
+ r = minor;
+ set_bit(minor, _minor_bits);
}
+ spin_unlock(&_minor_lock);
- return minor;
+ return r;
}
-static int any_old_dev(struct mapped_device *md)
+static int any_old_minor(void)
{
- int i;
+ int i, r = -EBUSY;
- for (i = 0; i < MAX_DEVICES; i++)
- if (specific_dev(i, md) >= 0) {
+ spin_lock(&_minor_lock);
+ for (i = 0; i < MAX_DEVICES; i++) {
+ if (!test_bit(i, _minor_bits)) {
+ r = i;
+ set_bit(i, _minor_bits);
DMWARN("allocating minor = %d", i);
- return i;
+ break;
}
+ }
+ spin_unlock(&_minor_lock);
- return -EBUSY;
+ return r;
}
/*
@@ -532,7 +550,7 @@
}
/* get a minor number for the dev */
- minor = (minor < 0) ? any_old_dev(md) : specific_dev(minor, md);
+ minor = (minor < 0) ? any_old_minor() : specific_minor(minor);
if (minor < 0) {
kfree(md);
return NULL;
@@ -547,6 +565,7 @@
md->disk = alloc_disk(1);
if (!md->disk) {
+ free_minor(md->disk->first_minor);
kfree(md);
return NULL;
}
@@ -566,6 +585,7 @@
static void free_dev(struct mapped_device *md)
{
+ free_minor(md->disk->first_minor);
del_gendisk(md->disk);
put_disk(md->disk);
kfree(md);
next reply other threads:[~2002-10-29 17:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-29 17:20 Joe Thornber [this message]
2002-10-29 17:42 ` Alexander Viro
2002-10-30 10:15 ` Joe Thornber
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20021029172016.GC1779@fib011235813.fsnet.co.uk \
--to=joe@fib011235813.fsnet.co.uk \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@transmeta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®