qemu: Prevent detaching SCSI controller used by hostdev
qemu: Prevent detaching SCSI controller used by hostdev

Consider the following XML snippets:

  $ cat scsicontroller.xml
      <controller type='scsi' model='virtio-scsi' index='0'/>
  $ cat scsihostdev.xml
      <hostdev mode='subsystem' type='scsi'>
        <source>
          <adapter name='scsi_host0'/>
          <address bus='0' target='8' unit='1074151456'/>
        </source>
      </hostdev>

If we create a guest that includes the contents of scsihostdev.xml,
but forget the virtio-scsi controller described in scsicontroller.xml,
one is silently created for us.  The same holds true when attaching
a hostdev before the matching virtio-scsi controller.
(See qemuDomainFindOrCreateSCSIDiskController for context.)

Detaching the hostdev, followed by the controller, works well and the
guest behaves appropriately.

If we detach the virtio-scsi controller device first, any associated
hostdevs are detached for us by the underlying virtio-scsi code (this
is fine, since the connection is broken).  But all is not well, as the
guest is unable to receive new virtio-scsi devices (the attach commands
succeed, but devices never appear within the guest), nor even be
shutdown, after this point.

While this is not libvirt's problem, we can prevent falling into this
scenario by checking if a controller is being used by any hostdev
devices.  The same is already done for disk elements today.

Applying this patch and then using the XML snippets from earlier:

  $ virsh detach-device guest_01 scsicontroller.xml
  error: Failed to detach device from scsicontroller.xml
  error: operation failed: device cannot be detached: device is busy

  $ virsh detach-device guest_01 scsihostdev.xml
  Device detached successfully

  $ virsh detach-device guest_01 scsicontroller.xml
  Device detached successfully

Signed-off-by: Eric Farman <45cff695b8fa5cc07d58f6876a8fd504d010e3e3@linux.vnet.ibm.com>
Reviewed-by: Bjoern Walk <40545c009452f40d2ae0bbfbd987b332d274d328@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <2648abdd55bfebf313fd324b464b047e6eea300e@linux.vnet.ibm.com>

fabianfreyer/libvirt,datto/libvirt,zippy2/libvirt,nertpinx/libvirt,zippy2/libvirt,nertpinx/libvirt,olafhering/libvirt,jardasgit/libvirt,andreabolognani/libvirt,eskultety/libvirt,VenkatDatta/libvirt,fabianfreyer/libvirt,libvirt/libvirt,crobinso/libvirt,jfehlig/libvirt,jfehlig/libvirt,taget/libvirt,crobinso/libvirt,datto/libvirt,jardasgit/libvirt,eskultety/libvirt,datto/libvirt,crobinso/libvirt,VenkatDatta/libvirt,jfehlig/libvirt,fabianfreyer/libvirt,jardasgit/libvirt,eskultety/libvirt,eskultety/libvirt,andreabolognani/libvirt,nertpinx/libvirt,andreabolognani/libvirt,fabianfreyer/libvirt,zippy2/libvirt,olafhering/libvirt,zippy2/libvirt,nertpinx/libvirt,fabianfreyer/libvirt,nertpinx/libvirt,andreabolognani/libvirt,VenkatDatta/libvirt,datto/libvirt,taget/libvirt,libvirt/libvirt,crobinso/libvirt,olafhering/libvirt,taget/libvirt,datto/libvirt,taget/libvirt,andreabolognani/libvirt,libvirt/libvirt,VenkatDatta/libvirt,taget/libvirt,VenkatDatta/libvirt,jfehlig/libvirt,libvirt/libvirt,eskultety/libvirt,olafhering/libvirt,jardasgit/libvirt,jardasgit/libvirt