Skip to contentSkip to conversation

nvmet: fix FID 82h status codes for invalid NSIDs

PATCH Applied Guixin Liu 4 participants 12 messages Sep 20 – Oct 3

Subsystems (2)
lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

v2 cover nvmet: fix FID 82h status codes for invalid NSIDs

The Reservation Notification Mask feature (FID 82h) defines its own status code for NSIDs that do not name a namespace supporting reservations:

"A Get Features command that uses a namespace ID of FFFFFFFFh shall be aborted with status code of Invalid Field in Command. [...] If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

-- NVMe Base Specification, section 5.2.26.1.33

nvmet routes the namespace lookup of both handlers through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. That is the correct status for the generic namespace-scoped admin commands, but not for this feature. A Set Features 82h naming a nonexistent NSID, a Get Features 82h naming a nonexistent NSID, and a Get Features 82h using the broadcast NSID are all aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Each patch fixes one handler and translates the lookup failure to Invalid Field in Command. The Set Features broadcast, the namespace-without-reservations case, the disabled namespace path (Internal Path Error, commit 505363957fad ("nvmet: fix nvme status code when namespace is disabled")) and everything else keep their current behavior.

Verified with two hosts (distinct host identifiers) connected over NVMe/TCP to an nvmet subsystem exporting two reservation-enabled null_blk namespaces: all three rejected cases now return 0x6002, while Set/Get Features on an active namespace, the Set Features broadcast and the reservation commands behave as before, with no splat in dmesg.

Changes since v1:

- drop the comments above the status translation.(Christoph Hellwig)
- return Invalid Field in Command directly instead of assigning it to status first, which also removes the braces and the overly long (Christoph Hellwig)

Guixin Liu (2):
nvmet: return Invalid Field for invalid NSIDs in Set Features 82h
nvmet: return Invalid Field for invalid NSIDs in Get Features 82h

 drivers/nvme/target/pr.c | 4 ++++
 1 file changed, 4 insertions(+)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v2 1/2 nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

The Reservation Notification Mask feature (FID 82h) defines its own status code for namespaces that do not support reservations:

"If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

-- NVMe Base Specification, section 5.2.26.1.33

The Set Features handler routes the namespace lookup through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. That is the correct status for the generic namespace-scoped admin commands, but not for this feature: a Set Features 82h naming a nonexistent NSID is aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Translate the lookup failure to Invalid Field in Command. The broadcast NSID, the namespace-without-reservations case and everything else keep their current behavior.

Fixes 5a47c2080a73 ("nvmet: support reservation feature")
---
 drivers/nvme/target/pr.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/target/pr.c +2 -0
index 0948a690a1c0..a36c732883d4 100644
@@ -51,6 +51,8 @@ u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
 
 	if (nsid != U32_MAX) {
 		status = nvmet_req_find_ns(req);
+		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
 		if (status)
 			return status;
 		if (!req->ns->pr.enable)
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH v2 1/2] nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

Quoted 6 lines · Guixin Liu
drivers/nvme/target/pr.c +0 -0
index 0948a690a1c0..a36c732883d4 100644

drivers/nvme/target/pr.c u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask) · line 58

Quoted 8 lines
drivers/nvme/target/pr.c +2 -0
@@ -51,6 +51,8 @@ u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
 
  	if (nsid != U32_MAX) {
  		status = nvmet_req_find_ns(req);
+		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  		if (status)
  			return status;
  		if (!req->ns->pr.enable)

This looks good to me. However, to avoid hand-coding the same error return code multiple times in the function and to make it a bit more readable, how about modifying it as follows?

u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req)

{
	u16 status;
	u16 err = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;

	status = nvmet_req_find_ns(req);
	if (status) {
		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
			return err;
		return status;
	}

	if (!req->ns->pr.enable)
		return err;

	nvmet_set_result(req, READ_ONCE(req->ns->pr.notify_mask));
	return NVME_SC_SUCCESS;
}

Thanks,
--Nilay

#3 lore raw

Re: [PATCH v2 1/2] nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

Quoted 10 lines · Nilay Shroff
Quoted 9 lines · Guixin Liu
drivers/nvme/target/pr.c +2 -0
index 0948a690a1c0..a36c732883d4 100644
@@ -51,6 +51,8 @@ u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
    	if (nsid != U32_MAX) {
  		status = nvmet_req_find_ns(req);
+		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
Quoted 8 lines

drivers/nvme/target/pr.c u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask) · line 58

Quoted 4 lines
drivers/nvme/target/pr.c +0 -0
  		if (status)
  			return status;
  		if (!req->ns->pr.enable)

This looks good to me. However, to avoid hand-coding the same error return code multiple times in the function and to make it a bit more readable, how about modifying it as follows?

I don't understand how that helps. If we really want to refactor, we could add a version of nvmet_req_find_ns that does it, but that feels like a bit of overkill.

Quoted 1 line
		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))

Still no need for the inner braces here in all versions.

author
#4 lore raw

Re: [PATCH v2 1/2] nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

Quoted 18 lines · Christoph Hellwig
Quoted 17 lines · Nilay Shroff

drivers/nvme/target/pr.c u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask) · line 57

Quoted 13 lines · Guixin Liu
drivers/nvme/target/pr.c +2 -0
index 0948a690a1c0..a36c732883d4 100644
@@ -51,6 +51,8 @@ u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
    	if (nsid != U32_MAX) {
  		status = nvmet_req_find_ns(req);
+		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  		if (status)
  			return status;
  		if (!req->ns->pr.enable)

This looks good to me. However, to avoid hand-coding the same error return code multiple times in the function and to make it a bit more readable, how about modifying it as follows?

Quoted 3 lines

I don't understand how that helps. If we really want to refactor, we could add a version of nvmet_req_find_ns that does it, but that feels like a bit of overkill.

Agree.

Quoted 3 lines
Quoted 2 lines
		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))

Still no need for the inner braces here in all versions.

Well, in C, the "==" operator has higher precedence than "|", so the inner braces are required here.

Best Regards,
Guixin Liu

#5 lore raw

Re: [PATCH v2 1/2] nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

In reply to Christoph Hellwig
Quoted 20 lines · Christoph Hellwig
Quoted 10 lines · Nilay Shroff
Quoted 9 lines · Guixin Liu
drivers/nvme/target/pr.c +2 -0
index 0948a690a1c0..a36c732883d4 100644
@@ -51,6 +51,8 @@ u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
     	if (nsid != U32_MAX) {
   		status = nvmet_req_find_ns(req);
+		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
Quoted 8 lines

drivers/nvme/target/pr.c u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask) · line 58

Quoted 4 lines
drivers/nvme/target/pr.c +0 -0
   		if (status)
   			return status;
   		if (!req->ns->pr.enable)

This looks good to me. However, to avoid hand-coding the same error return code multiple times in the function and to make it a bit more readable, how about modifying it as follows?

Quoted 4 lines

I don't understand how that helps. If we really want to refactor, we could add a version of nvmet_req_find_ns that does it, but that feels like a bit of overkill.

I agree that adding a separate helper just for this would be unnecessary.

My suggestion was mainly for readability and to avoid repeating the same NVME_SC_INVALID_FIELD | NVME_STATUS_DNR error code and keep the error handling in one place. I also found the two status checks a little less straightforward to read. When applying the proposed patch, the resulting code looks like below:

u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req)

{
	u16 status;

	status = nvmet_req_find_ns(req);
	if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
	if (status)
		return status;

	if (!req->ns->pr.enable)
		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;

	nvmet_set_result(req, READ_ONCE(req->ns->pr.notify_mask));
	return status;
}

That said, this is mostly a matter of personal preference rather than a functional concern, so I'm fine with keeping the current version if everyone agrees.

Quoted 4 lines
Quoted 1 line
		if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))

Still no need for the inner braces here in all versions.

I think Guixin has already replied to this comment in another email. The inner braces are required here otherwise compiler would complain:

drivers/nvme/target/pr.c:80:35: error: | has lower precedence than ==; == will be evaluated first [-Werror,-Wparentheses]
80 | if (status == NVME_SC_INVALID_NS | NVME_STATUS_DNR)

      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

Thanks,
--Nilay

Reviewed-by on v2 1/2
#7 lore raw

Re: [PATCH v2 1/2] nvmet: return Invalid Field for invalid NSIDs in Set Features 82h

