Skip to content

Commit ead11ac

Browse files
Mike SnitzerAnna Schumaker
authored andcommitted
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 to 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 nfs_localio_errno_to_nfs4_stat() to map a POSIX errno to an NFSv4 error code. Care was taken to factor out nfs4_errtbl_common[] to avoid duplicating the same NFS error to errno table. nfs4_errtbl_common[] is checked first by both nfs4_stat_to_errno and nfs_localio_errno_to_nfs4_stat before they check their own more specialized tables (nfs4_errtbl[] and nfs4_errtbl_localio[] respectively). While auditing the associated error mapping tables, the (ab)use of -1 for the last table entry was removed in favor of using ARRAY_SIZE to iterate the nfs_errtbl[] and nfs4_errtbl[]. And 'errno_NFSERR_IO' was removed because it caused needless obfuscation. Fixes: 70ba381 ("nfs: add LOCALIO support") Reported-by: Trond Myklebust <[email protected]> Signed-off-by: Mike Snitzer <[email protected]> Signed-off-by: Anna Schumaker <[email protected]>
1 parent 4a48922 commit ead11ac

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

fs/nfs/localio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ nfs_local_pgio_done(struct nfs_pgio_header *hdr, long status)
388388
hdr->res.op_status = NFS4_OK;
389389
hdr->task.tk_status = 0;
390390
} else {
391-
hdr->res.op_status = nfs4_stat_to_errno(status);
391+
hdr->res.op_status = nfs_localio_errno_to_nfs4_stat(status);
392392
hdr->task.tk_status = status;
393393
}
394394
}
@@ -786,7 +786,7 @@ nfs_local_commit_done(struct nfs_commit_data *data, int status)
786786
data->task.tk_status = 0;
787787
} else {
788788
nfs_reset_boot_verifier(data->inode);
789-
data->res.op_status = nfs4_stat_to_errno(status);
789+
data->res.op_status = nfs_localio_errno_to_nfs4_stat(status);
790790
data->task.tk_status = status;
791791
}
792792
}

