Commit 8f9c8fc5 authored by Yu Kuai's avatar Yu Kuai Committed by Jialin Zhang
Browse files

block: fix scan partition for exclusively open device again

mainline inclusion
from mainline-v6.3-rc1
commit e5cfefa9
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I6MQLP
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5cfefa97bccf956ea0bb6464c1f6c84fd7a8d9f



--------------------------------

As explained in commit 36369f46 ("block: Do not reread partition table
on exclusively open device"), reread partition on the device that is
exclusively opened by someone else is problematic.

This patch will make sure partition scan will only be proceed if current
thread open the device exclusively, or the device is not opened
exclusively, and in the later case, other scanners and exclusive openers
will be blocked temporarily until partition scan is done.

Fixes: 10c70d95 ("block: remove the bd_openers checks in blk_drop_partitions")
Cc: <stable@vger.kernel.org>
Suggested-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarYu Kuai <yukuai3@huawei.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20230217022200.3092987-3-yukuai1@huaweicloud.com


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>

Conflicts:
	block/genhd.c
	block/ioctl.c
Signed-off-by: default avatarYu Kuai <yukuai3@huawei.com>
Reviewed-by: default avatarHou Tao <houtao1@huawei.com>
Signed-off-by: default avatarJialin Zhang <zhangjialin11@huawei.com>
parent 1c0b1b48
Loading
Loading
Loading
Loading
+34 −4
Original line number Diff line number Diff line
@@ -739,16 +739,42 @@ static void register_disk(struct device *parent, struct gendisk *disk,
int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
{
	struct block_device *bdev;
	struct block_device *claim;
	int ret = 0;

	if (!disk_part_scan_enabled(disk))
		return -EINVAL;

	/*
	 * If the device is opened exclusively by current thread already, it's
	 * safe to scan partitons, otherwise, use bd_prepare_to_claim() to
	 * synchronize with other exclusive openers and other partition
	 * scanners.
	 */
	if (!(mode & FMODE_EXCL)) {
		claim = bdget_part(&disk->part0);
		if (!claim)
			return -ENOMEM;

		ret = bd_prepare_to_claim(claim, claim, disk_scan_partitions);
		if (ret) {
			bdput(claim);
			return ret;
		}
	}

	set_bit(GD_NEED_PART_SCAN, &disk->state);
	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
	bdev = blkdev_get_by_dev(disk_devt(disk), mode & ~FMODE_EXCL, NULL);
	if (IS_ERR(bdev))
		return PTR_ERR(bdev);
		ret = PTR_ERR(bdev);
	else
		blkdev_put(bdev, mode);
	return 0;

	if (!(mode & FMODE_EXCL)) {
		bd_abort_claiming(claim, claim, disk_scan_partitions);
		bdput(claim);
	}
	return ret;
}

static void disk_init_partition(struct gendisk *disk)
@@ -850,6 +876,10 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk,
	disk_add_events(disk);
	blk_integrity_add(disk);

	/* Make sure the first partition scan will be proceed */
	if (get_capacity(disk) && disk_part_scan_enabled(disk))
		set_bit(GD_NEED_PART_SCAN, &disk->state);

	/*
	 * Set the flag at last, so that block devcie can't be opened
	 * before it's registration is done.
+1 −1
Original line number Diff line number Diff line
@@ -543,7 +543,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
			return -EINVAL;
		if (bdev->bd_part_count)
			return -EBUSY;
		return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
		return disk_scan_partitions(bdev->bd_disk, mode);
	case BLKTRACESTART:
	case BLKTRACESTOP:
	case BLKTRACETEARDOWN: