Linux-NVME Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH V2] nvme: code cleanup nvme_identify_ns_desc()
@ 2020-02-15  1:13 Chaitanya Kulkarni
  2020-02-19 15:02 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Chaitanya Kulkarni @ 2020-02-15  1:13 UTC (permalink / raw)
  To: kbusch; +Cc: Chaitanya Kulkarni, hch, linux-nvme

The function nvme_identify_ns_desc() has 3 levels of nesting which make
error message to exceeded > 80 char per line which is not aligned with
the kernel code standards and rest of the NVMe subsystem code.
 
Add a helper function to move to the processing of the log when the
command is successful by reducing the nesting and keeping the
code < 80 char per line.

Signed-off-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
---
Changes from V1: -

1. Pass struct nvme_ns_id_desc *cur as it is to avoid data and pos
   parameters to have a slightly cleaner patch. (Keith)

Here is quick test result with this patch with V2 :-

# cat config/nvmet/subsystems/fs/namespaces/1/device_nguid 
11111111-1234-5678-9123-222222222222
# cat config/nvmet/subsystems/fs/namespaces/1/device_uuid 
2b3d1019-6403-4f6b-8394-db42a0316e82

# nvme ns-descs /dev/nvme1n1 
NVME Namespace Identification Descriptors NS 1:
uuid    : 2b3d1019-6403-4f6b-8394-db42a0316e82
nguid   : 11111111123456789123222222222222

---
 drivers/nvme/host/core.c | 86 +++++++++++++++++++++++-----------------
 1 file changed, 49 insertions(+), 37 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index cd0b47ed219e..9ca719d04b1a 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -1055,8 +1055,52 @@ static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
 	return error;
 }
 
+static int nvme_process_ns_desc(struct nvme_ctrl *ctrl, struct nvme_ns_ids *ids,
+				struct nvme_ns_id_desc *cur)
+{
+	const char *warn_str = "ctrl returned bogus length:";
+	void *data = cur;
+	int len;
+
+	switch (cur->nidt) {
+	case NVME_NIDT_EUI64:
+		if (cur->nidl != NVME_NIDT_EUI64_LEN) {
+			dev_warn(ctrl->device, "%s %d for NVME_NIDT_EUI64\n",
+				 warn_str, cur->nidl);
+			return -1;
+		}
+		len = NVME_NIDT_EUI64_LEN;
+		memcpy(ids->eui64, data + sizeof(*cur), len);
+		break;
+	case NVME_NIDT_NGUID:
+		if (cur->nidl != NVME_NIDT_NGUID_LEN) {
+			dev_warn(ctrl->device, "%s %d for NVME_NIDT_NGUID\n",
+				 warn_str, cur->nidl);
+			return -1;
+		}
+		len = NVME_NIDT_NGUID_LEN;
+		memcpy(ids->nguid, data + sizeof(*cur), len);
+		break;
+	case NVME_NIDT_UUID:
+		if (cur->nidl != NVME_NIDT_UUID_LEN) {
+			dev_warn(ctrl->device, "%s %d for NVME_NIDT_UUID\n",
+				 warn_str, cur->nidl);
+			return -1;
+		}
+		len = NVME_NIDT_UUID_LEN;
+		uuid_copy(&ids->uuid, data + sizeof(*cur));
+		break;
+	default:
+		/* Skip unknown types */
+		len = cur->nidl;
+		break;
+	}
+
+	return len;
+}
+
 static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
