qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Maxim Levitsky <mlevitsk@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Fam Zheng" <fam@euphon.net>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Stefan Hajnoczi" <stefanha@gmail.com>,
	"Maxim Levitsky" <mlevitsk@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH v4 7/9] scsi/scsi_bus: Add scsi_device_get
Date: Mon, 31 Aug 2020 18:01:22 +0300	[thread overview]
Message-ID: <20200831150124.206267-8-mlevitsk@redhat.com> (raw)
In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com>

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     | 22 +++++++++++++++++-----
 include/hw/scsi/scsi.h |  1 +
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c
index 7ceae2c92b..feab20b76d 100644
--- a/hw/scsi/scsi-bus.c
+++ b/hw/scsi/scsi-bus.c
@@ -52,7 +52,7 @@ static const TypeInfo scsi_bus_info = {
 static int next_scsi_bus;
 
 static SCSIDevice *_scsi_device_find(SCSIBus *bus, int channel, int id, int lun,
-                                     bool include_unrealized)
+                                     bool include_unrealized, bool take_ref)
 {
     BusChild *kid;
     SCSIDevice *retval = NULL;
@@ -92,13 +92,22 @@ out:
         retval = NULL;
     }
 
+    if (take_ref) {
+        object_ref(OBJECT(retval));
+    }
+
     rcu_read_unlock();
     return retval;
 }
 
 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
 {
-    return _scsi_device_find(bus, channel, id, lun, false);
+    return _scsi_device_find(bus, channel, id, lun, false, false);
+}
+
+SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
+{
+    return _scsi_device_find(bus, channel, id, lun, false, true);
 }
 
 static void scsi_device_realize(SCSIDevice *s, Error **errp)
@@ -236,7 +245,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
             dev->lun = 0;
         }
         do {
-            d = _scsi_device_find(bus, dev->channel, ++id, dev->lun, true);
+            d = _scsi_device_find(bus, dev->channel, ++id, dev->lun,
+                                  true, false);
         } while (d && d->lun == dev->lun && id < bus->info->max_target);
         if (d && d->lun == dev->lun) {
             error_setg(errp, "no free target");
@@ -246,7 +256,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
     } else if (dev->lun == -1) {
         int lun = -1;
         do {
-            d = _scsi_device_find(bus, dev->channel, dev->id, ++lun, true);
+            d = _scsi_device_find(bus, dev->channel, dev->id, ++lun,
+                                 true, false);
         } while (d && d->lun == lun && lun < bus->info->max_lun);
         if (d && d->lun == lun) {
             error_setg(errp, "no free lun");
@@ -254,7 +265,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
         }
         dev->lun = lun;
     } else {
-        d = _scsi_device_find(bus, dev->channel, dev->id, dev->lun, true);
+        d = _scsi_device_find(bus, dev->channel, dev->id, dev->lun,
+                              true, false);
         assert(d);
         if (d->lun == dev->lun && dev != d) {
             error_setg(errp, "lun already used by '%s'", d->qdev.id);
diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
index 2fc23e44ba..e939a81789 100644
--- a/include/hw/scsi/scsi.h
+++ b/include/hw/scsi/scsi.h
@@ -194,6 +194,7 @@ 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



  parent reply	other threads:[~2020-08-31 15:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-31 15:01 [PATCH v4 0/9] Fix scsi devices plug/unplug races w.r.t virtio-scsi iothread Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 1/9] scsi/scsi_bus: switch search direction in scsi_device_find Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 2/9] rcu: Implement drain_call_rcu Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 3/9] device_core: use drain_call_rcu in in hmp_device_del/qmp_device_add Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 4/9] device-core: use RCU for list of childs of a bus Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 5/9] device-core: use atomic_set on .realized property Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 6/9] scsi/scsi-bus: scsi_device_find: don't return unrealized devices Maxim Levitsky
2020-09-08 15:00   ` Stefan Hajnoczi
2020-09-09  8:15     ` Maxim Levitsky
2020-08-31 15:01 ` Maxim Levitsky [this message]
2020-08-31 15:01 ` [PATCH v4 8/9] virtio-scsi: use scsi_device_get Maxim Levitsky
2020-08-31 15:01 ` [PATCH v4 9/9] scsi/scsi_bus: fix races in REPORT LUNS Maxim Levitsky
2020-09-01  9:15   ` Maxim Levitsky
2020-09-08 15:27   ` Stefan Hajnoczi
2020-09-09  8:20     ` Maxim Levitsky
2020-09-11 15:12       ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200831150124.206267-8-mlevitsk@redhat.com \
    --to=mlevitsk@redhat.com \
    --cc=berrange@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=fam@euphon.net \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).