Patchwork [RFC,4/6] scsi-disk: allow customization of the lun

login
register
mail settings
Submitter Paolo Bonzini
Date May 20, 2011, 3:03 p.m.
Message ID <1305903817-25476-5-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/96619/
State New
Headers show

Comments

Paolo Bonzini - May 20, 2011, 3:03 p.m.
This will not work until there is a device that can answer REPORT LUNS
for disks with LUN != 0.  However, it provides the infrastructure.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/scsi-disk.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)
Christoph Hellwig - May 25, 2011, 1:13 p.m.
>      case REPORT_LUNS:
> +        assert(!s->lun);

Besides REPORT_LUNS really belonging into the core code as mentioned before
the assert seems dangerous to me.  What protects a guest from issuing a
REPORT LUNS for a non-zero LUN and hitting this assert?  Note that SPC
explicitly allows sending REPORT LUNS to either LUN 0 or the well known
LUN if it exists, even if at least Linux doesn't make use of the latter
yet.

Patch

diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index b14c32f..f41550a 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -66,6 +66,7 @@  struct SCSIDiskState
     /* The qemu block layer uses a fixed 512 byte sector size.
        This is the number of 512 byte blocks in a single scsi sector.  */
     int cluster_size;
+    uint32_t lun;
     uint32_t removable;
     uint64_t max_lba;
     QEMUBH *bh;
@@ -516,7 +517,7 @@  static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
 
     memset(outbuf, 0, buflen);
 
-    if (req->lun) {
+    if (req->lun != s->lun) {
         outbuf[0] = 0x7f;	/* LUN not supported */
         return buflen;
     }
@@ -955,6 +956,7 @@  static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         DPRINTF("Unsupported Service Action In\n");
         goto illegal_request;
     case REPORT_LUNS:
+        assert(!s->lun);
         if (req->cmd.xfer < 16)
             goto illegal_request;
         memset(outbuf, 0, 16);
@@ -1022,14 +1024,12 @@  static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     }
 #endif
 
-    if (req->lun) {
-        /* Only LUN 0 supported.  */
+    if (command != REQUEST_SENSE && command != INQUIRY && req->lun != s->lun) {
+        /* Only one LUN supported.  */
         DPRINTF("Unimplemented LUN %d\n", req->lun);
-        if (command != REQUEST_SENSE && command != INQUIRY) {
-            scsi_command_complete(r, CHECK_CONDITION,
-                                  SENSE_CODE(LUN_NOT_SUPPORTED));
-            return 0;
-        }
+        scsi_command_complete(r, CHECK_CONDITION,
+                              SENSE_CODE(LUN_NOT_SUPPORTED));
+        return 0;
     }
     switch (command) {
     case TEST_UNIT_READY:
@@ -1247,6 +1247,7 @@  static SCSIDeviceInfo scsi_disk_info = {
     .get_sense    = scsi_get_sense,
     .qdev.props   = (Property[]) {
         DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),
+        DEFINE_PROP_UINT32("lun",  SCSIDiskState, lun, 0),
         DEFINE_PROP_STRING("ver",  SCSIDiskState, version),
         DEFINE_PROP_STRING("serial",  SCSIDiskState, serial),
         DEFINE_PROP_BIT("removable", SCSIDiskState, removable, 0, false),