Skip to contentSkip to conversation

nfsd/nfsd3_proc_readdir: fix buffer count and page pointers

PATCH In review Murphy Zhou 3 participants 4 messages Apr 4, 2019 – Apr 5, 2019 2 messages not in the archive

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

PATCH v2 1/1 nfsd/nfsd3_proc_readdir: fix buffer count and page pointers

After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger than PAGE_SIZE. So if the directory been read is large enough, we can use multiple pages in rq_respages. Update buffer count and page pointers like we do in readdirplus to make this happen.

Now listing a directory within 3000 files will panic because we are counting in a wrong way and would write on random page.

Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"

---

v2:
fix nfs3svc_decode_readdirargs to set page pointers like decode_readdirplusargs do not test pointers in encode_entry as we've fixed them when decoding

 fs/nfsd/nfs3proc.c | 17 +++++++++++++++--
 fs/nfsd/nfs3xdr.c  | 11 +++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
fs/nfsd/nfs3proc.c +15 -2
index 8f933e8..9bc32af 100644
@@ -442,7 +442,9 @@
 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
 	__be32		nfserr;
-	int		count;
+	int		count = 0;
+	struct page	**p;
+	caddr_t		page_addr = NULL;
 
 	dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
 				SVCFH_fmt(&argp->fh),
@@ -462,7 +464,18 @@
 	nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie, 
 					&resp->common, nfs3svc_encode_entry);
 	memcpy(resp->verf, argp->verf, 8);
-	resp->count = resp->buffer - argp->buffer;
+	count = 0;
+	for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
+		page_addr = page_address(*p);
+
+		if (((caddr_t)resp->buffer >= page_addr) &&
+		    ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
+			count += (caddr_t)resp->buffer - page_addr;
+			break;
+		}
+		count += PAGE_SIZE;
+	}
+	resp->count = count >> 2;
 	if (resp->offset) {
 		loff_t offset = argp->cookie;
 
fs/nfsd/nfs3xdr.c +9 -2
index 93fea24..8d78912 100644
@@ -573,6 +573,7 @@ void fill_post_wcc(struct svc_fh *fhp)
 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
 {
 	struct nfsd3_readdirargs *args = rqstp->rq_argp;
+	int len;
 	u32 max_blocksize = svc_max_payload(rqstp);
 
 	p = decode_fh(p, &args->fh);
@@ -582,8 +583,14 @@ void fill_post_wcc(struct svc_fh *fhp)
 	args->verf   = p; p += 2;
 	args->dircount = ~0;
 	args->count  = ntohl(*p++);
-	args->count  = min_t(u32, args->count, max_blocksize);
-	args->buffer = page_address(*(rqstp->rq_next_page++));
+	len = args->count  = min_t(u32, args->count, max_blocksize);
+
+	while (len > 0) {
+		struct page *p = *(rqstp->rq_next_page++);
+		if (!args->buffer)
+			args->buffer = page_address(p);
+		len -= PAGE_SIZE;
+	}
 
 	return xdr_argsize_check(rqstp, p);
 }
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 38 lines · Murphy Zhou

After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger than PAGE_SIZE. So if the directory been read is large enough, we can use multiple pages in rq_respages. Update buffer count and page pointers like we do in readdirplus to make this happen.

Now listing a directory within 3000 files will panic because we are counting in a wrong way and would write on random page.

Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"

---

v2:
fix nfs3svc_decode_readdirargs to set page pointers like decode_readdirplusargs do not test pointers in encode_entry as we've fixed them when decoding

 fs/nfsd/nfs3proc.c | 17 +++++++++++++++--
 fs/nfsd/nfs3xdr.c  | 11 +++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
fs/nfsd/nfs3proc.c +3 -1
index 8f933e8..9bc32af 100644
@@ -442,7 +442,9 @@
 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
 	__be32		nfserr;
-	int		count;
+	int		count = 0;
+	struct page	**p;
+	caddr_t		page_addr = NULL;
 
 	dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
 				SVCFH_fmt(&argp->fh),

fs/nfsd/nfs3proc.c line 467

Quoted 5 lines
fs/nfsd/nfs3proc.c +1 -1
@@ -462,7 +464,18 @@
 	nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie, 
 					&resp->common, nfs3svc_encode_entry);
 	memcpy(resp->verf, argp->verf, 8);
-	resp->count = resp->buffer - argp->buffer;
+	count = 0;

Thanks - looks good.
Setting 'count' to zero a second time looks a bit clumsy, but that is a minor detail.

Thanks,
NeilBrown

