Sat Jul 29 15:04:29 2023 UTC ()
Add/fix statx, readahead, close_range. From GSoC 2023 by Theodore Preduta


(christos)
diff -r1.8 -r1.9 src/sys/compat/linux/arch/aarch64/syscalls.master
diff -r1.103 -r1.104 src/sys/compat/linux/arch/alpha/syscalls.master
diff -r1.69 -r1.70 src/sys/compat/linux/arch/amd64/syscalls.master
diff -r1.76 -r1.77 src/sys/compat/linux/arch/arm/syscalls.master
diff -r1.131 -r1.132 src/sys/compat/linux/arch/i386/syscalls.master
diff -r1.102 -r1.103 src/sys/compat/linux/arch/m68k/syscalls.master
diff -r1.75 -r1.76 src/sys/compat/linux/arch/mips/syscalls.master
diff -r1.81 -r1.82 src/sys/compat/linux/arch/powerpc/syscalls.master
diff -r1.67 -r1.68 src/sys/compat/linux/common/linux_file64.c
diff -r1.259 -r1.260 src/sys/compat/linux/common/linux_misc.c

cvs diff -r1.8 -r1.9 src/sys/compat/linux/arch/aarch64/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/aarch64/syscalls.master 2023/07/29 06:47:26 1.8
+++ src/sys/compat/linux/arch/aarch64/syscalls.master 2023/07/29 15:04:28 1.9
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.8 2023/07/29 06:47:26 rin Exp $
+	$NetBSD: syscalls.master,v 1.9 2023/07/29 15:04:28 christos Exp $
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -433,7 +433,8 @@
 			    const struct linux_msghdr *msg, int flags); }
 212	STD		{ ssize_t|linux_sys||recvmsg(int s, \
 			    struct linux_msghdr *msg, int flags); }
