Date: Wed, 4 Aug 2021 17:16:54 GMT From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: git: 0ef5eee9d941 - main - Add vn_lktype_write() Message-ID: <202108041716.174HGsab006970@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=0ef5eee9d94162ac3cefa0bd8aedf1bfa6f87192 commit 0ef5eee9d94162ac3cefa0bd8aedf1bfa6f87192 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2021-08-04 02:34:48 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2021-08-04 16:40:13 +0000 Add vn_lktype_write() and remove repetetive code that calculates vnode locking type for write. Reviewed by: khng, markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D31405 --- sys/cam/ctl/ctl_backend_block.c | 20 +++++--------------- sys/kern/vfs_syscalls.c | 10 ++-------- sys/kern/vfs_vnops.c | 34 ++++++++++++++-------------------- sys/sys/vnode.h | 1 + sys/vm/vm_pageout.c | 6 ++---- 5 files changed, 24 insertions(+), 47 deletions(-) diff --git a/sys/cam/ctl/ctl_backend_block.c b/sys/cam/ctl/ctl_backend_block.c index 4ffaf0912915..83ea6b43dac3 100644 --- a/sys/cam/ctl/ctl_backend_block.c +++ b/sys/cam/ctl/ctl_backend_block.c @@ -588,7 +588,7 @@ ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, { union ctl_io *io = beio->io; struct mount *mountpoint; - int error, lock_flags; + int error; DPRINTF("entered\n"); @@ -597,12 +597,8 @@ ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); - if (MNT_SHARED_WRITES(mountpoint) || - ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount))) - lock_flags = LK_SHARED; - else - lock_flags = LK_EXCLUSIVE; - vn_lock(be_lun->vn, lock_flags | LK_RETRY); + vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) | + LK_RETRY); error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, curthread); VOP_UNLOCK(be_lun->vn); @@ -722,16 +718,10 @@ ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, } } else { struct mount *mountpoint; - int lock_flags; (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); - - if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL) - && MNT_SHARED_WRITES(be_lun->vn->v_mount))) - lock_flags = LK_SHARED; - else - lock_flags = LK_EXCLUSIVE; - vn_lock(be_lun->vn, lock_flags | LK_RETRY); + vn_lock(be_lun->vn, vn_lktype_write(mountpoint, + be_lun->vn) | LK_RETRY); /* * UFS pays attention to IO_DIRECT for writes. The write diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 82629a4f5947..5701932a0163 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -3517,7 +3517,7 @@ kern_fsync(struct thread *td, int fd, bool fullsync) struct vnode *vp; struct mount *mp; struct file *fp; - int error, lock_flags; + int error; AUDIT_ARG_FD(fd); error = getvnode(td, fd, &cap_fsync_rights, &fp); @@ -3532,13 +3532,7 @@ retry: error = vn_start_write(vp, &mp, V_WAIT | PCATCH); if (error != 0) goto drop; - if (MNT_SHARED_WRITES(mp) || - ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) { - lock_flags = LK_SHARED; - } else { - lock_flags = LK_EXCLUSIVE; - } - vn_lock(vp, lock_flags | LK_RETRY); + vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY); AUDIT_ARG_VNODE1(vp); if (vp->v_object != NULL) { VM_OBJECT_WLOCK(vp->v_object); diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index fc5118e8aa24..b7e53add5a35 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -653,11 +653,7 @@ vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) goto out; - if (MNT_SHARED_WRITES(mp) || - ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) - lock_flags = LK_SHARED; - else - lock_flags = LK_EXCLUSIVE; + lock_flags = vn_lktype_write(mp, vp); } else lock_flags = LK_SHARED; vn_lock(vp, lock_flags | LK_RETRY); @@ -1106,7 +1102,7 @@ vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct vnode *vp; struct mount *mp; off_t orig_offset; - int error, ioflag, lock_flags; + int error, ioflag; int advice; bool need_finished_write; @@ -1147,14 +1143,7 @@ vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, advice = get_advice(fp, uio); - if (MNT_SHARED_WRITES(mp) || - (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) { - lock_flags = LK_SHARED; - } else { - lock_flags = LK_EXCLUSIVE; - } - - vn_lock(vp, lock_flags | LK_RETRY); + vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY); switch (advice) { case POSIX_FADV_NORMAL: case POSIX_FADV_SEQUENTIAL: @@ -3060,7 +3049,7 @@ vn_write_outvp(struct vnode *outvp, char *dat, off_t outoff, off_t xfer, { struct mount *mp; off_t dataoff, holeoff, xfer2; - int error, lckf; + int error; /* * Loop around doing writes of blksize until write has been completed. @@ -3099,11 +3088,7 @@ vn_write_outvp(struct vnode *outvp, char *dat, off_t outoff, off_t xfer, VOP_UNLOCK(outvp); } } else { - if (MNT_SHARED_WRITES(mp)) - lckf = LK_SHARED; - else - lckf = LK_EXCLUSIVE; - error = vn_lock(outvp, lckf); + error = vn_lock(outvp, vn_lktype_write(mp, outvp)); if (error == 0) { error = vn_rdwr(UIO_WRITE, outvp, dat, xfer2, outoff, UIO_SYSSPACE, IO_NODELOCKED, @@ -3532,3 +3517,12 @@ vn_lock_pair(struct vnode *vp1, bool vp1_locked, struct vnode *vp2, if (vp2 != NULL) ASSERT_VOP_ELOCKED(vp2, "vp2 ret"); } + +int +vn_lktype_write(struct mount *mp, struct vnode *vp) +{ + if (MNT_SHARED_WRITES(mp) || + (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) + return (LK_SHARED); + return (LK_EXCLUSIVE); +} diff --git a/sys/sys/vnode.h b/sys/sys/vnode.h index 64fb00c5845e..1f2d74fdf5f1 100644 --- a/sys/sys/vnode.h +++ b/sys/sys/vnode.h @@ -1089,6 +1089,7 @@ int vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, void vn_fsid(struct vnode *vp, struct vattr *va); int vn_dir_check_exec(struct vnode *vp, struct componentname *cnp); +int vn_lktype_write(struct mount *mp, struct vnode *vp); #define VOP_UNLOCK_FLAGS(vp, flags) ({ \ struct vnode *_vp = (vp); \ diff --git a/sys/vm/vm_pageout.c b/sys/vm/vm_pageout.c index 216e76359631..9a0b295569e2 100644 --- a/sys/vm/vm_pageout.c +++ b/sys/vm/vm_pageout.c @@ -606,7 +606,7 @@ vm_pageout_clean(vm_page_t m, int *numpagedout) struct mount *mp; vm_object_t object; vm_pindex_t pindex; - int error, lockmode; + int error; object = m->object; VM_OBJECT_ASSERT_WLOCKED(object); @@ -640,9 +640,7 @@ vm_pageout_clean(vm_page_t m, int *numpagedout) vm_object_reference_locked(object); pindex = m->pindex; VM_OBJECT_WUNLOCK(object); - lockmode = MNT_SHARED_WRITES(vp->v_mount) ? - LK_SHARED : LK_EXCLUSIVE; - if (vget(vp, lockmode | LK_TIMELOCK)) { + if (vget(vp, vn_lktype_write(NULL, vp) | LK_TIMELOCK) != 0) { vp = NULL; error = EDEADLK; goto unlock_mp;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202108041716.174HGsab006970>