Quoted 45 lines
fs/nfsd/nfs3proc.c +11 -0
+	for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
+		page_addr = page_address(*p);
+
+		if (((caddr_t)resp->buffer >= page_addr) &&
+		    ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
+			count += (caddr_t)resp->buffer - page_addr;
+			break;
+		}
+		count += PAGE_SIZE;
+	}
+	resp->count = count >> 2;
 	if (resp->offset) {
 		loff_t offset = argp->cookie;
 
fs/nfsd/nfs3xdr.c +9 -2
index 93fea24..8d78912 100644
@@ -573,6 +573,7 @@ void fill_post_wcc(struct svc_fh *fhp)
 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
 {
 	struct nfsd3_readdirargs *args = rqstp->rq_argp;
+	int len;
 	u32 max_blocksize = svc_max_payload(rqstp);
 
 	p = decode_fh(p, &args->fh);
@@ -582,8 +583,14 @@ void fill_post_wcc(struct svc_fh *fhp)
 	args->verf   = p; p += 2;
 	args->dircount = ~0;
 	args->count  = ntohl(*p++);
-	args->count  = min_t(u32, args->count, max_blocksize);
-	args->buffer = page_address(*(rqstp->rq_next_page++));
+	len = args->count  = min_t(u32, args->count, max_blocksize);
+
+	while (len > 0) {
+		struct page *p = *(rqstp->rq_next_page++);
+		if (!args->buffer)
+			args->buffer = page_address(p);
+		len -= PAGE_SIZE;
+	}
 
 	return xdr_argsize_check(rqstp, p);
 }
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): CADJHv_t_Cqav7Wr8dHo0kBBX3-ws0qVDcYi4HZt26Dq8jO+DGQ@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CADJHv_u-F+D-p-uhJzxKTwQijZHe8wHfcyC97tjAgOO_ToJYHw@mail.gmail.com

#4 lore raw

missed nfs list. resend.

Quoted 119 lines · Murphy Zhou
Quoted 42 lines · On Fri, Apr 5, 2019 at 14:08 Murphy Zhou <[email protected]> wrote:
Quoted 38 lines · On Fri, Apr 5, 2019 at 06:47 NeilBrown <[email protected]> wrote:
Quoted 35 lines · Murphy Zhou

After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger than PAGE_SIZE. So if the directory been read is large enough, we can use multiple pages in rq_respages. Update buffer count and page pointers like we do in readdirplus to make this happen.

Now listing a directory within 3000 files will panic because we are counting in a wrong way and would write on random page.

Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"

---

v2:
fix nfs3svc_decode_readdirargs to set page pointers like decode_readdirplusargs do not test pointers in encode_entry as we've fixed them when decoding

 fs/nfsd/nfs3proc.c | 17 +++++++++++++++--
 fs/nfsd/nfs3xdr.c  | 11 +++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
fs/nfsd/nfs3proc.c +3 -1
index 8f933e8..9bc32af 100644
@@ -442,7 +442,9 @@
      struct nfsd3_readdirargs *argp = rqstp->rq_argp;
      struct nfsd3_readdirres  *resp = rqstp->rq_resp;
      __be32          nfserr;
-     int             count;
+     int             count = 0;
+     struct page     **p;
+     caddr_t         page_addr = NULL;
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
fs/nfsd/nfs3proc.c +0 -0
      dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
                              SVCFH_fmt(&argp->fh),
@@ -462,7 +464,18 @@
      nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
Quoted 8 lines

fs/nfsd/nfs3proc.c line 467

Quoted 4 lines
fs/nfsd/nfs3proc.c +1 -1
                                      &resp->common, nfs3svc_encode_entry);
      memcpy(resp->verf, argp->verf, 8);
-     resp->count = resp->buffer - argp->buffer;
+     count = 0;

Thanks - looks good.
Setting 'count' to zero a second time looks a bit clumsy, but that is a minor detail.

Save some space in the stack :)

Thanks for your review!

M

Quoted 54 lines
Quoted 54 lines

Thanks,
NeilBrown

