From patchwork Tue Jul 1 07:52:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 365885 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A6AD71400AF for ; Tue, 1 Jul 2014 17:52:56 +1000 (EST) Received: from localhost ([::1]:39053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1ss2-0000UR-V9 for incoming@patchwork.ozlabs.org; Tue, 01 Jul 2014 03:52:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1srb-0008Uj-8V for qemu-devel@nongnu.org; Tue, 01 Jul 2014 03:52:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X1srU-0007Yx-WA for qemu-devel@nongnu.org; Tue, 01 Jul 2014 03:52:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:5665) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1srU-0007Yh-Nd for qemu-devel@nongnu.org; Tue, 01 Jul 2014 03:52:20 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s617qJNI018825 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 1 Jul 2014 03:52:19 -0400 Received: from localhost (ovpn-112-49.ams2.redhat.com [10.36.112.49]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s617qHMS025901; Tue, 1 Jul 2014 03:52:18 -0400 From: Stefan Hajnoczi To: Date: Tue, 1 Jul 2014 09:52:16 +0200 Message-Id: <1404201136-30412-1-git-send-email-stefanha@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Jeff Cody , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v8 for 2.1 4/4] block: add QAPI command to allow live backing file change X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jeff Cody This allows a user to make a live change to the backing file recorded in an open image. The image file to modify can be specified 2 ways: 1) image filename 2) image node-name Note: this does not cause the backing file itself to be reopened; it merely changes the backing filename in the image file structure, and in internal BDS structures. It is the responsibility of the user to pass a filename string that can be resolved when the image chain is reopened, and the filename string is not validated. A good analogy for this command is that it is a live version of 'qemu-img rebase -u', with respect to changing the backing file string. [Jeff is offline so I respun this patch in his absence. Dropped image filename since using node-name is preferred and this is a new command. No need to introduce the limitations of finding images by filename. --Stefan] Reviewed-by: Eric Blake Signed-off-by: Jeff Cody Signed-off-by: Stefan Hajnoczi Reviewed-by: Kevin Wolf --- blockdev.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 26 +++++++++++++++++ qmp-commands.hx | 39 ++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/blockdev.c b/blockdev.c index dae92bb..48bd9a3 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2388,6 +2388,85 @@ void qmp_block_job_complete(const char *device, Error **errp) block_job_complete(job, errp); } +void qmp_change_backing_file(const char *device, + const char *image_node_name, + const char *backing_file, + Error **errp) +{ + BlockDriverState *bs = NULL; + BlockDriverState *image_bs = NULL; + Error *local_err = NULL; + bool ro; + int open_flags; + int ret; + + /* find the top layer BDS of the chain */ + bs = bdrv_find(device); + if (!bs) { + error_set(errp, QERR_DEVICE_NOT_FOUND, device); + return; + } + + image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + if (!image_bs) { + error_setg(errp, "image file not found"); + return; + } + + if (bdrv_find_base(image_bs) == image_bs) { + error_setg(errp, "not allowing backing file change on an image " + "without a backing file"); + return; + } + + /* even though we are not necessarily operating on bs, we need it to + * determine if block ops are currently prohibited on the chain */ + if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { + return; + } + + /* final sanity check */ + if (!bdrv_chain_contains(bs, image_bs)) { + error_setg(errp, "'%s' and image file are not in the same chain", + device); + return; + } + + /* if not r/w, reopen to make r/w */ + open_flags = image_bs->open_flags; + ro = bdrv_is_read_only(image_bs); + + if (ro) { + bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + } + + ret = bdrv_change_backing_file(image_bs, backing_file, + image_bs->drv ? image_bs->drv->format_name : ""); + + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not change backing file to '%s'", + backing_file); + /* don't exit here, so we can try to restore open flags if + * appropriate */ + } + + if (ro) { + bdrv_reopen(image_bs, open_flags, &local_err); + if (local_err) { + error_propagate(errp, local_err); /* will preserve prior errp */ + } + } +} + void qmp_blockdev_add(BlockdevOptions *options, Error **errp) { QmpOutputVisitor *ov = qmp_output_visitor_new(); diff --git a/qapi/block-core.json b/qapi/block-core.json index c241967..e378653 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -680,6 +680,32 @@ 'data': 'BlockdevSnapshot' } ## +# @change-backing-file +# +# Change the backing file in the image file metadata. This does not +# cause QEMU to reopen the image file to reparse the backing filename +# (it may, however, perform a reopen to change permissions from +# r/o -> r/w -> r/o, if needed). The new backing file string is written +# into the image file metadata, and the QEMU internal strings are +# updated. +# +# @image-node-name: The name of the block driver state node of the +# image to modify. +# +# @device: The name of the device that owns image-node-name. +# +# @backing-file: The string to write as the backing file. This +# string is not validated, so care should be taken +# when specifying the string or the image chain may +# not be able to be reopened again. +# +# Since: 2.1 +## +{ 'command': 'change-backing-file', + 'data': { 'device': 'str', 'image-node-name': 'str', + 'backing-file': 'str' } } + +## # @block-commit # # Live commit of data from overlay image nodes into backing nodes - i.e., diff --git a/qmp-commands.hx b/qmp-commands.hx index f900dff..4be4765 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -1370,6 +1370,45 @@ Example: EQMP { + .name = "change-backing-file", + .args_type = "device:s,image-node-name:s,backing-file:s", + .mhandler.cmd_new = qmp_marshal_input_change_backing_file, + }, + +SQMP +change-backing-file +------------------- +Since: 2.1 + +Change the backing file in the image file metadata. This does not cause +QEMU to reopen the image file to reparse the backing filename (it may, +however, perform a reopen to change permissions from r/o -> r/w -> r/o, +if needed). The new backing file string is written into the image file +metadata, and the QEMU internal strings are updated. + +Arguments: + +- "image-node-name": The name of the block driver state node of the + image to modify. The "device" is argument is used to + verify "image-node-name" is in the chain described by + "device". + (json-string, optional) + +- "device": The name of the device. + (json-string) + +- "backing-file": The string to write as the backing file. This string is + not validated, so care should be taken when specifying + the string or the image chain may not be able to be + reopened again. + (json-string) + +Returns: Nothing on success + If "device" does not exist or cannot be determined, DeviceNotFound + +EQMP + + { .name = "balloon", .args_type = "value:M", .mhandler.cmd_new = qmp_marshal_input_balloon,