diff mbox

[06/11] target: add helper to iterate over devices v2

Message ID 1497649650-7605-7-git-send-email-mchristi@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Mike Christie June 16, 2017, 9:47 p.m. UTC
This adds a wrapper around idr_for_each so the xcopy code can loop over
the devices in the next patch.

v2:
Add check for configured devices.

Signed-off-by: Mike Christie <mchristi@redhat.com>
---
 drivers/target/target_core_device.c   | 45 +++++++++++++++++++++++++++++++++++
 drivers/target/target_core_internal.h |  2 ++
 2 files changed, 47 insertions(+)

Comments

Bart Van Assche June 21, 2017, 6:18 p.m. UTC | #1
On Fri, 2017-06-16 at 16:47 -0500, Mike Christie wrote:
> This adds a wrapper around idr_for_each so the xcopy code can loop over
> the devices in the next patch.

Reviewed-by: Bart Van Assche <bart.vanassche@wdc.com>

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c
index 544e38e..01b90d3 100644
--- a/drivers/target/target_core_device.c
+++ b/drivers/target/target_core_device.c
@@ -904,6 +904,51 @@  struct se_device *target_find_device(int id, bool do_depend)
 }
 EXPORT_SYMBOL(target_find_device);
 
+struct devices_idr_iter {
+	int (*fn)(struct se_device *dev, void *data);
+	void *data;
+};
+
+static int target_devices_idr_iter(int id, void *p, void *data)
+{
+	struct devices_idr_iter *iter = data;
+	struct se_device *dev = p;
+
+	/*
+	 * We add the device early to the idr, so it can be used
+	 * by backend modules during configuration. We do not want
+	 * to allow other callers to access partially setup devices,
+	 * so we skip them here.
+	 */
+	if (!(dev->dev_flags & DF_CONFIGURED))
+		return 0;
+
+	return iter->fn(dev, iter->data);
+}
+
+/**
+ * target_for_each_device - iterate over configured devices
+ * @fn: iterator function
+ * @data: pointer to data that will be passed to fn
+ *
+ * fn must return 0 to continue looping over devices. non-zero will break
+ * from the loop and return that value to the caller.
+ */
+int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
+			   void *data)
+{
+	struct devices_idr_iter iter;
+	int ret;
+
+	iter.fn = fn;
+	iter.data = data;
+
+	mutex_lock(&g_device_mutex);
+	ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
+	mutex_unlock(&g_device_mutex);
+	return ret;
+}
+
 void target_init_device_idr(void)
 {
 	idr_init(&devices_idr);
diff --git a/drivers/target/target_core_internal.h b/drivers/target/target_core_internal.h
index dfa923b..f3fa187 100644
--- a/drivers/target/target_core_internal.h
+++ b/drivers/target/target_core_internal.h
@@ -87,6 +87,8 @@  void	core_dev_free_initiator_node_lun_acl(struct se_portal_group *,
 struct se_device *target_alloc_device(struct se_hba *hba, const char *name);
 int	target_configure_device(struct se_device *dev);
 void	target_free_device(struct se_device *);
+int	target_for_each_device(int (*fn)(struct se_device *dev, void *data),
+			       void *data);
 
 /* target_core_configfs.c */
 void	target_setup_backend_cits(struct target_backend *);