From patchwork Tue Jun 23 10:45:00 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dimitris Aragiorgis X-Patchwork-Id: 487579 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C82CB1401AD for ; Tue, 23 Jun 2015 20:47:23 +1000 (AEST) Received: from localhost ([::1]:44489 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7Ljd-0000uk-RB for incoming@patchwork.ozlabs.org; Tue, 23 Jun 2015 06:47:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55304) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7Lhx-0006a1-No for qemu-devel@nongnu.org; Tue, 23 Jun 2015 06:45:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z7Lhw-000673-P3 for qemu-devel@nongnu.org; Tue, 23 Jun 2015 06:45:37 -0400 Received: from mx0.arrikto.com ([2a01:7e00::f03c:91ff:fe6e:d7ab]:55810) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7Lhp-0005pp-BK; Tue, 23 Jun 2015 06:45:29 -0400 Received: from troi.arr-srv (mail.arr-srv [192.168.98.4]) by mx0.arrikto.com (Postfix) with ESMTP id ED8C3A093; Tue, 23 Jun 2015 13:45:26 +0300 (EEST) Received: from lenovo.loc (unknown [192.168.94.120]) by troi.arr-srv (Postfix) with ESMTPSA id 8BFAA29F; Tue, 23 Jun 2015 13:45:26 +0300 (EEST) From: Dimitris Aragiorgis To: qemu-devel@nongnu.org Date: Tue, 23 Jun 2015 13:45:00 +0300 Message-Id: <1435056300-14924-6-git-send-email-dimara@arrikto.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1435056300-14924-1-git-send-email-dimara@arrikto.com> References: <1435056300-14924-1-git-send-email-dimara@arrikto.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:7e00::f03c:91ff:fe6e:d7ab Cc: kwolf@redhat.com, pbonzini@redhat.com, qemu-block@nongnu.org, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v5 5/5] raw-posix: Introduce hdev_is_sg() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Stefan Hajnoczi --- 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 69ae251..942cda3 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -57,6 +57,7 @@ #include #include #include +#include #ifdef __s390__ #include #endif @@ -2084,15 +2085,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; - const char *filename = qdict_get_str(options, "filename"); #if defined(__APPLE__) && defined(__MACH__) + const char *filename = qdict_get_str(options, "filename"); + if (strstart(filename, "/dev/cdrom", NULL)) { kern_return_t kernResult; io_iterator_t mediaIterator; @@ -2121,16 +2145,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) { @@ -2140,6 +2154,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) {