Skip to content

Commit 52f019d

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: add a hard-readonly flag to struct gendisk
Commit 20bd1d0 ("scsi: sd: Keep disk read-only when re-reading partition") addressed a long-standing problem with user read-only policy being overridden as a result of a device-initiated revalidate. The commit has since been reverted due to a regression that left some USB devices read-only indefinitely. To fix the underlying problems with revalidate we need to keep track of hardware state and user policy separately. The gendisk has been updated to reflect the current hardware state set by the device driver. This is done to allow returning the device to the hardware state once the user clears the BLKROSET flag. The resulting semantics are as follows: - If BLKROSET sets a given partition read-only, that partition will remain read-only even if the underlying storage stack initiates a revalidate. However, the BLKRRPART ioctl will cause the partition table to be dropped and any user policy on partitions will be lost. - If BLKROSET has not been set, both the whole disk device and any partitions will reflect the current write-protect state of the underlying device. Based on a patch from Martin K. Petersen <martin.petersen@oracle.com>. Reported-by: Oleksii Kurochko <olkuroch@cisco.com> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=201221 Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 6f0d968 commit 52f019d

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

block/blk-core.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,7 @@ static inline bool should_fail_request(struct block_device *part,
694694

695695
static inline bool bio_check_ro(struct bio *bio, struct block_device *part)
696696
{
697-
const int op = bio_op(bio);
698-
699-
if (part->bd_read_only && op_is_write(op)) {
697+
if (op_is_write(bio_op(bio)) && bdev_read_only(part)) {
700698
char b[BDEVNAME_SIZE];
701699

702700
if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))

block/genhd.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,27 +1637,32 @@ static void set_disk_ro_uevent(struct gendisk *gd, int ro)
16371637
kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
16381638
}
16391639

1640-
void set_disk_ro(struct gendisk *disk, int flag)
1640+
/**
1641+
* set_disk_ro - set a gendisk read-only
1642+
* @disk: gendisk to operate on
1643+
* @ready_only: %true to set the disk read-only, %false set the disk read/write
1644+
*
1645+
* This function is used to indicate whether a given disk device should have its
1646+
* read-only flag set. set_disk_ro() is typically used by device drivers to
1647+
* indicate whether the underlying physical device is write-protected.
1648+
*/
1649+
void set_disk_ro(struct gendisk *disk, bool read_only)
16411650
{
1642-
struct disk_part_iter piter;
1643-
struct block_device *part;
1644-
1645-
if (disk->part0->bd_read_only != flag) {
1646-
set_disk_ro_uevent(disk, flag);
1647-
disk->part0->bd_read_only = flag;
1651+
if (read_only) {
1652+
if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1653+
return;
1654+
} else {
1655+
if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1656+
return;
16481657
}
1649-
1650-
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1651-
while ((part = disk_part_iter_next(&piter)))
1652-
part->bd_read_only = flag;
1653-
disk_part_iter_exit(&piter);
1658+
set_disk_ro_uevent(disk, read_only);
16541659
}
1655-
16561660
EXPORT_SYMBOL(set_disk_ro);
16571661

16581662
int bdev_read_only(struct block_device *bdev)
16591663
{
1660-
return bdev->bd_read_only;
1664+
return bdev->bd_read_only ||
1665+
test_bit(GD_READ_ONLY, &bdev->bd_disk->state);
16611666
}
16621667
EXPORT_SYMBOL(bdev_read_only);
16631668

block/partitions/core.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static ssize_t part_start_show(struct device *dev,
195195
static ssize_t part_ro_show(struct device *dev,
196196
struct device_attribute *attr, char *buf)
197197
{
198-
return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_read_only);
198+
return sprintf(buf, "%d\n", bdev_read_only(dev_to_bdev(dev)));
199199
}
200200

201201
static ssize_t part_alignment_offset_show(struct device *dev,
@@ -361,7 +361,6 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
361361

362362
bdev->bd_start_sect = start;
363363
bdev_set_nr_sectors(bdev, len);
364-
bdev->bd_read_only = get_disk_ro(disk);
365364

366365
if (info) {
367366
err = -ENOMEM;

include/linux/genhd.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct gendisk {
163163
int flags;
164164
unsigned long state;
165165
#define GD_NEED_PART_SCAN 0
166+
#define GD_READ_ONLY 1
166167
struct kobject *slave_dir;
167168

168169
struct timer_rand_state *random;
@@ -249,11 +250,12 @@ static inline void add_disk_no_queue_reg(struct gendisk *disk)
249250
extern void del_gendisk(struct gendisk *gp);
250251
extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
251252

252-
extern void set_disk_ro(struct gendisk *disk, int flag);
253+
void set_disk_ro(struct gendisk *disk, bool read_only);
253254

254255
static inline int get_disk_ro(struct gendisk *disk)
255256
{
256-
return disk->part0->bd_read_only;
257+
return disk->part0->bd_read_only ||
258+
test_bit(GD_READ_ONLY, &disk->state);
257259
}
258260

259261
extern void disk_block_events(struct gendisk *disk);

0 commit comments

Comments
 (0)