2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020 Alan Somers
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
39 * Tests for thorny cache problems not specific to any one opcode
42 using namespace testing;
46 * - reopen file - If true, close and reopen the file between reads
47 * - cache lookups - If true, allow lookups to be cached
48 * - cache attrs - If true, allow file attributes to be cached
49 * - cache_mode - uncached, writeback, or writethrough
50 * - initial size - File size before truncation
51 * - truncated size - File size after truncation
53 typedef tuple<tuple<bool, bool, bool>, cache_mode, ssize_t, ssize_t> CacheParam;
55 class Cache: public FuseTest, public WithParamInterface<CacheParam> {
59 Cache(): m_direct_io(false) {};
61 virtual void SetUp() {
62 int cache_mode = get<1>(GetParam());
71 m_init_flags |= FUSE_WRITEBACK_CACHE;
76 FAIL() << "Unknown cache mode";
78 m_noatime = true; // To prevent SETATTR for atime on close
85 void expect_getattr(uint64_t ino, int times, uint64_t size, uint64_t attr_valid)
87 EXPECT_CALL(*m_mock, process(
88 ResultOf([=](auto in) {
89 return (in.header.opcode == FUSE_GETATTR &&
90 in.header.nodeid == ino);
94 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
95 SET_OUT_HEADER_LEN(out, attr);
96 out.body.attr.attr_valid = attr_valid;
97 out.body.attr.attr.ino = ino;
98 out.body.attr.attr.mode = S_IFREG | 0644;
99 out.body.attr.attr.size = size;
103 void expect_lookup(const char *relpath, uint64_t ino,
104 uint64_t size, uint64_t entry_valid, uint64_t attr_valid)
106 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
107 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
108 SET_OUT_HEADER_LEN(out, entry);
109 out.body.entry.attr.mode = S_IFREG | 0644;
110 out.body.entry.nodeid = ino;
111 out.body.entry.attr.nlink = 1;
112 out.body.entry.attr_valid = attr_valid;
113 out.body.entry.attr.size = size;
114 out.body.entry.entry_valid = entry_valid;
118 void expect_open(uint64_t ino, int times)
120 FuseTest::expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO: 0, times);
123 void expect_release(uint64_t ino, ProcessMockerT r)
125 EXPECT_CALL(*m_mock, process(
126 ResultOf([=](auto in) {
127 return (in.header.opcode == FUSE_RELEASE &&
128 in.header.nodeid == ino);
131 ).WillRepeatedly(Invoke(r));
136 // If the server truncates the file behind the kernel's back, the kernel should
137 // invalidate cached pages beyond the new EOF
138 TEST_P(Cache, truncate_by_surprise_invalidates_cache)
140 const char FULLPATH[] = "mountpoint/some_file.txt";
141 const char RELPATH[] = "some_file.txt";
142 const char *CONTENTS = "abcdefghijklmnopqrstuvwxyz";
144 uint64_t attr_valid, entry_valid;
146 ssize_t bufsize = strlen(CONTENTS);
147 uint8_t buf[bufsize];
148 bool reopen = get<0>(get<0>(GetParam()));
149 bool cache_lookups = get<1>(get<0>(GetParam()));
150 bool cache_attrs = get<2>(get<0>(GetParam()));
151 ssize_t osize = get<2>(GetParam());
152 ssize_t nsize = get<3>(GetParam());
154 ASSERT_LE(osize, bufsize);
155 ASSERT_LE(nsize, bufsize);
157 attr_valid = UINT64_MAX;
161 entry_valid = UINT64_MAX;
165 expect_lookup(RELPATH, ino, osize, entry_valid, attr_valid);
168 expect_getattr(ino, 2, osize, attr_valid);
169 expect_read(ino, 0, osize, osize, CONTENTS);
171 fd = open(FULLPATH, O_RDONLY);
172 ASSERT_LE(0, fd) << strerror(errno);
174 ASSERT_EQ(osize, read(fd, buf, bufsize)) << strerror(errno);
175 ASSERT_EQ(0, memcmp(buf, CONTENTS, osize));
177 // Now truncate the file behind the kernel's back. The next read
178 // should discard cache and fetch from disk again.
180 // Close and reopen the file
181 expect_flush(ino, 1, ReturnErrno(ENOSYS));
182 expect_release(ino, ReturnErrno(0));
183 ASSERT_EQ(0, close(fd));
184 expect_lookup(RELPATH, ino, nsize, entry_valid, attr_valid);
186 fd = open(FULLPATH, O_RDONLY);
187 ASSERT_LE(0, fd) << strerror(errno);
191 expect_getattr(ino, 1, nsize, attr_valid);
192 expect_read(ino, 0, nsize, nsize, CONTENTS);
193 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
194 ASSERT_EQ(nsize, read(fd, buf, bufsize)) << strerror(errno);
195 ASSERT_EQ(0, memcmp(buf, CONTENTS, nsize));
200 INSTANTIATE_TEST_CASE_P(Cache, Cache,
202 /* Test every combination that:
203 * - does not cache at least one of entries and attrs
204 * - either doesn't cache attrs, or reopens the file
205 * In the other combinations, the kernel will never learn that
206 * the file's size has changed.
209 std::make_tuple(false, false, false),
210 std::make_tuple(false, true, false),
211 std::make_tuple(true, false, false),
212 std::make_tuple(true, false, true),
213 std::make_tuple(true, true, false)
215 Values(Writethrough, Writeback),
216 /* Test both reductions and extensions to file size */