From 81dc4778cc2b8ecd8ce6a0312e701550df766bbb Mon Sep 17 00:00:00 2001 From: kib Date: Tue, 15 Sep 2020 21:55:21 +0000 Subject: [PATCH] Do not copy vp into f_data for DTYPE_VNODE files. The pointer to vnode is already stored into f_vnode, so f_data can be reused. Fix all found users of f_data for DTYPE_VNODE. Provide finit_vnode() helper to initialize file of DTYPE_VNODE type. Reviewed by: markj (previous version) Discussed with: freqlabs (openzfs chunk) Tested by: pho (previous version) Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D26346 --- .../openzfs/module/os/freebsd/zfs/zfs_file_os.c | 2 +- sys/fs/nfs/nfsdport.h | 2 +- sys/kern/kern_descrip.c | 9 +++++++++ sys/kern/vfs_syscalls.c | 11 ++--------- sys/sys/file.h | 1 + sys/ufs/ffs/ffs_alloc.c | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_file_os.c b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_file_os.c index ec7c04717c8..81796915f5d 100644 --- a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_file_os.c +++ b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_file_os.c @@ -239,7 +239,7 @@ zfs_file_fsync(zfs_file_t *fp, int flags) if (fp->f_type != DTYPE_VNODE) return (EINVAL); - v = fp->f_data; + v = fp->f_vnode; return (zfs_vop_fsync(v)); } diff --git a/sys/fs/nfs/nfsdport.h b/sys/fs/nfs/nfsdport.h index 28b155fba72..6d468219adf 100644 --- a/sys/fs/nfs/nfsdport.h +++ b/sys/fs/nfs/nfsdport.h @@ -94,7 +94,7 @@ struct nfsexstuff { #define NFSLOCKHASH(f) \ (&nfslockhash[nfsrv_hashfh(f) % nfsrv_lockhashsize]) -#define NFSFPVNODE(f) ((struct vnode *)((f)->f_data)) +#define NFSFPVNODE(f) ((f)->f_vnode) #define NFSFPCRED(f) ((f)->f_cred) #define NFSFPFLAG(f) ((f)->f_flag) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 3bfe913682e..6d67f9ce2e5 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2622,6 +2622,15 @@ finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops) atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); } +void +finit_vnode(struct file *fp, u_int flag, void *data, struct fileops *ops) +{ + fp->f_seqcount[UIO_READ] = 1; + fp->f_seqcount[UIO_WRITE] = 1; + finit(fp, (flag & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, + data, ops); +} + int fget_cap_locked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp, struct file **fpp, struct filecaps *havecapsp) diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index f44b440be66..718ae706de5 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -1163,10 +1163,7 @@ kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg, */ if (fp->f_ops == &badfileops) { KASSERT(vp->v_type != VFIFO, ("Unexpected fifo.")); - fp->f_seqcount[UIO_READ] = 1; - fp->f_seqcount[UIO_WRITE] = 1; - finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK), - DTYPE_VNODE, vp, &vnops); + finit_vnode(fp, flags, NULL, &vnops); } VOP_UNLOCK(vp); @@ -4138,7 +4135,6 @@ kern_getdirentries(struct thread *td, int fd, char *buf, size_t count, vp = vp->v_mount->mnt_vnodecovered; VREF(vp); fp->f_vnode = vp; - fp->f_data = vp; foffset = 0; vput(tvp); goto unionread; @@ -4502,10 +4498,7 @@ sys_fhopen(struct thread *td, struct fhopen_args *uap) td->td_dupfd = 0; #endif fp->f_vnode = vp; - fp->f_seqcount[UIO_READ] = 1; - fp->f_seqcount[UIO_WRITE] = 1; - finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp, - &vnops); + finit_vnode(fp, fmode, NULL, &vnops); VOP_UNLOCK(vp); if ((fmode & O_TRUNC) != 0) { error = fo_truncate(fp, 0, td->td_ucred, td); diff --git a/sys/sys/file.h b/sys/sys/file.h index 10d48376446..7c69ee2ae98 100644 --- a/sys/sys/file.h +++ b/sys/sys/file.h @@ -268,6 +268,7 @@ fo_fill_kinfo_t vn_fill_kinfo; int vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif); void finit(struct file *, u_int, short, void *, struct fileops *); +void finit_vnode(struct file *, u_int, void *, struct fileops *); int fgetvp(struct thread *td, int fd, cap_rights_t *rightsp, struct vnode **vpp); int fgetvp_exec(struct thread *td, int fd, cap_rights_t *rightsp, diff --git a/sys/ufs/ffs/ffs_alloc.c b/sys/ufs/ffs/ffs_alloc.c index 9677a236cc7..7ad39e63bb1 100644 --- a/sys/ufs/ffs/ffs_alloc.c +++ b/sys/ufs/ffs/ffs_alloc.c @@ -3220,7 +3220,7 @@ sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS) if ((error = getvnode(td, cmd.handle, cap_rights_init(&rights, CAP_FSCK), &fp)) != 0) return (error); - vp = fp->f_data; + vp = fp->f_vnode; if (vp->v_type != VREG && vp->v_type != VDIR) { fdrop(fp, td); return (EINVAL); -- 2.45.0