Skip to contentSkip to conversation

SCSI: Make write commands fail if the device is readonly

PATCH New Ronnie Sahlberg 2 participants 3 messages Jul 13, 2012 – Jul 16, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SCSI: Make write commands fail if the device is readonly

List, Paolo

Please find a patch that makes WRITE and WRITE_VERIFY fail with DATA_PROTECT/WRITE_PROTECTED sense code for devices that are write-protected via readonly=on

Tests have been berformed to verify the functionality using :

./x86_64-softmmu/qemu-system-x86_64 -enable-kvm -m 128 -drive file=20mb.img -drive file=./scsi-disk.img,if=scsi,readonly=on -net nic -net user,hostfwd=tcp:127.0.0.1:32601-:3260 -vnc 0.0.0.0:0

iscsi-test --dataloss --test="*300_readonly*" iscsi://127.0.0.1:32601/iqn.qemu.scsi-test/3 0300_readonly:
==============
WRITE10 to LUN 0 ... [OK]
WRITE12 to LUN 0 ... [OK]
WRITE16 to LUN 0 ... [OK]
WRITESAME10 to LUN 0 ... [OK]
WRITESAME16 to LUN 0 ... [OK]
WRITESAME10 to UNMAP LUN 0 ... LUN is not thin-provisioned. [SKIPPED] WRITESAME16 to UNMAP LUN 0 ... LUN is not thin-provisioned. [SKIPPED]
UNMAP LUN 0 ... LUN is not thin-provisioned. [SKIPPED]
TEST T0300_readonly [OK]

regards
ronnie sahlberg

#1 lore raw

PATCH 1/1 SCSI: Fail medium writes with proper sense for readonly LUNs

Add sense code for DATA_PROTECT/WRITE_PROTECTED and return this error for any WRITE*/WRITE_VERIFY* calls if the device is readonly=on, i.e. write-protected

---
 hw/scsi-bus.c  |    5 +++++
 hw/scsi-disk.c |   16 +++++++++++++---
 hw/scsi.h      |    2 ++
 3 files changed, 20 insertions(+), 3 deletions(-)
hw/scsi-bus.c +5 -0
index 5ad1013..6299094 100644
@@ -1172,6 +1172,11 @@ const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
 };
 
+/* Data Protection, Write Protected */
+const struct SCSISense sense_code_WRITE_PROTECTED = {
+    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
+};
+
 /*
  * scsi_build_sense
  *
hw/scsi-disk.c +13 -3
index 2c2be33..0aca383 100644
@@ -1565,9 +1565,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
         r->sector_count = len * (s->qdev.blocksize / 512);
         break;
-    case VERIFY_10:
-    case VERIFY_12:
-    case VERIFY_16:
     case WRITE_6:
     case WRITE_10:
     case WRITE_12:
@@ -1575,6 +1572,13 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     case WRITE_VERIFY_10:
     case WRITE_VERIFY_12:
     case WRITE_VERIFY_16:
+        if (bdrv_is_read_only(s->qdev.conf.bs)) {
+            goto write_protect;
+        }
+        /* fallthough */
+    case VERIFY_10:
+    case VERIFY_12:
+    case VERIFY_16:
         len = r->req.cmd.xfer / s->qdev.blocksize;
         DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
                 (command & 0xe) == 0xe ? "And Verify " : "",
@@ -1621,6 +1625,9 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         DPRINTF("WRITE SAME() (sector %" PRId64 ", count %d)\n",
                 r->req.cmd.lba, len);
 
+        if (bdrv_is_read_only(s->qdev.conf.bs)) {
+            goto write_protect;
+        }
         if (r->req.cmd.lba > s->qdev.max_lba) {
             goto illegal_lba;
         }
@@ -1651,6 +1658,9 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     illegal_lba:
         scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
         return 0;
+    write_protect:
+        scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
+        return 0;
     }
     if (r->sector_count == 0 && r->iov.iov_len == 0) {
         scsi_req_complete(&r->req, GOOD);
hw/scsi.h +2 -0
index 76f06d4..94d2962 100644
@@ -202,6 +202,8 @@ extern const struct SCSISense sense_code_MEDIUM_CHANGED;
 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
 /* Unit attention, Device internal reset */
 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
+/* Data Protection, Write Protected */
+extern const struct SCSISense sense_code_WRITE_PROTECTED;
 
 #define SENSE_CODE(x) sense_code_ ## x
 
Signature · 1 line
1.7.3.1
#2 lore raw

Re: [Qemu-devel] [PATCH] SCSI: Fail medium writes with proper sense for readonly LUNs

Il 13/07/2012 23:30, Ronnie Sahlberg ha scritto:

Quoted 5 lines

Add sense code for DATA_PROTECT/WRITE_PROTECTED and return this error for any WRITE*/WRITE_VERIFY* calls if the device is readonly=on, i.e. write-protected

Applied, thanks!

Paolo

Quoted 84 lines
---
 hw/scsi-bus.c  |    5 +++++
 hw/scsi-disk.c |   16 +++++++++++++---
 hw/scsi.h      |    2 ++
 3 files changed, 20 insertions(+), 3 deletions(-)
hw/scsi-bus.c +5 -0
index 5ad1013..6299094 100644
@@ -1172,6 +1172,11 @@ const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
 };
 
+/* Data Protection, Write Protected */
+const struct SCSISense sense_code_WRITE_PROTECTED = {
+    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
+};
+
 /*
  * scsi_build_sense
  *
hw/scsi-disk.c +13 -3
index 2c2be33..0aca383 100644
@@ -1565,9 +1565,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
         r->sector_count = len * (s->qdev.blocksize / 512);
         break;
-    case VERIFY_10:
-    case VERIFY_12:
-    case VERIFY_16:
     case WRITE_6:
     case WRITE_10:
     case WRITE_12:
@@ -1575,6 +1572,13 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     case WRITE_VERIFY_10:
     case WRITE_VERIFY_12:
     case WRITE_VERIFY_16:
+        if (bdrv_is_read_only(s->qdev.conf.bs)) {
+            goto write_protect;
+        }
+        /* fallthough */
+    case VERIFY_10:
+    case VERIFY_12:
+    case VERIFY_16:
         len = r->req.cmd.xfer / s->qdev.blocksize;
         DPRINTF("Write %s(sector %" PRId64 ", count %d)\n",
                 (command & 0xe) == 0xe ? "And Verify " : "",
@@ -1621,6 +1625,9 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
         DPRINTF("WRITE SAME() (sector %" PRId64 ", count %d)\n",
                 r->req.cmd.lba, len);
 
+        if (bdrv_is_read_only(s->qdev.conf.bs)) {
+            goto write_protect;
+        }
         if (r->req.cmd.lba > s->qdev.max_lba) {
             goto illegal_lba;
         }
@@ -1651,6 +1658,9 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     illegal_lba:
         scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
         return 0;
+    write_protect:
+        scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
+        return 0;
     }
     if (r->sector_count == 0 && r->iov.iov_len == 0) {
         scsi_req_complete(&r->req, GOOD);
hw/scsi.h +2 -0
index 76f06d4..94d2962 100644
@@ -202,6 +202,8 @@ extern const struct SCSISense sense_code_MEDIUM_CHANGED;
 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
 /* Unit attention, Device internal reset */
 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
+/* Data Protection, Write Protected */
+extern const struct SCSISense sense_code_WRITE_PROTECTED;
 
 #define SENSE_CODE(x) sense_code_ ## x