linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Hannes Reinecke <hare@suse.de>
To: Christoph Hellwig <hch@lst.de>
Cc: Hannes Reinecke <hare@suse.de>,
	linux-nvme@lists.infradead.org, Sagi Grimberg <sagi@grimberg.me>,
	Keith Busch <keith.busch@wdc.com>,
	James Smart <james.smart@broadcom.com>
Subject: [PATCH 3/7] nvme-fcloop: use IDA for port ids
Date: Tue, 22 Sep 2020 14:14:57 +0200	[thread overview]
Message-ID: <20200922121501.32851-4-hare@suse.de> (raw)
In-Reply-To: <20200922121501.32851-1-hare@suse.de>

Use an IDA to register port IDs to avoid duplicate port IDs. It
also will generate a unique port ID if none is specified.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 drivers/nvme/target/fcloop.c | 51 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 8 deletions(-)

diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index 082aa4dee406..e56f323fa7d4 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -12,6 +12,7 @@
 #include <linux/nvme-fc-driver.h>
 #include <linux/nvme-fc.h>
 
+#define FCLOOP_MAX_AL_PA 0xEF
 
 enum {
 	NVMF_OPT_ERR		= 0,
@@ -63,6 +64,9 @@ fcloop_parse_options(struct fcloop_ctrl_options *opts,
 	int token, ret = 0;
 	u64 token64;
 
+	/* Default setting */
+	opts->fcaddr = 1;
+
 	options = o = kstrdup(buf, GFP_KERNEL);
 	if (!options)
 		return -ENOMEM;
@@ -102,6 +106,10 @@ fcloop_parse_options(struct fcloop_ctrl_options *opts,
 				ret = -EINVAL;
 				goto out_free_options;
 			}
+			if (!token || token > FCLOOP_MAX_AL_PA) {
+				ret = -EINVAL;
+				goto out_free_options;
+			}
 			opts->fcaddr = token;
 			break;
 		case NVMF_OPT_LPWWNN:
@@ -203,11 +211,13 @@ fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
 static DEFINE_SPINLOCK(fcloop_lock);
 static LIST_HEAD(fcloop_lports);
 static LIST_HEAD(fcloop_nports);
+static DEFINE_IDA(fcloop_portid_ida);
 
 struct fcloop_lport {
 	struct nvme_fc_local_port *localport;
 	struct list_head lport_list;
 	struct completion unreg_done;
+	u32 port_id;
 };
 
 struct fcloop_lport_priv {
@@ -949,6 +959,8 @@ fcloop_nport_free(struct kref *ref)
 	list_del(&nport->nport_list);
 	spin_unlock_irqrestore(&fcloop_lock, flags);
 
+	ida_free(&fcloop_portid_ida, nport->port_id);
+
 	kfree(nport);
 }
 
@@ -1047,7 +1059,7 @@ fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
 	struct fcloop_lport *lport;
 	struct fcloop_lport_priv *lport_priv;
 	unsigned long flags;
-	int ret = -ENOMEM;
+	int ret = -ENOMEM, port_id;
 
 	lport = kzalloc(sizeof(*lport), GFP_KERNEL);
 	if (!lport)
@@ -1067,11 +1079,22 @@ fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
 		goto out_free_opts;
 	}
 
+	port_id = ida_alloc_range(&fcloop_portid_ida,
+				  opts->fcaddr, FCLOOP_MAX_AL_PA, GFP_KERNEL);
+	if (port_id < 0) {
+		ret = port_id;
+		goto out_free_opts;
+	}
+	if ((opts->mask & NVMF_OPT_FCADDR) && opts->fcaddr != port_id) {
+		ret = -EINVAL;
+		goto out_free_ida;
+	}
+
 	memset(&pinfo, 0, sizeof(pinfo));
 	pinfo.node_name = opts->wwnn;
 	pinfo.port_name = opts->wwpn;
 	pinfo.port_role = opts->roles;
-	pinfo.port_id = opts->fcaddr;
+	pinfo.port_id = port_id;
 
 	ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
 	if (!ret) {
@@ -1086,7 +1109,9 @@ fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
 		list_add_tail(&lport->lport_list, &fcloop_lports);
 		spin_unlock_irqrestore(&fcloop_lock, flags);
 	}
-
+out_free_ida:
+	if (ret)
+		ida_free(&fcloop_portid_ida, port_id);
 out_free_opts:
 	kfree(opts);
 out_free_lport:
@@ -1115,6 +1140,8 @@ __wait_localport_unreg(struct fcloop_lport *lport)
 
 	wait_for_completion(&lport->unreg_done);
 
+	ida_free(&fcloop_portid_ida, lport->port_id);
+
 	kfree(lport);
 
 	return ret;
@@ -1162,7 +1189,7 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 	struct fcloop_ctrl_options *opts;
 	unsigned long flags;
 	u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
-	int ret;
+	int ret, port_id;
 
 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
 	if (!opts)
@@ -1182,13 +1209,21 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 	if (!newnport)
 		goto out_free_opts;
 
+	port_id = ida_alloc_range(&fcloop_portid_ida,
+				  opts->fcaddr, FCLOOP_MAX_AL_PA, GFP_KERNEL);
+	if (port_id < 0)
+		goto out_free_newnport;
+
+	if ((opts->mask & NVMF_OPT_FCADDR) && opts->fcaddr != port_id)
+		goto out_free_ida;
+
 	INIT_LIST_HEAD(&newnport->nport_list);
 	newnport->node_name = opts->wwnn;
 	newnport->port_name = opts->wwpn;
+	newnport->port_id = port_id;
 	if (opts->mask & NVMF_OPT_ROLES)
 		newnport->port_role = opts->roles;
-	if (opts->mask & NVMF_OPT_FCADDR)
-		newnport->port_id = opts->fcaddr;
+
 	kref_init(&newnport->ref);
 
 	spin_lock_irqsave(&fcloop_lock, flags);
@@ -1226,8 +1261,6 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 				nport->lport = lport;
 			if (opts->mask & NVMF_OPT_ROLES)
 				nport->port_role = opts->roles;
-			if (opts->mask & NVMF_OPT_FCADDR)
-				nport->port_id = opts->fcaddr;
 			goto out_free_newnport;
 		}
 	}
