From patchwork Fri Sep 20 11:54:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 276367 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 93EA92C00C6 for ; Fri, 20 Sep 2013 21:58:56 +1000 (EST) Received: from localhost ([::1]:55248 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMzMM-0008Uo-Ob for incoming@patchwork.ozlabs.org; Fri, 20 Sep 2013 07:58:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43054) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMzIX-0002Te-HH for qemu-devel@nongnu.org; Fri, 20 Sep 2013 07:55:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VMzIQ-0008Gn-Rl for qemu-devel@nongnu.org; Fri, 20 Sep 2013 07:54:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58068) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMzIQ-0008Gg-K8 for qemu-devel@nongnu.org; Fri, 20 Sep 2013 07:54:50 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8KBsnDF023195 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 20 Sep 2013 07:54:50 -0400 Received: from dhcp-200-207.str.redhat.com (dhcp-192-197.str.redhat.com [10.33.192.197]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r8KBsP7A019396; Fri, 20 Sep 2013 07:54:48 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Fri, 20 Sep 2013 13:54:24 +0200 Message-Id: <1379678070-14346-12-git-send-email-kwolf@redhat.com> In-Reply-To: <1379678070-14346-1-git-send-email-kwolf@redhat.com> References: <1379678070-14346-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, armbru@redhat.com, stefanha@redhat.com, mreitz@redhat.com Subject: [Qemu-devel] [PATCH 11/17] blockdev: Move bus/unit/index processing to drive_init 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 This requires moving the automatic ID generation at the same time, so let's do that as well. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- blockdev.c | 157 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 73 insertions(+), 84 deletions(-) diff --git a/blockdev.c b/blockdev.c index 33383be..26127b3 100644 --- a/blockdev.c +++ b/blockdev.c @@ -312,10 +312,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts, const char *buf; const char *file = NULL; const char *serial; - const char *mediastr = ""; - int bus_id, unit_id; - int max_devs; - int index; int ro = 0; int bdrv_flags = 0; int on_read_error, on_write_error; @@ -355,10 +351,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts, has_driver_specific_opts = !!qdict_size(bs_opts); /* extract parameters */ - bus_id = qemu_opt_get_number(opts, "bus", 0); - unit_id = qemu_opt_get_number(opts, "unit", -1); - index = qemu_opt_get_number(opts, "index", -1); - snapshot = qemu_opt_get_bool(opts, "snapshot", 0); ro = qemu_opt_get_bool(opts, "read-only", 0); copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); @@ -366,8 +358,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts, file = qemu_opt_get(opts, "file"); serial = qemu_opt_get(opts, "serial"); - max_devs = if_max_devs[type]; - if ((buf = qemu_opt_get(opts, "discard")) != NULL) { if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { error_report("invalid discard option"); @@ -486,66 +476,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts, } } - /* compute bus and unit according index */ - - if (index != -1) { - if (bus_id != 0 || unit_id != -1) { - error_report("index cannot be used with bus and unit"); - return NULL; - } - bus_id = drive_index_to_bus_id(type, index); - unit_id = drive_index_to_unit_id(type, index); - } - - /* if user doesn't specify a unit_id, - * try to find the first free - */ - - if (unit_id == -1) { - unit_id = 0; - while (drive_get(type, bus_id, unit_id) != NULL) { - unit_id++; - if (max_devs && unit_id >= max_devs) { - unit_id -= max_devs; - bus_id++; - } - } - } - - /* check unit id */ - - if (max_devs && unit_id >= max_devs) { - error_report("unit %d too big (max is %d)", - unit_id, max_devs - 1); - return NULL; - } - - /* - * catch multiple definitions - */ - - if (drive_get(type, bus_id, unit_id) != NULL) { - error_report("drive with bus=%d, unit=%d (index=%d) exists", - bus_id, unit_id, index); - return NULL; - } - - /* no id supplied -> create one */ - if (qemu_opts_id(opts) == NULL) { - char *new_id; - if (type == IF_IDE || type == IF_SCSI) { - mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; - } - if (max_devs) { - new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, - mediastr, unit_id); - } else { - new_id = g_strdup_printf("%s%s%i", if_name[type], - mediastr, unit_id); - } - qemu_opts_set_id(opts, new_id); - } - /* init */ dinfo = g_malloc0(sizeof(*dinfo)); dinfo->id = g_strdup(qemu_opts_id(opts)); @@ -554,8 +484,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts, dinfo->bdrv->read_only = ro; dinfo->devaddr = devaddr; dinfo->type = type; - dinfo->bus = bus_id; - dinfo->unit = unit_id; dinfo->refcount = 1; if (serial != NULL) { dinfo->serial = g_strdup(serial); @@ -681,6 +609,18 @@ QemuOptsList qemu_legacy_drive_opts = { .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), .desc = { { + .name = "bus", + .type = QEMU_OPT_NUMBER, + .help = "bus number", + },{ + .name = "unit", + .type = QEMU_OPT_NUMBER, + .help = "unit number (i.e. lun for scsi)", + },{ + .name = "index", + .type = QEMU_OPT_NUMBER, + .help = "index number", + },{ .name = "media", .type = QEMU_OPT_STRING, .help = "media type (disk, cdrom)", @@ -722,6 +662,7 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) DriveMediaType media = MEDIA_DISK; BlockInterfaceType type; int cyls, heads, secs, translation; + int max_devs, bus_id, unit_id, index; Error *local_err = NULL; /* Change legacy command line options into QMP ones */ @@ -865,6 +806,63 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) } } + /* Device address specified by bus/unit or index. + * If none was specified, try to find the first free one. */ + bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); + unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); + index = qemu_opt_get_number(legacy_opts, "index", -1); + + max_devs = if_max_devs[type]; + + if (index != -1) { + if (bus_id != 0 || unit_id != -1) { + error_report("index cannot be used with bus and unit"); + goto fail; + } + bus_id = drive_index_to_bus_id(type, index); + unit_id = drive_index_to_unit_id(type, index); + } + + if (unit_id == -1) { + unit_id = 0; + while (drive_get(type, bus_id, unit_id) != NULL) { + unit_id++; + if (max_devs && unit_id >= max_devs) { + unit_id -= max_devs; + bus_id++; + } + } + } + + if (max_devs && unit_id >= max_devs) { + error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); + goto fail; + } + + if (drive_get(type, bus_id, unit_id) != NULL) { + error_report("drive with bus=%d, unit=%d (index=%d) exists", + bus_id, unit_id, index); + goto fail; + } + + /* no id supplied -> create one */ + if (qemu_opts_id(all_opts) == NULL) { + char *new_id; + const char *mediastr = ""; + if (type == IF_IDE || type == IF_SCSI) { + mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; + } + if (max_devs) { + new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, + mediastr, unit_id); + } else { + new_id = g_strdup_printf("%s%s%i", if_name[type], + mediastr, unit_id); + } + qdict_put(bs_opts, "id", qstring_from_str(new_id)); + g_free(new_id); + } + /* Actual block device init: Functionality shared with blockdev-add */ dinfo = blockdev_init(bs_opts, type, media); if (dinfo == NULL) { @@ -880,6 +878,9 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) dinfo->secs = secs; dinfo->trans = translation; + dinfo->bus = bus_id; + dinfo->unit = unit_id; + fail: qemu_opts_del(legacy_opts); return dinfo; @@ -2211,18 +2212,6 @@ QemuOptsList qemu_common_drive_opts = { .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), .desc = { { - .name = "bus", - .type = QEMU_OPT_NUMBER, - .help = "bus number", - },{ - .name = "unit", - .type = QEMU_OPT_NUMBER, - .help = "unit number (i.e. lun for scsi)", - },{ - .name = "index", - .type = QEMU_OPT_NUMBER, - .help = "index number", - },{ .name = "snapshot", .type = QEMU_OPT_BOOL, .help = "enable/disable snapshot mode",