]> git.proxmox.com Git - qemu.git/commit - block.c
block: Produce zeros when protocols reading beyond end of file
authorMORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Tue, 6 Aug 2013 01:53:40 +0000 (09:53 +0800)
committerStefan Hajnoczi <stefanha@redhat.com>
Thu, 22 Aug 2013 12:14:56 +0000 (14:14 +0200)
commit893a8f6220368a9ebff9a74bd48359928545cf6a
tree7ef438d1504126ea408d9c29c84ac51224210405
parent0d51b4debee6fb322751a57097a1d675c7a7c38d
block: Produce zeros when protocols reading beyond end of file

While Asias is debugging an issue creating qcow2 images on top of
non-file protocols.  It boils down to this example using NBD:

$ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512'

Notice the open -g option to set bs->growable.  This means you can
read/write beyond end of file.  Reading beyond end of file is supposed
to produce zeroes.

We rely on this behavior in qcow2_create2() during qcow2 image
creation.  We create a new file and then write the qcow2 header
structure using bdrv_pwrite().  Since QCowHeader is not a multiple of
sector size, block.c first uses bdrv_read() on the empty file to fetch
the first sector (should be all zeroes).

Here is the output from the qemu-io NBD example above:

$ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512'
00000000:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
00000010:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
00000020:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
...

We are not zeroing the buffer!  As a result qcow2 image creation on top
of protocols is not guaranteed to work even when file creation is
supported by the protocol.

[Adapted this patch to use bs->zero_beyond_eof.
-- Stefan]

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Signed-off-by: Asias He <asias@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
block.c