git: 65d70b3bae0c - main - fusefs: fix copy_file_range when extending a file

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Mon, 29 Nov 2021 01:36:22 UTC
The branch main has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=65d70b3bae0c70798b0a2b8ed129bc146fed1cce

commit 65d70b3bae0c70798b0a2b8ed129bc146fed1cce
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2021-11-29 01:35:58 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2021-11-29 01:35:58 +0000

    fusefs: fix copy_file_range when extending a file
    
    When copy_file_range extends a file, it must update the cached file
    size.
    
    MFC after:      2 weeks
    Reviewed by:    rmacklem, pfg
    Differential Revision: https://reviews.freebsd.org/D33151
---
 sys/fs/fuse/fuse_vnops.c               |  3 +++
 tests/sys/fs/fusefs/copy_file_range.cc | 47 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/sys/fs/fuse/fuse_vnops.c b/sys/fs/fuse/fuse_vnops.c
index e61a3cda80a7..d87639ec3f79 100644
--- a/sys/fs/fuse/fuse_vnops.c
+++ b/sys/fs/fuse/fuse_vnops.c
@@ -650,6 +650,7 @@ fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
 	struct vnode *invp = ap->a_invp;
 	struct vnode *outvp = ap->a_outvp;
 	struct mount *mp = vnode_mount(invp);
+	struct fuse_vnode_data *outfvdat = VTOFUD(outvp);
 	struct fuse_dispatcher fdi;
 	struct fuse_filehandle *infufh, *outfufh;
 	struct fuse_copy_file_range_in *fcfri;
@@ -731,6 +732,8 @@ fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap)
 		*ap->a_inoffp += fwo->size;
 		*ap->a_outoffp += fwo->size;
 		fuse_internal_clear_suid_on_write(outvp, outcred, td);
+		if (*ap->a_outoffp > outfvdat->cached_attrs.va_size)
+			fuse_vnode_setsize(outvp, *ap->a_outoffp, false);
 	}
 	fdisp_destroy(&fdi);
 
diff --git a/tests/sys/fs/fusefs/copy_file_range.cc b/tests/sys/fs/fusefs/copy_file_range.cc
index bb8eecf8b862..03a892d35d29 100644
--- a/tests/sys/fs/fusefs/copy_file_range.cc
+++ b/tests/sys/fs/fusefs/copy_file_range.cc
@@ -353,6 +353,53 @@ TEST_F(CopyFileRange, same_file)
 	ASSERT_EQ(len, copy_file_range(fd, &off_in, fd, &off_out, len, 0));
 }
 
+/*
+ * copy_file_range can extend the size of a file
+ * */
+TEST_F(CopyFileRange, extend)
+{
+	const char FULLPATH[] = "mountpoint/src.txt";
+	const char RELPATH[] = "src.txt";
+	struct stat sb;
+	const uint64_t ino = 4;
+	const uint64_t fh = 0xdeadbeefa7ebabe;
+	off_t fsize = 65536;
+	off_t off_in = 0;
+	off_t off_out = 65536;
+	ssize_t len = 65536;
+	int fd;
+
+	expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
+	expect_open(ino, 0, 1, fh);
+	EXPECT_CALL(*m_mock, process(
+		ResultOf([=](auto in) {
+			return (in.header.opcode == FUSE_COPY_FILE_RANGE &&
+				in.header.nodeid == ino &&
+				in.body.copy_file_range.fh_in == fh &&
+				(off_t)in.body.copy_file_range.off_in == off_in &&
+				in.body.copy_file_range.nodeid_out == ino &&
+				in.body.copy_file_range.fh_out == fh &&
+				(off_t)in.body.copy_file_range.off_out == off_out &&
+				in.body.copy_file_range.len == (size_t)len &&
+				in.body.copy_file_range.flags == 0);
+		}, Eq(true)),
+		_)
+	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
+		SET_OUT_HEADER_LEN(out, write);
+		out.body.write.size = len;
+	})));
+
+	fd = open(FULLPATH, O_RDWR);
+	ASSERT_GE(fd, 0);
+	ASSERT_EQ(len, copy_file_range(fd, &off_in, fd, &off_out, len, 0));
+
+	/* Check that cached attributes were updated appropriately */
+	ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
+	EXPECT_EQ(fsize + len, sb.st_size);
+
+	leak(fd);
+}
+
 /* With older protocol versions, no FUSE_COPY_FILE_RANGE should be attempted */
 TEST_F(CopyFileRange_7_27, fallback)
 {