File kvm-qemu-pre0.13-flush-unsafe.patch of Package kvm

From 016f5cf6ff465411733878a17c8f8febb7668321 Mon Sep 17 00:00:00 2001
From: Alexander Graf <agraf@suse.de>
Date: Wed, 26 May 2010 17:51:49 +0200
Subject: [PATCH] Add cache=unsafe parameter to -drive

Usually the guest can tell the host to flush data to disk. In some cases we
don't want to flush though, but try to keep everything in cache.

So let's add a new cache value to -drive that allows us to set the cache
policy to most aggressive, disabling flushes. We call this mode "unsafe",
as guest data is not guaranteed to survive host crashes anymore.

This patch also adds a noop function for aio, so we can do nothing in AIO
fashion.

Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
---
 block.c         |   29 +++++++++++++++++++++++++++++
 block.h         |    1 +
 qemu-config.c   |    2 +-
 qemu-options.hx |   13 ++++++++++---
 vl.c            |    3 +++
 5 files changed, 44 insertions(+), 4 deletions(-)

Index: qemu-kvm-0.12.3/block.c
===================================================================
--- qemu-kvm-0.12.3.orig/block.c
+++ qemu-kvm-0.12.3/block.c
@@ -50,6 +50,8 @@ static BlockDriverAIOCB *bdrv_aio_writev
         BlockDriverCompletionFunc *cb, void *opaque);
 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
         BlockDriverCompletionFunc *cb, void *opaque);
+static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque);
 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
                         uint8_t *buf, int nb_sectors);
 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
@@ -433,6 +435,7 @@ int bdrv_open2(BlockDriverState *bs, con
     }
     bs->drv = drv;
     bs->opaque = qemu_mallocz(drv->instance_size);
+    bs->open_flags = flags;
 
     /*
      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
@@ -1137,6 +1140,11 @@ void bdrv_flush(BlockDriverState *bs)
 {
     if (!bs->drv)
         return;
+
+    if (bs->open_flags & BDRV_O_NO_FLUSH) {
+        return;
+    }
+
     if (bs->drv->bdrv_flush)
         bs->drv->bdrv_flush(bs);
     if (bs->backing_hd)
@@ -1855,6 +1863,10 @@ BlockDriverAIOCB *bdrv_aio_flush(BlockDr
 {
     BlockDriver *drv = bs->drv;
 
+    if (bs->open_flags & BDRV_O_NO_FLUSH) {
+        return bdrv_aio_noop_em(bs, cb, opaque);
+    }
+
     if (!drv)
         return NULL;
 
@@ -1973,6 +1985,25 @@ static BlockDriverAIOCB *bdrv_aio_flush_
     qemu_bh_schedule(acb->bh);
     return &acb->common;
 }
+
+static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    BlockDriverAIOCBSync *acb;
+
+    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
+    acb->is_write = 1; /* don't bounce in the completion handler */
+    acb->qiov = NULL;
+    acb->bounce = NULL;
+    acb->ret = 0;
+
+    if (!acb->bh) {
+        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
+    }
+
+    qemu_bh_schedule(acb->bh);
+    return &acb->common;
+}
 
 /**************************************************************/
 /* sync block device emulation */
Index: qemu-kvm-0.12.3/block.h
===================================================================
--- qemu-kvm-0.12.3.orig/block.h
+++ qemu-kvm-0.12.3/block.h
@@ -39,6 +39,7 @@ typedef struct QEMUSnapshotInfo {
 #define BDRV_O_NOCACHE     0x0020 /* do not use the host page cache */
 #define BDRV_O_CACHE_WB    0x0040 /* use write-back caching */
 #define BDRV_O_NATIVE_AIO  0x0080 /* use native AIO instead of the thread pool */
+#define BDRV_O_NO_FLUSH    0x0200 /* disable flushing on this disk */
 
 #define BDRV_O_CACHE_MASK  (BDRV_O_NOCACHE | BDRV_O_CACHE_WB)
 
Index: qemu-kvm-0.12.3/qemu-config.c
===================================================================
--- qemu-kvm-0.12.3.orig/qemu-config.c
+++ qemu-kvm-0.12.3/qemu-config.c
@@ -53,7 +53,7 @@ QemuOptsList qemu_drive_opts = {
         },{
             .name = "cache",
             .type = QEMU_OPT_STRING,
-            .help = "host cache usage (none, writeback, writethrough)",
+            .help = "host cache usage (none, writeback, writethrough, unsafe)",
         },{
             .name = "aio",
             .type = QEMU_OPT_STRING,
Index: qemu-kvm-0.12.3/qemu-options.hx
===================================================================
--- qemu-kvm-0.12.3.orig/qemu-options.hx
+++ qemu-kvm-0.12.3/qemu-options.hx
@@ -102,7 +102,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][,format=f][,serial=s]\n"
+    "       [,cache=writethrough|writeback|unsafe|none][,format=f][,serial=s]\n"
     "       [,addr=A][,id=name][,aio=threads|native]\n"
     "       [,boot=on|off]\n"
     "                use 'file' as a drive image\n")
@@ -139,7 +139,7 @@ These options have the same definition a
 @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", or "writethrough" and controls how the host cache is used to access block data.
+@var{cache} is "none", "writeback", "unsafe", 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}
@@ -173,6 +173,12 @@ Some block drivers perform badly with @o
 qcow2.  If performance is more important than correctness,
 @option{cache=writeback} should be used with qcow2.
 
+In case you don't care about data integrity over host failures, use
+cache=unsafe. This option tells qemu that it never needs to write any data
+to the disk but can instead keeps things in cache. If anything goes wrong,
+like your host losing power, the disk storage getting disconnected accidently,
+etc. you're image will most probably be rendered unusable.
+
 Instead of @option{-cdrom} you can use:
 @example
 qemu -drive file=file,index=2,media=cdrom
Index: qemu-kvm-0.12.3/vl.c
===================================================================
--- qemu-kvm-0.12.3.orig/vl.c
+++ qemu-kvm-0.12.3/vl.c
@@ -2254,6 +2254,8 @@ DriveInfo *drive_init(QemuOpts *opts, vo
             cache = 1;
         else if (!strcmp(buf, "writeback"))
             cache = 2;
+        else if (!strcmp(buf, "unsafe"))
+            cache = 3;
         else {
            fprintf(stderr, "qemu: invalid cache option\n");
            return NULL;
@@ -2458,6 +2460,8 @@ DriveInfo *drive_init(QemuOpts *opts, vo
         bdrv_flags |= BDRV_O_NOCACHE;
     else if (cache == 2) /* write-back */
         bdrv_flags |= BDRV_O_CACHE_WB;
+    else if (cache == 3) /* unsafe */
+        bdrv_flags |= BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH;
 
     if (aio == 1) {
         bdrv_flags |= BDRV_O_NATIVE_AIO;
openSUSE Build Service is sponsored by