[PATCH 6/7] nvme-fcloop: use a device for nport

Hannes Reinecke hare at suse.de
Tue Sep 22 08:15:00 EDT 2020


Add a 'struct device' to the nport such that the nport will show
up in sysfs and we get a valid name in the logging output.

Signed-off-by: Hannes Reinecke <hare at suse.de>
---
 drivers/nvme/target/fcloop.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index 7dab98545979..b84f8754b3a3 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -245,11 +245,11 @@ struct fcloop_tport {
 };
 
 struct fcloop_nport {
+	struct device dev;
 	struct fcloop_rport *rport;
 	struct fcloop_tport *tport;
 	struct fcloop_lport *lport;
 	struct list_head nport_list;
-	struct kref ref;
 	u64 node_name;
 	u64 port_name;
 	u32 port_role;
@@ -949,10 +949,10 @@ fcloop_fcp_abort(struct nvme_fc_local_port *localport,
 }
 
 static void
-fcloop_nport_free(struct kref *ref)
+fcloop_release_nport(struct device *dev)
 {
 	struct fcloop_nport *nport =
-		container_of(ref, struct fcloop_nport, ref);
+		container_of(dev, struct fcloop_nport, dev);
 	unsigned long flags;
 
 	spin_lock_irqsave(&fcloop_lock, flags);
@@ -967,13 +967,13 @@ fcloop_nport_free(struct kref *ref)
 static void
 fcloop_nport_put(struct fcloop_nport *nport)
 {
-	kref_put(&nport->ref, fcloop_nport_free);
+	put_device(&nport->dev);
 }
 
-static int
+static void
 fcloop_nport_get(struct fcloop_nport *nport)
 {
-	return kref_get_unless_zero(&nport->ref);
+	get_device(&nport->dev);
 }
 
 static void
@@ -1007,6 +1007,8 @@ fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
 #define	FCLOOP_SGL_SEGS			256
 #define FCLOOP_DMABOUND_4G		0xFFFFFFFF
 
+static struct class *fcloop_class;
+
 static struct nvme_fc_port_template fctemplate = {
 	.localport_delete	= fcloop_localport_delete,
 	.remoteport_delete	= fcloop_remoteport_delete,
@@ -1225,8 +1227,10 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 	newnport->port_id = port_id;
 	if (opts->mask & NVMF_OPT_ROLES)
 		newnport->port_role = opts->roles;
-
-	kref_init(&newnport->ref);
+	newnport->dev.class = fcloop_class;
+	newnport->dev.release = fcloop_release_nport;
+	dev_set_name(&newnport->dev, "nport%d", newnport->port_id);
+	device_initialize(&newnport->dev);
 
 	spin_lock_irqsave(&fcloop_lock, flags);
 
@@ -1267,6 +1271,10 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 		}
 	}
 
+	ret = device_add(&newnport->dev);
+	if (ret)
+		goto out_invalid_opts;
+
 	list_add_tail(&newnport->nport_list, &fcloop_nports);
 
 	spin_unlock_irqrestore(&fcloop_lock, flags);
@@ -1339,6 +1347,8 @@ __unlink_remote_port(struct fcloop_nport *nport)
 	if (rport && nport->tport)
 		nport->tport->remoteport = NULL;
 	nport->rport = NULL;
+	if (!nport->tport)
+		device_del(&nport->dev);
 
 	return rport;
 }
@@ -1406,7 +1416,7 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
 	tinfo.port_name = nport->port_name;
 	tinfo.port_id = nport->port_id;
 
-	ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
+	ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, &nport->dev,
 						&targetport);
 	if (ret) {
 		fcloop_nport_put(nport);
@@ -1438,6 +1448,8 @@ __unlink_target_port(struct fcloop_nport *nport)
 	if (tport && nport->rport)
 		nport->rport->targetport = NULL;
 	nport->tport = NULL;
+	if (!nport->rport)
+		device_del(&nport->dev);
 
 	return tport;
 }
@@ -1513,7 +1525,6 @@ static const struct attribute_group *fcloop_dev_attr_groups[] = {
 	NULL,
 };
 
-static struct class *fcloop_class;
 static struct device *fcloop_device;
 
 
-- 
2.16.4




More information about the Linux-nvme mailing list