buffered_file: move to migration.c

We want to make QEMUBufferedFile a thinner wrapper, using MigrationState
directly.  Move its contents to migration.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: make functions static

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

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

The "magic" divisions by 10 are there because of the value of BUFFER_DELAY.
Introduce a constant to explain them better.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: fix qemu_get_fd for BufferedFile

Not really used, but nice to have it correct. :)

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: merge QEMUFileBuffered into MigrationState

Avoid splitting the state of outgoing migration, more or less arbitrarily,
between two data structures.  QEMUFileBuffered anyway is used only during
migration.

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

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: inline migrate_fd_put_ready into caller

Prepare for simplifying the body of the migration thread.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: simplify beginning of inlined migrate_fd_put_ready

last_round remain false until explicitly set to true, and s->state
can be checked outside the iothread mutex.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: make first_time a local variable

It is not written (except for ensuring it is initially true) outside of
buffered_file_thread.

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

And since it is now public, make it set the last_error of the QEMUFile.

We will need this to simplify the switch to MIG_STATE_COMPLETED.

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: simplify error handling

Always use qemu_file_get_error to detect errors.  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: 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>