Skip to contentSkip to conversation

block: Expose host block dev I/O size limit in scsi-block/scsi-generic

PATCH Superseded Fam Zheng 4 participants 8 messages May 26, 2016 – Jun 3, 2016 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover block: Expose host block dev I/O size limit in scsi-block/scsi-generic

Host devices passed through as scsi-block or scsi-generic may have a compound maximum I/O limit (out of physical hardware limit, driver quirks and file system configuration, etc) that is presented in the sysfs entry. SG_IOs we issue should respect this. However the value is currently not discoverable by guest, because the vHBA (virtio-scsi) would present an fixed emulated limit, while the INQUIRY (vpd=0xb0, block limits page) response solely speaks for the LUN itself, not the host kernel. The issue is observed on scsi passthrough of host usb or dm-multipath disks, and it is not specific to certain device types.

The proposed solution is collecting the host sysfs limit in raw-posix driver when a block device is used, and intercepting INQUIRY response to clip the max xfer len field.

This fixes booting a SanDisk usb-key with an upstream kernel. The usb disk reports a nonsense large value in INQUIRY, while the host (usb quirk?) only allows 120KB.

Fam Zheng (2):
raw-posix: Fetch max sectors for host block device from sysfs
scsi-generic: Merge block max xfer len in INQUIRY response

 block/raw-posix.c      | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/scsi/scsi-generic.c | 12 ++++++++++++
 2 files changed, 59 insertions(+)
Signature · 1 line
2.8.2
author
#1 lore raw

PATCH 1/2 raw-posix: Fetch max sectors for host block device from sysfs

This is sometimes a useful value we should count in.

---
 block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
block/raw-posix.c +47 -0
index a4f5a1b..d3796ad 100644
@@ -729,9 +729,56 @@ static void raw_reopen_abort(BDRVReopenState *state)
     state->opaque = NULL;
 }
 
+static int hdev_get_max_transfer_length(dev_t dev)
+{
+    int ret;
+    int fd;
+    char *path;
+    const char *end;
+    char buf[32];
+    long len;
+
+    path = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_kb",
+                           major(dev), minor(dev));
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        ret = -errno;
+        goto out;
+    }
+    ret = read(fd, buf, sizeof(buf));
+    if (ret < 0) {
+        ret = -errno;
+        goto out;
+    } else if (ret == 0) {
+        ret = -EIO;
+        goto out;
+    }
+    buf[ret] = 0;
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtol(buf, &end, 10, &len);
+    if (ret == 0 && end && *end == '\n') {
+        ret = len * 1024 / BDRV_SECTOR_SIZE;
+    }
+
+    close(fd);
+out:
+    g_free(path);
+    return ret;
+}
+
 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 {
     BDRVRawState *s = bs->opaque;
+    struct stat st;
+
+    if (!fstat(s->fd, &st)) {
+        if (S_ISBLK(st.st_mode)) {
+            int ret = hdev_get_max_transfer_length(st.st_rdev);
+            if (ret >= 0) {
+                bs->bl.max_transfer_length = ret;
+            }
+        }
+    }
 
     raw_probe_alignment(bs, s->fd, errp);
     bs->bl.min_mem_alignment = s->buf_align;
Signature · 1 line
2.8.2
author
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs

Quoted 1 line · Fam Zheng

This is sometimes a useful value we should count in.

Kevin, Max, could you review this please?

Fam

Quoted 71 lines
---
 block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
block/raw-posix.c +47 -0
index a4f5a1b..d3796ad 100644
@@ -729,9 +729,56 @@ static void raw_reopen_abort(BDRVReopenState *state)
     state->opaque = NULL;
 }
 
