Commit 0d809b38 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Song Liu

md: do not return existing mddevs from mddev_find_or_alloc

Instead of returning an existing mddev, just for it to be discarded
later directly return -EEXIST.  Rename the function to mddev_alloc now
that it doesn't find an existing mddev.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarSong Liu <song@kernel.org>
parent d144fe6f
...@@ -782,26 +782,24 @@ static struct mddev *mddev_find(dev_t unit) ...@@ -782,26 +782,24 @@ static struct mddev *mddev_find(dev_t unit)
return mddev; return mddev;
} }
static struct mddev *mddev_find_or_alloc(dev_t unit) static struct mddev *mddev_alloc(dev_t unit)
{ {
struct mddev *mddev = NULL, *new; struct mddev *new;
int error;
if (unit && MAJOR(unit) != MD_MAJOR) if (unit && MAJOR(unit) != MD_MAJOR)
unit &= ~((1 << MdpMinorShift) - 1); unit &= ~((1 << MdpMinorShift) - 1);
new = kzalloc(sizeof(*new), GFP_KERNEL); new = kzalloc(sizeof(*new), GFP_KERNEL);
if (!new) if (!new)
return NULL; return ERR_PTR(-ENOMEM);
mddev_init(new); mddev_init(new);
spin_lock(&all_mddevs_lock); spin_lock(&all_mddevs_lock);
if (unit) { if (unit) {
mddev = mddev_find_locked(unit); error = -EEXIST;
if (mddev) { if (mddev_find_locked(unit))
mddev_get(mddev);
goto out_free_new; goto out_free_new;
}
new->unit = unit; new->unit = unit;
if (MAJOR(unit) == MD_MAJOR) if (MAJOR(unit) == MD_MAJOR)
new->md_minor = MINOR(unit); new->md_minor = MINOR(unit);
...@@ -809,6 +807,7 @@ static struct mddev *mddev_find_or_alloc(dev_t unit) ...@@ -809,6 +807,7 @@ static struct mddev *mddev_find_or_alloc(dev_t unit)
new->md_minor = MINOR(unit) >> MdpMinorShift; new->md_minor = MINOR(unit) >> MdpMinorShift;
new->hold_active = UNTIL_IOCTL; new->hold_active = UNTIL_IOCTL;
} else { } else {
error = -ENODEV;
new->unit = mddev_alloc_unit(); new->unit = mddev_alloc_unit();
if (!new->unit) if (!new->unit)
goto out_free_new; goto out_free_new;
...@@ -822,7 +821,7 @@ static struct mddev *mddev_find_or_alloc(dev_t unit) ...@@ -822,7 +821,7 @@ static struct mddev *mddev_find_or_alloc(dev_t unit)
out_free_new: out_free_new:
spin_unlock(&all_mddevs_lock); spin_unlock(&all_mddevs_lock);
kfree(new); kfree(new);
return mddev; return ERR_PTR(error);
} }
static struct attribute_group md_redundancy_group; static struct attribute_group md_redundancy_group;
...@@ -5661,29 +5660,29 @@ static int md_alloc(dev_t dev, char *name) ...@@ -5661,29 +5660,29 @@ static int md_alloc(dev_t dev, char *name)
* writing to /sys/module/md_mod/parameters/new_array. * writing to /sys/module/md_mod/parameters/new_array.
*/ */
static DEFINE_MUTEX(disks_mutex); static DEFINE_MUTEX(disks_mutex);
struct mddev *mddev = mddev_find_or_alloc(dev); struct mddev *mddev;
struct gendisk *disk; struct gendisk *disk;
int partitioned; int partitioned;
int shift; int shift;
int unit; int unit;
int error; int error ;
if (!mddev)
return -ENODEV;
partitioned = (MAJOR(mddev->unit) != MD_MAJOR); /*
shift = partitioned ? MdpMinorShift : 0; * Wait for any previous instance of this device to be completely
unit = MINOR(mddev->unit) >> shift; * removed (mddev_delayed_delete).
/* wait for any previous instance of this device to be
* completely removed (mddev_delayed_delete).
*/ */
flush_workqueue(md_misc_wq); flush_workqueue(md_misc_wq);
mutex_lock(&disks_mutex); mutex_lock(&disks_mutex);
error = -EEXIST; mddev = mddev_alloc(dev);
if (mddev->gendisk) if (IS_ERR(mddev)) {
goto abort; mutex_unlock(&disks_mutex);
return PTR_ERR(mddev);
}
partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
shift = partitioned ? MdpMinorShift : 0;
unit = MINOR(mddev->unit) >> shift;
if (name && !dev) { if (name && !dev) {
/* Need to ensure that 'name' is not a duplicate. /* Need to ensure that 'name' is not a duplicate.
...@@ -5695,6 +5694,7 @@ static int md_alloc(dev_t dev, char *name) ...@@ -5695,6 +5694,7 @@ static int md_alloc(dev_t dev, char *name)
if (mddev2->gendisk && if (mddev2->gendisk &&
strcmp(mddev2->gendisk->disk_name, name) == 0) { strcmp(mddev2->gendisk->disk_name, name) == 0) {
spin_unlock(&all_mddevs_lock); spin_unlock(&all_mddevs_lock);
error = -EEXIST;
goto abort; goto abort;
} }
spin_unlock(&all_mddevs_lock); spin_unlock(&all_mddevs_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment