Skip to contentSkip to conversation

nfs: fix incorrect error handling in LOCALIO

PATCH New Mike Snitzer 1 participant 1 message Dec 27, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfs: fix incorrect error handling in LOCALIO

nfs4_stat_to_errno() expects a NFSv4 error code as an argument and returns a POSIX errno.

The problem is LOCALIO is passing nfs4_stat_to_errno() the POSIX errno return values from filp->f_op->read_iter(), filp->f_op->write_iter() and vfs_fsync_range().

So the POSIX errno that nfs_local_pgio_done() and nfs_local_commit_done() are passing to nfs4_stat_to_errno() are failing to match any NFSv4 error code, which results in nfs4_stat_to_errno() defaulting ot returning -EREMOTEIO. This causes assertions in upper layers due to -EREMOTEIO not being a valid NFSv4 error code.

Fix this by updating nfs_local_pgio_done() and nfs_local_commit_done() to use the new errno_to_nfs4_stat() to map a POSIX errno to an NFSv4 error code.

Fixes 70ba381e1a431 ("nfs: add LOCALIO support")
---
 fs/nfs/localio.c           |  4 ++--
 fs/nfs_common/common.c     | 19 +++++++++++++++++++
 include/linux/nfs_common.h |  2 ++
 3 files changed, 23 insertions(+), 2 deletions(-)
fs/nfs/localio.c +2 -2
index 1eee5aac2884..047f4197c53d 100644
@@ -388,7 +388,7 @@ nfs_local_pgio_done(struct nfs_pgio_header *hdr, long status)
 		hdr->res.op_status = NFS4_OK;
 		hdr->task.tk_status = 0;
 	} else {
-		hdr->res.op_status = nfs4_stat_to_errno(status);
+		hdr->res.op_status = errno_to_nfs4_stat(status);
 		hdr->task.tk_status = status;
 	}
 }
@@ -786,7 +786,7 @@ nfs_local_commit_done(struct nfs_commit_data *data, int status)
 		data->task.tk_status = 0;
 	} else {
 		nfs_reset_boot_verifier(data->inode);
-		data->res.op_status = nfs4_stat_to_errno(status);
+		data->res.op_status = errno_to_nfs4_stat(status);
 		data->task.tk_status = status;
 	}
 }
fs/nfs_common/common.c +19 -0
index 34a115176f97..a84f258f0131 100644
@@ -132,3 +132,22 @@ int nfs4_stat_to_errno(int stat)
 	return -stat;
 }
 EXPORT_SYMBOL_GPL(nfs4_stat_to_errno);
+
+/*
+ * Convert an errno to an NFS error code.
+ */
+__u32 errno_to_nfs4_stat(int errno)
+{
+	int i;
+	for (i = 0; i < ARRAY_SIZE(nfs4_errtbl); i++) {
+		if (nfs4_errtbl[i].errno == errno)
+			return nfs4_errtbl[i].stat;
+	}
+	/* If we cannot translate the error, the recovery routines should
+	 * handle it.
+	 * Note: remaining NFSv4 error codes have values > 10000, so should
+	 * not conflict with native Linux error codes.
+	 */
+	return NFS4ERR_SERVERFAULT;
+}
+EXPORT_SYMBOL_GPL(errno_to_nfs4_stat);
include/linux/nfs_common.h +2 -0
index 5fc02df88252..2bc35f85af15 100644
@@ -14,4 +14,6 @@
 int nfs_stat_to_errno(enum nfs_stat status);
 int nfs4_stat_to_errno(int stat);
 
+__u32 errno_to_nfs4_stat(int errno);
+
 #endif /* _LINUX_NFS_COMMON_H */
Signature · 1 line
2.44.0