+static int hdev_get_max_transfer_length(dev_t dev)
+{
+    int ret;
+    int fd;
+    char *path;
+    const char *end;
+    char buf[32];
+    long len;
+
+    path = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_kb",
+                           major(dev), minor(dev));
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        ret = -errno;
+        goto out;
+    }
+    ret = read(fd, buf, sizeof(buf));
+    if (ret < 0) {
+        ret = -errno;
+        goto out;
+    } else if (ret == 0) {
+        ret = -EIO;
+        goto out;
+    }
+    buf[ret] = 0;
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtol(buf, &end, 10, &len);
+    if (ret == 0 && end && *end == '\n') {
+        ret = len * 1024 / BDRV_SECTOR_SIZE;
+    }
+
+    close(fd);
+out:
+    g_free(path);
+    return ret;
+}
+
 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 {
     BDRVRawState *s = bs->opaque;
+    struct stat st;
+
+    if (!fstat(s->fd, &st)) {
+        if (S_ISBLK(st.st_mode)) {
+            int ret = hdev_get_max_transfer_length(st.st_rdev);
+            if (ret >= 0) {
+                bs->bl.max_transfer_length = ret;
+            }
+        }
+    }
 
     raw_probe_alignment(bs, s->fd, errp);
     bs->bl.min_mem_alignment = s->buf_align;
Signature · 1 line
2.8.2
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs

In reply to Fam Zheng
Quoted 16 lines · Fam Zheng

This is sometimes a useful value we should count in.

---
 block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
block/raw-posix.c +1 -0
index a4f5a1b..d3796ad 100644
@@ -729,9 +729,56 @@ static void raw_reopen_abort(BDRVReopenState *state)
     state->opaque = NULL;
 }
 
+static int hdev_get_max_transfer_length(dev_t dev)

block/raw-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 742

Quoted 10 lines
block/raw-posix.c +10 -0
+{
+    int ret;
+    int fd;
+    char *path;
+    const char *end;
+    char buf[32];
+    long len;
+
+    path = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_kb",
+                           major(dev), minor(dev));

I can't say I like this very much, but well, it won't do any harm on any systems that do not offer this path (i.e. any non-Linux system, I suppose). So I'm fine with it.

Quoted 4 lines
block/raw-posix.c +4 -0
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        ret = -errno;
+        goto out;

block/raw-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 756

Quoted 10 lines
block/raw-posix.c +10 -0
+    }
+    ret = read(fd, buf, sizeof(buf));
+    if (ret < 0) {
+        ret = -errno;
+        goto out;
+    } else if (ret == 0) {
+        ret = -EIO;
+        goto out;
+    }
+    buf[ret] = 0;

Potential buffer overflow if ret == sizeof(buf).

block/raw-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 760

Quoted 4 lines
block/raw-posix.c +4 -0
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtol(buf, &end, 10, &len);
+    if (ret == 0 && end && *end == '\n') {
+        ret = len * 1024 / BDRV_SECTOR_SIZE;

Maybe there should be an overflow check here.

block/raw-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 763

Quoted 3 lines
block/raw-posix.c +3 -0
+    }
+
+    close(fd);

This belongs in some error path, because otherwise the FD is leaked if the read() failed.

Max

Quoted 22 lines
block/raw-posix.c +15 -0
+out:
+    g_free(path);
+    return ret;
+}
+
 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 {
     BDRVRawState *s = bs->opaque;
+    struct stat st;
+
+    if (!fstat(s->fd, &st)) {
+        if (S_ISBLK(st.st_mode)) {
+            int ret = hdev_get_max_transfer_length(st.st_rdev);
+            if (ret >= 0) {
+                bs->bl.max_transfer_length = ret;
+            }
+        }
+    }
 
     raw_probe_alignment(bs, s->fd, errp);
     bs->bl.min_mem_alignment = s->buf_align;
#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs

Am 02.06.2016 um 14:30 hat Max Reitz geschrieben:

Quoted 16 lines · Fam Zheng
Quoted 15 lines

This is sometimes a useful value we should count in.

---
 block/raw-posix.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
block/raw-posix.c +0 -0
index a4f5a1b..d3796ad 100644
@@ -729,9 +729,56 @@ static void raw_reopen_abort(BDRVReopenState *state)
     state->opaque = NULL;
 }
 
Quoted 15 lines

block/raw-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 742

