[PATCH 1/2] block: move partitions check code into single helper

From: Ming Lei
Date: Thu Feb 04 2021 - 21:20:10 EST


No functional change, make code more readable, and prepare for
supporting safe re-read partitions.

Cc: Ewan D. Milne <emilne@xxxxxxxxxx>
Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx>
---
block/partitions/core.c | 51 ++++++++++++++++++++++++++++++-----------
1 file changed, 37 insertions(+), 14 deletions(-)

diff --git a/block/partitions/core.c b/block/partitions/core.c
index b1cdf88f96e2..154013ea8623 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -599,17 +599,15 @@ static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
return true;
}

-int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
+static int blk_check_partitions(struct gendisk *disk,
+ struct block_device *bdev, struct parsed_partitions **s)
{
- struct parsed_partitions *state;
- int ret = -EAGAIN, p;
-
- if (!disk_part_scan_enabled(disk))
- return 0;
+ int ret = -EAGAIN;
+ struct parsed_partitions *state = check_partition(disk, bdev);

- state = check_partition(disk, bdev);
if (!state)
- return 0;
+ goto out;
+
if (IS_ERR(state)) {
/*
* I/O error reading the partition table. If we tried to read
@@ -647,15 +645,40 @@ int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
goto out_free_state;
}

+out:
+ *s = state;
+ return 0;
+
+out_free_state:
+ free_partitions(state);
+ *s = NULL;
+ return ret;
+}
+
+int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
+{
+ struct parsed_partitions *state;
+ int ret, p;
+
+ if (!disk_part_scan_enabled(disk))
+ return 0;
+
+ ret = blk_check_partitions(disk, bdev, &state);
+ if (ret != 0)
+ return ret;
+
+ if (!state)
+ return 0;
+
/* tell userspace that the media / partition table may have changed */
kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);

- for (p = 1; p < state->limit; p++)
- if (!blk_add_partition(disk, bdev, state, p))
- goto out_free_state;
-
- ret = 0;
-out_free_state:
+ for (p = 1; p < state->limit; p++) {
+ if (!blk_add_partition(disk, bdev, state, p)) {
+ ret = -EAGAIN;
+ break;
+ }
+ }
free_partitions(state);
return ret;
}
--
2.29.2