libxl: merge libxl__device_del into libxl__device_remove
authorIan Campbell <ian.campbell@citrix.com>
Tue, 18 Oct 2011 12:36:42 +0000 (13:36 +0100)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 18 Oct 2011 12:36:42 +0000 (13:36 +0100)
Note that the "wait" parameter added to libxl_device_remove is different to the
wait paramter previously used by similar functions. In the past not-wait meant
forced whereas now in means wait for a graceful shutdown, as opposed to setting
off a graceful shutdown but not waiting.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson.citrix.com>
Committed-by: Ian Jackson <ian.jackson.citrix.com>
tools/libxl/libxl.c
tools/libxl/libxl_device.c
tools/libxl/libxl_internal.h

index b5ce3dad60ea4a5f0d3827c7e79bd5fbf43e432f..f3a6fe0a77adcf5b2fd553e42b34c27b6bd93cb9 100644 (file)
@@ -1075,7 +1075,7 @@ int libxl_device_disk_del(libxl_ctx *ctx, uint32_t domid,
     device.devid            = devid;
     device.kind             = LIBXL__DEVICE_KIND_VBD;
     if (wait)
-        rc = libxl__device_del(&gc, &device);
+        rc = libxl__device_remove(&gc, &device, wait);
     else
         rc = libxl__device_destroy(&gc, &device);
 out_free:
@@ -1290,7 +1290,7 @@ int libxl_device_nic_del(libxl_ctx *ctx, uint32_t domid,
     device.kind             = LIBXL__DEVICE_KIND_VIF;
 
     if (wait)
-        rc = libxl__device_del(&gc, &device);
+        rc = libxl__device_remove(&gc, &device, wait);
     else
         rc = libxl__device_destroy(&gc, &device);
 
index d4206293cbda4ee142b5fa73eb6e061394d4ab9d..16ee508f7e2d43c398d643b4a8f1a8c07ac4fe54 100644 (file)
@@ -367,7 +367,39 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
-int libxl__device_remove(libxl__gc *gc, libxl__device *dev)
+static int wait_for_dev_destroy(libxl__gc *gc, struct timeval *tv)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    int nfds, rc;
+    unsigned int n;
+    fd_set rfds;
+    char **l1 = NULL;
+
+    rc = 1;
+    nfds = xs_fileno(ctx->xsh) + 1;
+    FD_ZERO(&rfds);
+    FD_SET(xs_fileno(ctx->xsh), &rfds);
+    if (select(nfds, &rfds, NULL, NULL, tv) > 0) {
+        l1 = xs_read_watch(ctx->xsh, &n);
+        if (l1 != NULL) {
+            char *state = libxl__xs_read(gc, XBT_NULL, l1[XS_WATCH_PATH]);
+            if (!state || atoi(state) == 6) {
+                xs_unwatch(ctx->xsh, l1[0], l1[1]);
+                xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]);
+                LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Destroyed device backend at %s", l1[XS_WATCH_TOKEN]);
+                rc = 0;
+            }
+            free(l1);
+        }
+    }
+    return rc;
+}
+
+/*
+ * Returns 0 (device already destroyed) or 1 (caller must
+ * wait_for_dev_destroy) on success, ERROR_* on fail.
+ */
+int libxl__device_remove(libxl__gc *gc, libxl__device *dev, int wait)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     xs_transaction_t t;
@@ -392,14 +424,24 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
         else {
-            rc = -1;
+            rc = ERROR_FAIL;
             goto out;
         }
     }
 
     xs_watch(ctx->xsh, state_path, be_path);
     libxl__device_destroy_tapdisk(gc, be_path);
-    rc = 1;
+
+    if (wait) {
+        struct timeval tv;
+        tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
+        tv.tv_usec = 0;
+        (void)wait_for_dev_destroy(gc, &tv);
+        xs_rm(ctx->xsh, XBT_NULL, libxl__device_frontend_path(gc, dev));
+    } else {
+        rc = 1; /* Caller must wait_for_dev_destroy */
+    }
+
 out:
     return rc;
 }
@@ -418,34 +460,6 @@ int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
     return 0;
 }
 
-static int wait_for_dev_destroy(libxl__gc *gc, struct timeval *tv)
-{
-    libxl_ctx *ctx = libxl__gc_owner(gc);
-    int nfds, rc;
-    unsigned int n;
-    fd_set rfds;
-    char **l1 = NULL;
-
-    rc = 1;
-    nfds = xs_fileno(ctx->xsh) + 1;
-    FD_ZERO(&rfds);
-    FD_SET(xs_fileno(ctx->xsh), &rfds);
-    if (select(nfds, &rfds, NULL, NULL, tv) > 0) {
-        l1 = xs_read_watch(ctx->xsh, &n);
-        if (l1 != NULL) {
-            char *state = libxl__xs_read(gc, XBT_NULL, l1[XS_WATCH_PATH]);
-            if (!state || atoi(state) == 6) {
-                xs_unwatch(ctx->xsh, l1[0], l1[1]);
-                xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]);
-                LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Destroyed device backend at %s", l1[XS_WATCH_TOKEN]);
-                rc = 0;
-            }
-            free(l1);
-        }
-    }
-    return rc;
-}
-
 int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
@@ -485,8 +499,12 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force)
                 if (force) {
                     libxl__device_destroy(gc, &dev);
                 } else {
-                    if (libxl__device_remove(gc, &dev) > 0)
-                        n_watches++;
+                    int rc = libxl__device_remove(gc, &dev, 0);
+                    if (rc < 0)
+                        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                                   "cannot remove device %s\n", path);
+                    else
+                        n_watches += rc;
                 }
             }
         }
@@ -504,8 +522,12 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force)
         if (force) {
             libxl__device_destroy(gc, &dev);
         } else {
-            if (libxl__device_remove(gc, &dev) > 0)
-                n_watches++;
+            int rc = libxl__device_remove(gc, &dev, 0);
+            if (rc < 0)
+                LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                           "cannot remove device %s\n", path);
+            else
+                n_watches += rc;
         }
     }
 
@@ -530,29 +552,6 @@ out:
     return 0;
 }
 
-int libxl__device_del(libxl__gc *gc, libxl__device *dev)
-{
-    libxl_ctx *ctx = libxl__gc_owner(gc);
-    struct timeval tv;
-    int rc;
-
-    rc = libxl__device_remove(gc, dev);
-    if (rc == -1) {
-        rc = ERROR_FAIL;
-        goto out;
-    }
-
-    tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
-    tv.tv_usec = 0;
-    (void)wait_for_dev_destroy(gc, &tv);
-
-    xs_rm(ctx->xsh, XBT_NULL, libxl__device_frontend_path(gc, dev));
-    rc = 0;
-
-out:
-    return rc;
-}
-
 int libxl__wait_for_device_model(libxl__gc *gc,
                                  uint32_t domid, char *state,
                                  libxl__spawn_starting *spawning,
index 93dcefd81e0c899f8dbcc72b2b60c78b99799c85..d1a2fe20561231120514b99289c45939ae9550a1 100644 (file)
@@ -242,8 +242,7 @@ _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device);
 _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device);
 _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
                                       libxl__device *dev);
-_hidden int libxl__device_del(libxl__gc *gc, libxl__device *dev);
-_hidden int libxl__device_remove(libxl__gc *gc, libxl__device *dev);
+_hidden int libxl__device_remove(libxl__gc *gc, libxl__device *dev, int wait);
 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
 _hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force);
 _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);