From mboxrd@z Thu Jan 1 00:00:00 1970 From: Roger Pau Monne Subject: [PATCH 6/9] libxl: check backend state before setting it to "closing" Date: Wed, 11 Jul 2012 11:23:49 +0100 Message-ID: <1342002232-75531-7-git-send-email-roger.pau@citrix.com> References: <1342002232-75531-1-git-send-email-roger.pau@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1342002232-75531-1-git-send-email-roger.pau@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: xen-devel@lists.xen.org Cc: Ian Jackson , Roger Pau Monne List-Id: xen-devel@lists.xenproject.org Check backend state before unconditionally setting it to "closing" (5), since it might already be in "closed" (6). Cc: Ian Jackson Signed-off-by: Roger Pau Monne --- tools/libxl/libxl_device.c | 53 +++++++++++++++++++++++++++++++------------ 1 files changed, 38 insertions(+), 15 deletions(-) diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 8ae2bb5..18002a0 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -681,10 +681,11 @@ void libxl__initiate_device_remove(libxl__egc *egc, libxl__ao_device *aodev) { STATE_AO_GC(aodev->ao); - libxl_ctx *ctx = libxl__gc_owner(gc); xs_transaction_t t = 0; char *be_path = libxl__device_backend_path(gc, aodev->dev); char *state_path = libxl__sprintf(gc, "%s/state", be_path); + char *online_path = GCSPRINTF("%s/online", be_path); + const char *state; int rc = 0; /* We init this here because we might call device_hotplug_done @@ -701,23 +702,44 @@ void libxl__initiate_device_remove(libxl__egc *egc, goto out; } -retry_transaction: - t = xs_transaction_start(ctx->xsh); - if (aodev->force) - libxl__xs_path_cleanup(gc, t, - libxl__device_frontend_path(gc, aodev->dev)); - xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0")); - xs_write(ctx->xsh, t, state_path, "5", strlen("5")); - if (!xs_transaction_end(ctx->xsh, t, 0)) { - if (errno == EAGAIN) - goto retry_transaction; - else { - rc = ERROR_FAIL; + for (;;) { + rc = libxl__xs_transaction_start(gc, &t); + if (rc) { + LOG(ERROR, "unable to start transaction"); + goto out; + } + + rc = libxl__xs_read_checked(gc, t, state_path, &state); + if (rc) { + LOG(ERROR, "unable to read device state from path %s", state_path); goto out; } + + if (aodev->force) + libxl__xs_path_cleanup(gc, t, + libxl__device_frontend_path(gc, aodev->dev)); + + /* + * Check if device is already in "closed" state, in which case + * it should not be changed. + */ + if (atoi(state) != XenbusStateClosed) { + rc = libxl__xs_write_checked(gc, t, online_path, "0"); + if (rc) { + LOG(ERROR, "unable to write to xenstore path %s", online_path); + goto out; + } + rc = libxl__xs_write_checked(gc, t, state_path, "5"); + if (rc) { + LOG(ERROR, "unable to write to xenstore path %s", state_path); + goto out; + } + } + + rc = libxl__xs_transaction_commit(gc, &t); + if (!rc) break; + if (rc < 0) goto out; } - /* mark transaction as ended, to prevent double closing it on out_ok */ - t = 0; libxl__device_destroy_tapdisk(gc, be_path); @@ -733,6 +755,7 @@ retry_transaction: return; out: + libxl__xs_transaction_abort(gc, &t); aodev->rc = rc; device_hotplug_done(egc, aodev); return; -- 1.7.7.5 (Apple Git-26)