block: fix snapshot on QED

QED's opaque data includes a pointer back to the BlockDriverState.
This breaks when bdrv_append shuffles data between bs_new and bs_top.
To avoid this, add a "rebind" function that tells the driver about
the new relationship between the BlockDriverState and its opaque.

The patch also adds rebind to VVFAT for completeness, even though
it is not used with live snapshots.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: close unused image files at the end of streaming

Close the now unused images that were part of the previous backing file
chain and adjust ->backing_hd properly.

Note that this only works with relative paths.
Given the images:

 /tmp/a/base.raw
 /tmp/a/snap1.qcow2
 /tmp/b/snap2.qcow2

chained as:

 base(bak:"") <- snap1(bak:"base.raw") <- snap2(bak:"../a/snap1.qcow2")

merging snap1 and snap2 we will obtain:

 base(bak:"") <- snap2(bak:"base.raw")

However this should be maintained by the user/admin: one can also
screw up relative paths using qemu-img manually.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Federico Simoncelli <fsimonce@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: pass new base image format to bdrv_change_backing_file

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: push bdrv_change_backing_file error checking up from drivers

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: update in-memory backing file and format

These are needed to print "info block" output correctly.  QCOW2 does this
because it needs it to write the header, but QED does not, and common code
is the right place to do it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add mirroring to transaction

With it comes a new image creation mode, "no-backing-file", that can
be used to stream an image so that the destination does not need the
original image's backing file(s).

Both bdrv_append and blkmirror will set the backing_hd on the target,
even if the image is created without one, so that both streaming and
copy-on-write work properly (at least with qcow2 or qed, not raw).

Streaming mode works with the following gotchas:

- streaming will rewrite every bit of the source image;

- zero writes are not supported by the blkmirror driver, hence both
  the source and the destination image will grow to full size.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add drive-mirror command and HMP equivalent

Since QMP transactions are supposed to take (pseudo) QMP commands,
add a standalone drive-mirror command.

The corresponding HMP command does not provide atomic snapshot+mirror,
but it can still be used together with image streaming.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add the drive-reopen command

Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: copy over job and dirty bitmap fields in bdrv_append

While these should not be in use at the time a transaction
is started, a command in the prepare phase of a transaction
might have added them, so they need to be brought over.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: introduce new dirty bitmap functionality

Also factorize the repeated usage of sizeof(unsigned long) * 8.

Assert that write_compressed is never used with the dirty bitmap.
Setting the bits early is wrong, because a coroutine might concurrently
examine them and copy incomplete data from the source.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: allow interrupting a co_sleep_ns

In the next patch we want to reenter the coroutine from
block_job_cancel_sync and cancel the timer.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: allow doing I/O in a job after cancellation

Track the coroutine that executes the job, so that the wait can be
cancelled before block_job_cancel restarts.  This also gives to the
coroutine an opportunity to flip job->busy to true, and submit new
I/O before exiting.  block_job_cancel_sync will wait for job->busy
to become false again.

This is needed for mirroring.  Once mirroring reaches "steady state"
(i.e. all data from the source is also in the target, except for new
writes coming in from the guest), cancellation of the job will keep
the disks synchronized.  The job thus requires to handle cancellation
with care, and this patch provides the infrastructure.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add mirror job

This patch adds the implementation of a new job that mirrors a disk to
a new image while letting the guest continue using the old image.
The target is treated as a "black box" and data is copied from the
source to the target in the background.

The mirror job is never-ending, but it is logically structured into
two phases: 1) copy all data as fast as possible until the target
first gets in sync with the source; 2) keep target in sync and
ensure that reopening to the target gets a correct (full) copy
of the source data.

The second phase is indicated by the progress in "info block-jobs"
reporting the current offset to be equal to the length of the file.
When the job is cancelled in the second phase, QEMU will run the
job until the source is clean and quiescent, then it will report
successful completion of the job.  (Note that it could already happen
that management lost the race against QEMU and got a completion
event instead of cancellation).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: modify drive-mirror implementation

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: cancel job on drive-reopen

When used to switch storage after mirroring, drive-reopen must atomically
cancel the mirroring job and open the target.  Do this before calling
bdrv_in_use.  This has a nice side effect: if block_job_cancel_sync fails
to complete the job due to a bug, reopening will fail instead of proceeding
with corrupted data!

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
stream: fix ratelimiting corner case

This fixes inability to make progress in streaming if the quota
is set to less than the amount of data that an I/O operation has
to write.

In this case, limit->dispatched + n will always be above the
quota and, due to the "goto retry" to recheck cancellation and
allocation, streaming will livelock.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: move rate limiting to a separate header file

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
mirror: use ratelimit.h

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-io: fix the alloc command

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

Either FIEMAP, or SEEK_DATA+SEEK_HOLE can be used to implement the
is_allocated callback for raw files.  Ext4, btrfs and XFS all support
it.

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