Skip to content

Commit 51b4cb4

Browse files
committed
block: WARN_ON_ONCE() when we remove active partitions
The logic for disk->open_partitions is: blkdev_get_by_*() -> bdev_is_partition() -> blkdev_get_part() -> blkdev_get_whole() // bdev_whole->bd_openers++ -> if (part->bd_openers == 0) disk->open_partitions++ part->bd_openers In other words, when we first claim/open a partition we increment disk->open_partitions and only when all part->bd_openers are closed will disk->open_partitions be zero. That should mean that disk->open_partitions is always > 0 as long as there's anyone that has an open partition. So the check for disk->open_partitions should mean that we can never remove an active partition that has a holder and holder ops set. Assert that in the code. The main disk isn't removed so that check doesn't work for disk->part0 which is what we want. After all we only care about partition not about the main disk. Link: https://lore.kernel.org/r/20231017184823.1383356-3-hch@lst.de Reviewed-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent c30b978 commit 51b4cb4

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

block/partitions/core.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,6 @@ void drop_partition(struct block_device *part)
274274
put_device(&part->bd_device);
275275
}
276276

277-
static void delete_partition(struct block_device *part)
278-
{
279-
/*
280-
* Remove the block device from the inode hash, so that it cannot be
281-
* looked up any more even when openers still hold references.
282-
*/
283-
remove_inode_hash(part->bd_inode);
284-
bdev_mark_dead(part, false);
285-
drop_partition(part);
286-
}
287-
288277
static ssize_t whole_disk_show(struct device *dev,
289278
struct device_attribute *attr, char *buf)
290279
{
@@ -674,8 +663,23 @@ int bdev_disk_changed(struct gendisk *disk, bool invalidate)
674663
sync_blockdev(disk->part0);
675664
invalidate_bdev(disk->part0);
676665

677-
xa_for_each_start(&disk->part_tbl, idx, part, 1)
678-
delete_partition(part);
666+
xa_for_each_start(&disk->part_tbl, idx, part, 1) {
667+
/*
668+
* Remove the block device from the inode hash, so that
669+
* it cannot be looked up any more even when openers
670+
* still hold references.
671+
*/
672+
remove_inode_hash(part->bd_inode);
673+
674+
/*
675+
* If @disk->open_partitions isn't elevated but there's
676+
* still an active holder of that block device things
677+
* are broken.
678+
*/
679+
WARN_ON_ONCE(atomic_read(&part->bd_openers));
680+
invalidate_bdev(part);
681+
drop_partition(part);
682+
}
679683
clear_bit(GD_NEED_PART_SCAN, &disk->state);
680684

681685
/*

0 commit comments

Comments
 (0)