git: 068913e4ba3d - main - zfs: Add vfs.zfs.bclone_enabled sysctl.

From: Pawel Jakub Dawidek <pjd_at_FreeBSD.org>
Date: Mon, 17 Apr 2023 10:39:27 UTC
The branch main has been updated by pjd:

URL: https://cgit.FreeBSD.org/src/commit/?id=068913e4ba3dd9b3067056e832cefc5ed264b5cc

commit 068913e4ba3dd9b3067056e832cefc5ed264b5cc
Author:     Pawel Jakub Dawidek <pjd@FreeBSD.org>
AuthorDate: 2023-04-17 10:38:30 +0000
Commit:     Pawel Jakub Dawidek <pjd@FreeBSD.org>
CommitDate: 2023-04-17 10:38:30 +0000

    zfs: Add vfs.zfs.bclone_enabled sysctl.
    
    Keep block cloning disabled by default for now, but allow to enable and
    use it after setting vfs.zfs.bclone_enabled to 1, so people can easily
    try it.
    
    Approved by:    oshogbo
    Reviewed by:    mm, oshogbo
    Differential Revision:  https://reviews.freebsd.org/D39613
---
 sys/contrib/openzfs/include/os/freebsd/zfs/sys/zfs_vfsops_os.h | 1 +
 sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c         | 4 ++++
 sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c       | 8 ++++++--
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/sys/contrib/openzfs/include/os/freebsd/zfs/sys/zfs_vfsops_os.h b/sys/contrib/openzfs/include/os/freebsd/zfs/sys/zfs_vfsops_os.h
index f765d38dbac8..5948e44daab1 100644
--- a/sys/contrib/openzfs/include/os/freebsd/zfs/sys/zfs_vfsops_os.h
+++ b/sys/contrib/openzfs/include/os/freebsd/zfs/sys/zfs_vfsops_os.h
@@ -287,6 +287,7 @@ typedef struct zfid_long {
 
 extern uint_t zfs_fsyncer_key;
 extern int zfs_super_owner;
+extern int zfs_bclone_enabled;
 
 extern void zfs_init(void);
 extern void zfs_fini(void);
diff --git a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c
index 30851f5273a2..b63899ddede0 100644
--- a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c
+++ b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c
@@ -89,6 +89,10 @@ int zfs_debug_level;
 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RWTUN, &zfs_debug_level, 0,
 	"Debug level");
 
+int zfs_bclone_enabled;
+SYSCTL_INT(_vfs_zfs, OID_AUTO, bclone_enabled, CTLFLAG_RWTUN,
+	&zfs_bclone_enabled, 0, "Enable block cloning");
+
 struct zfs_jailparam {
 	int mount_snapshot;
 };
diff --git a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c
index 7b5fe8a3abe0..5079d0afa9e4 100644
--- a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c
+++ b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c
@@ -6214,7 +6214,6 @@ zfs_deallocate(struct vop_deallocate_args *ap)
 }
 #endif
 
-#if 0
 #ifndef _SYS_SYSPROTO_H_
 struct vop_copy_file_range_args {
 	struct vnode *a_invp;
@@ -6245,6 +6244,11 @@ zfs_freebsd_copy_file_range(struct vop_copy_file_range_args *ap)
 	int error;
 	uint64_t len = *ap->a_lenp;
 
+	if (!zfs_bclone_enabled) {
+		mp = NULL;
+		goto bad_write_fallback;
+	}
+
 	/*
 	 * TODO: If offset/length is not aligned to recordsize, use
 	 * vn_generic_copy_file_range() on this fragment.
@@ -6310,7 +6314,6 @@ bad_write_fallback:
 	    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
 	return (error);
 }
-#endif
 
 struct vop_vector zfs_vnodeops;
 struct vop_vector zfs_fifoops;
@@ -6375,6 +6378,7 @@ struct vop_vector zfs_vnodeops = {
 #if __FreeBSD_version >= 1400043
 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
 #endif
+	.vop_copy_file_range =	zfs_freebsd_copy_file_range,
 };
 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);