From patchwork Mon Nov 14 21:09:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 125614 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1C671B71F8 for ; Tue, 15 Nov 2011 08:27:23 +1100 (EST) Received: from localhost ([::1]:60505 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RQ43h-0006Ku-Pk for incoming@patchwork.ozlabs.org; Mon, 14 Nov 2011 16:27:17 -0500 Received: from eggs.gnu.org ([140.186.70.92]:59779) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RQ43X-0005y5-Md for qemu-devel@nongnu.org; Mon, 14 Nov 2011 16:27:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RQ3oc-00031F-5g for qemu-devel@nongnu.org; Mon, 14 Nov 2011 16:11:43 -0500 Received: from e37.co.us.ibm.com ([32.97.110.158]:39540) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RQ3ob-0002vV-VE for qemu-devel@nongnu.org; Mon, 14 Nov 2011 16:11:42 -0500 Received: from /spool/local by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 14 Nov 2011 14:10:59 -0700 Received: from d03relay04.boulder.ibm.com ([9.17.195.106]) by e37.co.us.ibm.com ([192.168.1.137]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 14 Nov 2011 14:10:16 -0700 Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pAEL9vnZ026718 for ; Mon, 14 Nov 2011 14:09:58 -0700 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pAEL9uaU000569 for ; Mon, 14 Nov 2011 14:09:57 -0700 Received: from titi.austin.rr.com (sig-9-65-229-191.mts.ibm.com [9.65.229.191]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pAEL9nce032518; Mon, 14 Nov 2011 14:09:55 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 14 Nov 2011 15:09:46 -0600 Message-Id: <1321304987-23041-4-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1321304987-23041-1-git-send-email-aliguori@us.ibm.com> References: <1321304987-23041-1-git-send-email-aliguori@us.ibm.com> x-cbid: 11111421-7408-0000-0000-000000D01A12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.158 Cc: Kevin Wolf , Lucas Meneghel Rodrigues , Anthony Liguori , Stefan Hajnoczi , Juan Quintela , Avi Kivity Subject: [Qemu-devel] [PATCH 4/5] qcow2: implement bdrv_invalidate_cache (v2) 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 We don't reopen the actual file, but instead invoke the close and open routines. We specifically ignore the backing file since it's contents are read-only and therefore immutable. Signed-off-by: Anthony Liguori --- v1 -> v2 - make invalidate_cache work with encrypted block devices --- block/qcow2.c | 34 ++++++++++++++++++++++++++++++++++ block/qcow2.h | 2 ++ 2 files changed, 36 insertions(+), 0 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 5c784ee..d7805ce 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -240,6 +240,7 @@ static int qcow2_open(BlockDriverState *bs, int flags) s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size + 512); s->cluster_cache_offset = -1; + s->flags = flags; ret = qcow2_refcount_init(bs); if (ret != 0) { @@ -632,6 +633,37 @@ static void qcow2_close(BlockDriverState *bs) qcow2_refcount_close(bs); } +static void qcow2_invalidate_cache(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + int flags = s->flags; + AES_KEY aes_encrypt_key; + AES_KEY aes_decrypt_key; + uint32_t crypt_method = 0; + + /* + * Backing files are read-only which makes all of their metadata immutable, + * that means we don't have to worry about reopening them here. + */ + + if (s->crypt_method) { + crypt_method = s->crypt_method; + memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key)); + memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key)); + } + + qcow2_close(bs); + + memset(s, 0, sizeof(BDRVQcowState)); + qcow2_open(bs, flags); + + if (crypt_method) { + s->crypt_method = crypt_method; + memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key)); + memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key)); + } +} + /* * Updates the variable length parts of the qcow2 header, i.e. the backing file * name and all extensions. qcow2 was not designed to allow such changes, so if @@ -1269,6 +1301,8 @@ static BlockDriver bdrv_qcow2 = { .bdrv_change_backing_file = qcow2_change_backing_file, + .bdrv_invalidate_cache = qcow2_invalidate_cache, + .create_options = qcow2_create_options, .bdrv_check = qcow2_check, }; diff --git a/block/qcow2.h b/block/qcow2.h index 531af39..4e44eea 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -125,6 +125,8 @@ typedef struct BDRVQcowState { int snapshots_size; int nb_snapshots; QCowSnapshot *snapshots; + + int flags; } BDRVQcowState; /* XXX: use std qcow open function ? */