From patchwork Fri Jun 22 15:08:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 166635 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 21DA5B6F13 for ; Sat, 23 Jun 2012 01:09:36 +1000 (EST) Received: from localhost ([::1]:36798 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si5UL-0006L6-OM for incoming@patchwork.ozlabs.org; Fri, 22 Jun 2012 11:09:33 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54277) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si5Tu-0005Xh-VZ for qemu-devel@nongnu.org; Fri, 22 Jun 2012 11:09:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Si5Tp-0007st-TM for qemu-devel@nongnu.org; Fri, 22 Jun 2012 11:09:06 -0400 Received: from e06smtp18.uk.ibm.com ([195.75.94.114]:39335) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Si5Tp-0007sY-Ku for qemu-devel@nongnu.org; Fri, 22 Jun 2012 11:09:01 -0400 Received: from /spool/local by e06smtp18.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 22 Jun 2012 16:08:58 +0100 Received: from d06nrmr1307.portsmouth.uk.ibm.com (9.149.38.129) by e06smtp18.uk.ibm.com (192.168.101.148) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 22 Jun 2012 16:08:55 +0100 Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5MF8tuc2756676 for ; Fri, 22 Jun 2012 16:08:55 +0100 Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5MEsiJI005275 for ; Fri, 22 Jun 2012 10:54:44 -0400 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.145]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q5MEsiBO005265; Fri, 22 Jun 2012 10:54:44 -0400 From: Stefan Hajnoczi To: Kevin Wolf Date: Fri, 22 Jun 2012 16:08:41 +0100 Message-Id: <1340377726-5896-3-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10 In-Reply-To: <1340377726-5896-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1340377726-5896-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12062215-6892-0000-0000-00000237B516 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.114 Cc: qemu-devel@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [RFC 2/7] qcow2: introduce dirty bit 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 This patch adds an incompatible feature bit to mark images that have not been closed cleanly. When a dirty image file is opened a consistency check and repair is performed. Signed-off-by: Stefan Hajnoczi --- block/qcow2.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- block/qcow2.h | 3 +++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 2c1cd0a..cc30784 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -214,6 +214,27 @@ static void report_unsupported_feature(BlockDriverState *bs, } } +/* + * Clears the dirty bit and flushes before if necessary. Only call this + * function when there are no pending requests, it does not guard against + * concurrent requests dirtying the image. + */ +static int qcow2_mark_clean(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + + if (s->incompatible_features & QCOW2_INCOMPATIBLE_FEAT_DIRTY) { + int ret = bdrv_flush(bs); + if (ret < 0) { + return ret; + } + + s->incompatible_features &= ~QCOW2_INCOMPATIBLE_FEAT_DIRTY; + return qcow2_update_header(bs); + } + return 0; +} + static int qcow2_open(BlockDriverState *bs, int flags) { BDRVQcowState *s = bs->opaque; @@ -287,12 +308,13 @@ static int qcow2_open(BlockDriverState *bs, int flags) s->compatible_features = header.compatible_features; s->autoclear_features = header.autoclear_features; - if (s->incompatible_features != 0) { + if (s->incompatible_features & ~QCOW2_INCOMPATIBLE_FEAT_MASK) { void *feature_table = NULL; qcow2_read_extensions(bs, header.header_length, ext_end, &feature_table); report_unsupported_feature(bs, feature_table, - s->incompatible_features); + s->incompatible_features & + ~QCOW2_INCOMPATIBLE_FEAT_MASK); ret = -ENOTSUP; goto fail; } @@ -412,6 +434,22 @@ static int qcow2_open(BlockDriverState *bs, int flags) /* Initialise locks */ qemu_co_mutex_init(&s->lock); + /* Repair image if dirty */ + if ((s->incompatible_features & QCOW2_INCOMPATIBLE_FEAT_DIRTY) && + !bs->read_only) { + BdrvCheckResult result = {0}; + + ret = qcow2_check_refcounts(bs, &result, BDRV_FIX_ERRORS); + if (ret < 0) { + goto fail; + } + + ret = qcow2_mark_clean(bs); + if (ret < 0) { + goto fail; + } + } + #ifdef DEBUG_ALLOC { BdrvCheckResult result = {0}; @@ -788,6 +826,8 @@ static void qcow2_close(BlockDriverState *bs) qcow2_cache_flush(bs, s->l2_table_cache); qcow2_cache_flush(bs, s->refcount_block_cache); + qcow2_mark_clean(bs); + qcow2_cache_destroy(bs, s->l2_table_cache); qcow2_cache_destroy(bs, s->refcount_block_cache); diff --git a/block/qcow2.h b/block/qcow2.h index 455b6d7..5c7cfb6 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -108,6 +108,9 @@ enum { QCOW2_FEAT_TYPE_INCOMPATIBLE = 0, QCOW2_FEAT_TYPE_COMPATIBLE = 1, QCOW2_FEAT_TYPE_AUTOCLEAR = 2, + + QCOW2_INCOMPATIBLE_FEAT_DIRTY = 0x1, + QCOW2_INCOMPATIBLE_FEAT_MASK = QCOW2_INCOMPATIBLE_FEAT_DIRTY, }; typedef struct Qcow2Feature {