git: 47b248ac6516 - main - Make locking assertions for VOP_FSYNC() and VOP_FDATASYNC() more correct

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 12 Nov 2021 23:04:20 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=47b248ac651683650f7852485c594e76ca375573

commit 47b248ac651683650f7852485c594e76ca375573
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-11-03 22:28:32 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-11-12 23:02:13 +0000

    Make locking assertions for VOP_FSYNC() and VOP_FDATASYNC() more correct
    
    For devfs vnodes, it is fine to not lock vnodes for VOP_FSYNC().
    Otherwise vnode must be locked exclusively, except for MNT_SHARED_WRITES()
    where the shared lock is enough.
    
    Reported and tested by: pho
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D32761
---
 sys/kern/vfs_subr.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 sys/kern/vnode_if.src |  8 ++++++--
 sys/sys/vnode.h       |  8 ++++++++
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c
index c0214e570628..046b74218b80 100644
--- a/sys/kern/vfs_subr.c
+++ b/sys/kern/vfs_subr.c
@@ -5538,6 +5538,54 @@ vop_fplookup_symlink_debugpost(void *ap __unused, int rc __unused)
 
 	VFS_SMR_ASSERT_ENTERED();
 }
+
+static void
+vop_fsync_debugprepost(struct vnode *vp, const char *name)
+{
+	if (vp->v_type == VCHR)
+		;
+	else if (MNT_EXTENDED_SHARED(vp->v_mount))
+		ASSERT_VOP_LOCKED(vp, name);
+	else
+		ASSERT_VOP_ELOCKED(vp, name);
+}
+
+void
+vop_fsync_debugpre(void *a)
+{
+	struct vop_fsync_args *ap;
+
+	ap = a;
+	vop_fsync_debugprepost(ap->a_vp, "fsync");
+}
+
+void
+vop_fsync_debugpost(void *a, int rc __unused)
+{
+	struct vop_fsync_args *ap;
+
+	ap = a;
+	vop_fsync_debugprepost(ap->a_vp, "fsync");
+}
+
+void
+vop_fdatasync_debugpre(void *a)
+{
+	struct vop_fdatasync_args *ap;
+
+	ap = a;
+	vop_fsync_debugprepost(ap->a_vp, "fsync");
+}
+
+void
+vop_fdatasync_debugpost(void *a, int rc __unused)
+{
+	struct vop_fdatasync_args *ap;
+
+	ap = a;
+	vop_fsync_debugprepost(ap->a_vp, "fsync");
+}
+
 void
 vop_strategy_debugpre(void *ap)
 {
diff --git a/sys/kern/vnode_if.src b/sys/kern/vnode_if.src
index 276382738504..8ec2d3ef90fd 100644
--- a/sys/kern/vnode_if.src
+++ b/sys/kern/vnode_if.src
@@ -295,7 +295,9 @@ vop_revoke {
 };
 
 
-%% fsync	vp	L L L
+%% fsync	vp	- - -
+%! fsync	pre	vop_fsync_debugpre
+%! fsync	post	vop_fsync_debugpost
 
 vop_fsync {
 	IN struct vnode *vp;
@@ -769,7 +771,9 @@ vop_add_writecount {
 };
 
 
-%% fdatasync	vp	L L L
+%% fdatasync	vp	- - -
+%! fdatasync	pre	vop_fdatasync_debugpre
+%! fdatasync	post	vop_fdatasync_debugpost
 
 vop_fdatasync {
 	IN struct vnode *vp;
diff --git a/sys/sys/vnode.h b/sys/sys/vnode.h
index 18bba17011e8..6bffd7656c62 100644
--- a/sys/sys/vnode.h
+++ b/sys/sys/vnode.h
@@ -927,10 +927,14 @@ void	vop_symlink_post(void *a, int rc);
 int	vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a);
 
 #ifdef DEBUG_VFS_LOCKS
+void	vop_fdatasync_debugpre(void *a);
+void	vop_fdatasync_debugpost(void *a, int rc);
 void	vop_fplookup_vexec_debugpre(void *a);
 void	vop_fplookup_vexec_debugpost(void *a, int rc);
 void	vop_fplookup_symlink_debugpre(void *a);
 void	vop_fplookup_symlink_debugpost(void *a, int rc);
+void	vop_fsync_debugpre(void *a);
+void	vop_fsync_debugpost(void *a, int rc);
 void	vop_strategy_debugpre(void *a);
 void	vop_lock_debugpre(void *a);
 void	vop_lock_debugpost(void *a, int rc);
@@ -939,10 +943,14 @@ void	vop_need_inactive_debugpre(void *a);
 void	vop_need_inactive_debugpost(void *a, int rc);
 void	vop_mkdir_debugpost(void *a, int rc);
 #else
+#define	vop_fdatasync_debugpre(x)		do { } while (0)
+#define	vop_fdatasync_debugpost(x, y)		do { } while (0)
 #define	vop_fplookup_vexec_debugpre(x)		do { } while (0)
 #define	vop_fplookup_vexec_debugpost(x, y)	do { } while (0)
 #define	vop_fplookup_symlink_debugpre(x)	do { } while (0)
 #define	vop_fplookup_symlink_debugpost(x, y)	do { } while (0)
+#define	vop_fsync_debugpre(x)			do { } while (0)
+#define	vop_fsync_debugpost(x, y)		do { } while (0)
 #define	vop_strategy_debugpre(x)		do { } while (0)
 #define	vop_lock_debugpre(x)			do { } while (0)
 #define	vop_lock_debugpost(x, y)		do { } while (0)