Commit d144fe6f authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Song Liu

md: refactor mddev_find_or_alloc

Allocate the new mddev first speculatively, which greatly simplifies
the code flow.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarSong Liu <song@kernel.org>
parent 85c8c3c1
...@@ -784,57 +784,45 @@ static struct mddev *mddev_find(dev_t unit) ...@@ -784,57 +784,45 @@ static struct mddev *mddev_find(dev_t unit)
static struct mddev *mddev_find_or_alloc(dev_t unit) static struct mddev *mddev_find_or_alloc(dev_t unit)
{ {
struct mddev *mddev, *new = NULL; struct mddev *mddev = NULL, *new;
if (unit && MAJOR(unit) != MD_MAJOR) if (unit && MAJOR(unit) != MD_MAJOR)
unit &= ~((1<<MdpMinorShift)-1); unit &= ~((1 << MdpMinorShift) - 1);
retry: new = kzalloc(sizeof(*new), GFP_KERNEL);
spin_lock(&all_mddevs_lock); if (!new)
return NULL;
mddev_init(new);
spin_lock(&all_mddevs_lock);
if (unit) { if (unit) {
mddev = mddev_find_locked(unit); mddev = mddev_find_locked(unit);
if (mddev) { if (mddev) {
mddev_get(mddev); mddev_get(mddev);
spin_unlock(&all_mddevs_lock); goto out_free_new;
kfree(new);
return mddev;
} }
if (new) { new->unit = unit;
list_add(&new->all_mddevs, &all_mddevs); if (MAJOR(unit) == MD_MAJOR)
spin_unlock(&all_mddevs_lock); new->md_minor = MINOR(unit);
else
new->md_minor = MINOR(unit) >> MdpMinorShift;
new->hold_active = UNTIL_IOCTL; new->hold_active = UNTIL_IOCTL;
return new; } else {
}
} else if (new) {
new->unit = mddev_alloc_unit(); new->unit = mddev_alloc_unit();
if (!new->unit) { if (!new->unit)
spin_unlock(&all_mddevs_lock); goto out_free_new;
kfree(new);
return NULL;
}
new->md_minor = MINOR(new->unit); new->md_minor = MINOR(new->unit);
new->hold_active = UNTIL_STOP; new->hold_active = UNTIL_STOP;
}
list_add(&new->all_mddevs, &all_mddevs); list_add(&new->all_mddevs, &all_mddevs);
spin_unlock(&all_mddevs_lock); spin_unlock(&all_mddevs_lock);
return new; return new;
} out_free_new:
spin_unlock(&all_mddevs_lock); spin_unlock(&all_mddevs_lock);
kfree(new);
new = kzalloc(sizeof(*new), GFP_KERNEL); return mddev;
if (!new)
return NULL;
new->unit = unit;
if (MAJOR(unit) == MD_MAJOR)
new->md_minor = MINOR(unit);
else
new->md_minor = MINOR(unit) >> MdpMinorShift;
mddev_init(new);
goto retry;
} }
static struct attribute_group md_redundancy_group; static struct attribute_group md_redundancy_group;
......
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