diff mbox

qemu-img info: show nocow info

Message ID 1404294640-16360-1-git-send-email-cyliu@suse.com
State New
Headers show

Commit Message

Chunyan Liu July 2, 2014, 9:50 a.m. UTC
Add nocow info in 'qemu-img info' output to show whether the file
currently has NOCOW flag set or not.

Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 block/qapi.c         | 25 +++++++++++++++++++++++++
 qapi/block-core.json |  3 ++-
 2 files changed, 27 insertions(+), 1 deletion(-)

Comments

Eric Blake July 2, 2014, 1:03 p.m. UTC | #1
On 07/02/2014 03:50 AM, Chunyan Liu wrote:
> Add nocow info in 'qemu-img info' output to show whether the file
> currently has NOCOW flag set or not.
> 
> Signed-off-by: Chunyan Liu <cyliu@suse.com>
> ---
>  block/qapi.c         | 25 +++++++++++++++++++++++++
>  qapi/block-core.json |  3 ++-
>  2 files changed, 27 insertions(+), 1 deletion(-)
> 

> +
> +    /* get NOCOW info */
> +    fd = qemu_open(bs->filename, O_RDONLY | O_NONBLOCK);
> +    if (fd >= 0) {
> +        if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0 && (attr & FS_NOCOW_FL)) {
> +            info->has_nocow = true;
> +            info->nocow = true;

Better is:

if (ioctl... == 0) {
    info->has_nocow = true;
    info->nocow = !!(attr & FS_NOCOW_FL)
}

to explicitly document the cases where we know nocow works but is not
set (and omitting it in cases where nocow doesn't exist because of the fs)


> +++ b/qapi/block-core.json
> @@ -126,7 +126,8 @@
>             '*backing-filename': 'str', '*full-backing-filename': 'str',
>             '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'],
>             '*backing-image': 'ImageInfo',
> -           '*format-specific': 'ImageInfoSpecific' } }
> +           '*format-specific': 'ImageInfoSpecific',
> +           '*nocow': 'bool' } }

Missing documentation. When adding that, remember to add '(since 2.2)'
(or even since 2.1 if you think this is a bug fix that should go in
during hard freeze)
Chunyan Liu July 3, 2014, 4:30 a.m. UTC | #2
>>> On 7/2/2014 at 09:03 PM, in message <53B4031E.3030400@redhat.com>, Eric Blake
<eblake@redhat.com> wrote: 
> On 07/02/2014 03:50 AM, Chunyan Liu wrote: 
> > Add nocow info in 'qemu-img info' output to show whether the file 
> > currently has NOCOW flag set or not. 
> >  
> > Signed-off-by: Chunyan Liu <cyliu@suse.com> 
> > --- 
> >  block/qapi.c         | 25 +++++++++++++++++++++++++ 
> >  qapi/block-core.json |  3 ++- 
> >  2 files changed, 27 insertions(+), 1 deletion(-) 
> >  
>  
> > + 
> > +    /* get NOCOW info */ 
> > +    fd = qemu_open(bs->filename, O_RDONLY | O_NONBLOCK); 
> > +    if (fd >= 0) { 
> > +        if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0 && (attr & FS_NOCOW_FL)) { 
> > +            info->has_nocow = true; 
> > +            info->nocow = true; 
>  
> Better is: 
>  
> if (ioctl... == 0) { 
>     info->has_nocow = true; 
>     info->nocow = !!(attr & FS_NOCOW_FL) 
> } 
>  
It won't work. FS_IOC_GETFLAGS is a common ioctl to all file systems, it only
gets flags info that the file is currently set. Output 'attr' contains the flags info.
Even the filesystem doesn't support NOCOW, that ioctl could return 0.

> to explicitly document the cases where we know nocow works but is not 
> set (and omitting it in cases where nocow doesn't exist because of the fs) 
>  
>  
> > +++ b/qapi/block-core.json 
> > @@ -126,7 +126,8 @@ 
> >             '*backing-filename': 'str', '*full-backing-filename': 'str', 
> >             '*backing-filename-format': 'str', '*snapshots':  
> ['SnapshotInfo'], 
> >             '*backing-image': 'ImageInfo', 
> > -           '*format-specific': 'ImageInfoSpecific' } } 
> > +           '*format-specific': 'ImageInfoSpecific', 
> > +           '*nocow': 'bool' } } 
>  
> Missing documentation. When adding that, remember to add '(since 2.2)' 
> (or even since 2.1 if you think this is a bug fix that should go in 
> during hard freeze) 

OK. Thanks.

-Chunyan

>  
> --  
> Eric Blake   eblake redhat com    +1-919-301-3266 
> Libvirt virtualization library http://libvirt.org 
>  
>
diff mbox

Patch

diff --git a/block/qapi.c b/block/qapi.c
index f44f6b4..d4dd5cc 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -28,6 +28,13 @@ 
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/qmp/types.h"
+#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
 
 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs)
 {
@@ -173,6 +180,20 @@  void bdrv_query_image_info(BlockDriverState *bs,
     Error *err = NULL;
     ImageInfo *info = g_new0(ImageInfo, 1);
 
+#ifdef __linux__
+    int fd, attr;
+
+    /* get NOCOW info */
+    fd = qemu_open(bs->filename, O_RDONLY | O_NONBLOCK);
+    if (fd >= 0) {
+        if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0 && (attr & FS_NOCOW_FL)) {
+            info->has_nocow = true;
+            info->nocow = true;
+        }
+        qemu_close(fd);
+    }
+#endif
+
     bdrv_get_geometry(bs, &total_sectors);
 
     info->filename        = g_strdup(bs->filename);
@@ -625,4 +646,8 @@  void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
         func_fprintf(f, "Format specific information:\n");
         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
     }
+
+    if (info->has_nocow && info->nocow) {
+        func_fprintf(f, "no copy-on-write: yes\n");
+    }
 }
diff --git a/qapi/block-core.json b/qapi/block-core.json
index e378653..b02c13c 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -126,7 +126,8 @@ 
            '*backing-filename': 'str', '*full-backing-filename': 'str',
            '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'],
            '*backing-image': 'ImageInfo',
-           '*format-specific': 'ImageInfoSpecific' } }
+           '*format-specific': 'ImageInfoSpecific',
+           '*nocow': 'bool' } }
 
 ##
 # @ImageCheck: