linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: sd: Keep disk read-only when re-reading partition
@ 2018-03-01 14:08 Jeremy Cline
  2018-03-01 14:14 ` Andy Shevchenko
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jeremy Cline @ 2018-03-01 14:08 UTC (permalink / raw)
  To: James E . J . Bottomley, Martin K . Petersen
  Cc: linux-scsi, linux-kernel, Jeremy Cline

If the read-only flag is true on a SCSI disk, re-reading the partition
table sets the flag back to false.

To observe this bug, you can run:

1. blockdev --setro /dev/sda
2. blockdev --rereadpt /dev/sda
3. blockdev --getro /dev/sda

This commit reads the disk's old state and combines it with the device
disk-reported state rather than unconditionally marking it as RW.

Reported-by: Li Ning <lining916740672@icloud.com>
Signed-off-by: Jeremy Cline <jeremy@jcline.org>
---
 drivers/scsi/sd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index bff21e636ddd..7a3a66a7890f 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -2595,6 +2595,7 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
 	int res;
 	struct scsi_device *sdp = sdkp->device;
 	struct scsi_mode_data data;
+	int disk_ro = get_disk_ro(sdkp->disk);
 	int old_wp = sdkp->write_prot;
 
 	set_disk_ro(sdkp->disk, 0);
@@ -2634,7 +2635,8 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
 		sd_first_printk(KERN_WARNING, sdkp,
 			  "Test WP failed, assume Write Enabled\n");
 	} else {
-		sdkp->write_prot = ((data.device_specific & 0x80) != 0);
+		sdkp->write_prot = ((data.device_specific & 0x80) != 0) ||
+			disk_ro;
 		set_disk_ro(sdkp->disk, sdkp->write_prot);
 		if (sdkp->first_scan || old_wp != sdkp->write_prot) {
 			sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
-- 
2.16.2

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] scsi: sd: Keep disk read-only when re-reading partition
  2018-03-01 14:08 [PATCH] scsi: sd: Keep disk read-only when re-reading partition Jeremy Cline
@ 2018-03-01 14:14 ` Andy Shevchenko
  2018-03-02  1:53 ` Martin K. Petersen
  2018-03-02 12:00 ` Johannes Thumshirn
  2 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2018-03-01 14:14 UTC (permalink / raw)
  To: Jeremy Cline
  Cc: James E . J . Bottomley, Martin K . Petersen, linux-scsi,
	Linux Kernel Mailing List

On Thu, Mar 1, 2018 at 4:08 PM, Jeremy Cline <jeremy@jcline.org> wrote:
> If the read-only flag is true on a SCSI disk, re-reading the partition
> table sets the flag back to false.
>
> To observe this bug, you can run:
>
> 1. blockdev --setro /dev/sda
> 2. blockdev --rereadpt /dev/sda
> 3. blockdev --getro /dev/sda
>
> This commit reads the disk's old state and combines it with the device
> disk-reported state rather than unconditionally marking it as RW.

> -               sdkp->write_prot = ((data.device_specific & 0x80) != 0);
> +               sdkp->write_prot = ((data.device_specific & 0x80) != 0) ||
> +                       disk_ro;

Perhaps
               sdkp->write_prot = (data.device_specific & 0x80) || disk_ro;