fs/nfs_common/common.c

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static const struct {
1515
{ NFS_OK, 0 },
1616
{ NFSERR_PERM, -EPERM },
1717
{ NFSERR_NOENT, -ENOENT },
18-
{ NFSERR_IO, -errno_NFSERR_IO},
18+
{ NFSERR_IO, -EIO },
1919
{ NFSERR_NXIO, -ENXIO },
2020
/* { NFSERR_EAGAIN, -EAGAIN }, */
2121
{ NFSERR_ACCES, -EACCES },
@@ -45,7 +45,6 @@ static const struct {
4545
{ NFSERR_SERVERFAULT, -EREMOTEIO },
4646
{ NFSERR_BADTYPE, -EBADTYPE },
4747
{ NFSERR_JUKEBOX, -EJUKEBOX },
48-
{ -1, -EIO }
4948
};
5049

5150
/**
@@ -59,26 +58,29 @@ int nfs_stat_to_errno(enum nfs_stat status)
5958
{
6059
int i;
6160

62-
for (i = 0; nfs_errtbl[i].stat != -1; i++) {
61+
for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
6362
if (nfs_errtbl[i].stat == (int)status)
6463
return nfs_errtbl[i].errno;
6564
}
66-
return nfs_errtbl[i].errno;
65+
return -EIO;
6766
}
6867
EXPORT_SYMBOL_GPL(nfs_stat_to_errno);
6968

7069
/*
7170
* We need to translate between nfs v4 status return values and
7271
* the local errno values which may not be the same.
72+
*
73+
* nfs4_errtbl_common[] is used before more specialized mappings
74+
* available in nfs4_errtbl[] or nfs4_errtbl_localio[].
7375
*/
7476
static const struct {
7577
int stat;
7678
int errno;
77-
} nfs4_errtbl[] = {
79+
} nfs4_errtbl_common[] = {
7880
{ NFS4_OK, 0 },
7981
{ NFS4ERR_PERM, -EPERM },
8082
{ NFS4ERR_NOENT, -ENOENT },
81-
{ NFS4ERR_IO, -errno_NFSERR_IO},
83+
{ NFS4ERR_IO, -EIO },
8284
{ NFS4ERR_NXIO, -ENXIO },
8385
{ NFS4ERR_ACCESS, -EACCES },
8486
{ NFS4ERR_EXIST, -EEXIST },
@@ -98,15 +100,20 @@ static const struct {
98100
{ NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
99101
{ NFS4ERR_NOTSUPP, -ENOTSUPP },
100102
{ NFS4ERR_TOOSMALL, -ETOOSMALL },
101-
{ NFS4ERR_SERVERFAULT, -EREMOTEIO },
102103
{ NFS4ERR_BADTYPE, -EBADTYPE },
103-
{ NFS4ERR_LOCKED, -EAGAIN },
104104
{ NFS4ERR_SYMLINK, -ELOOP },
105-
{ NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
106105
{ NFS4ERR_DEADLOCK, -EDEADLK },
106+
};
107+
108+
static const struct {
109+
int stat;
110+
int errno;
111+
} nfs4_errtbl[] = {
112+
{ NFS4ERR_SERVERFAULT, -EREMOTEIO },
113+
{ NFS4ERR_LOCKED, -EAGAIN },
114+
{ NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
107115
{ NFS4ERR_NOXATTR, -ENODATA },
108116
{ NFS4ERR_XATTR2BIG, -E2BIG },
109-
{ -1, -EIO }
110117
};
111118

112119
/*
@@ -116,7 +123,14 @@ static const struct {
116123
int nfs4_stat_to_errno(int stat)
117124
{
118125
int i;
119-
for (i = 0; nfs4_errtbl[i].stat != -1; i++) {
126+
127+
/* First check nfs4_errtbl_common */
128+
for (i = 0; i < ARRAY_SIZE(nfs4_errtbl_common); i++) {
129+
if (nfs4_errtbl_common[i].stat == stat)
130+
return nfs4_errtbl_common[i].errno;
131+
}
132+
/* Then check nfs4_errtbl */
133+
for (i = 0; i < ARRAY_SIZE(nfs4_errtbl); i++) {
120134
if (nfs4_errtbl[i].stat == stat)
121135
return nfs4_errtbl[i].errno;
122136
}
@@ -132,3 +146,56 @@ int nfs4_stat_to_errno(int stat)
132146
return -stat;
133147
}
134148
EXPORT_SYMBOL_GPL(nfs4_stat_to_errno);
149+
150+
/*
151+
* This table is useful for conversion from local errno to NFS error.
152+
* It provides more logically correct mappings for use with LOCALIO
153+
* (which is focused on converting from errno to NFS status).
154+
*/
155+
static const struct {
156+
int stat;
157+
int errno;
158+
} nfs4_errtbl_localio[] = {
159+
/* Map errors differently than nfs4_errtbl */
160+
{ NFS4ERR_IO, -EREMOTEIO },
161+
{ NFS4ERR_DELAY, -EAGAIN },
162+
{ NFS4ERR_FBIG, -E2BIG },
163+
/* Map errors not handled by nfs4_errtbl */
164+
{ NFS4ERR_STALE, -EBADF },
165+
{ NFS4ERR_STALE, -EOPENSTALE },
166+
{ NFS4ERR_DELAY, -ETIMEDOUT },
167+
{ NFS4ERR_DELAY, -ERESTARTSYS },
168+
{ NFS4ERR_DELAY, -ENOMEM },
169+
{ NFS4ERR_IO, -ETXTBSY },
170+
{ NFS4ERR_IO, -EBUSY },
171+
{ NFS4ERR_SERVERFAULT, -ESERVERFAULT },
172+
{ NFS4ERR_SERVERFAULT, -ENFILE },
173+
{ NFS4ERR_IO, -EUCLEAN },
174+
{ NFS4ERR_PERM, -ENOKEY },
175+
};
176+
177+
/*
178+
* Convert an errno to an NFS error code for LOCALIO.
179+
*/
180+
__u32 nfs_localio_errno_to_nfs4_stat(int errno)
181+
{
182+
int i;
183+
184+
/* First check nfs4_errtbl_common */
185+
for (i = 0; i < ARRAY_SIZE(nfs4_errtbl_common); i++) {
186+
if (nfs4_errtbl_common[i].errno == errno)
187+
return nfs4_errtbl_common[i].stat;
188+
}
189+
/* Then check nfs4_errtbl_localio */
190+
for (i = 0; i < ARRAY_SIZE(nfs4_errtbl_localio); i++) {
191+
if (nfs4_errtbl_localio[i].errno == errno)
192+
return nfs4_errtbl_localio[i].stat;
193+
}
194+
/* If we cannot translate the error, the recovery routines should
195+
* handle it.
196+
* Note: remaining NFSv4 error codes have values > 10000, so should
197+
* not conflict with native Linux error codes.
198+
*/
199+
return NFS4ERR_SERVERFAULT;
200+
}
201+
EXPORT_SYMBOL_GPL(nfs_localio_errno_to_nfs4_stat);

include/linux/nfs_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#include <uapi/linux/nfs.h>
1010

1111
/* Mapping from NFS error code to "errno" error code. */
12-
#define errno_NFSERR_IO EIO
1312

1413
int nfs_stat_to_errno(enum nfs_stat status);
1514
int nfs4_stat_to_errno(int stat);
1615

16+
__u32 nfs_localio_errno_to_nfs4_stat(int errno);
17+
1718
#endif /* _LINUX_NFS_COMMON_H */

0 commit comments

Comments
 (0)