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 <cy...@suse.com> --- block/raw-posix.c | 12 ++++++++++++ block/vdi.c | 13 +++++++++++++ block/vmdk.c | 13 +++++++++++++ include/qemu-common.h | 9 +++++++++ 4 files changed, 47 insertions(+), 0 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index f6d48bb..442e06e 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1072,6 +1072,18 @@ 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. + * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value will + * be ignored since any failure of this operation should not block the + * left work. + */ + int attr; + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { + 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..b6509a0 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -686,6 +686,19 @@ 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. + * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value will + * be ignored since any failure of this operation should not block the + * left work. + */ + int attr; + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { + 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..3f4d6ad 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1447,6 +1447,19 @@ 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. + * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value will + * be ignored since any failure of this operation should not block the + * left work. + */ + int attr; + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { + 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