commit ccc0b45917fa76a77ff83f1ddfd30836c8c3805e Author: Chunyan Liu Date: Wed May 7 12:45:40 2014 +0800 add nocow to vol xml Updated patch. Rebase to git master. Signed-off-by: Chunyan Liu Index: libvirt-1.2.6/docs/schemas/storagevol.rng =================================================================== --- libvirt-1.2.6.orig/docs/schemas/storagevol.rng +++ libvirt-1.2.6/docs/schemas/storagevol.rng @@ -138,6 +138,11 @@ + + + + + Index: libvirt-1.2.6/src/conf/storage_conf.c =================================================================== --- libvirt-1.2.6.orig/src/conf/storage_conf.c +++ libvirt-1.2.6/src/conf/storage_conf.c @@ -1397,6 +1397,9 @@ virStorageVolDefParseXML(virStoragePoolD virStringFreeList(version); } + if (virXPathNode("./target/nocow", ctxt)) + ret->target.nocow = true; + if (options->featureFromString && virXPathNode("./target/features", ctxt)) { if ((n = virXPathNodeSet("./target/features/*", ctxt, &nodes)) < 0) goto error; Index: libvirt-1.2.6/src/storage/storage_backend.c =================================================================== --- libvirt-1.2.6.orig/src/storage/storage_backend.c +++ libvirt-1.2.6/src/storage/storage_backend.c @@ -37,6 +37,9 @@ #ifdef __linux__ # include # include +# ifndef FS_NOCOW_FL +# define FS_NOCOW_FL 0x00800000 /* Do not cow file */ +# endif #endif #if WITH_SELINUX @@ -452,6 +455,21 @@ virStorageBackendCreateRaw(virConnectPtr goto cleanup; } + if (vol->target.nocow) { +#ifdef __linux__ + int attr; + + /* Set NOCOW flag. This is an optimisation for btrfs. + * The FS_IOC_SETFLAGS ioctl return value will be ignored since any + * failure of this operation should not block the left work. + */ + if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { + attr |= FS_NOCOW_FL; + ioctl(fd, FS_IOC_SETFLAGS, &attr); + } +#endif + } + if ((ret = createRawFile(fd, vol, inputvol)) < 0) /* createRawFile already reported the exact error. */ ret = -1; @@ -717,6 +735,7 @@ virStorageBackendCreateQemuImgOpts(char bool preallocate, int format, const char *compat, + bool nocow, virBitmapPtr features) { virBuffer buf = VIR_BUFFER_INITIALIZER; @@ -729,6 +748,8 @@ virStorageBackendCreateQemuImgOpts(char virBufferAddLit(&buf, "encryption=on,"); if (preallocate) virBufferAddLit(&buf, "preallocation=metadata,"); + if (nocow) + virBufferAddLit(&buf, "nocow=on,"); if (compat) virBufferAsprintf(&buf, "compat=%s,", compat); @@ -950,6 +971,7 @@ virStorageBackendCreateQemuImgCmd(virCon do_encryption, preallocate, vol->target.format, compat, + vol->target.nocow, vol->target.features) < 0) { virCommandFree(cmd); return NULL; Index: libvirt-1.2.6/src/util/virstoragefile.h =================================================================== --- libvirt-1.2.6.orig/src/util/virstoragefile.h +++ libvirt-1.2.6/src/util/virstoragefile.h @@ -232,6 +232,7 @@ struct _virStorageSource { * pool-specific enum for storage volumes */ virBitmapPtr features; char *compat; + bool nocow; virStoragePermsPtr perms; virStorageTimestampsPtr timestamps;