Skip to content

Commit 0cb4d23

Browse files
committed
NFSD: Fix the behavior of READ near OFFSET_MAX
Dan Aloni reports: > Due to commit 8cfb901 ("NFS: Always provide aligned buffers to > the RPC read layers") on the client, a read of 0xfff is aligned up > to server rsize of 0x1000. > > As a result, in a test where the server has a file of size > 0x7fffffffffffffff, and the client tries to read from the offset > 0x7ffffffffffff000, the read causes loff_t overflow in the server > and it returns an NFS code of EINVAL to the client. The client as > a result indefinitely retries the request. The Linux NFS client does not handle NFS?ERR_INVAL, even though all NFS specifications permit servers to return that status code for a READ. Instead of NFS?ERR_INVAL, have out-of-range READ requests succeed and return a short result. Set the EOF flag in the result to prevent the client from retrying the READ request. This behavior appears to be consistent with Solaris NFS servers. Note that NFSv3 and NFSv4 use u64 offset values on the wire. These must be converted to loff_t internally before use -- an implicit type cast is not adequate for this purpose. Otherwise VFS checks against sb->s_maxbytes do not work properly. Reported-by: Dan Aloni <dan.aloni@vastdata.com> Cc: stable@vger.kernel.org Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent ab451ea commit 0cb4d23

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

fs/nfsd/nfs3proc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,17 @@ nfsd3_proc_read(struct svc_rqst *rqstp)
150150
unsigned int len;
151151
int v;
152152

153-
argp->count = min_t(u32, argp->count, max_blocksize);
154-
155153
dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
156154
SVCFH_fmt(&argp->fh),
157155
(unsigned long) argp->count,
158156
(unsigned long long) argp->offset);
159157

158+
argp->count = min_t(u32, argp->count, max_blocksize);
159+
if (argp->offset > (u64)OFFSET_MAX)
160+
argp->offset = (u64)OFFSET_MAX;
161+
if (argp->offset + argp->count > (u64)OFFSET_MAX)
162+
argp->count = (u64)OFFSET_MAX - argp->offset;
163+
160164
v = 0;
161165
len = argp->count;
162166
resp->pages = rqstp->rq_next_page;

fs/nfsd/nfs4proc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,16 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
782782
__be32 status;
783783

784784
read->rd_nf = NULL;
785-
if (read->rd_offset >= OFFSET_MAX)
786-
return nfserr_inval;
787785

788786
trace_nfsd_read_start(rqstp, &cstate->current_fh,
789787
read->rd_offset, read->rd_length);
790788

789+
read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
790+
if (read->rd_offset > (u64)OFFSET_MAX)
791+
read->rd_offset = (u64)OFFSET_MAX;
792+
if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
793+
read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
794+
791795
/*
792796
* If we do a zero copy read, then a client will see read data
793797
* that reflects the state of the file *after* performing the

fs/nfsd/nfs4xdr.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,10 +3986,8 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
39863986
}
39873987
xdr_commit_encode(xdr);
39883988

3989-
maxcount = svc_max_payload(resp->rqstp);
3990-
maxcount = min_t(unsigned long, maxcount,
3989+
maxcount = min_t(unsigned long, read->rd_length,
39913990
(xdr->buf->buflen - xdr->buf->len));
3992-
maxcount = min_t(unsigned long, maxcount, read->rd_length);
39933991

39943992
if (file->f_op->splice_read &&
39953993
test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
@@ -4826,10 +4824,8 @@ nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
48264824
return nfserr_resource;
48274825
xdr_commit_encode(xdr);
48284826

4829-
maxcount = svc_max_payload(resp->rqstp);
4830-
maxcount = min_t(unsigned long, maxcount,
4827+
maxcount = min_t(unsigned long, read->rd_length,
48314828
(xdr->buf->buflen - xdr->buf->len));
4832-
maxcount = min_t(unsigned long, maxcount, read->rd_length);
48334829
count = maxcount;
48344830

48354831
eof = read->rd_offset >= i_size_read(file_inode(file));

0 commit comments

Comments
 (0)