From: Tejun Heo <tj@kernel.org>
To: Greg Kroah-Hartman <gregkh@suse.de>,
Abdel Benamrouche <draconux@gmail.com>
Cc: linux-kernel@vger.kernel.org, Tejun Heo <tj@kernel.org>,
Abdel Benamrouche <draconux@gmail.com>
Subject: [PATCH] block: update add_partition() error handling
Date: Mon, 25 Aug 2008 18:15:40 +0900 [thread overview]
Message-ID: <1219655740-11672-6-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1219655740-11672-1-git-send-email-tj@kernel.org>
d805dda4 tried to fix error case handling in add_partition() but had a
few problems.
* disk->part[] entry is set early and left dangling if operation
fails.
* Once device initialized, the last put_device() is responsible for
freeing all the resources. The failure path freed part_stats and p
regardless of put_device() causing double free.
* holders subdir holds reference to the disk device, so failure path
should remove it to release resources properly which was missing.
This patch fixes the above problems and while at it move partition
slot busy check into add_partition() for completeness and inlines
holders subdirectory creation. Using separate function for it just
obfuscates the code.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Abdel Benamrouche <draconux@gmail.com>
---
block/ioctl.c | 7 ++-----
fs/partitions/check.c | 40 +++++++++++++++++++++-------------------
2 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/block/ioctl.c b/block/ioctl.c
index c722de0..eb046ae 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -43,12 +43,9 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
|| pstart < 0 || plength < 0)
return -EINVAL;
}
- /* partition number in use? */
+
mutex_lock(&bdev->bd_mutex);
- if (disk->part[part - 1]) {
- mutex_unlock(&bdev->bd_mutex);
- return -EBUSY;
- }
+
/* overlap? */
for (i = 0; i < disk->minors - 1; i++) {
struct hd_struct *s = disk->part[i];
diff --git a/fs/partitions/check.c b/fs/partitions/check.c
index 1b1808e..43dbfab 100644
--- a/fs/partitions/check.c
+++ b/fs/partitions/check.c
@@ -300,15 +300,6 @@ struct device_type part_type = {
.release = part_release,
};
-static inline void partition_sysfs_add_subdir(struct hd_struct *p)
-{
- struct kobject *k;
-
- k = kobject_get(&p->dev.kobj);
- p->holder_dir = kobject_create_and_add("holders", k);
- kobject_put(k);
-}
-
static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
{
struct kobject *k;
@@ -347,13 +338,16 @@ int add_partition(struct gendisk *disk, int part, sector_t start, sector_t len,
struct hd_struct *p;
int err;
+ if (disk->part[part - 1])
+ return -EBUSY;
+
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p)
return -ENOMEM;
if (!init_part_stats(p)) {
err = -ENOMEM;
- goto out0;
+ goto out_free;
}
p->start_sect = start;
p->nr_sects = len;
@@ -372,34 +366,42 @@ int add_partition(struct gendisk *disk, int part, sector_t start, sector_t len,
p->dev.class = &block_class;
p->dev.type = &part_type;
p->dev.parent = &disk->dev;
- disk->part[part-1] = p;
/* delay uevent until 'holders' subdir is created */
p->dev.uevent_suppress = 1;
err = device_add(&p->dev);
if (err)
- goto out1;
- partition_sysfs_add_subdir(p);
+ goto out_put;
+
+ err = -ENOMEM;
+ p->holder_dir = kobject_create_and_add("holders", &p->dev.kobj);
+ if (!p->holder_dir)
+ goto out_del;
+
p->dev.uevent_suppress = 0;
if (flags & ADDPART_FLAG_WHOLEDISK) {
err = device_create_file(&p->dev, &dev_attr_whole_disk);
if (err)
- goto out2;
+ goto out_del;
}
+ /* everything is up and running, commence */
+ disk->part[part - 1] = p;
+
/* suppress uevent if the disk supresses it */
if (!disk->dev.uevent_suppress)
kobject_uevent(&p->dev.kobj, KOBJ_ADD);
return 0;
-out2:
+out_free:
+ kfree(p);
+ return err;
+out_del:
+ kobject_put(p->holder_dir);
device_del(&p->dev);
-out1:
+out_put:
put_device(&p->dev);
- free_part_stats(p);
-out0:
- kfree(p);
return err;
}
--
1.5.4.5
next prev parent reply other threads:[~2008-08-25 9:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-25 9:15 [PATCHSET 1/3 blk-for-2.6.28] block: misc fixes and improvements Tejun Heo
2008-08-25 9:15 ` [PATCH] block: fix partition info printouts Tejun Heo
2008-08-25 9:15 ` [PATCH] block: don't grab block_class_lock unnecessarily Tejun Heo
2008-08-25 9:15 ` [PATCH] block: use class_dev_iterator instead of class_for_each_device() Tejun Heo
2008-08-25 9:15 ` [PATCH] block: allow deleting zero length partition Tejun Heo
2008-08-25 9:15 ` Tejun Heo [this message]
2008-08-25 10:23 ` [PATCHSET 1/3 blk-for-2.6.28] block: misc fixes and improvements Tejun Heo
2008-08-25 14:47 ` Greg KH
2008-08-25 14:58 ` Tejun Heo
2008-08-25 14:59 ` Tejun Heo
2008-08-25 14:58 ` Jens Axboe
2008-08-25 21:42 ` Greg KH
-- strict thread matches above, loose matches on Subject: below --
2008-07-14 6:07 [PATCH] block: update add_partition() error handling Tejun Heo
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=1219655740-11672-6-git-send-email-tj@kernel.org \
--to=tj@kernel.org \
--cc=draconux@gmail.com \
--cc=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
/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