From patchwork Thu Feb 25 19:25:16 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: 8426081 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 4AFB09F2F0 for ; Thu, 25 Feb 2016 19:28:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5F2822039C for ; Thu, 25 Feb 2016 19:28:22 +0000 (UTC) Received: from lists.xen.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 68C6420263 for ; Thu, 25 Feb 2016 19:28:21 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1aZ1Xg-00056I-Sk; Thu, 25 Feb 2016 19:25:40 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1aZ1Xf-00054Y-8V for xen-devel@lists.xenproject.org; Thu, 25 Feb 2016 19:25:39 +0000 Received: from [193.109.254.147] by server-13.bemta-14.messagelabs.com id C3/6B-02874-2355FC65; Thu, 25 Feb 2016 19:25:38 +0000 X-Env-Sender: prvs=856d7c560=roger.pau@citrix.com X-Msg-Ref: server-11.tower-27.messagelabs.com!1456428330!18181714!3 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 40925 invoked from network); 25 Feb 2016 19:25:37 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-11.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 25 Feb 2016 19:25:37 -0000 X-IronPort-AV: E=Sophos;i="5.22,498,1449532800"; d="scan'208";a="341174607" From: Roger Pau Monne To: Date: Thu, 25 Feb 2016 20:25:16 +0100 Message-ID: <1456428318-8318-6-git-send-email-roger.pau@citrix.com> X-Mailer: git-send-email 2.5.4 (Apple Git-61) In-Reply-To: <1456428318-8318-1-git-send-email-roger.pau@citrix.com> References: <1456428318-8318-1-git-send-email-roger.pau@citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Wei Liu , Stefano Stabellini , Ian Jackson , Ian Campbell , Roger Pau Monne Subject: [Xen-devel] [PATCH v2 5/7] 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=-1.9 required=5.0 tests=BAYES_00, 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: Stefano Stabellini Cc: Ian Campbell 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 6d719d7..d55c699 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -3071,7 +3071,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; } @@ -3137,7 +3137,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; @@ -3151,9 +3151,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; @@ -3162,8 +3159,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 8bb5e93..f5b8bf6 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 */