From mboxrd@z Thu Jan 1 00:00:00 1970 From: Wei Liu Subject: [PATCH v2 13/18] libxl: make libxl_cd_insert "eject" + "insert" Date: Wed, 30 Jul 2014 19:23:54 +0100 Message-ID: <1406744639-28782-14-git-send-email-wei.liu2@citrix.com> References: <1406744639-28782-1-git-send-email-wei.liu2@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1406744639-28782-1-git-send-email-wei.liu2@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: Wei Liu , ian.jackson@eu.citrix.com, ian.campbell@citrix.com List-Id: xen-devel@lists.xenproject.org A "cdrom insert" is always processed as "eject" + "insert", with JSON config updated in between. So that we can know the correct state of CDROM later when we try to retrieve domain configuration: if xenstore is "empty", then CDROM is "empty"; otherwise use the information presented in JSON. Signed-off-by: Wei Liu --- tools/libxl/libxl.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 430a45f..d64d8f1 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -2640,8 +2640,9 @@ int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid, return 0; } -int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk, - const libxl_asyncop_how *ao_how) +static int libxl__cdrom_insert(libxl_ctx *ctx, uint32_t domid, + libxl_device_disk *disk, + const libxl_asyncop_how *ao_how) { AO_CREATE(ctx, domid, ao_how); int num = 0, i; @@ -2752,6 +2753,58 @@ out: return AO_INPROGRESS; } +/* + * A "cdrom insert" is always processed as "eject" + "insert", with + * updating JSON in between. So that we can know the current state of + * CDROM later when we try to retrieve domain configuration: if + * xenstore is "empty", then CDROM is "empty"; otherwise use the image + * in JSON. + * + * In order to maintain lock hierachy, CTX lock is taken when entering + * this function. + */ +int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk, + const libxl_asyncop_how *ao_how) +{ + GC_INIT(ctx); + libxl_device_disk empty; + int rc; + libxl__carefd *lock = NULL; + + CTX_LOCK; + + libxl_device_disk_init(&empty); + empty.format = LIBXL_DISK_FORMAT_EMPTY; + empty.vdev = libxl__strdup(NOGC, disk->vdev); + empty.is_cdrom = 1; + + rc = libxl__cdrom_insert(ctx, domid, &empty, NULL); + if (rc) + goto out; + + /* Optimisation: don't insert empty disk twice, and skip + * manipulating JSON. */ + if (disk->format == LIBXL_DISK_FORMAT_EMPTY) + goto out; + + lock = libxl__lock_domain_data(gc, domid); + if (!lock) { + rc = ERROR_LOCK_FAIL; + goto out; + } + rc = DEVICE_UPDATE_JSON(disk, disks, num_disks, domid, disk, + COMPARE_DISK); + libxl__unlock_domain_data(lock); + + rc = libxl__cdrom_insert(ctx, domid, disk, ao_how); + +out: + libxl_device_disk_dispose(&empty); + CTX_UNLOCK; + GC_FREE; + return rc; +} + /* libxl__alloc_vdev only works on the local domain, that is the domain * where the toolstack is running */ static char * libxl__alloc_vdev(libxl__gc *gc, void *get_vdev_user, -- 1.7.10.4