[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 12/15] raw-posix: Introduce hdev_is_sg()
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 12/15] raw-posix: Introduce hdev_is_sg() |
Date: |
Wed, 24 Jun 2015 16:28:03 +0100 |
From: Dimitris Aragiorgis <address@hidden>
Until now, an SG device was identified only by checking if its path
started with "/dev/sg". Then, hdev_open() would set the bs->sg flag
accordingly. The patch relies on the actual properties of the device
instead of the specified file path.
To this end, test for an SG device (e.g. /dev/sg0) by ensuring that
all of the following holds:
- The specified file name corresponds to a character device
- The device supports the SG_GET_VERSION_NUM ioctl
- The device supports the SG_GET_SCSI_ID ioctl
Signed-off-by: Dimitris Aragiorgis <address@hidden>
Message-id: address@hidden
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block/raw-posix.c | 39 ++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 57a243f..cbe6574 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -59,6 +59,7 @@
#include <linux/fd.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
+#include <scsi/sg.h>
#ifdef __s390__
#include <asm/dasd.h>
#endif
@@ -2086,15 +2087,38 @@ static void hdev_parse_filename(const char *filename,
QDict *options,
qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
}
+static bool hdev_is_sg(BlockDriverState *bs)
+{
+
+#if defined(__linux__)
+
+ struct stat st;
+ struct sg_scsi_id scsiid;
+ int sg_version;
+
+ if (stat(bs->filename, &st) >= 0 && S_ISCHR(st.st_mode) &&
+ !bdrv_ioctl(bs, SG_GET_VERSION_NUM, &sg_version) &&
+ !bdrv_ioctl(bs, SG_GET_SCSI_ID, &scsiid)) {
+ DPRINTF("SG device found: type=%d, version=%d\n",
+ scsiid.scsi_type, sg_version);
+ return true;
+ }
+
+#endif
+
+ return false;
+}
+
static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
BDRVRawState *s = bs->opaque;
Error *local_err = NULL;
int ret;
+
+#if defined(__APPLE__) && defined(__MACH__)
const char *filename = qdict_get_str(options, "filename");
-#if defined(__APPLE__) && defined(__MACH__)
if (strstart(filename, "/dev/cdrom", NULL)) {
kern_return_t kernResult;
io_iterator_t mediaIterator;
@@ -2123,16 +2147,6 @@ static int hdev_open(BlockDriverState *bs, QDict
*options, int flags,
#endif
s->type = FTYPE_FILE;
-#if defined(__linux__)
- {
- char resolved_path[ MAXPATHLEN ], *temp;
-
- temp = realpath(filename, resolved_path);
- if (temp && strstart(temp, "/dev/sg", NULL)) {
- bs->sg = 1;
- }
- }
-#endif
ret = raw_open_common(bs, options, flags, 0, &local_err);
if (ret < 0) {
@@ -2142,6 +2156,9 @@ static int hdev_open(BlockDriverState *bs, QDict
*options, int flags,
return ret;
}
+ /* Since this does ioctl the device must be already opened */
+ bs->sg = hdev_is_sg(bs);
+
if (flags & BDRV_O_RDWR) {
ret = check_hdev_writable(s);
if (ret < 0) {
--
2.4.3
- [Qemu-devel] [PULL 03/15] block-backend: Introduce blk_drain(), (continued)
- [Qemu-devel] [PULL 03/15] block-backend: Introduce blk_drain(), Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 04/15] virtio-blk: Use blk_drain() to drain IO requests, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 05/15] util/hbitmap: Add an API to reset all set bits in hbitmap, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 07/15] nvme: Fix memleak in nvme_dma_read_prp, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 06/15] vvfat: add a label option, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 08/15] block: Use bdrv_is_sg() everywhere, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 09/15] Fix migration in case of scsi-generic, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 10/15] raw-posix: DPRINTF instead of DEBUG_BLOCK_PRINT, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 11/15] raw-posix: Use DPRINTF for DEBUG_FLOPPY, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 13/15] iov: don't touch iov in iov_send_recv(), Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 12/15] raw-posix: Introduce hdev_is_sg(),
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 14/15] qemu-iotests: fix 051.out after qdev error message change, Stefan Hajnoczi, 2015/06/24
- [Qemu-devel] [PULL 15/15] virito-blk: drop duplicate check, Stefan Hajnoczi, 2015/06/24
- Re: [Qemu-devel] [PULL 00/15] Block patches, Peter Maydell, 2015/06/25