[PATCH] NFSD: Change ret code of xdr_stream_decode_opaque_fixed

Sergey Bashirov posted 1 patch 2 months, 2 weeks ago
fs/nfsd/blocklayoutxdr.c                                      | 2 +-
include/linux/nfs_xdr.h                                       | 2 +-
include/linux/sunrpc/xdr.h                                    | 4 ++--
.../xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2 | 2 +-
.../xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2  | 2 +-
.../xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2 | 2 +-
6 files changed, 7 insertions(+), 7 deletions(-)
[PATCH] NFSD: Change ret code of xdr_stream_decode_opaque_fixed
Posted by Sergey Bashirov 2 months, 2 weeks ago
Since the XDR field is fixed in size, the caller already knows how many
bytes were decoded, on success. Thus, xdr_stream_decode_opaque_fixed()
doesn't need to return that value. And, xdr_stream_decode_u32 and _u64
both return zero on success.

This patch also simplifies the error checking to avoid potential integer
promotion issues.

Signed-off-by: Sergey Bashirov <sergeybashirov@gmail.com>
---
 fs/nfsd/blocklayoutxdr.c                                      | 2 +-
 include/linux/nfs_xdr.h                                       | 2 +-
 include/linux/sunrpc/xdr.h                                    | 4 ++--
 .../xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2 | 2 +-
 .../xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2  | 2 +-
 .../xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2 | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/nfsd/blocklayoutxdr.c b/fs/nfsd/blocklayoutxdr.c
index 266b2737882e1..2a4dcb428d823 100644
--- a/fs/nfsd/blocklayoutxdr.c
+++ b/fs/nfsd/blocklayoutxdr.c
@@ -157,7 +157,7 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
 
 		ret = xdr_stream_decode_opaque_fixed(xdr,
 				&bex.vol_id, sizeof(bex.vol_id));
-		if (ret < sizeof(bex.vol_id)) {
+		if (ret) {
 			nfserr = nfserr_bad_xdr;
 			goto fail;
 		}
diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
index 67f6632f723b4..dd80163e0140c 100644
--- a/include/linux/nfs_xdr.h
+++ b/include/linux/nfs_xdr.h
@@ -1863,7 +1863,7 @@ static inline int decode_opaque_fixed(struct xdr_stream *xdr,
 				      void *buf, size_t len)
 {
 	ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len);
-	if (unlikely(ret < 0))
+	if (unlikely(ret))
 		return -EIO;
 	return 0;
 }
diff --git a/include/linux/sunrpc/xdr.h b/include/linux/sunrpc/xdr.h
index e3358c630ba18..ffb699a02b17d 100644
--- a/include/linux/sunrpc/xdr.h
+++ b/include/linux/sunrpc/xdr.h
@@ -730,7 +730,7 @@ xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
  * @len: size of buffer pointed to by @ptr
  *
  * Return values:
- *   On success, returns size of object stored in @ptr
+ *   %0 on success
  *   %-EBADMSG on XDR buffer overflow
  */
 static inline ssize_t
@@ -741,7 +741,7 @@ xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
 	if (unlikely(!p))
 		return -EBADMSG;
 	xdr_decode_opaque_fixed(p, ptr, len);
-	return len;
+	return 0;
 }
 
 /**
diff --git a/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
index b4695ece1884b..ea9295f91aecc 100644
--- a/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
+++ b/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
@@ -2,5 +2,5 @@
 {% if annotate %}
 	/* member {{ name }} (fixed-length opaque) */
 {% endif %}
-	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) < 0)
+	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) != 0)
 		return false;
diff --git a/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
index b4695ece1884b..ea9295f91aecc 100644
--- a/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
+++ b/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
@@ -2,5 +2,5 @@
 {% if annotate %}
 	/* member {{ name }} (fixed-length opaque) */
 {% endif %}
-	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) < 0)
+	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) != 0)
 		return false;
diff --git a/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
index 8b4ff08c49e5e..bdc7bd24ffb13 100644
--- a/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
+++ b/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
@@ -13,5 +13,5 @@ xdrgen_decode_{{ name }}(struct xdr_stream *xdr, {{ classifier }}{{ name }} *ptr
 {% if annotate %}
 	/* (fixed-length opaque) */
 {% endif %}
-	return xdr_stream_decode_opaque_fixed(xdr, ptr, {{ size }}) >= 0;
+	return xdr_stream_decode_opaque_fixed(xdr, ptr, {{ size }}) == 0;
 };
-- 
2.43.0
Re: [PATCH] NFSD: Change ret code of xdr_stream_decode_opaque_fixed
Posted by Dan Carpenter 2 months, 2 weeks ago
On Thu, Jul 17, 2025 at 10:48:30PM +0300, Sergey Bashirov wrote:
> diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
> index 67f6632f723b4..dd80163e0140c 100644
> --- a/include/linux/nfs_xdr.h
> +++ b/include/linux/nfs_xdr.h
> @@ -1863,7 +1863,7 @@ static inline int decode_opaque_fixed(struct xdr_stream *xdr,
>  				      void *buf, size_t len)
>  {
>  	ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len);
> -	if (unlikely(ret < 0))
> +	if (unlikely(ret))
>  		return -EIO;

We could propagate the error code (-EBADMSG) instead of return -EIO?

regards,
dan carpenter

