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>
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>
migration: use qemu_ftell to compute bandwidth

Prepare for when s->bytes_xfer will be removed.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: small changes around rate-limiting

This patch extracts a few small changes from the next patch, which
are unrelated to adding generic rate-limiting functionality to
QEMUFile.  Make migration_set_rate_limit a simple accessor, and
use qemu_file_set_rate_limit consistently.  Also fix a typo where
INT_MAX should have been SIZE_MAX.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: move rate limiting to QEMUFile

Rate limiting is now simply a byte counter; client call
qemu_file_rate_limit() manually to determine if they have to exit.
So it is possible and simple to move the functionality to QEMUFile.

This makes the remaining functionality of s->file redundant;
in the next patch we can remove it and write directly to s->migration_file.

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