block: update bs->total_sectors on writes

If a BlockDriverState is growable, after every write we need to
check if bs->total_sectors might have changed.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
cow: make writes go at a less indecent speed

Only sync once per write, rather than once per sector.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
cow: do not call bdrv_co_is_allocated

As we change bdrv_is_allocated to gather more information from bs and
bs->file, it will become a bit slower.  It is still appropriate for online
jobs, but not for reads/writes.  Call the internal function instead.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: make bdrv_co_is_allocated static

bdrv_is_allocated can detect coroutine context and go through a fast
path, similar to other block layer functions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: remove bdrv_is_allocated_above/bdrv_co_is_allocated_above distinction

Now that bdrv_is_allocated detects coroutine context, the two can
use the same code.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: expect errors from bdrv_co_is_allocated

Some bdrv_is_allocated callers do not expect errors, but the fallback
in qcow2.c might make other callers trip on assertion failures or
infinite loops.

Fix the callers to always look for errors.

Cc: qemu-stable@nongnu.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-img: always probe the input image for allocated sectors

qemu-img convert is assuming "that sectors which are unallocated in the input
image are present in both the output's and input's base images", but it is
only doing this if the output image is zero initialized.  And checking if
the output image is zero initialized does not make much sense if the
output image is copy-on-write.  Always do the test.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: make bdrv_has_zero_init return false for copy-on-write-images

This helps implementing is_allocated on top of get_block_status.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: introduce bdrv_get_block_status API

For now, bdrv_get_block_status is just another name for bdrv_is_allocated.
The next patches will add more flags.

This also touches all block drivers with a mostly mechanical rename.  The
sole exception is cow; because it calls cow_co_is_allocated from the read
code, we keep that function and make cow_co_get_block_status a wrapper.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: define get_block_status return value

Define the return value of get_block_status.  Bits 0, 1, 2 and 9-62
are valid; bit 63 (the sign bit) is reserved for errors.  Bits 3-7
are left for future extensions.

The return code is compatible with the old is_allocated API: returning
just 0 or 1 (aka BDRV_BLOCK_DATA) will not cause any behavioral change
in clients of is_allocated.  We will return more precise information
in the next patches.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qapi: document the JSON output of "qemu-img map"

Requested by Eric Blake so that it is published through the
introspection mechanism.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: return get_block_status data and flags for formats

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-img: add a "map" subcommand

This command dumps the metadata of an entire chain, in either tabular or JSON
format.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: use bdrv_has_zero_init to return BDRV_BLOCK_ZERO

Alternatively, this could use a "discard zeroes data" flag returned
by bdrv_get_info.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: return get_block_status data and flags

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: detect XFS unwritten extents

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add default get_block_status implementation for protocols

Protocols return raw data, so you can assume the offsets to pass
through unchanged.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: look for zero blocks in bs->file

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>