Skip to contentSkip to conversation

Misc patches for libxl_device_disk functions

PATCH In review Wei Liu 3 participants 17 messages Feb 9, 2015 – Mar 6, 2015

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover Misc patches for libxl_device_disk functions

The first two patches are cleaning up and refactoring.

The third patch fixes a bug discovered by Jim's libvirt test case.

Wei.

Wei Liu (3):
libxl, xl: don't init/dispose when not necessary
libxl: factor out libxl__disk_backend_from_xs_be
libxl: libxl__device_from_disk should retrieve backend from xenstore

 tools/libxl/libxl.c      | 58 +++++++++++++++++++++++++++++++++++-------------
 tools/libxl/libxl.h      |  7 ++++++
 tools/libxl/xl_cmdimpl.c |  1 +
 3 files changed, 50 insertions(+), 16 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/3 libxl, xl: don't init/dispose when not necessary

Functions like libxl__device_disk_from_xs_be and libxl_vdev_to_device_disk should not touch the disk struct passed in. It's caller's responsibility to do that.

This leads to some changes to the caller to prepare the disk struct.

Note that libxl_vdev_to_device_disk is a public API so a macro is defined in libxl.h to indicate the change of behaviour.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.c      | 6 +-----
 tools/libxl/libxl.h      | 7 +++++++
 tools/libxl/xl_cmdimpl.c | 1 +
 3 files changed, 9 insertions(+), 5 deletions(-)
tools/libxl/libxl.c +1 -5
index cd6f42c..ef48550 100644
@@ -2556,8 +2556,6 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
     char *tmp;
     int rc;
 
-    libxl_device_disk_init(disk);
-
     rc = sscanf(be_path, "/local/domain/%d/", &disk->backend_domid);
     if (rc != 1) {
         LOG(ERROR, "Unable to fetch device backend domid from %s", be_path);
@@ -2620,7 +2618,6 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
 
     return 0;
 cleanup:
-    libxl_device_disk_dispose(disk);
     return ERROR_FAIL;
 }
 
@@ -2635,8 +2632,6 @@ int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid,
     if (devid < 0)
         return ERROR_INVAL;
 