-213	UNIMPL		readahead
+213	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 214	STD		{ int|linux_sys||brk(char *nsize); }
 215	NOARGS		{ int|sys||munmap(void *addr, size_t len); }
 216	STD		{ void *|linux_sys||mremap(void *old_address, \
@@ -675,7 +676,8 @@
 433	UNIMPL		fspick
 434	UNIMPL		pidfd_open
 435	UNIMPL		clone3
-436	UNIMPL
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL		openat2
 438	UNIMPL		pidfd_getfd
 439	UNIMPL		faccessat2

cvs diff -r1.103 -r1.104 src/sys/compat/linux/arch/alpha/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/alpha/syscalls.master 2023/07/28 19:01:11 1.103
+++ src/sys/compat/linux/arch/alpha/syscalls.master 2023/07/29 15:04:28 1.104
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.103 2023/07/28 19:01:11 christos Exp $
+	$NetBSD: syscalls.master,v 1.104 2023/07/29 15:04:28 christos Exp $
 ;
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -614,7 +614,8 @@
 377	STD		{ int|linux_sys||getdents64(int fd, \
 			    struct linux_dirent64 *dent, unsigned int count); }
 378	NOARGS		{ pid_t|linux_sys||gettid(void); }
-379	UNIMPL		readahead
+379	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 380	UNIMPL		/* unused */
 381	STD		{ int|linux_sys||tkill(int tid, int sig); }
 382	STD		{ int|linux_sys||setxattr(char *path, char *name, \
@@ -830,7 +831,9 @@
 519	UNIMPL
 520	UNIMPL
 521	UNIMPL
-522	UNIMPL
+522     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 523	UNIMPL
 524	UNIMPL
 525	UNIMPL
@@ -854,7 +857,8 @@
 543	UNIMPL
 544	UNIMPL
 545	UNIMPL
-546	UNIMPL
+546	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 547	UNIMPL
 548	UNIMPL
 549	UNIMPL

cvs diff -r1.69 -r1.70 src/sys/compat/linux/arch/amd64/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/amd64/syscalls.master 2023/07/28 18:19:00 1.69
+++ src/sys/compat/linux/arch/amd64/syscalls.master 2023/07/29 15:04:28 1.70
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.69 2023/07/28 18:19:00 christos Exp $
+	$NetBSD: syscalls.master,v 1.70 2023/07/29 15:04:28 christos Exp $
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -374,7 +374,8 @@
 184	UNIMPL		tuxcall
 185	UNIMPL		security
 186	STD		{ pid_t|linux_sys||gettid(void); }
-187	UNIMPL		readahead
+187	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 188	STD		{ int|linux_sys||setxattr(char *path, char *name, \
 			    void *value, size_t size, int flags); }
 189	STD		{ int|linux_sys||lsetxattr(char *path, char *name, \
@@ -593,7 +594,9 @@
 329	UNIMPL		pkey_mprotect
 330	UNIMPL		pkey_alloc
 331	UNIMPL		pkey_free
-332	UNIMPL		statx
+332     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 333	UNIMPL		io_pgetevents
 334	UNIMPL		rseq
 335	UNIMPL
@@ -697,7 +700,8 @@
 433	UNIMPL		fspick
 434	UNIMPL		pidfd_open
 435	UNIMPL		clone3
-436	UNIMPL		close_range
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL		openat2
 438	UNIMPL		pidfd_getfd
 439	UNIMPL		faccessat2

cvs diff -r1.76 -r1.77 src/sys/compat/linux/arch/arm/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/arm/syscalls.master 2023/07/28 19:01:11 1.76
+++ src/sys/compat/linux/arch/arm/syscalls.master 2023/07/29 15:04:28 1.77
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.76 2023/07/28 19:01:11 christos Exp $
+	$NetBSD: syscalls.master,v 1.77 2023/07/29 15:04:28 christos Exp $
 
 ; Derived from sys/compat/linux/arch/*/syscalls.master
 ; and from Linux 2.4.12 arch/arm/kernel/calls.S
@@ -386,7 +386,8 @@
 222	UNIMPL		/* for tux */
 223	UNIMPL		/* unused */
 224	NOARGS		{ pid_t|linux_sys||gettid(void); }
-225	UNIMPL		readahead
+225	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 226	STD		{ int|linux_sys||setxattr(char *path, char *name, \
 			    void *value, size_t size, int flags); }
 227	STD		{ int|linux_sys||lsetxattr(char *path, char *name, \
@@ -644,7 +645,9 @@
 394	UNIMPL
 395	UNIMPL
 396	UNIMPL
-397	UNIMPL
+397     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 398	UNIMPL
 399	UNIMPL
 400	UNIMPL
@@ -683,7 +686,8 @@
 433	UNIMPL
 434	UNIMPL
 435	UNIMPL
-436	UNIMPL
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL
 438	UNIMPL
 439	UNIMPL

cvs diff -r1.131 -r1.132 src/sys/compat/linux/arch/i386/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/i386/syscalls.master 2023/07/28 19:01:11 1.131
+++ src/sys/compat/linux/arch/i386/syscalls.master 2023/07/29 15:04:28 1.132
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.131 2023/07/28 19:01:11 christos Exp $
+	$NetBSD: syscalls.master,v 1.132 2023/07/29 15:04:28 christos Exp $
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -383,7 +383,8 @@
 222	UNIMPL		/* unused */
 223	UNIMPL		/* unused */
 224	NOARGS		{ pid_t|linux_sys||gettid(void); }
-225	UNIMPL		readahead
+225	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 226	STD		{ int|linux_sys||setxattr(char *path, char *name, \
 			    void *value, size_t size, int flags); }
 227	STD		{ int|linux_sys||lsetxattr(char *path, char *name, \
@@ -622,7 +623,9 @@
 394	UNIMPL		semctl
 395	UNIMPL		shmget
 396	UNIMPL		shmctl
-397	UNIMPL		shmat
+397     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 398	UNIMPL		shmdt
 399	UNIMPL		msgget
 400	UNIMPL		msgsnd
@@ -661,7 +664,8 @@
 433	UNIMPL		fspick
 434	UNIMPL		pidfd_open
 435	UNIMPL		clone3
-436	UNIMPL		close_range
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL		openat2
 438	UNIMPL		pidfd_getfd
 439	UNIMPL		faccessat2

cvs diff -r1.102 -r1.103 src/sys/compat/linux/arch/m68k/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/m68k/syscalls.master 2023/07/28 19:01:11 1.102
+++ src/sys/compat/linux/arch/m68k/syscalls.master 2023/07/29 15:04:29 1.103
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.102 2023/07/28 19:01:11 christos Exp $
+	$NetBSD: syscalls.master,v 1.103 2023/07/29 15:04:29 christos Exp $
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -428,7 +428,8 @@
 237	NOARGS		{ int|sys||mincore(void *addr, size_t len, char *vec); }
 238	NOARGS		{ int|sys||madvise(void *addr, size_t len, int behav); }
 239	STD		{ int|linux_sys||fcntl64(int fd, int cmd, void *arg); }
-240	UNIMPL		readahead
+240	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 241	UNIMPL		io_setup
 242	UNIMPL		io_destroy
 243	UNIMPL		io_getevents
@@ -631,7 +632,9 @@
 376	UNIMPL		copy_file_range
 377	UNIMPL		preadv2
 378	UNIMPL		pwritev2
-379	UNIMPL
+379     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 380	UNIMPL
 381	UNIMPL
 382	UNIMPL
@@ -688,7 +691,8 @@
 433	UNIMPL
 434	UNIMPL
 435	UNIMPL
-436	UNIMPL
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL
 438	UNIMPL
 439	UNIMPL

cvs diff -r1.75 -r1.76 src/sys/compat/linux/arch/mips/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/mips/syscalls.master 2023/07/28 19:01:11 1.75
+++ src/sys/compat/linux/arch/mips/syscalls.master 2023/07/29 15:04:29 1.76
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.75 2023/07/28 19:01:11 christos Exp $  
+	$NetBSD: syscalls.master,v 1.76 2023/07/29 15:04:29 christos Exp $  
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -389,7 +389,8 @@
 			    int cmd, void *arg); }
 221	UNIMPL		/* reserved */
 222	NOARGS		{ pid_t|linux_sys||gettid(void); }
-223	UNIMPL		readahead
+223	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 224	STD		{ int|linux_sys||setxattr(char *path, char *name, \
 			    void *value, size_t size, int flags); }
 225	STD		{ int|linux_sys||lsetxattr(char *path, char *name, \
@@ -592,21 +593,24 @@
 350	UNIMPL		sched_setattr
 351	UNIMPL		sched_getattr
 352	UNIMPL		renameat2
-353	UNIMPL		seccomp
-354	NOARGS		{ ssize_t|sys||getrandom(void *buf, size_t buflen, \
+353	NOARGS		{ ssize_t|sys||getrandom(void *buf, size_t buflen, \
 			    unsigned int flags); }
-355	UNIMPL		memfd_create
-356	UNIMPL		bpf
-357	UNIMPL		execveat
-358	UNIMPL		userfaultfd
-359	UNIMPL		membarrier
-360	UNIMPL		mlock2
-361	UNIMPL		copy_file_range
-362	UNIMPL		preadv2
-363	UNIMPL		pwritev2
-364	UNIMPL		pkey_mprotect
-365	UNIMPL		pkey_alloc
-366	UNIMPL		pkey_free
+354	STD		{ int|linux_sys||memfd_create(const char *name, \
+			    unsigned int flags); }
+355	UNIMPL		bpf
+356	UNIMPL		execveat
+357	UNIMPL		userfaultfd
+358	UNIMPL		membarrier
+369	UNIMPL		mlock2
+360	UNIMPL		copy_file_range
+361	UNIMPL		preadv2
+362	UNIMPL		pwritev2
+363	UNIMPL		pkey_mprotect
+364	UNIMPL		pkey_alloc
+365	UNIMPL		pkey_free
+366     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 367	UNIMPL
 368	UNIMPL
 369	UNIMPL
@@ -676,7 +680,8 @@
 433	UNIMPL
 434	UNIMPL
 435	UNIMPL
-436	UNIMPL
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL
 438	UNIMPL
 439	UNIMPL

cvs diff -r1.81 -r1.82 src/sys/compat/linux/arch/powerpc/syscalls.master (expand / switch to context diff)
--- src/sys/compat/linux/arch/powerpc/syscalls.master 2023/07/28 19:01:11 1.81
+++ src/sys/compat/linux/arch/powerpc/syscalls.master 2023/07/29 15:04:29 1.82
@@ -1,4 +1,4 @@
-	$NetBSD: syscalls.master,v 1.81 2023/07/28 19:01:11 christos Exp $  
+	$NetBSD: syscalls.master,v 1.82 2023/07/29 15:04:29 christos Exp $  
 
 ;	@(#)syscalls.master	8.1 (Berkeley) 7/19/93
 
@@ -357,7 +357,8 @@
 189	NOARGS		{ int|sys|14|vfork(void); }
 190	STD		{ int|linux_sys||ugetrlimit(int which, \
 			    struct rlimit *rlp); }
-191	UNIMPL		/* unused */
+191	STD		{ ssize_t|linux_sys||readahead(int fd, off_t offset, \
+			    size_t count); }
 #define linux_sys_mmap2_args linux_sys_mmap_args
 192	NOARGS		{ linux_off_t|linux_sys||mmap2(unsigned long addr, \
 			    size_t len, int prot, int flags, int fd, \
@@ -633,7 +634,9 @@
 380	UNIMPL		preadv2
 381	UNIMPL		pwritev2
 382	UNIMPL		kexec_file_load
-383	UNIMPL
+383     STD		{ int|linux_sys||statx(int fd, const char *path, \
+			    int flag, unsigned int mask, \
+			    struct linux_statx *sp); }
 384	UNIMPL
 385	UNIMPL
 386	UNIMPL
@@ -686,7 +689,8 @@
 433	UNIMPL
 434	UNIMPL
 435	UNIMPL
-436	UNIMPL
+436	STD		{ int|linux_sys||close_range(unsigned int first, \
+			    unsigned int last, unsigned int flags); }
 437	UNIMPL
 438	UNIMPL
 439	UNIMPL

cvs diff -r1.67 -r1.68 src/sys/compat/linux/common/linux_file64.c (expand / switch to context diff)
--- src/sys/compat/linux/common/linux_file64.c 2021/11/25 02:27:08 1.67
+++ src/sys/compat/linux/common/linux_file64.c 2023/07/29 15:04:29 1.68
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_file64.c,v 1.67 2021/11/25 02:27:08 ryo Exp $	*/
+/*	$NetBSD: linux_file64.c,v 1.68 2023/07/29 15:04:29 christos Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.67 2021/11/25 02:27:08 ryo Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.68 2023/07/29 15:04:29 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -251,6 +251,10 @@
 	struct vnode *vp;
 	int error, nd_flag;
 	uint8_t c;
+
+	if (lflag & ~(LINUX_AT_EMPTY_PATH|LINUX_AT_NO_AUTOMOUNT
+            |LINUX_AT_SYMLINK_NOFOLLOW))
+		return EINVAL;
 
 	if (lflag & LINUX_AT_EMPTY_PATH) {
 		/*

cvs diff -r1.259 -r1.260 src/sys/compat/linux/common/linux_misc.c (expand / switch to context diff)
--- src/sys/compat/linux/common/linux_misc.c 2023/07/29 07:00:00 1.259
+++ src/sys/compat/linux/common/linux_misc.c 2023/07/29 15:04:29 1.260
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_misc.c,v 1.259 2023/07/29 07:00:00 rin Exp $	*/
+/*	$NetBSD: linux_misc.c,v 1.260 2023/07/29 15:04:29 christos Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 1999, 2008 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.259 2023/07/29 07:00:00 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.260 2023/07/29 15:04:29 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1976,4 +1976,84 @@
 	SCARG(&muap, flags) = lflags & LINUX_MFD_KNOWN_FLAGS;
 
 	return sys_memfd_create(l, &muap, retval);
+}
+
+#define	LINUX_CLOSE_RANGE_UNSHARE	0x02U
+#define	LINUX_CLOSE_RANGE_CLOEXEC	0x04U
+
+/*
+ * close_range(2).
+ */
+int
+linux_sys_close_range(struct lwp *l,
+    const struct linux_sys_close_range_args *uap, register_t *retval)
+{
+	/* {
+		syscallarg(unsigned int) first;
+		syscallarg(unsigned int) last;
+		syscallarg(unsigned int) flags;
+	} */
+	unsigned int fd, last;
+	file_t *fp;
+	filedesc_t *fdp;
+	const unsigned int flags = SCARG(uap, flags);
+
+	if (flags & ~(LINUX_CLOSE_RANGE_CLOEXEC|LINUX_CLOSE_RANGE_UNSHARE))
+		return EINVAL;
+	if (SCARG(uap, first) > SCARG(uap, last))
+		return EINVAL;
+
+	if (flags & LINUX_CLOSE_RANGE_UNSHARE) {
+		fdp = fd_copy();
+		fd_free();
+	        l->l_proc->p_fd = fdp;
+	        l->l_fd = fdp;
+	}
+
+	last = MIN(SCARG(uap, last), l->l_proc->p_fd->fd_lastfile);
+	for (fd = SCARG(uap, first); fd <= last; fd++) {
+		fp = fd_getfile(fd);
+		if (fp == NULL)
+			continue;
+
+		if (flags & LINUX_CLOSE_RANGE_CLOEXEC) {
+			fd_set_exclose(l, fd, true);
+			fd_putfile(fd);
+		} else
+			fd_close(fd);
+	}
+
+	return 0;
+}
+
+/*
+ * readahead(2).  Call posix_fadvise with POSIX_FADV_WILLNEED with some extra
+ * error checking.
+ */
+int
+linux_sys_readahead(struct lwp *l, const struct linux_sys_readahead_args *uap,
+    register_t *retval)
+{
+	/* {
+		syscallarg(int) fd;
+		syscallarg(off_t) offset;
+		syscallarg(size_t) count;
+	} */
+	file_t *fp;
+	int error = 0;
+	const int fd = SCARG(uap, fd);
+
+	fp = fd_getfile(fd);
+	if (fp == NULL)
+		return EBADF;
+	if ((fp->f_flag & FREAD) == 0)
+		error = EBADF;
+	else if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG)
+		error = EINVAL;
+	fd_putfile(fd);
+	if (error != 0)
+		return error;
+
+	return do_posix_fadvise(fd, SCARG(uap, offset), SCARG(uap, count),
+	    POSIX_FADV_WILLNEED);
 }