git: 0784b5768e00 - stable/14 - vn_copy_file_range(): busy both in and out mp around call to VOP_COPY_FILE_RANGE()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 20 Nov 2023 01:40:22 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=0784b5768e003bbf5677327d1a68f13d2ea2c230

commit 0784b5768e003bbf5677327d1a68f13d2ea2c230
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-11-12 18:37:29 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-11-20 01:39:22 +0000

    vn_copy_file_range(): busy both in and out mp around call to VOP_COPY_FILE_RANGE()
    
    (cherry picked from commit 23210f538a008788b2e16b9eddafa4f598a21663)
---
 sys/kern/vfs_vnops.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index e2227537dde1..27ce5401f15f 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -3078,6 +3078,29 @@ vn_copy_file_range(struct vnode *invp, off_t *inoffp, struct vnode *outvp,
 
 	inmp = invp->v_mount;
 	outmp = outvp->v_mount;
+	if (inmp == NULL || outmp == NULL) {
+		error = EBADF;
+		goto out;
+	}
+
+	for (;;) {
+		error = vfs_busy(inmp, 0);
+		if (error != 0)
+			goto out;
+		if (inmp == outmp)
+			break;
+		error = vfs_busy(outmp, MBF_NOWAIT);
+		if (error != 0) {
+			vfs_unbusy(inmp);
+			error = vfs_busy(outmp, 0);
+			if (error == 0) {
+				vfs_unbusy(outmp);
+				continue;
+			}
+			goto out;
+		}
+		break;
+	}
 
 	/*
 	 * If the two vnodes are for the same file system type, call
@@ -3092,6 +3115,9 @@ vn_copy_file_range(struct vnode *invp, off_t *inoffp, struct vnode *outvp,
 	else
 		error = vn_generic_copy_file_range(invp, inoffp, outvp,
 		    outoffp, lenp, flags, incred, outcred, fsize_td);
+	vfs_unbusy(outmp);
+	if (inmp != outmp)
+		vfs_unbusy(inmp);
 out:
 	return (error);
 }