From nobody Mon Jan 03 03:08:40 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 232E91924488; Mon, 3 Jan 2022 03:08:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JS10m62jgz3G69; Mon, 3 Jan 2022 03:08:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id AF0671E00A; Mon, 3 Jan 2022 03:08:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 20338eXC099960; Mon, 3 Jan 2022 03:08:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20338e55099959; Mon, 3 Jan 2022 03:08:40 GMT (envelope-from git) Date: Mon, 3 Jan 2022 03:08:40 GMT Message-Id: <202201030308.20338e55099959@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Alan Somers Subject: git: 642399932d58 - stable/13 - fusefs: invalidate the cache during copy_file_range List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: asomers X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 642399932d58e880ac4a65ab6011a94292b37134 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641179320; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EHL04AXN8inB/V1P4LxF6cLnvhxhJgQXCzgzmydURm0=; b=S5ozJlVDFuyquEHJSx1qH8dk5mjMC5KwYimu86DfWeUnRtMijMKKn47GkEX1F2V8FyRst+ 9nf/c+d3zzP5+e56gwDyL0lm3fjKHmGKgrcK/g/qnwhcc2hQQjzKy2BRSmOPcX7lO2YDmw pIi1whXKGmXKUNFurAn5cqYYpRY4LCfhu4tAswpBjmRwLI/E7hs3aRIrqQZ0iM4MyadlsH Fx8x2Ao6j0ugmW+qwQGzOm08WGO7HgtPca84rHD7geo8/vUl+W8lluaD5nH9PNiHXTyueQ tfBJX+ERgpuM1Oi+8nJnS5xiL5mVgKz0POt3HisNKTlh9yjtrJ/hnxPI3+hyag== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641179320; a=rsa-sha256; cv=none; b=TxjEZnNvlpFVsOmR3Cb7FnMcpk6rJicXJrZWAKzJg6ipTaDi/uj+Lq6GJq89DJe+kZchCN UQCeGHL8kKppK1sBT8zreExkcAfb0eAihpU/GGBeSzqYwKShhbDiCYtSnj8SvYGfENWKAu AUanQN8E8m2/bC4lgYMYwlCqApWlNWy7pY7svdSpl3IvBM1bMKeV5x08l96+HjLhaQYmJG gAr2B7gKL4kcM2acIadKDJ0Y+GGwwdD1VQf7slM0dK3d7yl58gkbCcjyVq288agyEi0aa3 n9p3K3g45kko5/xT5bJ2DbyzS6gbnTGcpz+ROe0CPra/1FqcJFQ7oSfUgmRj/g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by asomers: URL: https://cgit.FreeBSD.org/src/commit/?id=642399932d58e880ac4a65ab6011a94292b37134 commit 642399932d58e880ac4a65ab6011a94292b37134 Author: Alan Somers AuthorDate: 2021-12-05 20:39:10 +0000 Commit: Alan Somers CommitDate: 2022-01-03 03:03:54 +0000 fusefs: invalidate the cache during copy_file_range FUSE_COPY_FILE_RANGE instructs the server to write data to a file. fusefs must invalidate any cached data within the written range. PR: 260242 Reviewed by: pfg Differential Revision: https://reviews.freebsd.org/D33280 (cherry picked from commit 41ae9f9e644d1196bebacdb3748670f36b354384) --- sys/fs/fuse/fuse_vnops.c | 10 +++++ tests/sys/fs/fusefs/copy_file_range.cc | 78 ++++++++++++++++++++++++++++++++++ tests/sys/fs/fusefs/utils.cc | 4 +- tests/sys/fs/fusefs/utils.hh | 3 +- 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/sys/fs/fuse/fuse_vnops.c b/sys/fs/fuse/fuse_vnops.c index 36507fae4d59..7fe3f8271f4f 100644 --- a/sys/fs/fuse/fuse_vnops.c +++ b/sys/fs/fuse/fuse_vnops.c @@ -720,6 +720,7 @@ fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap) struct fuse_write_out *fwo; struct thread *td; struct uio io; + off_t outfilesize; pid_t pid; int err; @@ -775,6 +776,15 @@ fuse_vnop_copy_file_range(struct vop_copy_file_range_args *ap) goto unlock; } + err = fuse_vnode_size(outvp, &outfilesize, outcred, curthread); + if (err) + goto unlock; + + err = fuse_inval_buf_range(outvp, outfilesize, *ap->a_outoffp, + *ap->a_outoffp + *ap->a_lenp); + if (err) + goto unlock; + fdisp_init(&fdi, sizeof(*fcfri)); fdisp_make_vp(&fdi, FUSE_COPY_FILE_RANGE, invp, td, incred); fcfri = fdi.indata; diff --git a/tests/sys/fs/fusefs/copy_file_range.cc b/tests/sys/fs/fusefs/copy_file_range.cc index 03a892d35d29..a9dc9679cb6a 100644 --- a/tests/sys/fs/fusefs/copy_file_range.cc +++ b/tests/sys/fs/fusefs/copy_file_range.cc @@ -171,6 +171,84 @@ TEST_F(CopyFileRange, eio) EXPECT_EQ(EIO, errno); } +/* + * copy_file_range should evict cached data for the modified region of the + * destination file. + */ +TEST_F(CopyFileRange, evicts_cache) +{ + const char FULLPATH1[] = "mountpoint/src.txt"; + const char RELPATH1[] = "src.txt"; + const char FULLPATH2[] = "mountpoint/dst.txt"; + const char RELPATH2[] = "dst.txt"; + void *buf0, *buf1, *buf; + const uint64_t ino1 = 42; + const uint64_t ino2 = 43; + const uint64_t fh1 = 0xdeadbeef1a7ebabe; + const uint64_t fh2 = 0xdeadc0de88c0ffee; + off_t fsize1 = 1 << 20; /* 1 MiB */ + off_t fsize2 = 1 << 19; /* 512 KiB */ + off_t start1 = 1 << 18; + off_t start2 = 3 << 17; + ssize_t len = m_maxbcachebuf; + int fd1, fd2; + + buf0 = malloc(m_maxbcachebuf); + memset(buf0, 42, m_maxbcachebuf); + + expect_lookup(RELPATH1, ino1, S_IFREG | 0644, fsize1, 1); + expect_lookup(RELPATH2, ino2, S_IFREG | 0644, fsize2, 1); + expect_open(ino1, 0, 1, fh1); + expect_open(ino2, 0, 1, fh2); + expect_read(ino2, start2, m_maxbcachebuf, m_maxbcachebuf, buf0, -1, + fh2); + EXPECT_CALL(*m_mock, process( + ResultOf([=](auto in) { + return (in.header.opcode == FUSE_COPY_FILE_RANGE && + in.header.nodeid == ino1 && + in.body.copy_file_range.fh_in == fh1 && + (off_t)in.body.copy_file_range.off_in == start1 && + in.body.copy_file_range.nodeid_out == ino2 && + in.body.copy_file_range.fh_out == fh2 && + (off_t)in.body.copy_file_range.off_out == start2 && + 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; + }))); + + fd1 = open(FULLPATH1, O_RDONLY); + fd2 = open(FULLPATH2, O_RDWR); + + // Prime cache + buf = malloc(m_maxbcachebuf); + ASSERT_EQ(m_maxbcachebuf, pread(fd2, buf, m_maxbcachebuf, start2)) + << strerror(errno); + EXPECT_EQ(0, memcmp(buf0, buf, m_maxbcachebuf)); + + // Tell the FUSE server overwrite the region we just read + ASSERT_EQ(len, copy_file_range(fd1, &start1, fd2, &start2, len, 0)); + + // Read again. This should bypass the cache and read direct from server + buf1 = malloc(m_maxbcachebuf); + memset(buf1, 69, m_maxbcachebuf); + start2 -= len; + expect_read(ino2, start2, m_maxbcachebuf, m_maxbcachebuf, buf1, -1, + fh2); + ASSERT_EQ(m_maxbcachebuf, pread(fd2, buf, m_maxbcachebuf, start2)) + << strerror(errno); + EXPECT_EQ(0, memcmp(buf1, buf, m_maxbcachebuf)); + + free(buf1); + free(buf0); + free(buf); + leak(fd1); + leak(fd2); +} + /* * If the server doesn't support FUSE_COPY_FILE_RANGE, the kernel should * fallback to a read/write based implementation. diff --git a/tests/sys/fs/fusefs/utils.cc b/tests/sys/fs/fusefs/utils.cc index 16dfc9c52939..f733fef7ebe0 100644 --- a/tests/sys/fs/fusefs/utils.cc +++ b/tests/sys/fs/fusefs/utils.cc @@ -367,13 +367,13 @@ void FuseTest::expect_opendir(uint64_t ino) } void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize, - uint64_t osize, const void *contents, int flags) + uint64_t osize, const void *contents, int flags, uint64_t fh) { EXPECT_CALL(*m_mock, process( ResultOf([=](auto in) { return (in.header.opcode == FUSE_READ && in.header.nodeid == ino && - in.body.read.fh == FH && + in.body.read.fh == fh && in.body.read.offset == offset && in.body.read.size == isize && (flags == -1 ? diff --git a/tests/sys/fs/fusefs/utils.hh b/tests/sys/fs/fusefs/utils.hh index a6f1d63ada6b..6f1f91b02c97 100644 --- a/tests/sys/fs/fusefs/utils.hh +++ b/tests/sys/fs/fusefs/utils.hh @@ -175,7 +175,8 @@ class FuseTest : public ::testing::Test { * nothing currently validates the size of the fuse_read_in struct. */ void expect_read(uint64_t ino, uint64_t offset, uint64_t isize, - uint64_t osize, const void *contents, int flags = -1); + uint64_t osize, const void *contents, int flags = -1, + uint64_t fh = FH); /* * Create an expectation that FUSE_READIR will be called any number of