From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ian Campbell Subject: Re: [PATCH 5/6] libxl: introduce libxl__alloc_vdev Date: Tue, 27 Mar 2012 15:21:39 +0100 Message-ID: <1332858099.25560.32.camel@zakaz.uk.xensource.com> References: <1332856772-30292-5-git-send-email-stefano.stabellini@eu.citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1332856772-30292-5-git-send-email-stefano.stabellini@eu.citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Stefano Stabellini Cc: "xen-devel@lists.xensource.com" , Ian Jackson List-Id: xen-devel@lists.xenproject.org On Tue, 2012-03-27 at 14:59 +0100, Stefano Stabellini wrote: > Introduce libxl__alloc_vdev: find a spare virtual block device in the > domain passed as argument. > > Signed-off-by: Stefano Stabellini > --- > tools/libxl/libxl.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 files changed, 60 insertions(+), 0 deletions(-) > > diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c > index fe63fd5..fa7898a 100644 > --- a/tools/libxl/libxl.c > +++ b/tools/libxl/libxl.c > @@ -1228,6 +1228,13 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass) > > /******************************************************************************/ > > +static int libxl__append_disk_list_of_type(libxl__gc *gc, > + uint32_t domid, > + xs_transaction_t t, > + const char *type, > + libxl_device_disk **disks, > + int *ndisks); > + > int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk) > { > int rc; > @@ -1278,6 +1285,59 @@ static int libxl__device_from_disk(libxl__gc *gc, uint32_t domid, > return 0; > } > > +static int libxl__vdev_to_index(libxl__gc *gc, char *vdev) > +{ > + if (vdev == NULL) > + return 0; > + if (strlen(vdev) > 4 || strlen(vdev) < 4) > + return 0; > + /* assume xvdz is the last one available */ > + if (vdev[3] >= 'z') > + return -1; > + return vdev[3] - 'a'; > +} > + > +static char* libxl__index_to_vdev(libxl__gc *gc, int idx) > +{ > + if (idx < 0) > + return NULL; > + return libxl__sprintf(gc, "xvd%c", 'a' + idx); > +} > + > +static char * libxl__alloc_vdev(libxl__gc *gc, uint32_t domid, xs_transaction_t t, > + libxl_device_disk *disk) > +{ > + int rc = 0; > + libxl_device_disk *disks = NULL; > + int num = 0, idx = -1, max_idx = -1, i = 0; > + > + rc = libxl__append_disk_list_of_type(gc, domid, t, "vbd", &disks, &num); > + if (rc) goto out; > + > + rc = libxl__append_disk_list_of_type(gc, domid, t, "tap", &disks, &num); > + if (rc) goto out; > + > + rc = libxl__append_disk_list_of_type(gc, domid, t, "qdisk", &disks, &num); > + if (rc) goto out; This is basically an open-coded version of libxl_disk_list, isn't it? For this use though wouldn't it be as easy to simply iterate over idx checking if a frontend dir exists? > + > + for (i = 0; i < num; i++) { > + idx = libxl__vdev_to_index(gc, disks[i].vdev); > + if (idx < 0) { > + max_idx = -1; > + goto out; > + } > + if (idx > max_idx) > + max_idx = idx; > + } > + max_idx++; > +out: > + while (disks && num) { > + num--; > + libxl_device_disk_dispose(&disks[num]); > + } > + free(disks); > + return libxl__index_to_vdev(gc, max_idx); > +} > > static int libxl__device_disk_add_t(libxl__gc *gc, uint32_t domid, xs_transaction_t t, > libxl_device_disk *disk)