From nobody Tue Oct 03 21:46:14 2023 X-Original-To: dev-commits-src-all@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 4S0Wbp2NMYz4w2vL; Tue, 3 Oct 2023 21:46:14 +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 4S0Wbp1nTqz4njf; Tue, 3 Oct 2023 21:46:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696369574; 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=MpCTKU2uTDkDtnurt4mYH2sXL3Jg2uGsRuft5W7XAzc=; b=HJ6nNhzfbQ3VBdO4ql+xlRp6gl3BTLKvUanOnqR9AqiJDD/a1XIge8dQ/VbCFex3zS1O2+ ZqgN0HphhT3btuuEEPJngWeYwN39Uj+dISU/g/1WfsZVKVk0ZFUA3lXFV4Kmq1k+OE3Zhl iSwULJOXCKP7SjGhBx6I/BPlHq/8woYiSH6dslGZfmpur+ax7p5ocDmxI6ZSholCnVgUWj Vrfc4UAdh+CqAlwStcYFq4DR5gasDyllbVRLKYlHHKJ22PQm5b31uykvjJO9kovwuwkjfW xJxkVyu4L4tmxPWFeQV3OiIRrD1TPTBzja6Fj9P5DRT40reNt0DZMP+spk0KbQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696369574; a=rsa-sha256; cv=none; b=hnlIYHg81M3zYAANSOIx+vSkJSs7+ntM3SaevQQXVBF19F+VU4w8soDy97xAMxTa0dz8Aj IeUxye5pPo5tK4Yh74fZM4yN/zGXAt6wYYJRvcmVoh6LgHyJ+PbAUymhtIs8+2+JeRlfDk KUprwKwovOKbT6lopGZff3xnuMwXng15EHNHg6HFsySBWSkybuNiQr426Rit8LB+XIq4d0 zAxxByRP9/0JXPjCbfB9BJXRWQgzAJSXB/hA4+Qml2dPXZGoJYj9Nw0rSgXTgX3KJK2wjL pIVdLtAk4GlyXJhEf/i8AVawgPM5xOaTBHMnrDBHVmIVGho/HIlpmTz/bsUHmA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696369574; 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=MpCTKU2uTDkDtnurt4mYH2sXL3Jg2uGsRuft5W7XAzc=; b=Gb6J4BbCXAZ3UdhNWm14fdDo3oIekhmosqBwERSE/DWFI0rHfJV4pNO6jgsdPb1ODHJewz /LjaiGWmfkBVQuqePESg4/FG1SLkLWmtsbnAGYI7lyInCd0n4Q1CXar1KK3Q09yww/Dy+o ZfLjlm6iMNIUjS1KvoYwF51Fg2oK3Vpr8YfAyg9Pnrl+du2amYGIBJ9WJj1/oiN34AdbnV zsDOgZMJilufTuJ3xKw1jvBrFZmM89LErD4IrlTTS9Y1+4e2GEZpExr8/tLuKhZF0sRi5z UoN7+TzU4cz+EZBQfO7o//Eyzl4ywZG9UGqZQyzlk8vtOLOSiORI7SSd6ToRdw== 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 4S0Wbp0tN3z1B9S; Tue, 3 Oct 2023 21:46:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 393LkEIS020681; Tue, 3 Oct 2023 21:46:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 393LkEKV020678; Tue, 3 Oct 2023 21:46:14 GMT (envelope-from git) Date: Tue, 3 Oct 2023 21:46:14 GMT Message-Id: <202310032146.393LkEKV020678@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: 63c0b60063c7 - releng/14.0 - fusefs: fix some bugs updating atime during close List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/releng/14.0 X-Git-Reftype: branch X-Git-Commit: 63c0b60063c7ce1022b07879e63fcef26fc0259a Auto-Submitted: auto-generated The branch releng/14.0 has been updated by asomers: URL: https://cgit.FreeBSD.org/src/commit/?id=63c0b60063c7ce1022b07879e63fcef26fc0259a commit 63c0b60063c7ce1022b07879e63fcef26fc0259a Author: Alan Somers AuthorDate: 2023-09-20 21:37:31 +0000 Commit: Alan Somers CommitDate: 2023-10-03 21:45:28 +0000 fusefs: fix some bugs updating atime during close When using cached attributes, we must update a file's atime during close, if it has been read since the last attribute refresh. But, * Don't update atime if we lack write permissions to the file or if the file system is readonly. * If the daemon fails our atime update request for any reason, don't report this as a failure for VOP_CLOSE. PR: 270749 Reported by: Jamie Landeg-Jones Sponsored by: Axcient Reviewed by: pfg Approved by: re (gjb) Differential Revision: https://reviews.freebsd.org/D41925 (cherry picked from commit fb619c94c679e939496fe0cf94b8d2cba95e6e63) fusefs: fix unused variables from fb619c94c67 PR: 270749 Reported by: cy Sponsored by: Axcient Approved by: re (gjb) (cherry picked from commit e5236d25f2c0709acf3547e6af45f4bb4eec4f02) (cherry picked from commit 13f188ce0b51908c292cdbb85ef26962528af87b) --- sys/fs/fuse/fuse_vnops.c | 27 ++++++++-- tests/sys/fs/fusefs/access.cc | 2 +- tests/sys/fs/fusefs/default_permissions.cc | 41 ++++++++++++++- tests/sys/fs/fusefs/read.cc | 83 ++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 6 deletions(-) diff --git a/sys/fs/fuse/fuse_vnops.c b/sys/fs/fuse/fuse_vnops.c index be2885528570..21ee378b24c6 100644 --- a/sys/fs/fuse/fuse_vnops.c +++ b/sys/fs/fuse/fuse_vnops.c @@ -779,6 +779,7 @@ static int fuse_vnop_close(struct vop_close_args *ap) { struct vnode *vp = ap->a_vp; + struct mount *mp = vnode_mount(vp); struct ucred *cred = ap->a_cred; int fflag = ap->a_fflag; struct thread *td = ap->a_td; @@ -794,12 +795,30 @@ fuse_vnop_close(struct vop_close_args *ap) return 0; err = fuse_flush(vp, cred, pid, fflag); - if (err == 0 && (fvdat->flag & FN_ATIMECHANGE)) { + if (err == 0 && (fvdat->flag & FN_ATIMECHANGE) && !vfs_isrdonly(mp)) { struct vattr vap; + struct fuse_data *data; + int dataflags; + int access_e = 0; - VATTR_NULL(&vap); - vap.va_atime = fvdat->cached_attrs.va_atime; - err = fuse_internal_setattr(vp, &vap, td, NULL); + data = fuse_get_mpdata(mp); + dataflags = data->dataflags; + if (dataflags & FSESS_DEFAULT_PERMISSIONS) { + struct vattr va; + + fuse_internal_getattr(vp, &va, cred, td); + access_e = vaccess(vp->v_type, va.va_mode, va.va_uid, + va.va_gid, VWRITE, cred); + } + if (access_e == 0) { + VATTR_NULL(&vap); + vap.va_atime = fvdat->cached_attrs.va_atime; + /* + * Ignore errors setting when setting atime. That + * should not cause close(2) to fail. + */ + fuse_internal_setattr(vp, &vap, td, NULL); + } } /* TODO: close the file handle, if we're sure it's no longer used */ if ((fvdat->flag & FN_SIZECHANGE) != 0) { diff --git a/tests/sys/fs/fusefs/access.cc b/tests/sys/fs/fusefs/access.cc index 3d6cddb9417b..5762269fac7b 100644 --- a/tests/sys/fs/fusefs/access.cc +++ b/tests/sys/fs/fusefs/access.cc @@ -55,7 +55,7 @@ void expect_lookup(const char *relpath, uint64_t ino) } /* - * Expect tha FUSE_ACCESS will never be called for the given inode, with any + * Expect that FUSE_ACCESS will never be called for the given inode, with any * bits in the supplied access_mask set */ void expect_noaccess(uint64_t ino, mode_t access_mask) diff --git a/tests/sys/fs/fusefs/default_permissions.cc b/tests/sys/fs/fusefs/default_permissions.cc index 1a1ee5a54aed..4b04297d97ac 100644 --- a/tests/sys/fs/fusefs/default_permissions.cc +++ b/tests/sys/fs/fusefs/default_permissions.cc @@ -30,7 +30,7 @@ /* * Tests for the "default_permissions" mount option. They must be in their own - * file so they can be run as an unprivileged user + * file so they can be run as an unprivileged user. */ extern "C" { @@ -163,6 +163,7 @@ class Fspacectl: public DefaultPermissions {}; class Lookup: public DefaultPermissions {}; class Open: public DefaultPermissions {}; class PosixFallocate: public DefaultPermissions {}; +class Read: public DefaultPermissions {}; class Setattr: public DefaultPermissions {}; class Unlink: public DefaultPermissions {}; class Utimensat: public DefaultPermissions {}; @@ -1239,6 +1240,44 @@ TEST_F(Rename, ok_to_remove_src_because_of_stickiness) ASSERT_EQ(0, rename(FULLSRC, FULLDST)) << strerror(errno); } +// Don't update atime during close after read, if we lack permissions to write +// that file. +TEST_F(Read, atime_during_close) +{ + const char FULLPATH[] = "mountpoint/some_file.txt"; + const char RELPATH[] = "some_file.txt"; + uint64_t ino = 42; + int fd; + ssize_t bufsize = 100; + uint8_t buf[bufsize]; + const char *CONTENTS = "abcdefgh"; + ssize_t fsize = sizeof(CONTENTS); + + expect_getattr(FUSE_ROOT_ID, S_IFDIR | 0755, UINT64_MAX, 1); + FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0755, fsize, + 1, UINT64_MAX, 0, 0); + expect_open(ino, 0, 1); + expect_read(ino, 0, fsize, fsize, CONTENTS); + EXPECT_CALL(*m_mock, process( + ResultOf([&](auto in) { + return (in.header.opcode == FUSE_SETATTR); + }, Eq(true)), + _) + ).Times(0); + expect_flush(ino, 1, ReturnErrno(0)); + expect_release(ino, FuseTest::FH); + + fd = open(FULLPATH, O_RDONLY); + ASSERT_LE(0, fd) << strerror(errno); + + /* Ensure atime will be different than during lookup */ + nap(); + + ASSERT_EQ(fsize, read(fd, buf, bufsize)) << strerror(errno); + + close(fd); +} + TEST_F(Setattr, ok) { const char FULLPATH[] = "mountpoint/some_file.txt"; diff --git a/tests/sys/fs/fusefs/read.cc b/tests/sys/fs/fusefs/read.cc index cf2159df646b..3df0420facb9 100644 --- a/tests/sys/fs/fusefs/read.cc +++ b/tests/sys/fs/fusefs/read.cc @@ -57,6 +57,14 @@ void expect_lookup(const char *relpath, uint64_t ino, uint64_t size) } }; +class RofsRead: public Read { +public: +virtual void SetUp() { + m_ro = true; + Read::SetUp(); +} +}; + class Read_7_8: public FuseTest { public: virtual void SetUp() { @@ -454,6 +462,47 @@ TEST_F(Read, atime_during_close) close(fd); } +/* + * When not using -o default_permissions, the daemon may make its own decisions + * regarding access permissions, and these may be unpredictable. If it rejects + * our attempt to set atime, that should not cause close(2) to fail. + */ +TEST_F(Read, atime_during_close_eacces) +{ + const char FULLPATH[] = "mountpoint/some_file.txt"; + const char RELPATH[] = "some_file.txt"; + const char *CONTENTS = "abcdefgh"; + uint64_t ino = 42; + int fd; + ssize_t bufsize = strlen(CONTENTS); + uint8_t buf[bufsize]; + + expect_lookup(RELPATH, ino, bufsize); + expect_open(ino, 0, 1); + expect_read(ino, 0, bufsize, bufsize, CONTENTS); + EXPECT_CALL(*m_mock, process( + ResultOf([&](auto in) { + uint32_t valid = FATTR_ATIME; + return (in.header.opcode == FUSE_SETATTR && + in.header.nodeid == ino && + in.body.setattr.valid == valid); + }, Eq(true)), + _) + ).WillOnce(Invoke(ReturnErrno(EACCES))); + expect_flush(ino, 1, ReturnErrno(0)); + expect_release(ino, FuseTest::FH); + + fd = open(FULLPATH, O_RDONLY); + ASSERT_LE(0, fd) << strerror(errno); + + /* Ensure atime will be different than during lookup */ + nap(); + + ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno); + + ASSERT_EQ(0, close(fd)); +} + /* A cached atime should be flushed during FUSE_SETATTR */ TEST_F(Read, atime_during_setattr) { @@ -1321,6 +1370,40 @@ INSTANTIATE_TEST_SUITE_P(RA, ReadAhead, tuple(true, 1), tuple(true, 2))); +/* With read-only mounts, fuse should never update atime during close */ +TEST_F(RofsRead, atime_during_close) +{ + const char FULLPATH[] = "mountpoint/some_file.txt"; + const char RELPATH[] = "some_file.txt"; + const char *CONTENTS = "abcdefgh"; + uint64_t ino = 42; + int fd; + ssize_t bufsize = strlen(CONTENTS); + uint8_t buf[bufsize]; + + expect_lookup(RELPATH, ino, bufsize); + expect_open(ino, 0, 1); + expect_read(ino, 0, bufsize, bufsize, CONTENTS); + EXPECT_CALL(*m_mock, process( + ResultOf([&](auto in) { + return (in.header.opcode == FUSE_SETATTR); + }, Eq(true)), + _) + ).Times(0); + expect_flush(ino, 1, ReturnErrno(0)); + expect_release(ino, FuseTest::FH); + + fd = open(FULLPATH, O_RDONLY); + ASSERT_LE(0, fd) << strerror(errno); + + /* Ensure atime will be different than during lookup */ + nap(); + + ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno); + + close(fd); +} + /* fuse_init_out.time_gran controls the granularity of timestamps */ TEST_P(TimeGran, atime_during_setattr) {