Skip to contentSkip to conversation

qemu-img create: set nocow flag by default

PATCH In review Chunyan Liu 2 participants 3 messages Nov 7, 2013 – Nov 12, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-img create: set nocow flag by default

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).

---
 qemu-img.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
qemu-img.c +15 -0
index bf3fb4f..d43e8f1 100644
@@ -34,11 +34,17 @@
 #include <getopt.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <linux/fs.h>
+#include <sys/ioctl.h>
 
 #ifdef _WIN32
 #include <windows.h>
 #endif
 
+#ifndef FS_NOCOW_FL
+#define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
+#endif
+
 typedef struct img_cmd_t {
     const char *name;
     int (*handler)(int argc, char **argv);
@@ -340,6 +346,7 @@ static int img_create(int argc, char **argv)
     char *options = NULL;
     Error *local_err = NULL;
     bool quiet = false;
+    int fd, attr;
 
     for(;;) {
         c = getopt(argc, argv, "F:b:f:he6o:q");
@@ -417,6 +424,14 @@ static int img_create(int argc, char **argv)
         return 1;
     }
 
+    /* set NOCOW by default to solve performance issue on btrfs */
+    fd = qemu_open(filename, O_RDONLY|O_NONBLOCK);
+    if (fd >= 0) {
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+        qemu_close(fd);
+    }
+
     return 0;
 }
 
Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 17 lines · Chunyan Liu

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).

---
  qemu-img.c |   15 +++++++++++++++
  1 files changed, 15 insertions(+), 0 deletions(-)
qemu-img.c +0 -0
index bf3fb4f..d43e8f1 100644

qemu-img.c line 38

Quoted 5 lines
qemu-img.c +2 -0
@@ -34,11 +34,17 @@
  #include <getopt.h>
  #include <stdio.h>
  #include <stdarg.h>
+#include <linux/fs.h>
+#include <sys/ioctl.h>

This should be in #ifdef to not break build on Windows.

Quoted 25 lines
qemu-img.c +6 -0
  #ifdef _WIN32
  #include <windows.h>
  #endif
 
+#ifndef FS_NOCOW_FL
+#define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
+#endif
+
  typedef struct img_cmd_t {
      const char *name;
      int (*handler)(int argc, char **argv);
@@ -340,6 +346,7 @@ static int img_create(int argc, char **argv)
      char *options = NULL;
      Error *local_err = NULL;
      bool quiet = false;
+    int fd, attr;
 
      for(;;) {
          c = getopt(argc, argv, "F:b:f:he6o:q");
@@ -417,6 +424,14 @@ static int img_create(int argc, char **argv)
          return 1;
      }
 
+    /* set NOCOW by default to solve performance issue on btrfs */

qemu-img.c static int img_create(int argc, char **argv) · line 437

Quoted 10 lines
qemu-img.c +7 -0
+    fd = qemu_open(filename, O_RDONLY|O_NONBLOCK);
+    if (fd >= 0) {
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+        qemu_close(fd);
+    }
+
      return 0;
  }
 

"man chattr" says:
... For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable...

But you are setting the attr after image creation, so what's the difference here? just want to make sure this does what's expected.

Thanks,
Fam

#2 lore raw

2013/11/12 Fam Zheng <[email protected]>

Quoted 68 lines · Chunyan Liu
Quoted 22 lines

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).

---
  qemu-img.c |   15 +++++++++++++++
  1 files changed, 15 insertions(+), 0 deletions(-)
qemu-img.c +0 -0
index bf3fb4f..d43e8f1 100644

qemu-img.c line 39

Quoted 6 lines
qemu-img.c +2 -0
@@ -34,11 +34,17 @@
  #include <getopt.h>
  #include <stdio.h>
  #include <stdarg.h>
+#include <linux/fs.h>
+#include <sys/ioctl.h>
 

This should be in #ifdef to not break build on Windows.

   #ifdef _WIN32
Quoted 34 lines
qemu-img.c +13 -0
  #include <windows.h>
  #endif
 
+#ifndef FS_NOCOW_FL
+#define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
+#endif
+
  typedef struct img_cmd_t {
      const char *name;
      int (*handler)(int argc, char **argv);
@@ -340,6 +346,7 @@ static int img_create(int argc, char **argv)
      char *options = NULL;
      Error *local_err = NULL;
      bool quiet = false;
+    int fd, attr;
 
      for(;;) {
          c = getopt(argc, argv, "F:b:f:he6o:q");
@@ -417,6 +424,14 @@ static int img_create(int argc, char **argv)
          return 1;
      }
 
+    /* set NOCOW by default to solve performance issue on btrfs */
+    fd = qemu_open(filename, O_RDONLY|O_NONBLOCK);
+    if (fd >= 0) {
+        attr = FS_NOCOW_FL;
+        ioctl(fd, FS_IOC_SETFLAGS, &attr);
+        qemu_close(fd);
+    }
+
      return 0;
  }
 
Quoted 8 lines
Quoted 1 line

"man chattr" says:

... For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable...

But you are setting the attr after image creation, so what's the difference here? just want to make sure this does what's expected.

I expected such question. Following the man page, I should add changes in each related block driver's drv_create, that seems too much changes. Just to make the change as little as possible, I asked in #btrfs irc about the reason why must empty and if it affects in our case. Maybe I misunderstand the reply, but now I check btrfs/ioctl.c, I should say I made a mistake here. If the file size is not 0, the COW flag won't be removed. Sorry! Still need to change in block driver's drv_create function. I'll revise. Thanks for correction.

Quoted 4 lines

Thanks,
Fam