Module Name:    src
Committed By:   riastradh
Date:           Sat Apr 22 13:53:03 UTC 2023

Modified Files:
        src/sys/kern: sys_descrip.c sys_pipe.c sys_socket.c vfs_vnops.c
        src/sys/sys: file.h

Log Message:
file(9): New fo_posix_fadvise operation.

XXX kernel revbump -- changes struct fileops API and ABI


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/sys/kern/sys_descrip.c
cvs rdiff -u -r1.159 -r1.160 src/sys/kern/sys_pipe.c
cvs rdiff -u -r1.80 -r1.81 src/sys/kern/sys_socket.c
cvs rdiff -u -r1.240 -r1.241 src/sys/kern/vfs_vnops.c
cvs rdiff -u -r1.91 -r1.92 src/sys/sys/file.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/sys_descrip.c
diff -u src/sys/kern/sys_descrip.c:1.42 src/sys/kern/sys_descrip.c:1.43
--- src/sys/kern/sys_descrip.c:1.42	Sat Apr 22 13:52:54 2023
+++ src/sys/kern/sys_descrip.c	Sat Apr 22 13:53:02 2023
@@ -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 sy
 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;
 }

Index: src/sys/kern/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.159 src/sys/kern/sys_pipe.c:1.160
--- src/sys/kern/sys_pipe.c:1.159	Sat Apr 22 13:52:54 2023
+++ src/sys/kern/sys_pipe.c	Sat Apr 22 13:53:02 2023
@@ -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 st
 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
 	}
 }
 
+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)
 {

Index: src/sys/kern/sys_socket.c
diff -u src/sys/kern/sys_socket.c:1.80 src/sys/kern/sys_socket.c:1.81
--- src/sys/kern/sys_socket.c:1.80	Sat Apr 22 13:52:54 2023
+++ src/sys/kern/sys_socket.c	Sat Apr 22 13:53:02 2023
@@ -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
 #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,
 		return EINVAL;
 	}
 }
+
+static int
+soo_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice)
+{
+
+	return ESPIPE;
+}

Index: src/sys/kern/vfs_vnops.c
diff -u src/sys/kern/vfs_vnops.c:1.240 src/sys/kern/vfs_vnops.c:1.241
--- src/sys/kern/vfs_vnops.c:1.240	Sat Apr 22 13:52:54 2023
+++ src/sys/kern/vfs_vnops.c	Sat Apr 22 13:53:02 2023
@@ -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 
 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, 
 	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.

Index: src/sys/sys/file.h
diff -u src/sys/sys/file.h:1.91 src/sys/sys/file.h:1.92
--- src/sys/sys/file.h:1.91	Sat Apr 22 13:52:55 2023
+++ src/sys/sys/file.h	Sat Apr 22 13:53:02 2023
@@ -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 {

Reply via email to