>  	return 0;
>  }
Re: [PATCH] NFSD: Change ret code of xdr_stream_decode_opaque_fixed
Posted by Chuck Lever 2 months, 2 weeks ago
On 7/17/25 3:48 PM, Sergey Bashirov wrote:
> Since the XDR field is fixed in size, the caller already knows how many
> bytes were decoded, on success. Thus, xdr_stream_decode_opaque_fixed()
> doesn't need to return that value. And, xdr_stream_decode_u32 and _u64
> both return zero on success.
> 
> This patch also simplifies the error checking to avoid potential integer
> promotion issues.
> 
> Signed-off-by: Sergey Bashirov <sergeybashirov@gmail.com>
> ---
>  fs/nfsd/blocklayoutxdr.c                                      | 2 +-
>  include/linux/nfs_xdr.h                                       | 2 +-
>  include/linux/sunrpc/xdr.h                                    | 4 ++--
>  .../xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2 | 2 +-
>  .../xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2  | 2 +-
>  .../xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2 | 2 +-
>  6 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/nfsd/blocklayoutxdr.c b/fs/nfsd/blocklayoutxdr.c
> index 266b2737882e1..2a4dcb428d823 100644
> --- a/fs/nfsd/blocklayoutxdr.c
> +++ b/fs/nfsd/blocklayoutxdr.c
> @@ -157,7 +157,7 @@ nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
>  
>  		ret = xdr_stream_decode_opaque_fixed(xdr,
>  				&bex.vol_id, sizeof(bex.vol_id));
> -		if (ret < sizeof(bex.vol_id)) {
> +		if (ret) {

I've dropped "nfsd: Implement large extent array support in pNFS" from
nfsd-next because of the number of (minor) issues that have been found
so far.

Also, I think this patch needs to come before "Implement large extent",
and so does the forthcoming patch that moves nfsd4_decode_deviceid4().

So, please rebase this one today's nfsd-next.


>  			nfserr = nfserr_bad_xdr;
>  			goto fail;
>  		}
> diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
> index 67f6632f723b4..dd80163e0140c 100644
> --- a/include/linux/nfs_xdr.h
> +++ b/include/linux/nfs_xdr.h
> @@ -1863,7 +1863,7 @@ static inline int decode_opaque_fixed(struct xdr_stream *xdr,
>  				      void *buf, size_t len)
>  {
>  	ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len);
> -	if (unlikely(ret < 0))
> +	if (unlikely(ret))

Unless I've misunderstood Dan's bug report, I don't think these
call sites need to change ... The compiler should promote the
naked integer to the proper integer type so that the less-than
comparison is still valid.


>  		return -EIO;
>  	return 0;
>  }
> diff --git a/include/linux/sunrpc/xdr.h b/include/linux/sunrpc/xdr.h
> index e3358c630ba18..ffb699a02b17d 100644
> --- a/include/linux/sunrpc/xdr.h
> +++ b/include/linux/sunrpc/xdr.h
> @@ -730,7 +730,7 @@ xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
>   * @len: size of buffer pointed to by @ptr
>   *
>   * Return values:
> - *   On success, returns size of object stored in @ptr
> + *   %0 on success
>   *   %-EBADMSG on XDR buffer overflow
>   */
>  static inline ssize_t
> @@ -741,7 +741,7 @@ xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
>  	if (unlikely(!p))
>  		return -EBADMSG;
>  	xdr_decode_opaque_fixed(p, ptr, len);
> -	return len;
> +	return 0;
>  }
>  
>  /**
> diff --git a/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
> index b4695ece1884b..ea9295f91aecc 100644
> --- a/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
> +++ b/tools/net/sunrpc/xdrgen/templates/C/pointer/decoder/fixed_length_opaque.j2
> @@ -2,5 +2,5 @@
>  {% if annotate %}
>  	/* member {{ name }} (fixed-length opaque) */
>  {% endif %}
> -	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) < 0)
> +	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) != 0)

Ditto.


>  		return false;
> diff --git a/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
> index b4695ece1884b..ea9295f91aecc 100644
> --- a/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
> +++ b/tools/net/sunrpc/xdrgen/templates/C/struct/decoder/fixed_length_opaque.j2
> @@ -2,5 +2,5 @@
>  {% if annotate %}
>  	/* member {{ name }} (fixed-length opaque) */
>  {% endif %}
> -	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) < 0)
> +	if (xdr_stream_decode_opaque_fixed(xdr, ptr->{{ name }}, {{ size }}) != 0)
>  		return false;

Ditto.


> diff --git a/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2 b/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
> index 8b4ff08c49e5e..bdc7bd24ffb13 100644
> --- a/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
> +++ b/tools/net/sunrpc/xdrgen/templates/C/typedef/decoder/fixed_length_opaque.j2
> @@ -13,5 +13,5 @@ xdrgen_decode_{{ name }}(struct xdr_stream *xdr, {{ classifier }}{{ name }} *ptr
>  {% if annotate %}
>  	/* (fixed-length opaque) */
>  {% endif %}
> -	return xdr_stream_decode_opaque_fixed(xdr, ptr, {{ size }}) >= 0;
> +	return xdr_stream_decode_opaque_fixed(xdr, ptr, {{ size }}) == 0;
>  };

The existing comparison will work fine. But "== 0" will match the new
return values better. LGTM.


-- 
Chuck Lever