will save a line.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] scsi: sd: Keep disk read-only when re-reading partition
  2018-03-01 14:08 [PATCH] scsi: sd: Keep disk read-only when re-reading partition Jeremy Cline
  2018-03-01 14:14 ` Andy Shevchenko
@ 2018-03-02  1:53 ` Martin K. Petersen
  2018-03-02 12:00 ` Johannes Thumshirn
  2 siblings, 0 replies; 6+ messages in thread
From: Martin K. Petersen @ 2018-03-02  1:53 UTC (permalink / raw)
  To: Jeremy Cline
  Cc: James E . J . Bottomley, Martin K . Petersen, linux-scsi, linux-kernel


Jeremy,

Thanks for looking into this!

> This commit reads the disk's old state and combines it with the device
> disk-reported state rather than unconditionally marking it as RW.

Applied to 4.16/scsi-fixes with a few minor tweaks. Thank you!

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] scsi: sd: Keep disk read-only when re-reading partition
  2018-03-01 14:08 [PATCH] scsi: sd: Keep disk read-only when re-reading partition Jeremy Cline
  2018-03-01 14:14 ` Andy Shevchenko
  2018-03-02  1:53 ` Martin K. Petersen
@ 2018-03-02 12:00 ` Johannes Thumshirn
  2018-03-02 14:11   ` Jeremy Cline
  2 siblings, 1 reply; 6+ messages in thread
From: Johannes Thumshirn @ 2018-03-02 12:00 UTC (permalink / raw)
  To: Jeremy Cline
  Cc: James E . J . Bottomley, Martin K . Petersen, linux-scsi, linux-kernel

On Thu, Mar 01, 2018 at 02:08:10PM +0000, Jeremy Cline wrote:
> If the read-only flag is true on a SCSI disk, re-reading the partition
> table sets the flag back to false.
> 
> To observe this bug, you can run:
> 
> 1. blockdev --setro /dev/sda
> 2. blockdev --rereadpt /dev/sda
> 3. blockdev --getro /dev/sda

hi Jeremy,

Do you mind wiring up a testcase in blktest [1] for this? Given that
you already have a rather trivial reproducer.

[1] https://github.com/osandov/

Thanks,
	Johannes

-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] scsi: sd: Keep disk read-only when re-reading partition
  2018-03-02 12:00 ` Johannes Thumshirn
@ 2018-03-02 14:11   ` Jeremy Cline
  2018-03-02 14:24     ` Johannes Thumshirn
  0 siblings, 1 reply; 6+ messages in thread
From: Jeremy Cline @ 2018-03-02 14:11 UTC (permalink / raw)
  To: Johannes Thumshirn
  Cc: James E . J . Bottomley, Martin K . Petersen, linux-scsi, linux-kernel

On 03/02/2018 07:00 AM, Johannes Thumshirn wrote:
> On Thu, Mar 01, 2018 at 02:08:10PM +0000, Jeremy Cline wrote:
>> If the read-only flag is true on a SCSI disk, re-reading the partition
>> table sets the flag back to false.
>>
>> To observe this bug, you can run:
>>
>> 1. blockdev --setro /dev/sda
>> 2. blockdev --rereadpt /dev/sda
>> 3. blockdev --getro /dev/sda
> 
> hi Jeremy,
> 
> Do you mind wiring up a testcase in blktest [1] for this? Given that
> you already have a rather trivial reproducer.
> 
> [1] https://github.com/osandov/

Hi Johannes,

Sure, I'll take care of it.

Regards,
Jeremy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] scsi: sd: Keep disk read-only when re-reading partition
  2018-03-02 14:11   ` Jeremy Cline
@ 2018-03-02 14:24     ` Johannes Thumshirn
  0 siblings, 0 replies; 6+ messages in thread
From: Johannes Thumshirn @ 2018-03-02 14:24 UTC (permalink / raw)
  To: Jeremy Cline
  Cc: James E . J . Bottomley, Martin K . Petersen, linux-scsi, linux-kernel

On Fri, Mar 02, 2018 at 02:11:02PM +0000, Jeremy Cline wrote:
> Sure, I'll take care of it.

Thanks a  lot.
       Johannes
-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-03-02 14:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-01 14:08 [PATCH] scsi: sd: Keep disk read-only when re-reading partition Jeremy Cline
2018-03-01 14:14 ` Andy Shevchenko
2018-03-02  1:53 ` Martin K. Petersen
2018-03-02 12:00 ` Johannes Thumshirn
2018-03-02 14:11   ` Jeremy Cline
2018-03-02 14:24     ` Johannes Thumshirn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).