migration: yay, buffering is gone

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: buffered_file is no more

In a dozen patches or so, we'll add QEMUFile buffering elsewhere.
Rename the callbacks to remove a source of confusion.

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>
qemu-file: add flush operation

Allow QEMUFiles to specify a function to execute when qemu_fflush is
called.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
savevm: only buffer data for savevm, or while writing devices data

Stop buffering data during migration.  To do this, introduce a new kind
of QEMUFile that adds buffering to a lower-level QEMUFile.  All other
files will send a qemu_put_buffer to the OS or block layer immediately.

The buffering can be transparent (i.e. closing the QEMUFileBuffered also
closes the lower-level file) or manual.

Saving device data is still buffered because it consists of a lot of small
fields; to free the layered QEMUFile without touching the underlying one,
the manual mode is used (close = false in qemu_fopen_buffer).  The transparent
kind is used by savevm, which also uses buffering when writing RAM (because
I'm not sure how QCOW2 likes many unaligned writes).

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>
migration: move contents of migration_close to migrate_fd_cleanup

With this patch, the migration_file is not needed anymore.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: eliminate s->migration_file

The indirection is useless now.  Backends can open s->file directly.

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

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