@@ -1241,6 +1274,8 @@ fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
 
 out_invalid_opts:
 	spin_unlock_irqrestore(&fcloop_lock, flags);
+out_free_ida:
+	ida_free(&fcloop_portid_ida, port_id);
 out_free_newnport:
 	kfree(newnport);
 out_free_opts:
-- 
2.16.4


_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  parent reply	other threads:[~2020-09-22 12:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-22 12:14 [PATCH 0/7] nvme-fcloop: fix shutdown and improve logging Hannes Reinecke
2020-09-22 12:14 ` [PATCH 1/7] nvme-fcloop: flush workqueue before calling nvme_fc_unregister_remoteport() Hannes Reinecke
2020-10-05 17:14   ` James Smart
2020-09-22 12:14 ` [PATCH 2/7] nvmet-fc: use per-target workqueue when removing associations Hannes Reinecke
2020-10-05 17:18   ` James Smart
2020-09-22 12:14 ` Hannes Reinecke [this message]
2020-10-05 17:33   ` [PATCH 3/7] nvme-fcloop: use IDA for port ids James Smart
2020-10-09 13:57     ` Hannes Reinecke
2020-09-22 12:14 ` [PATCH 4/7] nvmet-fc: use feature flag for virtual LLDD Hannes Reinecke
2020-10-05 17:37   ` James Smart
2020-09-22 12:14 ` [PATCH 5/7] nvme-fc: " Hannes Reinecke
2020-10-05 17:38   ` James Smart
2020-09-22 12:15 ` [PATCH 6/7] nvme-fcloop: use a device for nport Hannes Reinecke
2020-10-05 17:41   ` James Smart
2020-09-22 12:15 ` [PATCH 7/7] nvme-fcloop: use a device for lport Hannes Reinecke
2020-10-05 17:45   ` James Smart

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=20200922121501.32851-4-hare@suse.de \
    --to=hare@suse.de \
    --cc=hch@lst.de \
    --cc=james.smart@broadcom.com \
    --cc=keith.busch@wdc.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /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).