Quoted 45 lines
fs/nfsd/nfs3proc.c +11 -0
+     for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
+             page_addr = page_address(*p);
+
+             if (((caddr_t)resp->buffer >= page_addr) &&
+                 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
+                     count += (caddr_t)resp->buffer - page_addr;
+                     break;
+             }
+             count += PAGE_SIZE;
+     }
+     resp->count = count >> 2;
      if (resp->offset) {
              loff_t offset = argp->cookie;
 
fs/nfsd/nfs3xdr.c +9 -3
index 93fea24..8d78912 100644
@@ -573,6 +573,7 @@ void fill_post_wcc(struct svc_fh *fhp)
 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
 {
      struct nfsd3_readdirargs *args = rqstp->rq_argp;
+     int len;
      u32 max_blocksize = svc_max_payload(rqstp);
 
      p = decode_fh(p, &args->fh);
@@ -582,8 +583,14 @@ void fill_post_wcc(struct svc_fh *fhp)
      args->verf   = p; p += 2;
      args->dircount = ~0;
      args->count  = ntohl(*p++);
-     args->count  = min_t(u32, args->count, max_blocksize);
-     args->buffer = page_address(*(rqstp->rq_next_page++));
+     len = args->count  = min_t(u32, args->count, max_blocksize);
+
+     while (len > 0) {
+             struct page *p = *(rqstp->rq_next_page++);
+             if (!args->buffer)
+                     args->buffer = page_address(p);
+             len -= PAGE_SIZE;
+     }
 
      return xdr_argsize_check(rqstp, p);
 }
--

1.8.3.1

#5 lore raw
In reply to a message not in the archive
Quoted 54 lines · Murphy Zhou
Quoted 52 lines · On Fri, Apr 5, 2019 at 14:08 Murphy Zhou <[email protected]> wrote:
Quoted 48 lines · On Fri, Apr 5, 2019 at 06:47 NeilBrown <[email protected]> wrote:
Quoted 10 lines · Murphy Zhou

After this commit
f875a79 nfsd: allow nfsv3 readdir request to be larger.
nfsv3 readdir request size can be larger than PAGE_SIZE. So if the directory been read is large enough, we can use multiple pages in rq_respages. Update buffer count and page pointers like we do in readdirplus to make this happen.

Now listing a directory within 3000 files will panic because we are counting in a wrong way and would write on random page.

Quoted 6 lines
Fixes f875a79 "nfsd: allow nfsv3 readdir request to be larger"

---

v2:
fix nfs3svc_decode_readdirargs to set page pointers like

decode_readdirplusargs

Quoted 22 lines

do not test pointers in encode_entry as we've fixed them when decoding

 fs/nfsd/nfs3proc.c | 17 +++++++++++++++--
 fs/nfsd/nfs3xdr.c  | 11 +++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
fs/nfsd/nfs3proc.c +3 -1
index 8f933e8..9bc32af 100644
@@ -442,7 +442,9 @@
      struct nfsd3_readdirargs *argp = rqstp->rq_argp;
      struct nfsd3_readdirres  *resp = rqstp->rq_resp;
      __be32          nfserr;
-     int             count;
+     int             count = 0;
+     struct page     **p;
+     caddr_t         page_addr = NULL;
 
      dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
                              SVCFH_fmt(&argp->fh),

fs/nfsd/nfs3proc.c line 465

Quoted 2 lines
fs/nfsd/nfs3proc.c +0 -0
@@ -462,7 +464,18 @@
      nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
                                      &resp->common,

nfs3svc_encode_entry);

Quoted 3 lines
fs/nfsd/nfs3proc.c +1 -1
      memcpy(resp->verf, argp->verf, 8);
-     resp->count = resp->buffer - argp->buffer;
+     count = 0;
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Thanks - looks good.
Setting 'count' to zero a second time looks a bit clumsy, but that is a minor detail.

Save some space in the stack :)

We're not *that* desperate for stack space. But, I can live with it. Queuing up for 5.1.

--b.

Quoted 60 lines

Thanks for your review!

M

Quoted 55 lines
Quoted 54 lines

Thanks,
NeilBrown

Quoted 45 lines
fs/nfsd/nfs3proc.c +11 -0
+     for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
+             page_addr = page_address(*p);
+
+             if (((caddr_t)resp->buffer >= page_addr) &&
+                 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
+                     count += (caddr_t)resp->buffer - page_addr;
+                     break;
+             }
+             count += PAGE_SIZE;
+     }
+     resp->count = count >> 2;
      if (resp->offset) {
              loff_t offset = argp->cookie;
 
fs/nfsd/nfs3xdr.c +9 -3
index 93fea24..8d78912 100644
@@ -573,6 +573,7 @@ void fill_post_wcc(struct svc_fh *fhp)
 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
 {
      struct nfsd3_readdirargs *args = rqstp->rq_argp;
+     int len;
      u32 max_blocksize = svc_max_payload(rqstp);
 
      p = decode_fh(p, &args->fh);
@@ -582,8 +583,14 @@ void fill_post_wcc(struct svc_fh *fhp)
      args->verf   = p; p += 2;
      args->dircount = ~0;
      args->count  = ntohl(*p++);
-     args->count  = min_t(u32, args->count, max_blocksize);
-     args->buffer = page_address(*(rqstp->rq_next_page++));
+     len = args->count  = min_t(u32, args->count, max_blocksize);
+
+     while (len > 0) {
+             struct page *p = *(rqstp->rq_next_page++);
+             if (!args->buffer)
+                     args->buffer = page_address(p);
+             len -= PAGE_SIZE;
+     }
 
      return xdr_argsize_check(rqstp, p);
 }
--

1.8.3.1