Skip to content

Commit b5cfbd3

Browse files
Yufen Yuaxboe
authored andcommitted
block: check disk exist before trying to add partition
If disk have been deleted, we should return fail for ioctl BLKPG_DEL_PARTITION. Otherwise, the directory /sys/class/block may remain invalid symlinks file. The race as following: blkdev_open del_gendisk disk->flags &= ~GENHD_FL_UP; blk_drop_partitions blkpg_ioctl bdev_add_partition add_partition device_add device_add_class_symlinks ioctl may add_partition after del_gendisk() have tried to delete partitions. Then, symlinks file will be created. Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Yufen Yu <yuyufen@huawei.com> Link: https://lore.kernel.org/r/20210610023241.3646241-1-yuyufen@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent efee99e commit b5cfbd3

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

block/partitions/core.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,17 +453,26 @@ int bdev_add_partition(struct block_device *bdev, int partno,
453453
sector_t start, sector_t length)
454454
{
455455
struct block_device *part;
456+
struct gendisk *disk = bdev->bd_disk;
457+
int ret;
456458

457-
mutex_lock(&bdev->bd_disk->open_mutex);
458-
if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
459-
mutex_unlock(&bdev->bd_disk->open_mutex);
460-
return -EBUSY;
459+
mutex_lock(&disk->open_mutex);
460+
if (!(disk->flags & GENHD_FL_UP)) {
461+
ret = -ENXIO;
462+
goto out;
461463
}
462464

463-
part = add_partition(bdev->bd_disk, partno, start, length,
465+
if (partition_overlaps(disk, start, length, -1)) {
466+
ret = -EBUSY;
467+
goto out;
468+
}
469+
470+
part = add_partition(disk, partno, start, length,
464471
ADDPART_FLAG_NONE, NULL);
465-
mutex_unlock(&bdev->bd_disk->open_mutex);
466-
return PTR_ERR_OR_ZERO(part);
472+
ret = PTR_ERR_OR_ZERO(part);
473+
out:
474+
mutex_unlock(&disk->open_mutex);
475+
return ret;
467476
}
468477

469478
int bdev_del_partition(struct block_device *bdev, int partno)

0 commit comments

Comments
 (0)