提交 1196f8b8 编写于 作者: T Tejun Heo 提交者: Jens Axboe

block: rescan partitions on invalidated devices on -ENOMEDIA too

__blkdev_get() doesn't rescan partitions if disk->fops->open() fails,
which leads to ghost partition devices lingering after medimum removal
is known to both the kernel and userland.  The behavior also creates a
subtle inconsistency where O_NONBLOCK open, which doesn't fail even if
there's no medium, clears the ghots partitions, which is exploited to
work around the problem from userland.

Fix it by updating __blkdev_get() to issue partition rescan after
-ENOMEDIA too.

This was reported in the following bz.

 https://bugzilla.kernel.org/show_bug.cgi?id=13029

Note for stable: 2.6.38 and later only

Cc: stable@kernel.org
Signed-off-by: NTejun Heo <tj@kernel.org>
Reported-by: NDavid Zeuthen <zeuthen@gmail.com>
Reported-by: NMartin Pitt <martin.pitt@ubuntu.com>
Reported-by: NKay Sievers <kay.sievers@vrfy.org>
Tested-by: NKay Sievers <kay.sievers@vrfy.org>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
Signed-off-by: NJens Axboe <jaxboe@fusionio.com>
上级 ea6949b6
...@@ -1102,6 +1102,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) ...@@ -1102,6 +1102,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
if (!bdev->bd_part) if (!bdev->bd_part)
goto out_clear; goto out_clear;
ret = 0;
if (disk->fops->open) { if (disk->fops->open) {
ret = disk->fops->open(bdev, mode); ret = disk->fops->open(bdev, mode);
if (ret == -ERESTARTSYS) { if (ret == -ERESTARTSYS) {
...@@ -1118,9 +1119,18 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) ...@@ -1118,9 +1119,18 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
put_disk(disk); put_disk(disk);
goto restart; goto restart;
} }
if (ret)
goto out_clear;
} }
/*
* 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 && (!ret || ret == -ENOMEDIUM))
rescan_partitions(disk, bdev);
if (ret)
goto out_clear;
if (!bdev->bd_openers) { if (!bdev->bd_openers) {
bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
bdi = blk_get_backing_dev_info(bdev); bdi = blk_get_backing_dev_info(bdev);
...@@ -1128,8 +1138,6 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) ...@@ -1128,8 +1138,6 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
bdi = &default_backing_dev_info; bdi = &default_backing_dev_info;
bdev_inode_switch_bdi(bdev->bd_inode, bdi); bdev_inode_switch_bdi(bdev->bd_inode, bdi);
} }
if (bdev->bd_invalidated)
rescan_partitions(disk, bdev);
} else { } else {
struct block_device *whole; struct block_device *whole;
whole = bdget_disk(disk, 0); whole = bdget_disk(disk, 0);
...@@ -1153,13 +1161,14 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) ...@@ -1153,13 +1161,14 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
} }
} else { } else {
if (bdev->bd_contains == bdev) { if (bdev->bd_contains == bdev) {
if (bdev->bd_disk->fops->open) { ret = 0;
if (bdev->bd_disk->fops->open)
ret = bdev->bd_disk->fops->open(bdev, mode); ret = bdev->bd_disk->fops->open(bdev, mode);
if (ret) /* the same as first opener case, read comment there */
goto out_unlock_bdev; if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
}
if (bdev->bd_invalidated)
rescan_partitions(bdev->bd_disk, bdev); rescan_partitions(bdev->bd_disk, bdev);
if (ret)
goto out_unlock_bdev;
} }
/* only one opener holds refs to the module and disk */ /* only one opener holds refs to the module and disk */
module_put(disk->fops->owner); module_put(disk->fops->owner);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册