From patchwork Wed Jul 14 11:24:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 58876 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E095FB6F15 for ; Wed, 14 Jul 2010 21:37:57 +1000 (EST) Received: from localhost ([127.0.0.1]:42215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OZ0Hi-0006f8-S3 for incoming@patchwork.ozlabs.org; Wed, 14 Jul 2010 07:37:54 -0400 Received: from [140.186.70.92] (port=53532 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OZ04y-0006Ur-Ix for qemu-devel@nongnu.org; Wed, 14 Jul 2010 07:24:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OZ04x-0001TP-A2 for qemu-devel@nongnu.org; Wed, 14 Jul 2010 07:24:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36202) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OZ04x-0001TG-37 for qemu-devel@nongnu.org; Wed, 14 Jul 2010 07:24:43 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o6EBOY0q002138 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 14 Jul 2010 07:24:34 -0400 Received: from dhcp-5-188.str.redhat.com (vpn2-8-54.ams2.redhat.com [10.36.8.54]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o6EBOFBl023208; Wed, 14 Jul 2010 07:24:32 -0400 From: Kevin Wolf To: aurelien@aurel32.net Date: Wed, 14 Jul 2010 13:24:07 +0200 Message-Id: <1279106653-24351-9-git-send-email-kwolf@redhat.com> In-Reply-To: <1279106653-24351-1-git-send-email-kwolf@redhat.com> References: <1279106653-24351-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [STABLE][PATCH 08/14] block: Add bdrv_(p)write_sync X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add new functions that write and flush the written data to disk immediately. This is what needs to be used for image format metadata to maintain integrity for cache=... modes that don't use O_DSYNC. (Actually, we only need barriers, and therefore the functions are defined as such, but flushes is what is implemented in this patch - we can try to change that later) Signed-off-by: Kevin Wolf (cherry picked from commit f08145fe16470aca09304099888f68cfbc5d1de7) --- block.c | 39 +++++++++++++++++++++++++++++++++++++++ block.h | 4 ++++ block_int.h | 1 + 3 files changed, 44 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index 298414c..e3b3a55 100644 --- a/block.c +++ b/block.c @@ -452,6 +452,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); else open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); + + bs->open_flags = open_flags; if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) ret = -ENOTSUP; else @@ -779,6 +781,43 @@ int bdrv_pwrite(BlockDriverState *bs, int64_t offset, return count1; } +/* + * Writes to the file and ensures that no writes are reordered across this + * request (acts as a barrier) + * + * Returns 0 on success, -errno in error cases. + */ +int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, + const void *buf, int count) +{ + int ret; + + ret = bdrv_pwrite(bs, offset, buf, count); + if (ret < 0) { + return ret; + } + + /* No flush needed for cache=writethrough, it uses O_DSYNC */ + if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { + bdrv_flush(bs); + } + + return 0; +} + +/* + * Writes to the file and ensures that no writes are reordered across this + * request (acts as a barrier) + * + * Returns 0 on success, -errno in error cases. + */ +int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, + const uint8_t *buf, int nb_sectors) +{ + return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num, + buf, BDRV_SECTOR_SIZE * nb_sectors); +} + /** * Truncate file to 'offset' bytes (needed only for file protocols) */ diff --git a/block.h b/block.h index fa51ddf..762d88a 100644 --- a/block.h +++ b/block.h @@ -77,6 +77,10 @@ int bdrv_pread(BlockDriverState *bs, int64_t offset, void *buf, int count); int bdrv_pwrite(BlockDriverState *bs, int64_t offset, const void *buf, int count); +int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, + const void *buf, int count); +int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, + const uint8_t *buf, int nb_sectors); int bdrv_truncate(BlockDriverState *bs, int64_t offset); int64_t bdrv_getlength(BlockDriverState *bs); void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); diff --git a/block_int.h b/block_int.h index 9a3b2e0..631e8c5 100644 --- a/block_int.h +++ b/block_int.h @@ -127,6 +127,7 @@ struct BlockDriverState { int64_t total_sectors; /* if we are reading a disk image, give its size in sectors */ int read_only; /* if true, the media is read only */ + int open_flags; /* flags used to open the file, re-used for re-open */ int removable; /* if true, the media can be removed */ int locked; /* if true, the media cannot temporarily be ejected */ int encrypted; /* if true, the media is encrypted */