block: do not rely on open_flags for bdrv_is_snapshot

The BDRV_O_SNAPSHOT flag is stored in open_flags but not passed to
bdrv_open.  This makes the usage of bs->open_flags wrong in
bdrv_snapshot_goto.

(Instead, bdrv_commit uses the backing file's open_flags and those
flags never include any of BDRV_O_SNAPSHOT, BDRV_O_NO_BACKING
or BDRV_O_RDWR).

We will fix the open_flags soon.  In the meanwhile, do not rely
on the fact that BDRV_O_SNAPSHOT is stored in open_flags.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: store actual flags in bs->open_flags

The passed flags are changed slightly before passing them to
bdrv_open.  Store the same flags in bs->open_flags, so that
they are used correctly in bdrv.  In addition, this way we
will be able to query them and get back consistent values.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: pass protocol flags up to the format

In the next patches, the protocols will modify bs->open_flags to signify
that they cannot support the exact requested feature set.  Pass the
modified flags to the format.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: non-raw protocols never cache

Non-raw protocols never cache their data.  Make this visible by setting
BDRV_O_NOCACHE in the open_flags.  It will be used to prohibit block sizes
smaller than the backend's block size.

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

This duplicates a bit in open_flags, we do not need it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: move flag bits together

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw: remove the aligned_buf

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: rename buffer_alignment to guest_block_size

The alignment field is now set to the value that is promised to the guest,
rather than required by the host.  The next patches will make QEMU aware
of the host-provided values, so make this clear.

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

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

Use ioctls if possible, else see what alignment it takes for O_DIRECT
to succeed.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
iscsi: save host block size

The iSCSI backend already gets the block size from the READ CAPACITY
command it sends.  Save it so that the generic block layer gets it
too.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: allow waiting only for overlapping writes

To implement mismatching block size, we will reuse the request tracking
mechanism that is used for copy-on-read.  However, waiting for overlapping
reads is not needed to protect against "torn reads", so add a flag to
wait_for_overlapping_requests.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: allow waiting at arbitrary granularity

When emulating small logical block sizes, the only overlaps
that matter are at host block size granularity, not cluster.
Make wait_for_overlapping_requests more flexible in this
respect, too.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: protect against "torn reads" for guest_block_size > host_block_size

When the guest sees a higher alignment than the host, writes may be
done in multiple steps.  So, reads have to be serialized against
overlapping writes, so that the writes look atomic to the guest.
This is true even when O_DIRECT is not in use.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: align and serialize I/O when guest_block_size < host_block_size

When the guest sees a lower alignment than the host, and O_DIRECT
is in place, I/O might not span an integer number of host sectors.
If this is the case, add a few sectors at the beginning and the end.
When reading, copy interesting data from there to the guest buffer.

When writing, merge data from there with data from the guest buffer.
Since writes potentially become read-modify-write sequences, they have
to be serialized against other operations.  Reads are still atomic
(they only need some postprocessing), so they need not be
serialized.

The math is a bit tricky, but luckily all the primitives are already in
cutils.c and iov.c.

On top of this, the guest's buffers might be misaligned with respect
to the host block size.  However, that's caught by raw-posix.c and
posix-aio-compat.c, so we need not care about it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: default physical block size to host block size

Hopefully, this will help operating systems in making less misaligned
I/O operations.  Not really true for Linux, but why not try.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: default min_io_size to host block size when doing rmw

Same as the previous patch.  However, since the min_io_size is a byte
value rather than an exponent, we want to make sure that we pass a 0
when running on 512b-sector disks.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-io: add blocksize argument to open

Make it possible to test the alignment code using qemu-io.

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