Module Name: src Committed By: riastradh Date: Sat Apr 22 13:52:46 UTC 2023
Modified Files: src/sys/kern: kern_descrip.c sys_descrip.c vfs_vnops.c src/sys/sys: file.h Log Message: file(9): New fo_advlock operation. This moves the vnode-specific logic from sys_descrip.c into vfs_vnode.c, like we did for fo_seek. XXX kernel revbump -- struct fileops API and ABI change To generate a diff of this commit: cvs rdiff -u -r1.255 -r1.256 src/sys/kern/kern_descrip.c cvs rdiff -u -r1.40 -r1.41 src/sys/kern/sys_descrip.c cvs rdiff -u -r1.238 -r1.239 src/sys/kern/vfs_vnops.c cvs rdiff -u -r1.89 -r1.90 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/kern_descrip.c diff -u src/sys/kern/kern_descrip.c:1.255 src/sys/kern/kern_descrip.c:1.256 --- src/sys/kern/kern_descrip.c:1.255 Fri Feb 24 11:02:27 2023 +++ src/sys/kern/kern_descrip.c Sat Apr 22 13:52:46 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_descrip.c,v 1.255 2023/02/24 11:02:27 riastradh Exp $ */ +/* $NetBSD: kern_descrip.c,v 1.256 2023/04/22 13:52:46 riastradh Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -70,7 +70,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.255 2023/02/24 11:02:27 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.256 2023/04/22 13:52:46 riastradh Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -727,14 +727,14 @@ fd_close(unsigned fd) * If the descriptor was in a message, POSIX-style locks * aren't passed with the descriptor. */ - if (__predict_false((p->p_flag & PK_ADVLOCK) != 0 && - fp->f_type == DTYPE_VNODE)) { + if (__predict_false((p->p_flag & PK_ADVLOCK) != 0) && + fp->f_ops->fo_advlock != NULL) { lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; lf.l_type = F_UNLCK; mutex_exit(&fdp->fd_lock); - (void)VOP_ADVLOCK(fp->f_vnode, p, F_UNLCK, &lf, F_POSIX); + (void)(*fp->f_ops->fo_advlock)(fp, p, F_UNLCK, &lf, F_POSIX); mutex_enter(&fdp->fd_lock); } @@ -852,12 +852,12 @@ closef(file_t *fp) mutex_exit(&fp->f_lock); /* We held the last reference - release locks, close and free. */ - if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) { + if ((fp->f_flag & FHASLOCK) && fp->f_ops->fo_advlock != NULL) { lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; lf.l_type = F_UNLCK; - (void)VOP_ADVLOCK(fp->f_vnode, fp, F_UNLCK, &lf, F_FLOCK); + (void)(*fp->f_ops->fo_advlock)(fp, fp, F_UNLCK, &lf, F_FLOCK); } if (fp->f_ops != NULL) { error = (*fp->f_ops->fo_close)(fp); Index: src/sys/kern/sys_descrip.c diff -u src/sys/kern/sys_descrip.c:1.40 src/sys/kern/sys_descrip.c:1.41 --- src/sys/kern/sys_descrip.c:1.40 Sat Apr 16 07:59:02 2022 +++ src/sys/kern/sys_descrip.c Sat Apr 22 13:52:46 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: sys_descrip.c,v 1.40 2022/04/16 07:59:02 hannken Exp $ */ +/* $NetBSD: sys_descrip.c,v 1.41 2023/04/22 13:52:46 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.40 2022/04/16 07:59:02 hannken Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.41 2023/04/22 13:52:46 riastradh Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -235,19 +235,14 @@ int do_fcntl_lock(int fd, int cmd, struct flock *fl) { file_t *fp; - vnode_t *vp; proc_t *p; + int (*fo_advlock)(struct file *, void *, int, struct flock *, int); int error, flg; - if ((error = fd_getvnode(fd, &fp)) != 0) - return error; - - vp = fp->f_vnode; - if (fl->l_whence == SEEK_CUR) { - vn_lock(vp, LK_SHARED | LK_RETRY); - fl->l_start += fp->f_offset; - VOP_UNLOCK(vp); - } + if ((fp = fd_getfile(fd)) == NULL) + return EBADF; + if ((fo_advlock = fp->f_ops->fo_advlock) == NULL) + return EINVAL; flg = F_POSIX; p = curproc; @@ -270,7 +265,7 @@ do_fcntl_lock(int fd, int cmd, struct fl p->p_flag |= PK_ADVLOCK; mutex_exit(p->p_lock); } - error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg); + error = (*fo_advlock)(fp, p, F_SETLK, fl, flg); break; case F_WRLCK: @@ -283,11 +278,11 @@ do_fcntl_lock(int fd, int cmd, struct fl p->p_flag |= PK_ADVLOCK; mutex_exit(p->p_lock); } - error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg); + error = (*fo_advlock)(fp, p, F_SETLK, fl, flg); break; case F_UNLCK: - error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX); + error = (*fo_advlock)(fp, p, F_UNLCK, fl, F_POSIX); break; default: @@ -303,7 +298,7 @@ do_fcntl_lock(int fd, int cmd, struct fl error = EINVAL; break; } - error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX); + error = (*fo_advlock)(fp, p, F_GETLK, fl, F_POSIX); break; default: @@ -629,16 +624,17 @@ sys_flock(struct lwp *l, const struct sy } */ int fd, how, error; file_t *fp; - vnode_t *vp; + int (*fo_advlock)(struct file *, void *, int, struct flock *, int); struct flock lf; fd = SCARG(uap, fd); how = SCARG(uap, how); - if ((error = fd_getvnode(fd, &fp)) != 0) - return error == EINVAL ? EOPNOTSUPP : error; + if ((fp = fd_getfile(fd)) == NULL) + return EBADF; + if ((fo_advlock = fp->f_ops->fo_advlock) == NULL) + return EOPNOTSUPP; - vp = fp->f_vnode; lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; @@ -647,7 +643,7 @@ sys_flock(struct lwp *l, const struct sy case LOCK_UN: lf.l_type = F_UNLCK; atomic_and_uint(&fp->f_flag, ~FHASLOCK); - error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); + error = (*fo_advlock)(fp, fp, F_UNLCK, &lf, F_FLOCK); fd_putfile(fd); return error; case LOCK_EX: @@ -663,9 +659,9 @@ sys_flock(struct lwp *l, const struct sy atomic_or_uint(&fp->f_flag, FHASLOCK); if (how & LOCK_NB) { - error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK); + error = (*fo_advlock)(fp, fp, F_SETLK, &lf, F_FLOCK); } else { - error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT); + error = (*fo_advlock)(fp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT); } fd_putfile(fd); return error; Index: src/sys/kern/vfs_vnops.c diff -u src/sys/kern/vfs_vnops.c:1.238 src/sys/kern/vfs_vnops.c:1.239 --- src/sys/kern/vfs_vnops.c:1.238 Sat Apr 22 11:22:36 2023 +++ src/sys/kern/vfs_vnops.c Sat Apr 22 13:52:46 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_vnops.c,v 1.238 2023/04/22 11:22:36 riastradh Exp $ */ +/* $NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 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.238 2023/04/22 11:22:36 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 riastradh Exp $"); #include "veriexec.h" @@ -122,6 +122,7 @@ static int vn_ioctl(file_t *fp, u_long c 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); const struct fileops vnops = { .fo_name = "vn", @@ -136,6 +137,7 @@ const struct fileops vnops = { .fo_restart = fnullop_restart, .fo_mmap = vn_mmap, .fo_seek = vn_seek, + .fo_advlock = vn_advlock, }; /* @@ -1217,6 +1219,21 @@ out: VOP_UNLOCK(vp); return error; } +static int +vn_advlock(struct file *fp, void *id, int op, struct flock *fl, + int flags) +{ + struct vnode *const vp = fp->f_vnode; + + if (fl->l_whence == SEEK_CUR) { + vn_lock(vp, LK_SHARED | LK_RETRY); + fl->l_start += fp->f_offset; + VOP_UNLOCK(vp); + } + + return VOP_ADVLOCK(vp, id, op, fl, flags); +} + /* * 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.89 src/sys/sys/file.h:1.90 --- src/sys/sys/file.h:1.89 Sat Apr 22 13:52:37 2023 +++ src/sys/sys/file.h Sat Apr 22 13:52:46 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: file.h,v 1.89 2023/04/22 13:52:37 riastradh Exp $ */ +/* $NetBSD: file.h,v 1.90 2023/04/22 13:52:46 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -71,6 +71,7 @@ #include <sys/mutex.h> #include <sys/condvar.h> +struct flock; struct iovec; struct knote; struct lwp; @@ -95,6 +96,8 @@ struct fileops { int (*fo_mmap) (struct file *, off_t *, size_t, int, int *, int *, struct uvm_object **, int *); int (*fo_seek) (struct file *, off_t, int, off_t *, int); + int (*fo_advlock) (struct file *, void *, int, struct flock *, + int); }; union file_data {