[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 6/7] scsi: Add scsi_device_get
From: |
Maxim Levitsky |
Subject: |
[PATCH v3 6/7] scsi: Add scsi_device_get |
Date: |
Wed, 15 Jul 2020 18:01:58 +0300 |
Add scsi_device_get which finds the scsi device
and takes a reference to it.
Suggested-by: Stefan Hajnoczi <stefanha@gmail.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
hw/scsi/scsi-bus.c | 31 ++++++++++++++++++++++++-------
include/hw/scsi/scsi.h | 2 ++
2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c
index 92d412b65c..677074eaa6 100644
--- a/hw/scsi/scsi-bus.c
+++ b/hw/scsi/scsi-bus.c
@@ -1572,8 +1572,13 @@ static char *scsibus_get_fw_dev_path(DeviceState *dev)
return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
qdev_fw_name(dev), d->id, d->lun);
}
-
-SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
+/*
+ * Finds a matching channel/id/lun device on scsi bus, and
+ * takes a reference to it and returns it.
+ * If we don't find exact match (channel/bus/lun),
+ * we will return the first device which matches channel/bus
+ */
+SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
{
BusChild *kid;
SCSIDevice *target_dev = NULL;
@@ -1586,25 +1591,37 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channel,
int id, int lun)
if (dev->channel == channel && dev->id == id) {
if (dev->lun == lun) {
+ object_ref(OBJECT(dev));
rcu_read_unlock();
return dev;
}
- /*
- * If we don't find exact match (channel/bus/lun),
- * we will return the first device which matches channel/bus
- */
-
if (!target_dev) {
target_dev = dev;
}
}
}
+ object_ref(OBJECT(target_dev));
rcu_read_unlock();
return target_dev;
}
+/*
+ * This function works like scsi_device_get but doesn't take a reference
+ * to the returned object.
+ * Devices that run under the QEMU global mutex can use this function.
+ * Devices that run outside the QEMU global mutex must use
+ * scsi_device_get() instead.
+ */
+SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
+{
+ SCSIDevice *dev = scsi_device_get(bus, channel, id, lun);
+
+ object_unref(OBJECT(dev));
+ return dev;
+}
+
/* SCSI request list. For simplicity, pv points to the whole device */
static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
index 2fc23e44ba..6e11ece641 100644
--- a/include/hw/scsi/scsi.h
+++ b/include/hw/scsi/scsi.h
@@ -193,7 +193,9 @@ void scsi_generic_read_device_inquiry(SCSIDevice *dev);
int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
uint8_t *buf, uint8_t buf_size);
+
SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
+SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);
/* scsi-generic.c. */
extern const SCSIReqOps scsi_generic_req_ops;
--
2.26.2
- [PATCH v3 0/7] Fix scsi devices plug/unplug races w.r.t virtio-scsi iothread, Maxim Levitsky, 2020/07/15
- [PATCH v3 1/7] scsi/scsi_bus: switch search direction in scsi_device_find, Maxim Levitsky, 2020/07/15
- [PATCH v3 3/7] device-core: use RCU for list of childs of a bus, Maxim Levitsky, 2020/07/15
- [PATCH v3 4/7] device-core: use atomic_set on .realized property, Maxim Levitsky, 2020/07/15
- [PATCH v3 2/7] Implement drain_call_rcu and use it in hmp_device_del, Maxim Levitsky, 2020/07/15
- [PATCH v3 5/7] virtio-scsi: don't touch scsi devices that are not yet realized or about to be un-realized, Maxim Levitsky, 2020/07/15
- [PATCH v3 7/7] virtio-scsi: use scsi_device_get, Maxim Levitsky, 2020/07/15
- [PATCH v3 6/7] scsi: Add scsi_device_get,
Maxim Levitsky <=
- Re: [PATCH v3 0/7] Fix scsi devices plug/unplug races w.r.t virtio-scsi iothread, Maxim Levitsky, 2020/07/29