[v2,04/13] tools/nolibc: add missing nanoseconds support for __NR_statx

Message ID 4cd637be248b5bfad6f2a01b82a9fb6f3fe4c6fa.1685387484.git.falcon@tinylab.org
State New
Headers
Series nolibc: add part2 of support for rv32 |

Commit Message

Zhangjin Wu May 29, 2023, 7:50 p.m. UTC
  Commit a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
added nanoseconds for stat() but missed the statx case, this adds it.

The stx_atime, stx_mtime, stx_ctime are in type of 'struct
statx_timestamp', which is incompatible with 'struct timespec', should
convert explicitly.

    /* include/uapi/linux/stat.h */

    struct statx_timestamp {
    	__s64	tv_sec;
    	__u32	tv_nsec;
    	__s32	__reserved;
    };

    /* include/uapi/linux/time_types.h */
    struct __kernel_timespec {
    	__kernel_time64_t       tv_sec;                 /* seconds */
    	long long               tv_nsec;                /* nanoseconds */
    };

    /* tools/include/nolibc/types.h */
    #define timespec __kernel_timespec

Without this patch, the stat_timestamps test case would fail on rv32.

Fixes: a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/sys.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Thomas Weißschuh May 29, 2023, 9:39 p.m. UTC | #1
On 2023-05-30 03:50:34+0800, Zhangjin Wu wrote:
> Commit a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
> added nanoseconds for stat() but missed the statx case, this adds it.

Welp, I should have thought of that.
At least the testcase seems to have been useful.

Thanks for the fix!

> The stx_atime, stx_mtime, stx_ctime are in type of 'struct
> statx_timestamp', which is incompatible with 'struct timespec', should
> convert explicitly.
> 
>     /* include/uapi/linux/stat.h */
> 
>     struct statx_timestamp {
>     	__s64	tv_sec;
>     	__u32	tv_nsec;
>     	__s32	__reserved;
>     };
> 
>     /* include/uapi/linux/time_types.h */
>     struct __kernel_timespec {
>     	__kernel_time64_t       tv_sec;                 /* seconds */
>     	long long               tv_nsec;                /* nanoseconds */
>     };
> 
>     /* tools/include/nolibc/types.h */
>     #define timespec __kernel_timespec
> 
> Without this patch, the stat_timestamps test case would fail on rv32.
> 
> Fixes: a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  tools/include/nolibc/sys.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> index 154194056962..98cfa2f6d021 100644
> --- a/tools/include/nolibc/sys.h
> +++ b/tools/include/nolibc/sys.h
> @@ -1175,9 +1175,9 @@ int sys_stat(const char *path, struct stat *buf)
>  	buf->st_size    = statx.stx_size;
>  	buf->st_blksize = statx.stx_blksize;
>  	buf->st_blocks  = statx.stx_blocks;
> -	buf->st_atime   = statx.stx_atime.tv_sec;
> -	buf->st_mtime   = statx.stx_mtime.tv_sec;
> -	buf->st_ctime   = statx.stx_ctime.tv_sec;
> +	buf->st_atim    = (struct timespec){ .tv_sec = statx.stx_atime.tv_sec, .tv_nsec = statx.stx_atime.tv_nsec };
> +	buf->st_mtim    = (struct timespec){ .tv_sec = statx.stx_mtime.tv_sec, .tv_nsec = statx.stx_mtime.tv_nsec };
> +	buf->st_ctim    = (struct timespec){ .tv_sec = statx.stx_ctime.tv_sec, .tv_nsec = statx.stx_ctime.tv_nsec };

I would prefer to split the compound assignment into two single
assignments, though.

buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec;
buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;

>  	return ret;
>  }
>  #else
> -- 
> 2.25.1
>
  
Zhangjin Wu May 30, 2023, 5:21 a.m. UTC | #2
Thomas, Arnd, Willy

