This code will be reused in later patch. Hopefully putting it aside rather than cut&pasting another copy will make the function more readable. Signed-off-by: Michal Suchanek <msuchanek@xxxxxxx> --- fs/block_dev.c | 52 +++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/fs/block_dev.c b/fs/block_dev.c index a3083fbada8b..10c9e0ae2bdc 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1512,6 +1512,28 @@ EXPORT_SYMBOL(bd_set_size); static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part); +static void blkdev_init_size(struct block_device *bdev, struct gendisk *disk) +{ + bd_set_size(bdev, (loff_t)get_capacity(disk) << 9); + set_init_blocksize(bdev); +} + +static void blkdev_do_partitions(struct block_device *bdev, + struct gendisk *disk, int ret) +{ + /* + * If the device is invalidated, rescan partition if open succeeded or + * failed with -ENOMEDIUM. The latter is necessary to prevent ghost + * partitions on a removed medium. + */ + if (bdev->bd_invalidated) { + if (!ret) + rescan_partitions(disk, bdev); + else if (ret == -ENOMEDIUM) + invalidate_partitions(disk, bdev); + } +} + /* * bd_mutex locking: * @@ -1584,23 +1606,9 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) } } - if (!ret) { - bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); - set_init_blocksize(bdev); - } - - /* - * If the device is invalidated, rescan partition - * if open succeeded or failed with -ENOMEDIUM. - * The latter is necessary to prevent ghost - * partitions on a removed medium. - */ - if (bdev->bd_invalidated) { - if (!ret) - rescan_partitions(disk, bdev); - else if (ret == -ENOMEDIUM) - invalidate_partitions(disk, bdev); - } + if (!ret) + blkdev_init_size(bdev, disk); + blkdev_do_partitions(bdev, disk, ret); if (ret) goto out_clear; @@ -1632,13 +1640,9 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) ret = 0; if (bdev->bd_disk->fops->open) ret = bdev->bd_disk->fops->open(bdev, mode); - /* the same as first opener case, read comment there */ - if (bdev->bd_invalidated) { - if (!ret) - rescan_partitions(bdev->bd_disk, bdev); - else if (ret == -ENOMEDIUM) - invalidate_partitions(bdev->bd_disk, bdev); - } + + blkdev_do_partitions(bdev, disk, ret); + if (ret) goto out_unlock_bdev; } -- 2.23.0