raw-posix: always prefer specific devices to hdev

There is no need to try matching device names; using ioctls is more
effective.  So, always return a low priority from the generic
hdev_probe_device and let the ioctl tests override it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: put Linux fd fields into a union

We will add CDROM-specific fields in the next patch, reuse the space
in the struct.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: keep complete control of door locking if possible

Try to open the disk O_EXCL so that udev will not receive eject request
and media change events.  If we cannot do that, and the guest is using
scsi-block, the host and guest would race on receiving media change
notifications from the disc.  So, without O_EXCL the only sensible
choice is to ignore completely the guest's door management.

With IDE passthrough, we do not need O_EXCL in principle.  However,
using the emulated passthrough CD-ROM gets confusing without O_EXCL
(to the user and to the guest, always; and sometimes, to the host udev
as well).  The guest tries to lock the disk, but the host udev runs as
root and will unlock it as soon as it receives an eject request.

It is possible to fix IDE with more work, tracking the host tray state
and converting it to media change events and sense codes in the guest.
It can be still confusing to the user, because you will need to use the
monitor in order to eject the disk and put it back.  However, it looks
good to the host and VM.  The missing bit would be to (re)implement
polling ourselves using GET EVENT STATUS NOTIFICATION.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: do not report tray movement errors

Emulation of the CD-ROM tray has to be cooperative; you can never
be sure that nobody else opens /dev/cdrom after you, and at that
point the kernel will prevent you from ejecting the disk.  So we
cannot be sure that the ioctls will succeed, and printing errors
with strerror is not going to produce helpful messages in general;
better shut up and swallow.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
configure: probe for dbus

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
raw-posix: unmount CD-ROM filesystem via udisks

We need to use O_EXCL in order to suppress event generation in the
kernel.  However, O_EXCL by definition fails when the CD-ROM drive
is mounted.  Automatically unmount it when it is passed through to
the guest.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: change bdrv_is_inserted to bdrv_media_state

For IDE CD-ROM passthrough we need to track the host's tray state
and media readiness.  Extend bdrv_is_inserted so that the devices
can provide the information.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ide: switch to bdrv_media_state

Start by switching to the new function without any behavioral
change (except that we only issue the CDROM_DRIVE_STATUS ioctl only
once).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ide: derive ascq from tray_open

The sense codes that QEMU is providing are not too helpful.  We can
provide information on the tray status in the additional sense code
qualified.  Luckily, that's all that we need to do with the ascq,
so we can build it on the fly when processing REQUEST SENSE and not
care about migration.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: return emulated tray state from bdrv_media_state

We can emulate the MEDIUM_TRAY_OPEN state too.  This simplifies a
bit the detection of not ready conditions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ide: track host tray state

If we find that the host's tray changed state, report this to the guest.
The change media callback is perfect for this!

With this change, the tray state will always be "closed" when the
media state is MEDIUM_OK, so simplify another conditional.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ide: report media readiness

If the host says that the medium is not ready (but "is in the process
of becoming ready", as SPC puts it) the NOT READY sense key is fine,
but we should report a different asc/ascq pair.

Also, report the "medium not present" unit attention condition as soon
as the new medium has been detected, but delay "medium may have
changed" until after the drive leaves not ready state.  This has
no effect on operation with ISO images, because they do not report
MEDIUM_NOT_READY, but matches this sequence of sense codes from a
real drive:

1) Sense key: Not Ready, Additional sense: Medium not present - tray open
2) Sense key: Not Ready, Additional sense: Medium not present - tray closed
3) Sense key: Not Ready, Additional sense: Logical unit is in process
   of becoming ready
4) Sense key: Unit Attention, Additional sense: Not ready to ready change,
   medium may have changed

(The sense codes were obtained with a continuous stream of TEST UNIT READY
commands, using sg_turs while the CD is opened with O_EXCL):

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: poll for host eject requests

We would like the host eject button to work even when the host CD drive
is being passed down as an emulated IDE drive.  To do this, whenever the
guest sends GET EVENT STATUS NOTIFICATION we look for eject request events
and pass them down.

This is a major usability improvement, so we do it for emulated SCSI disks
as well (even though for SCSI it is simpler and better to use scsi-block
instead).

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