All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qemu-img create: set nocow flag by default
@ 2013-11-07  8:08 Chunyan Liu
  2013-11-12  6:15 ` Fam Zheng
  0 siblings, 1 reply; 3+ messages in thread
From: Chunyan Liu @ 2013-11-07  8:08 UTC (permalink / raw)
  To: qemu-devel; +Cc: stefanha, 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).

Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 qemu-img.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index bf3fb4f..d43e8f1 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -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;
 }
 
-- 
1.6.0.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH] qemu-img create: set nocow flag by default
  2013-11-07  8:08 [Qemu-devel] [PATCH] qemu-img create: set nocow flag by default Chunyan Liu
@ 2013-11-12  6:15 ` Fam Zheng
  2013-11-12  8:06   ` Chunyan Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Fam Zheng @ 2013-11-12  6:15 UTC (permalink / raw)
  To: Chunyan Liu, qemu-devel; +Cc: stefanha

On 2013年11月07日 16:08, Chunyan Liu wrote:
> 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 <cyliu@suse.com>
> ---
>   qemu-img.c |   15 +++++++++++++++
>   1 files changed, 15 insertions(+), 0 deletions(-)
>
> diff --git a/qemu-img.c b/qemu-img.c
> index bf3fb4f..d43e8f1 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -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
>   #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;
>   }
>
>
"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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH] qemu-img create: set nocow flag by default
  2013-11-12  6:15 ` Fam Zheng
@ 2013-11-12  8:06   ` Chunyan Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Chunyan Liu @ 2013-11-12  8:06 UTC (permalink / raw)
  To: Fam Zheng; +Cc: Stefan Hajnoczi, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 2932 bytes --]

2013/11/12 Fam Zheng <famz@redhat.com>

> On 2013年11月07日 16:08, Chunyan Liu wrote:
>
>> 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 <cyliu@suse.com>
>> ---
>>   qemu-img.c |   15 +++++++++++++++
>>   1 files changed, 15 insertions(+), 0 deletions(-)
>>
>> diff --git a/qemu-img.c b/qemu-img.c
>> index bf3fb4f..d43e8f1 100644
>> --- a/qemu-img.c
>> +++ b/qemu-img.c
>> @@ -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
>>   #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;
>>   }
>>
>>
>>  "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.



> Thanks,
> Fam
>
>

[-- Attachment #2: Type: text/html, Size: 4035 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-11-12  8:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-07  8:08 [Qemu-devel] [PATCH] qemu-img create: set nocow flag by default Chunyan Liu
2013-11-12  6:15 ` Fam Zheng
2013-11-12  8:06   ` Chunyan Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.