-    libxl_device_disk_init(disk);
-
     dompath = libxl__xs_get_dompath(gc, domid);
     if (!dompath) {
         goto out;
@@ -2675,6 +2670,7 @@ static int libxl__append_disk_list_of_type(libxl__gc *gc,
         tmp = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         if (tmp == NULL)
             return ERROR_NOMEM;
+        libxl_device_disk_init(tmp);
         *disks = tmp;
         pdisk = *disks + initial_disks;
         pdisk_end = *disks + initial_disks + n;
tools/libxl/libxl.h +7 -0
index c219f59..8f6ed3c 100644
@@ -135,6 +135,13 @@
 #define LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE 1
 
 /*
+ * The caller of libxl_vdev_to_device_disk should call
+ * libxl_device_disk_init before passing in the disk struct because
+ * libxl_vdev_to_device_disk doesn't do that anymore.
+ */
+#define LIBXL_VDEV_TO_DEVICE_DISK_NO_INIT 1
+
+/*
  * LIBXL_HAVE_BUILDINFO_IOMEM_START_GFN indicates that it is possible
  * to specify the start guest frame number used to map a range of I/O
  * memory machine frame numbers via the 'gfn' field (of type uint64)
tools/libxl/xl_cmdimpl.c +1 -0
index b7eac29..ef101c3 100644
@@ -6411,6 +6411,7 @@ int main_blockdetach(int argc, char **argv)
     }
 
     domid = find_domain(argv[optind]);
+    libxl_device_disk_init(&disk);
 
     if (libxl_vdev_to_device_disk(ctx, domid, argv[optind+1], &disk)) {
         fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]);
Signature · 1 line
1.9.1
author
#2 lore raw

Re: [PATCH 1/3] libxl, xl: don't init/dispose when not necessary

Er... The title should be

"libxl, xl: don't init/dispose when not unnecessary"

                                        ----------
author
#3 lore raw

Re: [PATCH 1/3] libxl, xl: don't init/dispose when not necessary

Quoted 4 lines · Wei Liu

Er... The title should be

"libxl, xl: don't init/dispose when not unnecessary"

                                        ----------

Grrr... What was I thinking. The initial title was correct.

Sorry for the noise and spam.

#4 lore raw

Re: [PATCH 1/3] libxl, xl: don't init/dispose when not necessary

In reply to Wei Liu

Wei Liu writes ("[PATCH 1/3] libxl, xl: don't init/dispose when not necessary"):

Quoted 3 lines

Functions like libxl__device_disk_from_xs_be and libxl_vdev_to_device_disk should not touch the disk struct passed in. It's caller's responsibility to do that.

...

Quoted 2 lines

Note that libxl_vdev_to_device_disk is a public API so a macro is defined in libxl.h to indicate the change of behaviour.

I don't think this is really on, in the public API.

Why not make libxl_..._free idempotent ? Then extra calls to _init are harmless.

Ian.

author
#5 lore raw

Re: [PATCH 1/3] libxl, xl: don't init/dispose when not necessary

Quoted 10 lines · Ian Jackson

Wei Liu writes ("[PATCH 1/3] libxl, xl: don't init/dispose when not necessary"):

Quoted 3 lines

Functions like libxl__device_disk_from_xs_be and libxl_vdev_to_device_disk should not touch the disk struct passed in. It's caller's responsibility to do that.

...

Quoted 2 lines

Note that libxl_vdev_to_device_disk is a public API so a macro is defined in libxl.h to indicate the change of behaviour.

I don't think this is really on, in the public API.

Yes, it's public API -- not in libxl.h, but in libxl_utils.h. And xl is using it.

The problem is that calling _init inside libxl_vdev_to_device_disk violates our pattern of using libxl types.

Quoted 3 lines

Why not make libxl_..._free idempotent ? Then extra calls to _init are harmless.

That would be a good idea and it should be done in a separate patch set.

Strictly speaking this patch is not required to fix the bug Jim spotted. I will just drop it in this series and rework in while I make _dispose idempotent.

Wei.

Quoted 1 line

Ian.

author
#6 lore raw

PATCH 2/3 libxl: factor out libxl__disk_backend_from_xs_be

In reply to Wei Liu
Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
tools/libxl/libxl.c +23 -8
index ef48550..01a2d11 100644
@@ -2258,6 +2258,27 @@ int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk)
     return rc;
 }
 
+static int libxl__disk_backend_from_xs_be(libxl__gc *gc,
+                                          const char *be_path,
+                                          libxl_device_disk *disk)
+{
+    char *type;
+    int rc = ERROR_FAIL;
+
+    type = libxl__xs_read(gc, XBT_NULL,
+                          libxl__sprintf(gc, "%s/type", be_path));
+    if (!type) {
+        LOG(ERROR, "Missing xenstore node %s/type", be_path);
+        goto out;
+    }
+
+    libxl_string_to_backend(CTX, type, &disk->backend);
+
+    rc = 0;
+out:
+    return rc;
+}
+
 int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
                                    libxl_device_disk *disk,
                                    libxl__device *device)
@@ -2572,14 +2593,8 @@ static int libxl__device_disk_from_xs_be(libxl__gc *gc,
         disk->pdev_path = tmp;
     }
 
-
-    tmp = libxl__xs_read(gc, XBT_NULL,
-                         libxl__sprintf(gc, "%s/type", be_path));
-    if (!tmp) {
-        LOG(ERROR, "Missing xenstore node %s/type", be_path);
-        goto cleanup;
-    }
-    libxl_string_to_backend(ctx, tmp, &(disk->backend));
+    rc = libxl__disk_backend_from_xs_be(gc, be_path, disk);
+    if (rc) goto cleanup;
 
     disk->vdev = xs_read(ctx->xsh, XBT_NULL,
                          libxl__sprintf(gc, "%s/dev", be_path), &len);
Signature · 1 line
1.9.1
#7 lore raw

Re: [PATCH 2/3] libxl: factor out libxl__disk_backend_from_xs_be

Wei Liu writes ("[PATCH 2/3] libxl: factor out libxl__disk_backend_from_xs_be"):

Quoted 1 line

Commit messages should include the phrase "No functional change" where appropriate.

I think it is in this case.

Thanks,
Ian.

author
#8 lore raw

Re: [PATCH 2/3] libxl: factor out libxl__disk_backend_from_xs_be

Quoted 8 lines · Ian Jackson

Wei Liu writes ("[PATCH 2/3] libxl: factor out libxl__disk_backend_from_xs_be"):

Quoted 1 line

Commit messages should include the phrase "No functional change" where appropriate.

I think it is in this case.

Yes. I will add that to commit message.

Wei.

Quoted 2 lines

Thanks,
Ian.

author
#9 lore raw

PATCH 3/3 libxl: libxl__device_from_disk should retrieve backend from xenstore

In reply to Wei Liu

... if backend is not set by caller.

Also change the function to use "goto" idiom while I was there.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
---
 tools/libxl/libxl.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +18 -3
index 01a2d11..56c2e09 100644
@@ -2285,12 +2285,24 @@ int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     int devid;
+    int rc;
 
     devid = libxl__device_disk_dev_number(disk->vdev, NULL, NULL);
     if (devid==-1) {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
                " virtual disk identifier %s", disk->vdev);
-        return ERROR_INVAL;
+        rc = ERROR_INVAL;
+        goto out;
+    }
+
+    if (disk->backend == LIBXL_DISK_BACKEND_UNKNOWN) {
+        char *be_path;
+
+        be_path = GCSPRINTF("/local/domain/%u/backend/vbd/%u/%d",
+                            disk->backend_domid,
+                            domid, devid);
+        rc = libxl__disk_backend_from_xs_be(gc, be_path, disk);
+        if (rc) goto out;
     }
 
     device->backend_domid = disk->backend_domid;
@@ -2309,14 +2321,17 @@ int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
         default:
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend type: %d\n",
                        disk->backend);
-            return ERROR_INVAL;
+            rc = ERROR_INVAL;
+            goto out;
     }
 
     device->domid = domid;
     device->devid = devid;
     device->kind  = LIBXL__DEVICE_KIND_VBD;
 
-    return 0;
+    rc = 0;
+out:
+    return rc;
 }
 
 /* Specific function called directly only by local disk attach,
Signature · 1 line
1.9.1
Acked-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Wei Liu writes ("[PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore"):

Quoted 1 line

... if backend is not set by caller.

as far as it goes, but I think you may want a more radical change - see below.

Quoted 1 line

Also change the function to use "goto" idiom while I was there.

(Although usually it would be better to split this kind of thing into a pre-patch, in this case it's small and easily reviewed.)

Is the backend type the only missing or potentially-wrong information ? ISTM that perhaps the caller might not know the target, either.

What should happen if the caller specifies a different target in disk to the one the device is actually using ? The documentation should specify which of the fields are important.

Maybe libxl_device_disk_remove needs to call libxl_vdev_to_device_disk and check that the supplied disk struct is plausible somehow. In that case it might be nice for the caller to be able to fill in only the vdev.

Ian.

author
#11 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Quoted 13 lines · Ian Jackson

Wei Liu writes ("[PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore"):

Quoted 1 line

... if backend is not set by caller.

as far as it goes, but I think you may want a more radical change - see below.

Quoted 1 line

Also change the function to use "goto" idiom while I was there.

(Although usually it would be better to split this kind of thing into a pre-patch, in this case it's small and easily reviewed.)

Quoted 8 lines

Is the backend type the only missing or potentially-wrong information ? ISTM that perhaps the caller might not know the target, either.

What should happen if the caller specifies a different target in disk to the one the device is actually using ? The documentation should specify which of the fields are important.

I'm not sure because it's not documented.

We should take a step back to define the important fields first.

Quoted 5 lines

Maybe libxl_device_disk_remove needs to call libxl_vdev_to_device_disk and check that the supplied disk struct is plausible somehow. In that case it might be nice for the caller to be able to fill in only the vdev.

If so we need to make clear in the documentation. I'm of course fine with this behaviour.

Jim, does libvirt (as an example of libxl user) actually cares specifying every fields in that struct? The other user (xl) doesn't seem to care that much.

Wei.

Quoted 1 line

Ian.

#12 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Quoted 39 lines · Wei Liu
Quoted 15 lines · Ian Jackson

Wei Liu writes ("[PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore"):

Quoted 2 lines

... if backend is not set by caller.

as far as it goes, but I think you may want a more radical change - see below.

Quoted 2 lines

Also change the function to use "goto" idiom while I was there.

(Although usually it would be better to split this kind of thing into a pre-patch, in this case it's small and easily reviewed.)

Quoted 9 lines

Is the backend type the only missing or potentially-wrong information ? ISTM that perhaps the caller might not know the target, either.

What should happen if the caller specifies a different target in disk to the one the device is actually using ? The documentation should specify which of the fields are important.

I'm not sure because it's not documented.

We should take a step back to define the important fields first.

Quoted 6 lines

Maybe libxl_device_disk_remove needs to call libxl_vdev_to_device_disk and check that the supplied disk struct is plausible somehow. In that case it might be nice for the caller to be able to fill in only the vdev.

Quoted 7 lines

If so we need to make clear in the documentation. I'm of course fine with this behaviour.

Jim, does libvirt (as an example of libxl user) actually cares specifying every fields in that struct? The other user (xl) doesn't seem to care that much.

At minimum, libvirt will populate the pdev_path, vdev, backend, and format fields. If backend and format (which, in libvirt-speack correspond to the 'name' and 'type' attributes on the optional <driver> element) are not specified, they are set to LIBXL_DISK_BACKEND_UNKNOWN and LIBXL_DISK_FORMAT_RAW respectively.

Regards,
Jim

#13 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Jim Fehlig writes ("Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore"):

Quoted 7 lines
Quoted 6 lines · Wei Liu
Quoted 3 lines · Ian Jackson

What should happen if the caller specifies a different target in disk to the one the device is actually using ? The documentation should specify which of the fields are important.

I'm not sure because it's not documented.

I know :-). I meant: what should we write in the documentation ? (And, obviously, implement.)

Quoted 1 line
Quoted 1 line

We should take a step back to define the important fields first.

Right.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Maybe libxl_device_disk_remove needs to call libxl_vdev_to_device_disk and check that the supplied disk struct is plausible somehow. In that case it might be nice for the caller to be able to fill in only the vdev.

If so we need to make clear in the documentation. I'm of course fine with this behaviour.

Well, feel free to object if you think my (rather vague) suggestion is wrong.

Quoted 9 lines
Quoted 3 lines

Jim, does libvirt (as an example of libxl user) actually cares specifying every fields in that struct? The other user (xl) doesn't seem to care that much.

At minimum, libvirt will populate the pdev_path, vdev, backend, and format fields. If backend and format (which, in libvirt-speack correspond to the 'name' and 'type' attributes on the optional <driver> element) are not specified, they are set to LIBXL_DISK_BACKEND_UNKNOWN and LIBXL_DISK_FORMAT_RAW respectively.

I think for fields libvirt has gone to the trouble of specifying, libxl should check that they match.

Ian.

author
#14 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

In reply to Jim Fehlig
Quoted 48 lines · Jim Fehlig
Quoted 39 lines · Wei Liu
Quoted 15 lines · Ian Jackson

Wei Liu writes ("[PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore"):

Quoted 2 lines

... if backend is not set by caller.

as far as it goes, but I think you may want a more radical change - see below.

Quoted 2 lines

Also change the function to use "goto" idiom while I was there.

(Although usually it would be better to split this kind of thing into a pre-patch, in this case it's small and easily reviewed.)

Quoted 9 lines

Is the backend type the only missing or potentially-wrong information ? ISTM that perhaps the caller might not know the target, either.

What should happen if the caller specifies a different target in disk to the one the device is actually using ? The documentation should specify which of the fields are important.

I'm not sure because it's not documented.

We should take a step back to define the important fields first.

Quoted 6 lines

Maybe libxl_device_disk_remove needs to call libxl_vdev_to_device_disk and check that the supplied disk struct is plausible somehow. In that case it might be nice for the caller to be able to fill in only the vdev.

Quoted 7 lines

If so we need to make clear in the documentation. I'm of course fine with this behaviour.

Jim, does libvirt (as an example of libxl user) actually cares specifying every fields in that struct? The other user (xl) doesn't seem to care that much.

Quoted 6 lines

At minimum, libvirt will populate the pdev_path, vdev, backend, and format fields. If backend and format (which, in libvirt-speack correspond to the 'name' and 'type' attributes on the optional <driver> element) are not specified, they are set to LIBXL_DISK_BACKEND_UNKNOWN and LIBXL_DISK_FORMAT_RAW respectively.

Since libvirt has a tendency of specifying everything, how come there is no "name" and "type" in <driver>? Can we actually generate all the fields needed when attaching a disk and store in libvirt's diskspec?

Wei.

Quoted 2 lines

Regards,
Jim

#15 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Quoted 10 lines · Wei Liu
Quoted 7 lines · Jim Fehlig

At minimum, libvirt will populate the pdev_path, vdev, backend, and format fields. If backend and format (which, in libvirt-speack correspond to the 'name' and 'type' attributes on the optional <driver> element) are not specified, they are set to LIBXL_DISK_BACKEND_UNKNOWN and LIBXL_DISK_FORMAT_RAW respectively.

Quoted 2 lines

Since libvirt has a tendency of specifying everything, how come there is no "name" and "type" in <driver>?

The <driver> element is optional. From
http://libvirt.org/formatdomain.html#elementsDisks

"|driver: |The optional driver element allows specifying further details related to the hypervisor driver used to provide the disk"

And when not specified, Ian C. recommended allowing libxl to pick suitable defaults

https://www.redhat.com/archives/libvir-list/2013-February/msg01126.html

Quoted 3 lines

Can we actually generate all the

fields needed when attaching a disk and store in libvirt's diskspec?

Yes, it was this way before the suggested change.

Regards,
Jim

author
#16 lore raw

Re: [PATCH 3/3] libxl: libxl__device_from_disk should retrieve backend from xenstore

Sorry for the late reply.

Quoted 23 lines · Jim Fehlig
Quoted 10 lines · Wei Liu
Quoted 7 lines · Jim Fehlig

At minimum, libvirt will populate the pdev_path, vdev, backend, and format fields. If backend and format (which, in libvirt-speack correspond to the 'name' and 'type' attributes on the optional <driver> element) are not specified, they are set to LIBXL_DISK_BACKEND_UNKNOWN and LIBXL_DISK_FORMAT_RAW respectively.

Quoted 2 lines

Since libvirt has a tendency of specifying everything, how come there is no "name" and "type" in <driver>?

The <driver> element is optional. From
http://libvirt.org/formatdomain.html#elementsDisks

"|driver: |The optional driver element allows specifying further details related to the hypervisor driver used to provide the disk"

And when not specified, Ian C. recommended allowing libxl to pick suitable defaults

Quoted 8 lines

https://www.redhat.com/archives/libvir-list/2013-February/msg01126.html

Quoted 3 lines

Can we actually generate all the

fields needed when attaching a disk and store in libvirt's diskspec?

Yes, it was this way before the suggested change.

I'm now of the opinion that we shouldn't change libxl__device_from_disk to fill in specific information, otherwise it becomes a special case for all the libxl__device_from_$foo functions.

And from the information you provide above it seems to be easily fixable on libvirt side -- you have all the information at hand when attaching the disk, so why not just store it for reuse later?

Ian C, what do you think?

Wei.

Quoted 2 lines

Regards,
Jim