-		struct nvme_ns_ids *ids)
+				  struct nvme_ns_ids *ids)
 {
 	struct nvme_command c = { };
 	int status;
@@ -1083,42 +1127,10 @@ static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
 		if (cur->nidl == 0)
 			break;
 
-		switch (cur->nidt) {
-		case NVME_NIDT_EUI64:
-			if (cur->nidl != NVME_NIDT_EUI64_LEN) {
-				dev_warn(ctrl->device,
-					 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
-					 cur->nidl);
-				goto free_data;
-			}
-			len = NVME_NIDT_EUI64_LEN;
-			memcpy(ids->eui64, data + pos + sizeof(*cur), len);
-			break;
-		case NVME_NIDT_NGUID:
-			if (cur->nidl != NVME_NIDT_NGUID_LEN) {
-				dev_warn(ctrl->device,
-					 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
-					 cur->nidl);
-				goto free_data;
-			}
-			len = NVME_NIDT_NGUID_LEN;
-			memcpy(ids->nguid, data + pos + sizeof(*cur), len);
-			break;
-		case NVME_NIDT_UUID:
-			if (cur->nidl != NVME_NIDT_UUID_LEN) {
-				dev_warn(ctrl->device,
-					 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
-					 cur->nidl);
-				goto free_data;
-			}
-			len = NVME_NIDT_UUID_LEN;
-			uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
-			break;
-		default:
-			/* Skip unknown types */
-			len = cur->nidl;
-			break;
-		}
+		len = nvme_process_ns_desc(ctrl, ids, cur);
+
+		if (len < 0)
+			goto free_data;
 
 		len += sizeof(*cur);
 	}
-- 
2.22.1


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH V2] nvme: code cleanup nvme_identify_ns_desc()
  2020-02-15  1:13 [PATCH V2] nvme: code cleanup nvme_identify_ns_desc() Chaitanya Kulkarni
@ 2020-02-19 15:02 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2020-02-19 15:02 UTC (permalink / raw)
  To: Chaitanya Kulkarni; +Cc: kbusch, hch, linux-nvme

static int nvme_process_ns_desc(struct nvme_ctrl *ctrl, struct nvme_ns_ids *ids,
> +				struct nvme_ns_id_desc *cur)
> +{
> +	const char *warn_str = "ctrl returned bogus length:";
> +	void *data = cur;
> +	int len;
> +
> +	switch (cur->nidt) {
> +	case NVME_NIDT_EUI64:
> +		if (cur->nidl != NVME_NIDT_EUI64_LEN) {
> +			dev_warn(ctrl->device, "%s %d for NVME_NIDT_EUI64\n",
> +				 warn_str, cur->nidl);
> +			return -1;
> +		}
> +		len = NVME_NIDT_EUI64_LEN;
> +		memcpy(ids->eui64, data + sizeof(*cur), len);
> +		break;
> +	case NVME_NIDT_NGUID:
> +		if (cur->nidl != NVME_NIDT_NGUID_LEN) {
> +			dev_warn(ctrl->device, "%s %d for NVME_NIDT_NGUID\n",
> +				 warn_str, cur->nidl);
> +			return -1;
> +		}
> +		len = NVME_NIDT_NGUID_LEN;
> +		memcpy(ids->nguid, data + sizeof(*cur), len);
> +		break;
> +	case NVME_NIDT_UUID:
> +		if (cur->nidl != NVME_NIDT_UUID_LEN) {
> +			dev_warn(ctrl->device, "%s %d for NVME_NIDT_UUID\n",
> +				 warn_str, cur->nidl);
> +			return -1;
> +		}
> +		len = NVME_NIDT_UUID_LEN;
> +		uuid_copy(&ids->uuid, data + sizeof(*cur));
> +		break;
> +	default:
> +		/* Skip unknown types */
> +		len = cur->nidl;
> +		break;
> +	}
> +
> +	return len;

Each of the cases could just return the length directly and thus avoid
the len variable.

> +}
> +
>  static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
> -		struct nvme_ns_ids *ids)
> +				  struct nvme_ns_ids *ids)

Gratious change from one common coding style to another.  And one that
I find more annoying as it needs more work for trivial things like
name changes for the identifiers..

> +		len = nvme_process_ns_desc(ctrl, ids, cur);
> +
> +		if (len < 0)
> +			goto free_data;

Gratious empty line above.

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-15  1:13 [PATCH V2] nvme: code cleanup nvme_identify_ns_desc() Chaitanya Kulkarni
2020-02-19 15:02 ` Christoph Hellwig

Linux-NVME Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-nvme/0 linux-nvme/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-nvme linux-nvme/ https://lore.kernel.org/linux-nvme \
		linux-nvme@lists.infradead.org
	public-inbox-index linux-nvme

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.infradead.lists.linux-nvme


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git