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

From: Luis Chamberlain
Date: Thu Jul 15 2021 - 00:55:52 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 partition 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 | 44 ++++++++++++++++++++++++++------------------
1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/block/genhd.c b/block/genhd.c
index e5535879d629..b84ba22eed39 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -399,6 +399,25 @@ static void disk_announce(struct gendisk *disk)
disk_uevent(disk, KOBJ_ADD);
}

+static void unregister_disk_partitions(struct gendisk *disk)
+{
+ mutex_lock(&disk->open_mutex);
+ disk->flags &= ~GENHD_FL_UP;
+ blk_drop_partitions(disk);
+ mutex_unlock(&disk->open_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);
+}
+
static void disk_invalidate(struct gendisk *disk)
{
if (!(disk->flags & GENHD_FL_HIDDEN)) {
@@ -424,6 +443,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)
{
@@ -614,24 +639,7 @@ void del_gendisk(struct gendisk *disk)

blk_integrity_del(disk);
disk_del_events(disk);
-
- mutex_lock(&disk->open_mutex);
- disk->flags &= ~GENHD_FL_UP;
- blk_drop_partitions(disk);
- mutex_unlock(&disk->open_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_invalidate(disk);
+ unregister_disk(disk);
}
EXPORT_SYMBOL(del_gendisk);

--
2.27.0