2021-05-12 07:03:46

by Luis Chamberlain

[permalink] [raw]
Subject: [PATCH v1 4/8] block: move disk unregistration work from del_gendisk() to a helper

There is quite a bit of code on del_gendisk() which relates to
unregistering the disk, using register_disk() as an counter.
Move all this code into a helper instead of re-writing our own,
which we'll need later to handle errors on add_disk().

Since disk unregistrationa also deals with parition unregistration,
provide a halper for that as well, as we'll later need this when
adding error handling for add_disk().

Signed-off-by: Luis Chamberlain <[email protected]>
---
block/genhd.c | 56 +++++++++++++++++++++++++++++----------------------
1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/block/genhd.c b/block/genhd.c
index 40a34981f9e2..baa68192ebb3 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -438,6 +438,31 @@ static void disk_announce(struct gendisk *disk)
disk_uevent(disk, KOBJ_ADD);
}

+static void unregister_disk_partitions(struct gendisk *disk)
+{
+ /*
+ * Block lookups of the disk until all bdevs are unhashed and the
+ * disk is marked as dead (GENHD_FL_UP cleared).
+ */
+ down_write(&bdev_lookup_sem);
+
+ mutex_lock(&disk->part0->bd_mutex);
+ blk_drop_partitions(disk);
+ mutex_unlock(&disk->part0->bd_mutex);
+ fsync_bdev(disk->part0);
+ __invalidate_device(disk->part0, true);
+
+ /*
+ * Unhash the bdev inode for this device so that it can't be looked
+ * up any more even if openers still hold references to it.
+ */
+ remove_inode_hash(disk->part0->bd_inode);
+
+ set_capacity(disk, 0);
+ disk->flags &= ~GENHD_FL_UP;
+ up_write(&bdev_lookup_sem);
+}
+
static void disk_invalidate(struct gendisk *disk)
{
if (!(disk->flags & GENHD_FL_HIDDEN)) {
@@ -463,6 +488,12 @@ static void disk_invalidate(struct gendisk *disk)
device_del(disk_to_dev(disk));
}

+static void unregister_disk(struct gendisk *disk)
+{
+ unregister_disk_partitions(disk);
+ disk_invalidate(disk);
+}
+
static void register_disk(struct device *parent, struct gendisk *disk,
const struct attribute_group **groups)
{
@@ -639,30 +670,7 @@ void del_gendisk(struct gendisk *disk)

blk_integrity_del(disk);
disk_del_events(disk);
- /*
- * Block lookups of the disk until all bdevs are unhashed and the
- * disk is marked as dead (GENHD_FL_UP cleared).
- */
- down_write(&bdev_lookup_sem);
-
- mutex_lock(&disk->part0->bd_mutex);
- blk_drop_partitions(disk);
- mutex_unlock(&disk->part0->bd_mutex);
-
- fsync_bdev(disk->part0);
- __invalidate_device(disk->part0, true);
-
- /*
- * Unhash the bdev inode for this device so that it can't be looked
- * up any more even if openers still hold references to it.
- */
- remove_inode_hash(disk->part0->bd_inode);
-
- set_capacity(disk, 0);
- disk->flags &= ~GENHD_FL_UP;
- up_write(&bdev_lookup_sem);
-
- disk_invalidate(disk);
+ unregister_disk(disk);
}
EXPORT_SYMBOL(del_gendisk);

--
2.30.2


2021-05-12 15:22:34

by Hannes Reinecke

[permalink] [raw]
Subject: Re: [PATCH v1 4/8] block: move disk unregistration work from del_gendisk() to a helper

On 5/12/21 8:46 AM, Luis Chamberlain wrote:
> There is quite a bit of code on del_gendisk() which relates to
> unregistering the disk, using register_disk() as an counter.
> Move all this code into a helper instead of re-writing our own,
> which we'll need later to handle errors on add_disk().
>
> Since disk unregistrationa also deals with parition unregistration,
> provide a halper for that as well, as we'll later need this when
> adding error handling for add_disk().
>
> Signed-off-by: Luis Chamberlain <[email protected]>
> ---
> block/genhd.c | 56 +++++++++++++++++++++++++++++----------------------
> 1 file changed, 32 insertions(+), 24 deletions(-)
>
Reviewed-by: Hannes Reinecke <[email protected]>

Cheers,

Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
[email protected] +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer