On Tue, Jul 07, 2020 at 07:53:12PM +0200, Christoph Hellwig wrote: > On Tue, Jul 07, 2020 at 04:45:52PM +0800, Ming Lei wrote: > > After clearing fd or changing fd, we have to delete old partitions, > > otherwise they may become ghost partitions. > > > > Fix this issue by clearing GENHD_FL_NO_PART_SCAN during calling > > bdev_disk_changed() which won't drop old partitions if GENHD_FL_NO_PART_SCAN > > isn't set. > > I don't think messing with GENHD_FL_NO_PART_SCAN is a good idea, as > that will also cause an actual partition scan. But except for historic > reasons I can't think of a good idea to even check for > GENHD_FL_NO_PART_SCAN in blk_drop_partitions. I think it is safe to not check it in blk_drop_partitions(), how about the following patch? >From a20209464c367c338beee5555f2cb5c8e8ad9f78 Mon Sep 17 00:00:00 2001 From: Ming Lei <ming.lei@xxxxxxxxxx> Date: Wed, 8 Jul 2020 16:07:19 +0800 Subject: [PATCH] block: always remove partitions in blk_drop_partitions() So far blk_drop_partitions() only removes partitions when disk_part_scan_enabled() return true. This way can make ghost partition on loop device after changing/clearing FD in case that PARTSCAN is disabled. Fix this issue by always removing partitions in blk_drop_partitions(), and this way is correct because: 1) only loop, mmc and GENHD_FL_HIDDEN disks(nvme multipath) may set GENHD_FL_NO_PART_SCAN 2) GENHD_FL_HIDDEN disks doesn't expose disk to block device fs, and bdev_disk_changed()/blk_drop_partitions() won't be called for this kind of disk 3) for mmc, if GENHD_FL_NO_PART_SCAN is set, no any partitions can be added for this kind of disk, so blk_drop_partitions() basically does nothing no matter if GENHD_FL_NO_PART_SCAN is set or not because disk_max_parts(disk) <= 1 4) for loop, bdev_disk_changed() is called in two cases: one is set fd and set status, when there shouldn't be any partitions; another is clearing/changing fd, we need to remove old partitions and re-read new partitions if there are and PART_SCAN is enabled. Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- block/partitions/core.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/block/partitions/core.c b/block/partitions/core.c index 78951e33b2d7..e62a98a8eeb7 100644 --- a/block/partitions/core.c +++ b/block/partitions/core.c @@ -619,8 +619,6 @@ int blk_drop_partitions(struct block_device *bdev) struct disk_part_iter piter; struct hd_struct *part; - if (!disk_part_scan_enabled(bdev->bd_disk)) - return 0; if (bdev->bd_part_count) return -EBUSY; -- 2.25.2 thanks, Ming