qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] qemu-img: set nocow flag to new file


From: Chunyan Liu
Subject: [Qemu-devel] [PATCH] qemu-img: set nocow flag to new file
Date: Thu, 14 Nov 2013 16:15:28 +0800

Set NOCOW flag to newly created images to solve performance issues on btrfs.

Btrfs has terrible performance when hosting VM images, even more when the guest
in those VM are also using btrfs as file system. One way to mitigate this bad
performance is to turn off COW attributes on VM files (since having copy on
write for this kind of data is not useful).

Signed-off-by: Chunyan Liu <address@hidden>
---
 block/raw-posix.c     |    6 ++++++
 block/vdi.c           |    7 +++++++
 block/vmdk.c          |    7 +++++++
 include/qemu-common.h |    9 +++++++++
 4 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index f6d48bb..4a3e9d0 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -1072,6 +1072,12 @@ static int raw_create(const char *filename, 
QEMUOptionParameter *options,
         result = -errno;
         error_setg_errno(errp, -result, "Could not create file");
     } else {
+#ifdef __linux__
+        /* set NOCOW flag to solve performance issue on fs like btrfs */
+        int attr;
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+#endif
         if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
             result = -errno;
             error_setg_errno(errp, -result, "Could not resize file");
diff --git a/block/vdi.c b/block/vdi.c
index b6ec002..dfaa905 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -686,6 +686,13 @@ static int vdi_create(const char *filename, 
QEMUOptionParameter *options,
                    0644);
     if (fd < 0) {
         return -errno;
+    } else {
+#ifdef __linux__
+        /* set NOCOW flag to solve performance issue on fs like btrfs */
+        int attr;
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+#endif
     }
 
     /* We need enough blocks to store the given disk size,
diff --git a/block/vmdk.c b/block/vmdk.c
index a7ebd0f..bd953bc 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -1447,6 +1447,13 @@ static int vmdk_create_extent(const char *filename, 
int64_t filesize,
                    0644);
     if (fd < 0) {
         return -errno;
+    } else {
+#ifdef __linux__
+        /* set NOCOW flag to solve performance issue on fs like btrfs */
+        int attr;
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+#endif
     }
     if (flat) {
         ret = ftruncate(fd, filesize);
diff --git a/include/qemu-common.h b/include/qemu-common.h
index 5054836..fe7dd9b 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -50,6 +50,15 @@
 #include "sysemu/os-posix.h"
 #endif
 
+#ifdef __linux__
+#include <linux/fs.h>
+#include <sys/ioctl.h>
+
+#ifndef FS_NOCOW_FL
+#define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
+#endif
+#endif
+
 #ifndef O_LARGEFILE
 #define O_LARGEFILE 0
 #endif
-- 
1.6.0.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]