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

From: Luis Chamberlain
Date: Wed May 12 2021 - 03:02:24 EST


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 <mcgrof@xxxxxxxxxx>
---
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