Skip to content

Commit 02e3522

Browse files
htejunJens Axboe
authored andcommitted
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 Stable: 2.6.38 Signed-off-by: Tejun Heo <tj@kernel.org> Reported-by: David Zeuthen <zeuthen@gmail.com> Reported-by: Martin Pitt <martin.pitt@ubuntu.com> Reported-by: Kay Sievers <kay.sievers@vrfy.org> Tested-by: Kay Sievers <kay.sievers@vrfy.org> Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Cc: stable@kernel.org Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent bf2253a commit 02e3522

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

‎fs/block_dev.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
11021102
if (!bdev->bd_part)
11031103
goto out_clear;
11041104

1105+
ret = 0;
11051106
if (disk->fops->open) {
11061107
ret = disk->fops->open(bdev, mode);
11071108
if (ret == -ERESTARTSYS) {
@@ -1118,18 +1119,25 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
11181119
put_disk(disk);
11191120
goto restart;
11201121
}
1121-
if (ret)
1122-
goto out_clear;
11231122
}
1123+
/*
1124+
* If the device is invalidated, rescan partition
1125+
* if open succeeded or failed with -ENOMEDIUM.
1126+
* The latter is necessary to prevent ghost
1127+
* partitions on a removed medium.
1128+
*/
1129+
if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1130+
rescan_partitions(disk, bdev);
1131+
if (ret)
1132+
goto out_clear;
1133+
11241134
if (!bdev->bd_openers) {
11251135
bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
11261136
bdi = blk_get_backing_dev_info(bdev);
11271137
if (bdi == NULL)
11281138
bdi = &default_backing_dev_info;
11291139
bdev_inode_switch_bdi(bdev->bd_inode, bdi);
11301140
}
1131-
if (bdev->bd_invalidated)
1132-
rescan_partitions(disk, bdev);
11331141
} else {
11341142
struct block_device *whole;
11351143
whole = bdget_disk(disk, 0);
@@ -1153,13 +1161,14 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
11531161
}
11541162
} else {
11551163
if (bdev->bd_contains == bdev) {
1156-
if (bdev->bd_disk->fops->open) {
1164+
ret = 0;
1165+
if (bdev->bd_disk->fops->open)
11571166
ret = bdev->bd_disk->fops->open(bdev, mode);
1158-
if (ret)
1159-
goto out_unlock_bdev;
1160-
}
1161-
if (bdev->bd_invalidated)
1167+
/* the same as first opener case, read comment there */
1168+
if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
11621169
rescan_partitions(bdev->bd_disk, bdev);
1170+
if (ret)
1171+
goto out_unlock_bdev;
11631172
}
11641173
/* only one opener holds refs to the module and disk */
11651174
module_put(disk->fops->owner);

0 commit comments

Comments
 (0)