block-migration: remove useless calls to blk_mig_cleanup

Now that the cancel callback is called consistently for all errors,
we can avoid doing its work in the other callbacks.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: pass errno from qemu_fflush via f->last_error

This is done by almost all callers of qemu_fflush, move the code
directly to qemu_fflush.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: use qemu_file_set_error to pass error codes back to qemu_savevm_state

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: temporarily expose qemu_file_set_error and qemu_fflush

Right now, migration cannot entirely rely on QEMUFile's automatic
drop of I/O after an error, because it does its "real" I/O outside
the put_buffer callback.  To fix this until buffering is gone, expose
qemu_file_set_error which we will use in buffered_flush.

Similarly, buffered_flush is not a complete flush because some data may
still reside in the QEMUFile's own buffer.  This somewhat complicates the
process of closing the migration thread.  Again, when buffering is gone
buffered_flush will disappear and calling qemu_fflush will not be needed;
in the meanwhile, we expose the function for use in migration.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: flush all data to fd when buffered_flush is called

Including data that resided in the QEMUFile's own buffer.

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

Remove the return value of buffered_flush, pass it via the error code
of s->file.  Once this is done, the error can be retrieved simply
via migrate_fd_close's call to qemu_fclose.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: simplify error handling

Always use qemu_file_get_error to detect errors, since that is how
QEMUFile itself drops I/O after an error occurs.  There is no need
to propagate and check return values all the time.

Also remove the "complete" member, since we know that it is set (via
migrate_fd_cleanup) only when the state changes.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: do not nest flushing of device data

Completion of migration is currently done with a "nested" loop that
invokes buffered_flush: migrate_fd_completed is called by
buffered_file_thread, which calls migrate_fd_cleanup, which calls
buffered_close (via qemu_fclose), which flushes the buffer.

Simplify this, by reusing the buffered_flush call of buffered_file_thread.
Then if qemu_savevm_state_complete was called, and the buffer is empty
(including the QEMUFile buffer, for which we need the previous patch), we
are done.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: prepare to access s->state outside critical sections

Accessing s->state outside the big QEMU lock will simplify a bit the
locking/unlocking of the iothread lock.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: cleanup migration (including thread) in the iothread

Perform final cleanup in a bottom half, and add joining the thread to
the series of cleanup actions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block-migration: remove variables that are never read

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block-migration: small preparatory changes for locking

Some small changes that will simplify the positioning of lock/unlock
primitives
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block-migration: document usage of state across threads

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

Some state is shared between the block migration code and its AIO
callbacks.  Once block migration will run outside the iothread,
the block migration code and the AIO callbacks will be able to
run concurrently.  Protect the critical sections with a separate
lock.  Do the same for completed_sectors, which can be used from
the monitor.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: reorder SaveVMHandlers members

This groups together the callbacks that later will have similar
locking rules.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: run pending/iterate callbacks out of big lock

This makes it possible to do blocking writes directly to the socket,
with no buffer in the middle.  For RAM, only the migration_bitmap_sync()
call needs the iothread lock.  For block migration, it is needed by
the block layer (including bdrv_drain_all and dirty bitmap access),
but because some code is shared between iterate and complete, all of
mig_save_device_dirty is run with the lock taken.

In the savevm case, the iterate callback runs within the big lock.
This is annoying because it complicates the rules.  Luckily we do not
need to do anything about it: the RAM iterate callback does not need
the iothread lock, and block migration never runs during savevm.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: run setup callbacks out of big lock

Only the migration_bitmap_sync() call needs the iothread lock.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: yay, buffering is gone

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Rename buffered_ to migration_

This is consistent once that we have moved everything to migration.c

Signed-off-by: Juan Quintela <quintela@redhat.com>
qemu-file: make qemu_fflush and qemu_file_set_error private again

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