Skip to contentSkip to conversation

scsi-generic: let guests recognize readonly=on on passthrough devices

PATCH In review Paolo Bonzini 2 participants 2 messages Sep 16, 2015 – Sep 17, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi-generic: let guests recognize readonly=on on passthrough devices

Passed-through SCSI devices can be opened with the readonly=on option. When this happens, Linux filters away write commands so that the guest cannot overwrite the contents of the device.

However, the guest does not know that the device is read-only, and accepts writes. The writes only fail later when the page cache is flushed.

This patch modifies scsi-generic to modify the MODE SENSE data and set the read-only bit in the device-specific parameters, so that the guest OS treats the disk as write protected.

---
 hw/scsi/scsi-generic.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/scsi/scsi-generic.c +14 -0
index 1b6350b..a4626f7 100644
@@ -210,6 +210,20 @@ static void scsi_read_complete(void * opaque, int ret)
     }
     blk_set_guest_block_size(s->conf.blk, s->blocksize);
 
+    /* Patch MODE SENSE device specific parameters if the BDS is opened
+     * readonly.
+     */
+    if ((s->type == TYPE_DISK || s->type == TYPE_TAPE) &&
+        blk_is_read_only(s->conf.blk) &&
+        (r->req.cmd.buf[0] == MODE_SENSE ||
+         r->req.cmd.buf[0] == MODE_SENSE_10) &&
+        (r->req.cmd.buf[1] & 0x8) == 0) {
+        if (r->req.cmd.buf[0] == MODE_SENSE) {
+            r->buf[2] |= 0x80;
+        } else  {
+            r->buf[3] |= 0x80;
+        }
+    }
     scsi_req_data(&r->req, len);
     scsi_req_unref(&r->req);
 }
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 46 lines · Paolo Bonzini

Passed-through SCSI devices can be opened with the readonly=on option. When this happens, Linux filters away write commands so that the guest cannot overwrite the contents of the device.

However, the guest does not know that the device is read-only, and accepts writes. The writes only fail later when the page cache is flushed.

This patch modifies scsi-generic to modify the MODE SENSE data and set the read-only bit in the device-specific parameters, so that the guest OS treats the disk as write protected.

---
 hw/scsi/scsi-generic.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/scsi/scsi-generic.c +14 -0
index 1b6350b..a4626f7 100644
@@ -210,6 +210,20 @@ static void scsi_read_complete(void * opaque, int ret)
     }
     blk_set_guest_block_size(s->conf.blk, s->blocksize);
 
+    /* Patch MODE SENSE device specific parameters if the BDS is opened
+     * readonly.
+     */
+    if ((s->type == TYPE_DISK || s->type == TYPE_TAPE) &&
+        blk_is_read_only(s->conf.blk) &&
+        (r->req.cmd.buf[0] == MODE_SENSE ||
+         r->req.cmd.buf[0] == MODE_SENSE_10) &&
+        (r->req.cmd.buf[1] & 0x8) == 0) {
+        if (r->req.cmd.buf[0] == MODE_SENSE) {
+            r->buf[2] |= 0x80;
+        } else  {
+            r->buf[3] |= 0x80;
+        }
+    }
     scsi_req_data(&r->req, len);
     scsi_req_unref(&r->req);
 }
Signature · 1 line
2.5.0