Quoted 11 lines
block/raw-posix.c +11 -0
+static int hdev_get_max_transfer_length(dev_t dev)
+{
+    int ret;
+    int fd;
+    char *path;
+    const char *end;
+    char buf[32];
+    long len;
+
+    path = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_sectors_kb",
+                           major(dev), minor(dev));

I can't say I like this very much, but well, it won't do any harm on any systems that do not offer this path (i.e. any non-Linux system, I suppose). So I'm fine with it.

Haven't looked at the patch in detail yet, so I didn't want to send a comment yet, but I think this should be #ifdef-ed out for non-Linux.

Also a quick search on the internet suggests that the BLKSECTGET ioctl is what we're looking for, so hopefully using sysfs is unnecessary anyway.

Kevin

author
#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fetch max sectors for host block device from sysfs

Quoted 3 lines · Kevin Wolf

Also a quick search on the internet suggests that the BLKSECTGET ioctl is what we're looking for, so hopefully using sysfs is unnecessary anyway.

Oops! Looks like something went terribly wrong with my "quick search", will post v2. Thanks!

Fam

author
#6 lore raw

PATCH 2/2 scsi-generic: Merge block max xfer len in INQUIRY response

In reply to Fam Zheng

The rationale is similar to the above mode sense response interception:
this is practically the only channel to communicate restraints from elsewhere such as host and block driver.

The scsi bus we attach onto can have a larger max xfer len than what is accepted by the host file system (guarding between the host scsi LUN and QEMU), in which case the SG_IO we generate would get -EINVAL.

---
 hw/scsi/scsi-generic.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/scsi/scsi-generic.c +12 -0
index 7459465..71372a8 100644
@@ -222,6 +222,18 @@ static void scsi_read_complete(void * opaque, int ret)
             r->buf[3] |= 0x80;
         }
     }
+    if (s->type == TYPE_DISK &&
+        r->req.cmd.buf[0] == INQUIRY &&
+        r->req.cmd.buf[2] == 0xb0) {
+        uint32_t max_xfer_len = blk_get_max_transfer_length(s->conf.blk);
+        if (max_xfer_len) {
+            stl_be_p(&r->buf[8], max_xfer_len);
+            /* Also take care of the opt xfer len. */
+            if (ldl_be_p(&r->buf[12]) > max_xfer_len) {
+                stl_be_p(&r->buf[12], max_xfer_len);
+            }
+        }
+    }
     scsi_req_data(&r->req, len);
     scsi_req_unref(&r->req);
 }
Signature · 1 line
2.8.2
#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] scsi-generic: Merge block max xfer len in INQUIRY response

Quoted 9 lines · Fam Zheng

The rationale is similar to the above mode sense response interception:
this is practically the only channel to communicate restraints from elsewhere such as host and block driver.

The scsi bus we attach onto can have a larger max xfer len than what is accepted by the host file system (guarding between the host scsi LUN and QEMU), in which case the SG_IO we generate would get -EINVAL.

The two patches are pretty much separate, so I'm queuing this myself.

Thanks,

Paolo

Quoted 28 lines
---
 hw/scsi/scsi-generic.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/scsi/scsi-generic.c +12 -0
index 7459465..71372a8 100644
@@ -222,6 +222,18 @@ static void scsi_read_complete(void * opaque, int ret)
             r->buf[3] |= 0x80;
         }
     }
+    if (s->type == TYPE_DISK &&
+        r->req.cmd.buf[0] == INQUIRY &&
+        r->req.cmd.buf[2] == 0xb0) {
+        uint32_t max_xfer_len = blk_get_max_transfer_length(s->conf.blk);
+        if (max_xfer_len) {
+            stl_be_p(&r->buf[8], max_xfer_len);
+            /* Also take care of the opt xfer len. */
+            if (ldl_be_p(&r->buf[12]) > max_xfer_len) {
+                stl_be_p(&r->buf[12], max_xfer_len);
+            }
+        }
+    }
     scsi_req_data(&r->req, len);
     scsi_req_unref(&r->req);
 }