block-migration: remove variables that are never read

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

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: 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.  Only the migration_bitmap_sync() call
needs the iothread lock.

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>
migration: eliminate last_round

We will go around the loop exactly once after setting last_round.
Eliminate the variable altogether.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: detect error before sleeping

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

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: merge qemu_popen_cmd with qemu_popen

There is no reason for outgoing exec migration to do popen manually
anymore (the reason used to be that we needed the FILE* to make it
non-blocking).  Use qemu_popen_cmd.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: fsync a writable stdio QEMUFile

This is what fd_close does.  Prepare for switching to a QEMUFile.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: check exit status when closing a pipe QEMUFile

This is what exec_close does.  Move this to the underlying QEMUFile.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: add writable socket QEMUFile

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: simplify and export qemu_ftell

Force a flush when qemu_ftell is called.  This simplifies the buffer magic
(it also breaks qemu_ftell for input QEMUFiles, but we never use it).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: remove useless qemu_file_get_error check

migration_put_buffer is never called if there has been an error.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: use QEMUFile for migration channel lifetime

As a start, use QEMUFile to store the destination and close it.
qemu_get_fd gets a file descriptor that will be used by the write
callbacks.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: use QEMUFile for writing outgoing migration data

Second, drop the file descriptor indirection, and write directly to the
QEMUFile.

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