From ca38e52195090063cad0680fa8cf71c23583a7dc Mon Sep 17 00:00:00 2001 From: riastradh Date: Sat, 22 Apr 2023 13:52:54 +0000 Subject: file(9): New fo_fpathconf operation. XXX kernel revbump -- struct fileops API and ABI change --- sys/kern/kern_event.c | 13 +++++++++++-- sys/kern/sys_descrip.c | 40 ++++++++++------------------------------ sys/kern/sys_pipe.c | 19 +++++++++++++++++-- sys/kern/sys_socket.c | 20 ++++++++++++++++++-- sys/kern/vfs_vnops.c | 19 +++++++++++++++++-- sys/sys/file.h | 5 ++++- 6 files changed, 77 insertions(+), 39 deletions(-) diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index adab90bd94b..4983d7e4665 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -1,4 +1,4 @@ -/* $NetBSD: kern_event.c,v 1.147 2023/04/09 09:18:09 riastradh Exp $ */ +/* $NetBSD: kern_event.c,v 1.148 2023/04/22 13:52:54 riastradh Exp $ */ /*- * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc. @@ -63,7 +63,7 @@ #endif /* _KERNEL_OPT */ #include -__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.147 2023/04/09 09:18:09 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.148 2023/04/22 13:52:54 riastradh Exp $"); #include #include @@ -95,6 +95,7 @@ static int kqueue_kqfilter(file_t *, struct knote *); static int kqueue_stat(file_t *, struct stat *); static int kqueue_close(file_t *); static void kqueue_restart(file_t *); +static int kqueue_fpathconf(file_t *, int, register_t *); static int kqueue_register(struct kqueue *, struct kevent *); static void kqueue_doclose(struct kqueue *, struct klist *, int); @@ -186,6 +187,7 @@ static const struct fileops kqueueops = { .fo_close = kqueue_close, .fo_kqfilter = kqueue_kqfilter, .fo_restart = kqueue_restart, + .fo_fpathconf = kqueue_fpathconf, }; static void @@ -2249,6 +2251,13 @@ kqueue_restart(file_t *fp) mutex_spin_exit(&kq->kq_lock); } +static int +kqueue_fpathconf(struct file *fp, int name, register_t *retval) +{ + + return EINVAL; +} + /* * Scan through the list of events on fp (for a maximum of maxevents), * returning the results in to ulistp. Timeout is determined by tsp; if diff --git a/sys/kern/sys_descrip.c b/sys/kern/sys_descrip.c index 1ca0d1b4030..afce46c48d8 100644 --- a/sys/kern/sys_descrip.c +++ b/sys/kern/sys_descrip.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_descrip.c,v 1.41 2023/04/22 13:52:46 riastradh Exp $ */ +/* $NetBSD: sys_descrip.c,v 1.42 2023/04/22 13:52:54 riastradh Exp $ */ /*- * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc. @@ -67,7 +67,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.41 2023/04/22 13:52:46 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.42 2023/04/22 13:52:54 riastradh Exp $"); #include #include @@ -571,41 +571,21 @@ sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap, syscallarg(int) fd; syscallarg(int) name; } */ - int fd, error; + int fd, name, error; file_t *fp; fd = SCARG(uap, fd); + name = SCARG(uap, name); error = 0; - if ((fp = fd_getfile(fd)) == NULL) { - return (EBADF); - } - switch (fp->f_type) { - case DTYPE_SOCKET: - case DTYPE_PIPE: - if (SCARG(uap, name) != _PC_PIPE_BUF) - error = EINVAL; - else - *retval = PIPE_BUF; - break; - - case DTYPE_VNODE: - vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); - error = VOP_PATHCONF(fp->f_vnode, SCARG(uap, name), retval); - VOP_UNLOCK(fp->f_vnode); - break; - - case DTYPE_KQUEUE: - error = EINVAL; - break; - - default: + if ((fp = fd_getfile(fd)) == NULL) + return EBADF; + if (fp->f_ops->fo_fpathconf == NULL) error = EOPNOTSUPP; - break; - } - + else + error = (*fp->f_ops->fo_fpathconf)(fp, name, retval); fd_putfile(fd); - return (error); + return error; } /* diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c index 4a73eab8894..f2d0072e8fb 100644 --- a/sys/kern/sys_pipe.c +++ b/sys/kern/sys_pipe.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_pipe.c,v 1.158 2021/10/11 01:07:36 thorpej Exp $ */ +/* $NetBSD: sys_pipe.c,v 1.159 2023/04/22 13:52:54 riastradh Exp $ */ /*- * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -68,7 +68,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.158 2021/10/11 01:07:36 thorpej Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.159 2023/04/22 13:52:54 riastradh Exp $"); #include #include @@ -100,6 +100,7 @@ static int pipe_kqfilter(file_t *, struct knote *); static int pipe_stat(file_t *, struct stat *); static int pipe_ioctl(file_t *, u_long, void *); static void pipe_restart(file_t *); +static int pipe_fpathconf(file_t *, int, register_t *); static const struct fileops pipeops = { .fo_name = "pipe", @@ -112,6 +113,7 @@ static const struct fileops pipeops = { .fo_close = pipe_close, .fo_kqfilter = pipe_kqfilter, .fo_restart = pipe_restart, + .fo_fpathconf = pipe_fpathconf, }; /* @@ -913,6 +915,19 @@ pipe_restart(file_t *fp) mutex_exit(pipe->pipe_lock); } +static int +pipe_fpathconf(struct file *fp, int name, register_t *retval) +{ + + switch (name) { + case _PC_PIPE_BUF: + *retval = PIPE_BUF; + return 0; + default: + return EINVAL; + } +} + static void pipe_free_kmem(struct pipe *pipe) { diff --git a/sys/kern/sys_socket.c b/sys/kern/sys_socket.c index 8135a8a1e55..eb1c5dc1969 100644 --- a/sys/kern/sys_socket.c +++ b/sys/kern/sys_socket.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_socket.c,v 1.79 2020/11/17 03:22:33 chs Exp $ */ +/* $NetBSD: sys_socket.c,v 1.80 2023/04/22 13:52:54 riastradh Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -61,7 +61,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.79 2020/11/17 03:22:33 chs Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.80 2023/04/22 13:52:54 riastradh Exp $"); #include #include @@ -80,6 +80,8 @@ __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.79 2020/11/17 03:22:33 chs Exp $"); #include #include +static int soo_fpathconf(struct file *, int, register_t *); + const struct fileops socketops = { .fo_name = "socket", .fo_read = soo_read, @@ -91,6 +93,7 @@ const struct fileops socketops = { .fo_close = soo_close, .fo_kqfilter = soo_kqfilter, .fo_restart = soo_restart, + .fo_fpathconf = soo_fpathconf, }; int (*ifioctl)(struct socket *, u_long, void *, struct lwp *) = (void *)eopnotsupp; @@ -263,3 +266,16 @@ soo_restart(file_t *fp) sorestart(fp->f_socket); } + +static int +soo_fpathconf(struct file *fp, int name, register_t *retval) +{ + + switch (name) { + case _PC_PIPE_BUF: + *retval = PIPE_BUF; + return 0; + default: + return EINVAL; + } +} diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index 65e22d86981..f301b430c25 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 riastradh Exp $ */ +/* $NetBSD: vfs_vnops.c,v 1.240 2023/04/22 13:52:54 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -66,7 +66,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.240 2023/04/22 13:52:54 riastradh Exp $"); #include "veriexec.h" @@ -123,6 +123,7 @@ static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *, struct uvm_object **, int *); static int vn_seek(struct file *, off_t, int, off_t *, int); static int vn_advlock(struct file *, void *, int, struct flock *, int); +static int vn_fpathconf(struct file *, int, register_t *); const struct fileops vnops = { .fo_name = "vn", @@ -138,6 +139,7 @@ const struct fileops vnops = { .fo_mmap = vn_mmap, .fo_seek = vn_seek, .fo_advlock = vn_advlock, + .fo_fpathconf = vn_fpathconf, }; /* @@ -1234,6 +1236,19 @@ vn_advlock(struct file *fp, void *id, int op, struct flock *fl, return VOP_ADVLOCK(vp, id, op, fl, flags); } +static int +vn_fpathconf(struct file *fp, int name, register_t *retval) +{ + struct vnode *const vp = fp->f_vnode; + int error; + + vn_lock(vp, LK_SHARED | LK_RETRY); + error = VOP_PATHCONF(vp, name, retval); + VOP_UNLOCK(vp); + + return error; +} + /* * Check that the vnode is still valid, and if so * acquire requested lock. diff --git a/sys/sys/file.h b/sys/sys/file.h index aa9f8167c80..c586de40232 100644 --- a/sys/sys/file.h +++ b/sys/sys/file.h @@ -1,4 +1,4 @@ -/* $NetBSD: file.h,v 1.90 2023/04/22 13:52:46 riastradh Exp $ */ +/* $NetBSD: file.h,v 1.91 2023/04/22 13:52:55 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -63,6 +63,8 @@ #ifndef _SYS_FILE_H_ #define _SYS_FILE_H_ +#include + #include #include @@ -98,6 +100,7 @@ struct fileops { int (*fo_seek) (struct file *, off_t, int, off_t *, int); int (*fo_advlock) (struct file *, void *, int, struct flock *, int); + int (*fo_fpathconf) (struct file *, int, register_t *); }; union file_data { -- cgit