In reply to Guixin Liu
Quoted 17 lines · Guixin Liu

The Reservation Notification Mask feature (FID 82h) defines its own status code for namespaces that do not support reservations:

"If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

   -- NVMe Base Specification, section 5.2.26.1.33

The Set Features handler routes the namespace lookup through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. That is the correct status for the generic namespace-scoped admin commands, but not for this feature: a Set Features 82h naming a nonexistent NSID is aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Quoted 6 lines

Translate the lookup failure to Invalid Field in Command. The broadcast NSID, the namespace-without-reservations case and everything else keep their current behavior.

Fixes 5a47c2080a73 ("nvmet: support reservation feature")

Looks good to me.

author
#8 lore raw

PATCH v2 2/2 nvmet: return Invalid Field for invalid NSIDs in Get Features 82h

In reply to Guixin Liu

The Reservation Notification Mask feature (FID 82h) rejects the same NSIDs for Get Features as for Set Features, plus the broadcast NSID:

"A Get Features command that uses a namespace ID of FFFFFFFFh shall be aborted with status code of Invalid Field in Command. If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

-- NVMe Base Specification, section 5.2.26.1.33

The Get Features handler routes the namespace lookup through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. Both rejected cases land there: a nonexistent NSID fails the lookup, and so does the broadcast NSID, which never names a namespace. Both are aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Translate the lookup failure to Invalid Field in Command, as was done for Set Features.

Fixes 5a47c2080a73 ("nvmet: support reservation feature")
---
 drivers/nvme/target/pr.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/target/pr.c +2 -0
index a36c732883d4..62ef8e85f7e9 100644
@@ -77,6 +77,8 @@ u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req)
 	u16 status;
 
 	status = nvmet_req_find_ns(req);
+	if (status == (NVME_SC_INVALID_NS | NVME_STATUS_DNR))
+		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
 	if (status)
 		return status;
 
Signature · 1 line
2.43.7
Reviewed-by on v2 2/2
#10 lore raw

Re: [PATCH v2 2/2] nvmet: return Invalid Field for invalid NSIDs in Get Features 82h

In reply to Guixin Liu
Quoted 19 lines · Guixin Liu

The Reservation Notification Mask feature (FID 82h) rejects the same NSIDs for Get Features as for Set Features, plus the broadcast NSID:

"A Get Features command that uses a namespace ID of FFFFFFFFh shall be aborted with status code of Invalid Field in Command. If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

   -- NVMe Base Specification, section 5.2.26.1.33

The Get Features handler routes the namespace lookup through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. Both rejected cases land there: a nonexistent NSID fails the lookup, and so does the broadcast NSID, which never names a namespace. Both are aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Quoted 5 lines

Translate the lookup failure to Invalid Field in Command, as was done for Set Features.

Fixes 5a47c2080a73 ("nvmet: support reservation feature")

Looks good to me.

maintainer Applied
#11 lore raw
In reply to Guixin Liu
Quoted 22 lines · Guixin Liu

The Reservation Notification Mask feature (FID 82h) defines its own status code for NSIDs that do not name a namespace supporting reservations:

"A Get Features command that uses a namespace ID of FFFFFFFFh shall be aborted with status code of Invalid Field in Command. [...] If a Set Features command or a Get Features command attempts to access the Reservation Notification Mask on a namespace that does not support reservations or is invalid, then that command is aborted with status code of Invalid Field in Command."

-- NVMe Base Specification, section 5.2.26.1.33

nvmet routes the namespace lookup of both handlers through nvmet_req_find_ns(), which returns Invalid Namespace or Format for an unknown NSID. That is the correct status for the generic namespace-scoped admin commands, but not for this feature. A Set Features 82h naming a nonexistent NSID, a Get Features 82h naming a nonexistent NSID, and a Get Features 82h using the broadcast NSID are all aborted with Invalid Namespace or Format instead of Invalid Field in Command.

Quoted 6 lines

Each patch fixes one handler and translates the lookup failure to Invalid Field in Command. The Set Features broadcast, the namespace-without-reservations case, the disabled namespace path (Internal Path Error, commit 505363957fad ("nvmet: fix nvme status code when namespace is disabled")) and everything else keep their current behavior.

Thanks, applied to nvme-7.3.