From patchwork Fri Jun 18 15:03:24 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 56198 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 271A5B7D4F for ; Sat, 19 Jun 2010 01:07:09 +1000 (EST) Received: from localhost ([127.0.0.1]:53275 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPd9u-0000xO-3W for incoming@patchwork.ozlabs.org; Fri, 18 Jun 2010 11:07:06 -0400 Received: from [140.186.70.92] (port=52861 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPd6f-0007re-N9 for qemu-devel@nongnu.org; Fri, 18 Jun 2010 11:03:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OPd6e-0007La-7Q for qemu-devel@nongnu.org; Fri, 18 Jun 2010 11:03:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54425) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OPd6d-0007LP-Vp for qemu-devel@nongnu.org; Fri, 18 Jun 2010 11:03:44 -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 o5IF3h2D018979 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 18 Jun 2010 11:03:43 -0400 Received: from localhost.localdomain (vpn1-6-103.ams2.redhat.com [10.36.6.103]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o5IF3dsV028608; Fri, 18 Jun 2010 11:03:42 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Fri, 18 Jun 2010 17:03:24 +0200 Message-Id: <1276873409-8235-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1276873409-8235-1-git-send-email-kwolf@redhat.com> References: <1276873409-8235-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 Subject: [Qemu-devel] [PATCH 1/6] 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 --- block.c | 37 +++++++++++++++++++++++++++++++++++++ block.h | 4 ++++ 2 files changed, 41 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index 0765fbc..7b64c2d 100644 --- a/block.c +++ b/block.c @@ -1010,6 +1010,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 9df9b38..6a157f4 100644 --- a/block.h +++ b/block.h @@ -80,6 +80,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);