Received: from eggs.gnu.org ([2001:4830:134:3::10]:45298) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X0My2-0000PU-Bp for qemu-devel@nongnu.org; Thu, 26 Jun 2014 23:36:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X0Mxx-00088L-UR for qemu-devel@nongnu.org; Thu, 26 Jun 2014 23:36:50 -0400 Received: from soto.provo.novell.com ([137.65.250.214]:39639) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X0Mxx-000864-L0 for qemu-devel@nongnu.org; Thu, 26 Jun 2014 23:36:45 -0400 Message-Id: <53AD574502000066000400C2@soto.provo.novell.com> Date: Thu, 26 Jun 2014 21:36:37 -0600 From: "Chun Yan Liu" References: <1403515022-24802-1-git-send-email-cyliu@suse.com> In-Reply-To: <1403515022-24802-1-git-send-email-cyliu@suse.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Subject: Re: [Qemu-devel] [PATCH V3] qemu-img create: add 'nocow' option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi , kwolf@redhat.com Cc: qemu-devel@nongnu.org Hi, Stefan & Kevin, Could you help to have a look at this version? We've discussed about this last November and now switch it to QemuOpts. Thanks, Chunyan >>> On 6/23/2014 at 05:17 PM, in message <1403515022-24802-1-git-send-email-cyliu@suse.com>, Chunyan Liu wrote:=20 > Add 'nocow' option so that users could have a chance to set NOCOW flag = to=20 > newly created files. It's useful on btrfs file system to enhance =20 > performance.=20 > =20 > Btrfs has low performance when hosting VM images, even more when the = guest=20 > in those VM are also using btrfs as file system. One way to mitigate = this =20 > bad=20 > performance is to turn off COW attributes on VM files. Generally, there = are=20 > two ways to turn off NOCOW on btrfs: a) by mounting fs with nodatacow, = then=20 > all newly created files will be NOCOW. b) per file. Add the NOCOW = file=20 > attribute. It could only be done to empty or new files.=20 > =20 > This patch tries the second way, according to the option, it could add = NOCOW=20 > per file.=20 > =20 > For most block drivers, since the create file step is in raw-posix.c, so = we=20 > can do setting NOCOW flag ioctl in raw-posix.c only.=20 > =20 > But there are some exceptions, like block/vpc.c and block/vdi.c, they = are=20 > creating file by calling qemu_open directly. For them, do the same = setting=20 > NOCOW flag ioctl work in them separately.=20 > =20 > Signed-off-by: Chunyan Liu =20 > ---=20 > Changes to v2:=20 > * based on QemuOpts instead of old QEMUOptionParameters=20 > * add nocow description in man page and html doc=20 > =20 > Old v2 is here:=20 > http://lists.gnu.org/archive/html/qemu-devel/2013-11/msg02429.html=20 > =20 > ---=20 > block/cow.c | 5 +++++=20 > block/qcow.c | 5 +++++=20 > block/qcow2.c | 5 +++++=20 > block/qed.c | 11 ++++++++---=20 > block/raw-posix.c | 25 +++++++++++++++++++++++++=20 > block/vdi.c | 29 +++++++++++++++++++++++++++++=20 > block/vhdx.c | 5 +++++=20 > block/vmdk.c | 11 ++++++++---=20 > block/vpc.c | 29 +++++++++++++++++++++++++++++=20 > include/block/block_int.h | 1 +=20 > qemu-doc.texi | 16 ++++++++++++++++=20 > qemu-img.texi | 16 ++++++++++++++++=20 > 12 files changed, 152 insertions(+), 6 deletions(-)=20 > =20 > diff --git a/block/cow.c b/block/cow.c=20 > index a05a92c..43b537c 100644=20 > --- a/block/cow.c=20 > +++ b/block/cow.c=20 > @@ -401,6 +401,11 @@ static QemuOptsList cow_create_opts =3D {=20 > .type =3D QEMU_OPT_STRING,=20 > .help =3D "File name of a base image"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/qcow.c b/block/qcow.c=20 > index 1f2bac8..5b23540 100644=20 > --- a/block/qcow.c=20 > +++ b/block/qcow.c=20 > @@ -928,6 +928,11 @@ static QemuOptsList qcow_create_opts =3D {=20 > .help =3D "Encrypt the image",=20 > .def_value_str =3D "off"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/qcow2.c b/block/qcow2.c=20 > index b9d2fa6..3a4cc8a 100644=20 > --- a/block/qcow2.c=20 > +++ b/block/qcow2.c=20 > @@ -2382,6 +2382,11 @@ static QemuOptsList qcow2_create_opts =3D {=20 > .help =3D "Postpone refcount updates",=20 > .def_value_str =3D "off"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/qed.c b/block/qed.c=20 > index 092e6fb..460ac92 100644=20 > --- a/block/qed.c=20 > +++ b/block/qed.c=20 > @@ -567,7 +567,7 @@ static void bdrv_qed_close(BlockDriverState *bs)=20 > static int qed_create(const char *filename, uint32_t cluster_size,=20 > uint64_t image_size, uint32_t table_size,=20 > const char *backing_file, const char *backing_fmt,= =20 > - Error **errp)=20 > + QemuOpts *opts, Error **errp)=20 > {=20 > QEDHeader header =3D {=20 > .magic =3D QED_MAGIC,=20 > @@ -586,7 +586,7 @@ static int qed_create(const char *filename, uint32_t = =20 > cluster_size,=20 > int ret =3D 0;=20 > BlockDriverState *bs;=20 > =20 > - ret =3D bdrv_create_file(filename, NULL, &local_err);=20 > + ret =3D bdrv_create_file(filename, opts, &local_err);=20 > if (ret < 0) {=20 > error_propagate(errp, local_err);=20 > return ret;=20 > @@ -682,7 +682,7 @@ static int bdrv_qed_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > }=20 > =20 > ret =3D qed_create(filename, cluster_size, image_size, table_size,= =20 > - backing_file, backing_fmt, errp);=20 > + backing_file, backing_fmt, opts, errp);=20 > =20 > finish:=20 > g_free(backing_file);=20 > @@ -1644,6 +1644,11 @@ static QemuOptsList qed_create_opts =3D {=20 > .type =3D QEMU_OPT_SIZE,=20 > .help =3D "L1/L2 table size (in clusters)"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/raw-posix.c b/block/raw-posix.c=20 > index dacf4fb..825a0c8 100644=20 > --- a/block/raw-posix.c=20 > +++ b/block/raw-posix.c=20 > @@ -55,6 +55,9 @@=20 > #include =20 > #include =20 > #include =20 > +#ifndef FS_NOCOW_FL=20 > +#define FS_NOCOW_FL 0x00800000 /* Do not cow file = */=20 > +#endif=20 > #endif=20 > #ifdef CONFIG_FIEMAP=20 > #include =20 > @@ -1278,12 +1281,14 @@ static int raw_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > int fd;=20 > int result =3D 0;=20 > int64_t total_size =3D 0;=20 > + bool nocow =3D false;=20 > =20 > strstart(filename, "file:", &filename);=20 > =20 > /* Read out options */=20 > total_size =3D=20 > qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZ= E;=20 > + nocow =3D qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);=20 > =20 > fd =3D qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | = O_BINARY,=20 > 0644);=20 > @@ -1291,6 +1296,21 @@ static int raw_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > result =3D -errno;=20 > error_setg_errno(errp, -result, "Could not create file");=20 > } else {=20 > + if (nocow) {=20 > +#ifdef __linux__=20 > + /* Set NOCOW flag to solve performance issue on fs like = btrfs.=20 > + * This is an optimisation. The FS_IOC_SETFLAGS ioctl = return =20 > value=20 > + * will be ignored since any failure of this operation = should =20 > not=20 > + * block the left work.=20 > + */=20 > + int attr;=20 > + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) =3D=3D 0) {=20 > + attr |=3D FS_NOCOW_FL;=20 > + ioctl(fd, FS_IOC_SETFLAGS, &attr);=20 > + }=20 > +#endif=20 > + }=20 > +=20 > if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) !=3D 0) {=20 > result =3D -errno;=20 > error_setg_errno(errp, -result, "Could not resize file");=20= > @@ -1477,6 +1497,11 @@ static QemuOptsList raw_create_opts =3D {=20 > .type =3D QEMU_OPT_SIZE,=20 > .help =3D "Virtual disk size"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/vdi.c b/block/vdi.c=20 > index 01fe22e..197bd77 100644=20 > --- a/block/vdi.c=20 > +++ b/block/vdi.c=20 > @@ -53,6 +53,13 @@=20 > #include "block/block_int.h"=20 > #include "qemu/module.h"=20 > #include "migration/migration.h"=20 > +#ifdef __linux__=20 > +#include =20 > +#include =20 > +#ifndef FS_NOCOW_FL=20 > +#define FS_NOCOW_FL 0x00800000 /* Do not cow file = */=20 > +#endif=20 > +#endif=20 > =20 > #if defined(CONFIG_UUID)=20 > #include =20 > @@ -683,6 +690,7 @@ static int vdi_create(const char *filename, QemuOpts = =20 > *opts, Error **errp)=20 > VdiHeader header;=20 > size_t i;=20 > size_t bmap_size;=20 > + bool nocow =3D false;=20 > =20 > logout("\n");=20 > =20 > @@ -699,6 +707,7 @@ static int vdi_create(const char *filename, QemuOpts = =20 > *opts, Error **errp)=20 > image_type =3D VDI_TYPE_STATIC;=20 > }=20 > #endif=20 > + nocow =3D qemu_opt_get_bool_del(opts, BLOCK_OPT_NOCOW, false);=20 > =20 > if (bytes > VDI_DISK_SIZE_MAX) {=20 > result =3D -ENOTSUP;=20 > @@ -716,6 +725,21 @@ static int vdi_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > goto exit;=20 > }=20 > =20 > + if (nocow) {=20 > +#ifdef __linux__=20 > + /* Set NOCOW flag to solve performance issue on fs like = btrfs.=20 > + * This is an optimisation. The FS_IOC_SETFLAGS ioctl return = value =20 > will=20 > + * be ignored since any failure of this operation should not = block =20 > the=20 > + * left work.=20 > + */=20 > + int attr;=20 > + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) =3D=3D 0) {=20 > + attr |=3D FS_NOCOW_FL;=20 > + ioctl(fd, FS_IOC_SETFLAGS, &attr);=20 > + }=20 > +#endif=20 > + }=20 > +=20 > /* We need enough blocks to store the given disk size,=20 > so always round up. */=20 > blocks =3D (bytes + block_size - 1) / block_size;=20 > @@ -818,6 +842,11 @@ static QemuOptsList vdi_create_opts =3D {=20 > .def_value_str =3D "off"=20 > },=20 > #endif=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > /* TODO: An additional option to set UUID values might be = useful. =20 > */=20 > { /* end of list */ }=20 > }=20 > diff --git a/block/vhdx.c b/block/vhdx.c=20 > index fedcf9f..7bdb456 100644=20 > --- a/block/vhdx.c=20 > +++ b/block/vhdx.c=20 > @@ -1909,6 +1909,11 @@ static QemuOptsList vhdx_create_opts =3D {=20 > .type =3D QEMU_OPT_BOOL,=20 > .help =3D "Force use of payload blocks of type 'ZERO'. =20 > Non-standard."=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { NULL }=20 > }=20 > };=20 > diff --git a/block/vmdk.c b/block/vmdk.c=20 > index 83dd6fe..94e1ff7 100644=20 > --- a/block/vmdk.c=20 > +++ b/block/vmdk.c=20 > @@ -1529,7 +1529,7 @@ static int coroutine_fn =20 > vmdk_co_write_zeroes(BlockDriverState *bs,=20 > =20 > static int vmdk_create_extent(const char *filename, int64_t filesize,=20= > bool flat, bool compress, bool zeroed_grai= n,=20 > - Error **errp)=20 > + QemuOpts *opts, Error **errp)=20 > {=20 > int ret, i;=20 > BlockDriverState *bs =3D NULL;=20 > @@ -1539,7 +1539,7 @@ static int vmdk_create_extent(const char *filename,= =20 > int64_t filesize,=20 > uint32_t *gd_buf =3D NULL;=20 > int gd_buf_size;=20 > =20 > - ret =3D bdrv_create_file(filename, NULL, &local_err);=20 > + ret =3D bdrv_create_file(filename, opts, &local_err);=20 > if (ret < 0) {=20 > error_propagate(errp, local_err);=20 > goto exit;=20 > @@ -1845,7 +1845,7 @@ static int vmdk_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > path, desc_filename);=20 > =20 > if (vmdk_create_extent(ext_filename, size,=20 > - flat, compress, zeroed_grain, errp)) = {=20 > + flat, compress, zeroed_grain, opts, = errp)) {=20 > ret =3D -EINVAL;=20 > goto exit;=20 > }=20 > @@ -2153,6 +2153,11 @@ static QemuOptsList vmdk_create_opts =3D {=20 > .help =3D "Enable efficient zero writes "=20 > "using the zeroed-grain GTE feature"=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/block/vpc.c b/block/vpc.c=20 > index 798d854..8b376a4 100644=20 > --- a/block/vpc.c=20 > +++ b/block/vpc.c=20 > @@ -29,6 +29,13 @@=20 > #if defined(CONFIG_UUID)=20 > #include =20 > #endif=20 > +#ifdef __linux__=20 > +#include =20 > +#include =20 > +#ifndef FS_NOCOW_FL=20 > +#define FS_NOCOW_FL 0x00800000 /* Do not cow file = */=20 > +#endif=20 > +#endif=20 > =20 > /**************************************************************/=20 > =20 > @@ -751,6 +758,7 @@ static int vpc_create(const char *filename, QemuOpts = =20 > *opts, Error **errp)=20 > int64_t total_size;=20 > int disk_type;=20 > int ret =3D -EIO;=20 > + bool nocow =3D false;=20 > =20 > /* Read out options */=20 > total_size =3D qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);=20 > @@ -767,6 +775,7 @@ static int vpc_create(const char *filename, QemuOpts = =20 > *opts, Error **errp)=20 > } else {=20 > disk_type =3D VHD_DYNAMIC;=20 > }=20 > + nocow =3D qemu_opt_get_bool_del(opts, BLOCK_OPT_NOCOW, false);=20 > =20 > /* Create the file */=20 > fd =3D qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, = =20 > 0644);=20 > @@ -775,6 +784,21 @@ static int vpc_create(const char *filename, = QemuOpts =20 > *opts, Error **errp)=20 > goto out;=20 > }=20 > =20 > + if (nocow) {=20 > +#ifdef __linux__=20 > + /* Set NOCOW flag to solve performance issue on fs like = btrfs.=20 > + * This is an optimisation. The FS_IOC_SETFLAGS ioctl return = value =20 > will=20 > + * be ignored since any failure of this operation should not = block =20 > the=20 > + * left work.=20 > + */=20 > + int attr;=20 > + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) =3D=3D 0) {=20 > + attr |=3D FS_NOCOW_FL;=20 > + ioctl(fd, FS_IOC_SETFLAGS, &attr);=20 > + }=20 > +#endif=20 > + }=20 > +=20 > /*=20 > * Calculate matching total_size and geometry. Increase the number = of=20 > * sectors requested until we get enough (or fail). This ensures = that=20 > @@ -884,6 +908,11 @@ static QemuOptsList vpc_create_opts =3D {=20 > "Type of virtual hard disk format. Supported formats = are "=20 > "{dynamic (default) | fixed} "=20 > },=20 > + {=20 > + .name =3D BLOCK_OPT_NOCOW,=20 > + .type =3D QEMU_OPT_BOOL,=20 > + .help =3D "Turn off copy-on-write (valid only on btrfs)"=20 > + },=20 > { /* end of list */ }=20 > }=20 > };=20 > diff --git a/include/block/block_int.h b/include/block/block_int.h=20 > index 7aa2213..4e5022a 100644=20 > --- a/include/block/block_int.h=20 > +++ b/include/block/block_int.h=20 > @@ -54,6 +54,7 @@=20 > #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"=20 > #define BLOCK_OPT_ADAPTER_TYPE "adapter_type"=20 > #define BLOCK_OPT_REDUNDANCY "redundancy"=20 > +#define BLOCK_OPT_NOCOW "nocow"=20 > =20 > typedef struct BdrvTrackedRequest {=20 > BlockDriverState *bs;=20 > diff --git a/qemu-doc.texi b/qemu-doc.texi=20 > index 88ec9bb..ad92c85 100644=20 > --- a/qemu-doc.texi=20 > +++ b/qemu-doc.texi=20 > @@ -589,6 +589,22 @@ check -r all} is required, which may take some = time.=20 > =20 > This option can only be enabled if @code{compat=3D1.1} is specified.=20 > =20 > +@item nocow=20 > +If this option is set to @code{on}, it will trun off COW of the file. = It's =20 > only=20 > +valid on btrfs, no effect on other file systems.=20 > +=20 > +Btrfs has low performance when hosting a VM image file, even more when = the =20 > guest=20 > +on the VM also using btrfs as file system. Turning off COW is a way to = =20 > mitigate=20 > +this bad performance. Generally there are two ways to turn off COW on = =20 > btrfs:=20 > +a) Disable it by mounting with nodatacow, then all newly created files = will =20 > be=20 > +NOCOW. b) For an empty file, add the NOCOW file attribute. That's what = this =20 > option=20 > +does.=20 > +=20 > +Note: this option is only valid to new or empty files. If there is an = =20 > existing=20 > +file which is COW and has data blocks already, it couldn't be changed = to =20 > NOCOW=20 > +by setting @code{nocow=3Don}. One can issue @code{lsattr filename} to = check =20 > if=20 > +the NOCOW flag is set or not (Capitabl 'C' is NOCOW flag).=20 > +=20 > @end table=20 > =20 > @item qed=20 > diff --git a/qemu-img.texi b/qemu-img.texi=20 > index c68b541..8496f3b 100644=20 > --- a/qemu-img.texi=20 > +++ b/qemu-img.texi=20 > @@ -474,6 +474,22 @@ check -r all} is required, which may take some = time.=20 > =20 > This option can only be enabled if @code{compat=3D1.1} is specified.=20 > =20 > +@item nocow=20 > +If this option is set to @code{on}, it will trun off COW of the file. = It's =20 > only=20 > +valid on btrfs, no effect on other file systems.=20 > +=20 > +Btrfs has low performance when hosting a VM image file, even more when = the =20 > guest=20 > +on the VM also using btrfs as file system. Turning off COW is a way to = =20 > mitigate=20 > +this bad performance. Generally there are two ways to turn off COW on = =20 > btrfs:=20 > +a) Disable it by mounting with nodatacow, then all newly created files = will =20 > be=20 > +NOCOW. b) For an empty file, add the NOCOW file attribute. That's what = this =20 > option=20 > +does.=20 > +=20 > +Note: this option is only valid to new or empty files. If there is an = =20 > existing=20 > +file which is COW and has data blocks already, it couldn't be changed = to =20 > NOCOW=20 > +by setting @code{nocow=3Don}. One can issue @code{lsattr filename} to = check =20 > if=20 > +the NOCOW flag is set or not (Capitabl 'C' is NOCOW flag).=20 > +=20 > @end table=20 > =20 > @item Other=20 =20