file-posix: Clear out first sector in hdev_create
PATCH 1/1 file-posix: Clear out first sector in hdev_create
People get surprised when, after "qemu-imc create -f raw /dev/sdX", they still see qcow2 with "qemu-img info", if previously the bdev had a qcow2 header. While this is natural because raw doesn't need to write any magic bytes during creation, hdev_create is free to clear out the first sector to make sure the stale qcow2 header doesn't cause such a confusion.
--- block/file-posix.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
block/file-posix.c +11 -0
@@ -2703,6 +2703,17 @@ static int hdev_create(const char *filename, QemuOpts *opts, ret = -ENOSPC ; } +if (total_size) { +int64_t zero_size =MIN (BDRV_SECTOR_SIZE , total_size); +uint8_t *buf; +if (lseek (fd,0 ,SEEK_SET ) ==-1 ) { + ret = -errno; + }else { + buf =g_malloc0 (zero_size); + ret =qemu_write_full (fd, buf, zero_size); +g_free (buf); + } + }qemu_close (fd);return ret; }
Signature · 1 line
2.13.3
Quoted 17 lines · Fam Zheng
People get surprised when, after "qemu-imc create -f raw
/dev/sdX", they still see qcow2 with "qemu-img info", if previously the bdev had a qcow2 header. While this is natural because raw doesn't need to write any magic bytes during creation,hdev_createis free to clear out the first sector to make sure the stale qcow2 header doesn't cause such a confusion.Signed-off-by Fam Zheng <[email protected]>--- block/file-posix.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/file-posix.c +0
-0
block/file-posix.c static int hdev_create(const char *filename, QemuOpts *opts, · line 2708
Quoted 6 lines
block/file-posix.c +3
-0@@ -2703,6 +2703,17 @@ static int hdev_create(const char *filename, QemuOpts *opts, ret = -ENOSPC ; } +if (total_size) { +int64_t zero_size =MIN (BDRV_SECTOR_SIZE , total_size); +uint8_t *buf;
Since BDRV_SECTOR_SIZE is small enough to stack-allocate, you could skip the malloc by doing:
uint8_t buf[BDRV_SECTOR_SIZE] = "";
block/file-posix.c static int hdev_create(const char *filename, QemuOpts *opts, · line 2713
Quoted 5 lines
block/file-posix.c +5
-0+if (lseek (fd,0 ,SEEK_SET ) ==-1 ) { + ret = -errno; + }else { + buf =g_malloc0 (zero_size); + ret =qemu_write_full (fd, buf, zero_size);
Instead of doing lseek + qemu_write_full, can we just use qemu_pwritev(fd, &iov, 1, 0) with an iov set up to point to the appropriate amount of buf?
At any rate, my ideas are micro-optimizations, so I can also live with how you wrote it.
Are you arguing that this is a bug-fix worthy of inclusion in 2.10, because it helps avoid user confusion? Or are you delaying it to 2.11, because we've had the existing behavior for longer than one release, so one release more won't hurt?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 36 lines · Eric Blake
Quoted 17 lines · Fam Zheng
People get surprised when, after "qemu-imc create -f raw
/dev/sdX", they still see qcow2 with "qemu-img info", if previously the bdev had a qcow2 header. While this is natural because raw doesn't need to write any magic bytes during creation,hdev_createis free to clear out the first sector to make sure the stale qcow2 header doesn't cause such a confusion.Signed-off-by Fam Zheng <[email protected]>--- block/file-posix.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/file-posix.c +0
-0block/file-posix.c static int hdev_create(const char *filename, QemuOpts *opts, · line 2708
Quoted 6 lines
block/file-posix.c +3
-0@@ -2703,6 +2703,17 @@ static int hdev_create(const char *filename, QemuOpts *opts, ret = -ENOSPC ; } +if (total_size) { +int64_t zero_size =MIN (BDRV_SECTOR_SIZE , total_size); +uint8_t *buf;Since
BDRV_SECTOR_SIZEis small enough to stack-allocate, you could skip the malloc by doing:
uint8_tbuf[BDRV_SECTOR_SIZE] = "";block/file-posix.c static int hdev_create(const char *filename, QemuOpts *opts, · line 2713
Quoted 5 lines
block/file-posix.c +5
-0+if (lseek (fd,0 ,SEEK_SET ) ==-1 ) { + ret = -errno; + }else { + buf =g_malloc0 (zero_size); + ret =qemu_write_full (fd, buf, zero_size);
Quoted 3 lines
Instead of doing lseek +
qemu_write_full, can we just useqemu_pwritev(fd, &iov, 1, 0)with an iov set up to point to the appropriate amount of buf?
Neat, will update.
Quoted 10 lines
At any rate, my ideas are micro-optimizations, so I can also live with how you wrote it.
Reviewed-by Eric Blake <[email protected]>Are you arguing that this is a bug-fix worthy of inclusion in 2.10, because it helps avoid user confusion? Or are you delaying it to 2.11, because we've had the existing behavior for longer than one release, so one release more won't hurt?
IMO no need to rush for 2.10, it can wait for 2.11.
Thanks!
Fam
Quoted 17 lines · Fam Zheng
Quoted 16 lines · Eric Blake
block/file-posix.c static int hdev_create(const char *filename, QemuOpts *opts, · line 2708
Quoted 3 lines · Fam Zheng
block/file-posix.c +3
-0+if (total_size) { +int64_t zero_size =MIN (BDRV_SECTOR_SIZE , total_size); +uint8_t *buf;Since
BDRV_SECTOR_SIZEis small enough to stack-allocate, you could skip the malloc by doing:
uint8_tbuf[BDRV_SECTOR_SIZE] = "";Quoted 5 lines
block/file-posix.c +5
-0+if (lseek (fd,0 ,SEEK_SET ) ==-1 ) { + ret = -errno; + }else { + buf =g_malloc0 (zero_size); + ret =qemu_write_full (fd, buf, zero_size);
Quoted 5 lines
Quoted 3 lines
Instead of doing lseek +
qemu_write_full, can we just useqemu_pwritev(fd, &iov, 1, 0)with an iov set up to point to the appropriate amount of buf?Neat, will update.
For stack-allocation yes, but qemu_write_full turns out to be better because of short write handling. So I'll keep it in v2.
Fam
Quoted 1 line · Fam Zheng
People get surprised when, after "qemu-imc create -f raw
/dev/sdX", they
s/qemu-imc/qemu-img/
Quoted 1 line
still see qcow2 with "qemu-img info", if previously the bdev had a qcow2
Signature · 2 lines
Thanks
-XieQuoted 4 lines · Xie Changlong
Quoted 1 line · Fam Zheng
People get surprised when, after "qemu-imc create -f raw
/dev/sdX", theys/qemu-imc/qemu-img/
Thanks, will fix in v2.
Fam