> On 2023-05-30 03:50:34+0800, Zhangjin Wu wrote:
> > Commit a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
> > added nanoseconds for stat() but missed the statx case, this adds it.
> 
> Welp, I should have thought of that.
> At least the testcase seems to have been useful.
>

yeah, your testcase telled me this issue.

> Thanks for the fix!
> 
> > The stx_atime, stx_mtime, stx_ctime are in type of 'struct
> > statx_timestamp', which is incompatible with 'struct timespec', should
> > convert explicitly.
> > 
> >     /* include/uapi/linux/stat.h */
> > 
> >     struct statx_timestamp {
> >     	__s64	tv_sec;
> >     	__u32	tv_nsec;
> >     	__s32	__reserved;
> >     };
> > 
> >     /* include/uapi/linux/time_types.h */
> >     struct __kernel_timespec {
> >     	__kernel_time64_t       tv_sec;                 /* seconds */
> >     	long long               tv_nsec;                /* nanoseconds */
> >     };
> > 
> >     /* tools/include/nolibc/types.h */
> >     #define timespec __kernel_timespec
> > 
> > Without this patch, the stat_timestamps test case would fail on rv32.
> > 
> > Fixes: a89c937d781a ("tools/nolibc: support nanoseconds in stat()")
> > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > ---
> >  tools/include/nolibc/sys.h | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> > index 154194056962..98cfa2f6d021 100644
> > --- a/tools/include/nolibc/sys.h
> > +++ b/tools/include/nolibc/sys.h
> > @@ -1175,9 +1175,9 @@ int sys_stat(const char *path, struct stat *buf)
> >  	buf->st_size    = statx.stx_size;
> >  	buf->st_blksize = statx.stx_blksize;
> >  	buf->st_blocks  = statx.stx_blocks;
> > -	buf->st_atime   = statx.stx_atime.tv_sec;
> > -	buf->st_mtime   = statx.stx_mtime.tv_sec;
> > -	buf->st_ctime   = statx.stx_ctime.tv_sec;
> > +	buf->st_atim    = (struct timespec){ .tv_sec = statx.stx_atime.tv_sec, .tv_nsec = statx.stx_atime.tv_nsec };
> > +	buf->st_mtim    = (struct timespec){ .tv_sec = statx.stx_mtime.tv_sec, .tv_nsec = statx.stx_mtime.tv_nsec };
> > +	buf->st_ctim    = (struct timespec){ .tv_sec = statx.stx_ctime.tv_sec, .tv_nsec = statx.stx_ctime.tv_nsec };
> 
> I would prefer to split the compound assignment into two single
> assignments, though.
> 
> buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec;
> buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
>

Ok, will update it in the v3 revision.

And further, what about removing the other !statx parts
(__NR_newfstatat, __NR_stat)? just like we are doing for the other 64bit
syscalls (llseek and time65).

Best regards,
Zhangjin

> >  	return ret;
> >  }
> >  #else
  

Patch

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 154194056962..98cfa2f6d021 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -1175,9 +1175,9 @@  int sys_stat(const char *path, struct stat *buf)
 	buf->st_size    = statx.stx_size;
 	buf->st_blksize = statx.stx_blksize;
 	buf->st_blocks  = statx.stx_blocks;
-	buf->st_atime   = statx.stx_atime.tv_sec;
-	buf->st_mtime   = statx.stx_mtime.tv_sec;
-	buf->st_ctime   = statx.stx_ctime.tv_sec;
+	buf->st_atim    = (struct timespec){ .tv_sec = statx.stx_atime.tv_sec, .tv_nsec = statx.stx_atime.tv_nsec };
+	buf->st_mtim    = (struct timespec){ .tv_sec = statx.stx_mtime.tv_sec, .tv_nsec = statx.stx_mtime.tv_nsec };
+	buf->st_ctim    = (struct timespec){ .tv_sec = statx.stx_ctime.tv_sec, .tv_nsec = statx.stx_ctime.tv_nsec };
 	return ret;
 }
 #else