migration: remove argument to qemu_savevm_state_cancel

We will have to call it after the QEMUFile is closed in the case
of migration.  Make it explicit that it is a legal thing to do.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: Only go to the iterate stage if there is anything to send

Signed-off-by: Orit Wasserman <owasserm@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
qemu-file: expose qemu_file_set_error

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: 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: reorganize rate-limiting logic

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: 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>
migration: push qemu_savevm_state_cancel out of qemu_savevm_state_*

This is useful, because it lets us keep the cancellation callbacks
inside the big lock while pushing the others out.

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>
block-migration: simplify block_save_setup

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

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.

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>
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: 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>