From patchwork Mon Aug 1 09:39:07 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 107704 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 39738B6F69 for ; Mon, 1 Aug 2011 19:39:29 +1000 (EST) Received: from localhost ([::1]:49423 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qnoy5-0000zn-GC for incoming@patchwork.ozlabs.org; Mon, 01 Aug 2011 05:39:25 -0400 Received: from eggs.gnu.org ([140.186.70.92]:39575) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qnoxy-0000zW-9X for qemu-devel@nongnu.org; Mon, 01 Aug 2011 05:39:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qnoxw-0002ez-Hf for qemu-devel@nongnu.org; Mon, 01 Aug 2011 05:39:18 -0400 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:45864) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qnoxw-0002eT-Ac for qemu-devel@nongnu.org; Mon, 01 Aug 2011 05:39:16 -0400 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p719dEtP009631 for ; Mon, 1 Aug 2011 09:39:14 GMT Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p719dEQE1900680 for ; Mon, 1 Aug 2011 10:39:14 +0100 Received: from d06av07.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p719dD1w009096 for ; Mon, 1 Aug 2011 03:39:13 -0600 Received: from stefanha-thinkpad.manchester-maybrook.uk.ibm.com (dyn-9-174-219-30.manchester-maybrook.uk.ibm.com [9.174.219.30]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p719dD9x009083; Mon, 1 Aug 2011 03:39:13 -0600 From: Stefan Hajnoczi To: Date: Mon, 1 Aug 2011 10:39:07 +0100 Message-Id: <1312191547-12777-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.5.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.162 Cc: Kevin Wolf , Andrew Theurer , Christoph Hellwig , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH] block: add cache=directsync parameter to -drive 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 -drive cache=directsync for O_DIRECT | O_SYNC host file I/O with no disk write cache presented to the guest. This mode is useful when guests may not be sending flushes when appropriate and therefore leave data at risk in case of power failure. When cache=directsync is used, write operations are only completed to the guest when data is safely on disk. This new mode is like cache=writethrough but it bypasses the host page cache. Signed-off-by: Stefan Hajnoczi --- block.c | 4 ++-- blockdev.c | 2 ++ qemu-config.c | 3 ++- qemu-options.hx | 8 ++++++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/block.c b/block.c index 9549b9e..bbf9fa7 100644 --- a/block.c +++ b/block.c @@ -1100,8 +1100,8 @@ int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, return ret; } - /* No flush needed for cache=writethrough, it uses O_DSYNC */ - if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { + /* No flush needed for cache modes that use O_DSYNC */ + if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { bdrv_flush(bs); } diff --git a/blockdev.c b/blockdev.c index 0b8d3a4..9f7275c 100644 --- a/blockdev.c +++ b/blockdev.c @@ -323,6 +323,8 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi) if ((buf = qemu_opt_get(opts, "cache")) != NULL) { if (!strcmp(buf, "off") || !strcmp(buf, "none")) { bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; + } else if (!strcmp(buf, "directsync")) { + bdrv_flags |= BDRV_O_NOCACHE; } else if (!strcmp(buf, "writeback")) { bdrv_flags |= BDRV_O_CACHE_WB; } else if (!strcmp(buf, "unsafe")) { diff --git a/qemu-config.c b/qemu-config.c index 1eb6b9a..139e077 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -55,7 +55,8 @@ static QemuOptsList qemu_drive_opts = { },{ .name = "cache", .type = QEMU_OPT_STRING, - .help = "host cache usage (none, writeback, writethrough, unsafe)", + .help = "host cache usage (none, writeback, writethrough, " + "directsync, unsafe)", },{ .name = "aio", .type = QEMU_OPT_STRING, diff --git a/qemu-options.hx b/qemu-options.hx index d86815d..35d95d1 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -133,7 +133,7 @@ ETEXI DEF("drive", HAS_ARG, QEMU_OPTION_drive, "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n" " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n" - " [,cache=writethrough|writeback|none|unsafe][,format=f]\n" + " [,cache=writethrough|writeback|none|directsync|unsafe][,format=f]\n" " [,serial=s][,addr=A][,id=name][,aio=threads|native]\n" " [,readonly=on|off]\n" " use 'file' as a drive image\n", QEMU_ARCH_ALL) @@ -164,7 +164,7 @@ These options have the same definition as they have in @option{-hdachs}. @item snapshot=@var{snapshot} @var{snapshot} is "on" or "off" and allows to enable snapshot for given drive (see @option{-snapshot}). @item cache=@var{cache} -@var{cache} is "none", "writeback", "unsafe", or "writethrough" and controls how the host cache is used to access block data. +@var{cache} is "none", "writeback", "unsafe", "directsync" or "writethrough" and controls how the host cache is used to access block data. @item aio=@var{aio} @var{aio} is "threads", or "native" and selects between pthread based disk I/O and native Linux AIO. @item format=@var{format} @@ -199,6 +199,10 @@ The host page cache can be avoided entirely with @option{cache=none}. This will attempt to do disk IO directly to the guests memory. QEMU may still perform an internal copy of the data. +The host page cache can be avoided while only sending write notifications to +the guest when the data has been reported as written by the storage subsystem +using @option{cache=directsync}. + Some block drivers perform badly with @option{cache=writethrough}, most notably, qcow2. If performance is more important than correctness, @option{cache=writeback} should be used with qcow2.