From patchwork Thu Mar 31 15:09:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 8714331 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 52DA79F30C for ; Thu, 31 Mar 2016 15:15:00 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2FF4020251 for ; Thu, 31 Mar 2016 15:14:59 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 06B0C202A1 for ; Thu, 31 Mar 2016 15:14:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aleH0-00070I-2x; Thu, 31 Mar 2016 15:12:38 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aleGy-0006yn-UP for xen-devel@lists.xenproject.org; Thu, 31 Mar 2016 15:12:37 +0000 Received: from [85.158.137.68] by server-16.bemta-3.messagelabs.com id B6/7F-02994-46E3DF65; Thu, 31 Mar 2016 15:12:36 +0000 X-Env-Sender: prvs=8914c038b=roger.pau@citrix.com X-Msg-Ref: server-11.tower-31.messagelabs.com!1459437149!6112105!4 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 3483 invoked from network); 31 Mar 2016 15:12:35 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-11.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 31 Mar 2016 15:12:35 -0000 X-IronPort-AV: E=Sophos;i="5.24,422,1454976000"; d="scan'208";a="343781198" From: Roger Pau Monne To: Date: Thu, 31 Mar 2016 17:09:36 +0200 Message-ID: <1459436979-7475-6-git-send-email-roger.pau@citrix.com> X-Mailer: git-send-email 2.6.4 (Apple Git-63) In-Reply-To: <1459436979-7475-1-git-send-email-roger.pau@citrix.com> References: <1459436979-7475-1-git-send-email-roger.pau@citrix.com> MIME-Version: 1.0 X-DLP: MIA2 Cc: Wei Liu , Ian Jackson , Roger Pau Monne Subject: [Xen-devel] [PATCH v3 5/8] libxl: properly use vdev vs local device X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The current code in libxl assumed that vdev is equal to local device, but this is only true for Linux systems. In other OSes the local device can use a nomenclature completely different from the virtual device one. Move the current libxl__devid_to_localdev Linux implementation out of the OS-specific file and rename it to libxl__devid_to_vdev, and then make sure local_device_attach_cb return the local device in the diskpath field. Signed-off-by: Roger Pau Monné Acked-by: Wei Liu --- Cc: Ian Jackson Cc: Wei Liu --- tools/libxl/libxl.c | 12 ++++------ tools/libxl/libxl_device.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 1 + tools/libxl/libxl_linux.c | 49 +------------------------------------ 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 944f4d1..a2767fa 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -2989,7 +2989,7 @@ static char * libxl__alloc_vdev(libxl__gc *gc, void *get_vdev_user, GCSPRINTF("%s/device/vbd/%d/backend", dompath, devid)) == NULL) { if (errno == ENOENT) - return libxl__devid_to_localdev(gc, devid); + return libxl__devid_to_vdev(gc, devid); else return NULL; } @@ -3126,7 +3126,7 @@ static void local_device_attach_cb(libxl__egc *egc, libxl__ao_device *aodev) { STATE_AO_GC(aodev->ao); libxl__disk_local_state *dls = CONTAINER_OF(aodev, *dls, aodev); - char *dev = NULL, *be_path = NULL; + char *be_path = NULL; int rc; libxl__device device; libxl_device_disk *disk = &dls->disk; @@ -3140,9 +3140,6 @@ static void local_device_attach_cb(libxl__egc *egc, libxl__ao_device *aodev) goto out; } - dev = GCSPRINTF("/dev/%s", disk->vdev); - LOG(DEBUG, "locally attaching disk %s", dev); - rc = libxl__device_from_disk(gc, LIBXL_TOOLSTACK_DOMID, disk, &device); if (rc < 0) goto out; @@ -3151,8 +3148,9 @@ static void local_device_attach_cb(libxl__egc *egc, libxl__ao_device *aodev) if (rc < 0) goto out; - if (dev != NULL) - dls->diskpath = libxl__strdup(gc, dev); + dls->diskpath = GCSPRINTF("/dev/%s", + libxl__devid_to_localdev(gc, device.devid)); + LOG(DEBUG, "locally attached disk %s", dls->diskpath); dls->callback(egc, dls, 0); return; diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 4ced9b6..9410563 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -436,6 +436,63 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, return -1; } +static char *encode_disk_name(char *ptr, unsigned int n) +{ + if (n >= 26) + ptr = encode_disk_name(ptr, n / 26 - 1); + *ptr = 'a' + n % 26; + return ptr + 1; +} + +char *libxl__devid_to_vdev(libxl__gc *gc, int devid) +{ + unsigned int minor; + int offset; + int nr_parts; + char *ptr = NULL; +/* Same as in Linux. + * encode_disk_name might end up using up to 29 bytes (BUFFER_SIZE - 3) + * including the trailing \0. + * + * The code is safe because 26 raised to the power of 28 (that is the + * maximum offset that can be stored in the allocated buffer as a + * string) is far greater than UINT_MAX on 64 bits so offset cannot be + * big enough to exhaust the available bytes in ret. */ +#define BUFFER_SIZE 32 + char *ret = libxl__zalloc(gc, BUFFER_SIZE); + +#define EXT_SHIFT 28 +#define EXTENDED (1<= 26) - ptr = encode_disk_name(ptr, n / 26 - 1); - *ptr = 'a' + n % 26; - return ptr + 1; -} - char *libxl__devid_to_localdev(libxl__gc *gc, int devid) { - unsigned int minor; - int offset; - int nr_parts; - char *ptr = NULL; - char *ret = libxl__zalloc(gc, BUFFER_SIZE); - - if (!VDEV_IS_EXTENDED(devid)) { - minor = devid & 0xff; - nr_parts = 16; - } else { - minor = BLKIF_MINOR_EXT(devid); - nr_parts = 256; - } - offset = minor / nr_parts; - - strcpy(ret, "xvd"); - ptr = encode_disk_name(ret + 3, offset); - if (minor % nr_parts == 0) - *ptr = 0; - else - /* overflow cannot happen, thanks to the upper bound */ - snprintf(ptr, ret + 32 - ptr, - "%d", minor & (nr_parts - 1)); - return ret; + return libxl__devid_to_vdev(gc, devid); } /* Hotplug scripts helpers */