diff options
| author | riastradh <riastradh@NetBSD.org> | 2023-04-22 13:53:02 +0000 |
|---|---|---|
| committer | riastradh <riastradh@NetBSD.org> | 2023-04-22 13:53:02 +0000 |
| commit | 8178a7a2d81f16ca831725c219370671b7fb89b9 (patch) | |
| tree | 3c34253f5cec10ea815d0fe70a3e508fb081c298 /sys | |
| parent | ca38e52195090063cad0680fa8cf71c23583a7dc (diff) | |
file(9): New fo_posix_fadvise operation.
XXX kernel revbump -- changes struct fileops API and ABI
Diffstat (limited to 'sys')
| -rw-r--r-- | sys/kern/sys_descrip.c | 97 | ||||
| -rw-r--r-- | sys/kern/sys_pipe.c | 13 | ||||
| -rw-r--r-- | sys/kern/sys_socket.c | 13 | ||||
| -rw-r--r-- | sys/kern/vfs_vnops.c | 86 | ||||
| -rw-r--r-- | sys/sys/file.h | 4 |
5 files changed, 117 insertions, 96 deletions
diff --git a/sys/kern/sys_descrip.c b/sys/kern/sys_descrip.c index afce46c48d8..bca05481cf2 100644 --- a/sys/kern/sys_descrip.c +++ b/sys/kern/sys_descrip.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_descrip.c,v 1.42 2023/04/22 13:52:54 riastradh Exp $ */ +/* $NetBSD: sys_descrip.c,v 1.43 2023/04/22 13:53:02 riastradh Exp $ */ /*- * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc. @@ -67,7 +67,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.42 2023/04/22 13:52:54 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.43 2023/04/22 13:53:02 riastradh Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -650,98 +650,17 @@ sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval) int do_posix_fadvise(int fd, off_t offset, off_t len, int advice) { - const off_t OFF_MAX = __type_max(off_t); file_t *fp; - vnode_t *vp; - off_t endoffset; int error; - CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL); - CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM); - CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL); - - if (offset < 0) { - return EINVAL; - } - if (len == 0) { - endoffset = OFF_MAX; - } else if (len > 0 && (OFF_MAX - offset) >= len) { - endoffset = offset + len; - } else { - return EINVAL; - } - if ((fp = fd_getfile(fd)) == NULL) { + if ((fp = fd_getfile(fd)) == NULL) return EBADF; + if (fp->f_ops->fo_posix_fadvise == NULL) { + error = EOPNOTSUPP; + } else { + error = (*fp->f_ops->fo_posix_fadvise)(fp, offset, len, + advice); } - if (fp->f_type != DTYPE_VNODE) { - if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { - error = ESPIPE; - } else { - error = EOPNOTSUPP; - } - fd_putfile(fd); - return error; - } - - switch (advice) { - case POSIX_FADV_WILLNEED: - case POSIX_FADV_DONTNEED: - vp = fp->f_vnode; - if (vp->v_type != VREG && vp->v_type != VBLK) { - fd_putfile(fd); - return 0; - } - break; - } - - switch (advice) { - case POSIX_FADV_NORMAL: - case POSIX_FADV_RANDOM: - case POSIX_FADV_SEQUENTIAL: - /* - * We ignore offset and size. Must lock the file to - * do this, as f_advice is sub-word sized. - */ - mutex_enter(&fp->f_lock); - fp->f_advice = (u_char)advice; - mutex_exit(&fp->f_lock); - error = 0; - break; - - case POSIX_FADV_WILLNEED: - vp = fp->f_vnode; - error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset); - break; - - case POSIX_FADV_DONTNEED: - vp = fp->f_vnode; - /* - * Align the region to page boundaries as VOP_PUTPAGES expects - * by shrinking it. We shrink instead of expand because we - * do not want to deactivate cache outside of the requested - * region. It means that if the specified region is smaller - * than PAGE_SIZE, we do nothing. - */ - if (offset <= trunc_page(OFF_MAX) && - round_page(offset) < trunc_page(endoffset)) { - rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); - error = VOP_PUTPAGES(vp, - round_page(offset), trunc_page(endoffset), - PGO_DEACTIVATE | PGO_CLEANIT); - } else { - error = 0; - } - break; - - case POSIX_FADV_NOREUSE: - /* Not implemented yet. */ - error = 0; - break; - default: - error = EINVAL; - break; - } - fd_putfile(fd); return error; } diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c index f2d0072e8fb..daf9e6bf746 100644 --- a/sys/kern/sys_pipe.c +++ b/sys/kern/sys_pipe.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_pipe.c,v 1.159 2023/04/22 13:52:54 riastradh Exp $ */ +/* $NetBSD: sys_pipe.c,v 1.160 2023/04/22 13:53:02 riastradh Exp $ */ /*- * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -68,7 +68,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.159 2023/04/22 13:52:54 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.160 2023/04/22 13:53:02 riastradh Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -101,6 +101,7 @@ 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 int pipe_posix_fadvise(file_t *, off_t, off_t, int); static const struct fileops pipeops = { .fo_name = "pipe", @@ -114,6 +115,7 @@ static const struct fileops pipeops = { .fo_kqfilter = pipe_kqfilter, .fo_restart = pipe_restart, .fo_fpathconf = pipe_fpathconf, + .fo_posix_fadvise = pipe_posix_fadvise, }; /* @@ -928,6 +930,13 @@ pipe_fpathconf(struct file *fp, int name, register_t *retval) } } +static int +pipe_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice) +{ + + return ESPIPE; +} + static void pipe_free_kmem(struct pipe *pipe) { diff --git a/sys/kern/sys_socket.c b/sys/kern/sys_socket.c index eb1c5dc1969..f923f5bf28e 100644 --- a/sys/kern/sys_socket.c +++ b/sys/kern/sys_socket.c @@ -1,4 +1,4 @@ -/* $NetBSD: sys_socket.c,v 1.80 2023/04/22 13:52:54 riastradh Exp $ */ +/* $NetBSD: sys_socket.c,v 1.81 2023/04/22 13:53:02 riastradh Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -61,7 +61,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.80 2023/04/22 13:52:54 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.81 2023/04/22 13:53:02 riastradh Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -81,6 +81,7 @@ __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.80 2023/04/22 13:52:54 riastradh Ex #include <net/route.h> static int soo_fpathconf(struct file *, int, register_t *); +static int soo_posix_fadvise(struct file *, off_t, off_t, int); const struct fileops socketops = { .fo_name = "socket", @@ -94,6 +95,7 @@ const struct fileops socketops = { .fo_kqfilter = soo_kqfilter, .fo_restart = soo_restart, .fo_fpathconf = soo_fpathconf, + .fo_posix_fadvise = soo_posix_fadvise, }; int (*ifioctl)(struct socket *, u_long, void *, struct lwp *) = (void *)eopnotsupp; @@ -279,3 +281,10 @@ soo_fpathconf(struct file *fp, int name, register_t *retval) return EINVAL; } } + +static int +soo_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice) +{ + + return ESPIPE; +} diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index f301b430c25..3bbb3b9fdce 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_vnops.c,v 1.240 2023/04/22 13:52:54 riastradh Exp $ */ +/* $NetBSD: vfs_vnops.c,v 1.241 2023/04/22 13:53:02 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -66,7 +66,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.240 2023/04/22 13:52:54 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.241 2023/04/22 13:53:02 riastradh Exp $"); #include "veriexec.h" @@ -124,6 +124,7 @@ static int vn_mmap(struct file *, off_t *, size_t, int, int *, 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 *); +static int vn_posix_fadvise(struct file *, off_t, off_t, int); const struct fileops vnops = { .fo_name = "vn", @@ -140,6 +141,7 @@ const struct fileops vnops = { .fo_seek = vn_seek, .fo_advlock = vn_advlock, .fo_fpathconf = vn_fpathconf, + .fo_posix_fadvise = vn_posix_fadvise, }; /* @@ -1249,6 +1251,86 @@ vn_fpathconf(struct file *fp, int name, register_t *retval) return error; } +static int +vn_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice) +{ + const off_t OFF_MAX = __type_max(off_t); + struct vnode *vp = fp->f_vnode; + off_t endoffset; + int error; + + if (offset < 0) { + return EINVAL; + } + if (len == 0) { + endoffset = OFF_MAX; + } else if (len > 0 && (OFF_MAX - offset) >= len) { + endoffset = offset + len; + } else { + return EINVAL; + } + + CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL); + CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM); + CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL); + + switch (advice) { + case POSIX_FADV_WILLNEED: + case POSIX_FADV_DONTNEED: + if (vp->v_type != VREG && vp->v_type != VBLK) + return 0; + break; + } + + switch (advice) { + case POSIX_FADV_NORMAL: + case POSIX_FADV_RANDOM: + case POSIX_FADV_SEQUENTIAL: + /* + * We ignore offset and size. Must lock the file to + * do this, as f_advice is sub-word sized. + */ + mutex_enter(&fp->f_lock); + fp->f_advice = (u_char)advice; + mutex_exit(&fp->f_lock); + error = 0; + break; + + case POSIX_FADV_WILLNEED: + error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset); + break; + + case POSIX_FADV_DONTNEED: + /* + * Align the region to page boundaries as VOP_PUTPAGES expects + * by shrinking it. We shrink instead of expand because we + * do not want to deactivate cache outside of the requested + * region. It means that if the specified region is smaller + * than PAGE_SIZE, we do nothing. + */ + if (offset <= trunc_page(OFF_MAX) && + round_page(offset) < trunc_page(endoffset)) { + rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); + error = VOP_PUTPAGES(vp, + round_page(offset), trunc_page(endoffset), + PGO_DEACTIVATE | PGO_CLEANIT); + } else { + error = 0; + } + break; + + case POSIX_FADV_NOREUSE: + /* Not implemented yet. */ + error = 0; + break; + default: + error = EINVAL; + break; + } + + 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 c586de40232..59ee80f5198 100644 --- a/sys/sys/file.h +++ b/sys/sys/file.h @@ -1,4 +1,4 @@ -/* $NetBSD: file.h,v 1.91 2023/04/22 13:52:55 riastradh Exp $ */ +/* $NetBSD: file.h,v 1.92 2023/04/22 13:53:02 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -101,6 +101,8 @@ struct fileops { int (*fo_advlock) (struct file *, void *, int, struct flock *, int); int (*fo_fpathconf) (struct file *, int, register_t *); + int (*fo_posix_fadvise) + (struct file *, off_t, off_t, int); }; union file_data { |
