xref: /freebsd/tests/sys/fs/fusefs/cache.cc (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Alan Somers
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 extern "C" {
31 #include <sys/param.h>
32 #include <fcntl.h>
33 }
34 
35 #include "mockfs.hh"
36 #include "utils.hh"
37 
38 /*
39  * Tests for thorny cache problems not specific to any one opcode
40  */
41 
42 using namespace testing;
43 
44 /*
45  * Parameters
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
52  */
53 typedef tuple<tuple<bool, bool, bool>, cache_mode, ssize_t, ssize_t> CacheParam;
54 
55 class Cache: public FuseTest, public WithParamInterface<CacheParam> {
56 public:
57 bool m_direct_io;
58 
59 Cache(): m_direct_io(false) {};
60 
61 virtual void SetUp() {
62 	int cache_mode = get<1>(GetParam());
63 	switch (cache_mode) {
64 		case Uncached:
65 			m_direct_io = true;
66 			break;
67 		case WritebackAsync:
68 			m_async = true;
69 			/* FALLTHROUGH */
70 		case Writeback:
71 			m_init_flags |= FUSE_WRITEBACK_CACHE;
72 			/* FALLTHROUGH */
73 		case Writethrough:
74 			break;
75 		default:
76 			FAIL() << "Unknown cache mode";
77 	}
78 	m_noatime = true;	// To prevent SETATTR for atime on close
79 
80 	FuseTest::SetUp();
81 	if (IsSkipped())
82 		return;
83 }
84 
85 void expect_getattr(uint64_t ino, int times, uint64_t size, uint64_t attr_valid)
86 {
87 	EXPECT_CALL(*m_mock, process(
88 		ResultOf([=](auto in) {
89 			return (in.header.opcode == FUSE_GETATTR &&
90 				in.header.nodeid == ino);
91 		}, Eq(true)),
92 		_)
93 	).Times(times)
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;
100 	})));
101 }
102 
103 void expect_lookup(const char *relpath, uint64_t ino,
104 	uint64_t size, uint64_t entry_valid, uint64_t attr_valid)
105 {
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;
115 	})));
116 }
117 
118 void expect_open(uint64_t ino, int times)
119 {
120 	FuseTest::expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO: 0, times);
121 }
122 
123 void expect_release(uint64_t ino, ProcessMockerT r)
124 {
125 	EXPECT_CALL(*m_mock, process(
126 		ResultOf([=](auto in) {
127 			return (in.header.opcode == FUSE_RELEASE &&
128 				in.header.nodeid == ino);
129 		}, Eq(true)),
130 		_)
131 	).WillRepeatedly(Invoke(r));
132 }
133 
134 };
135 
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)
139 {
140 	const char FULLPATH[] = "mountpoint/some_file.txt";
141 	const char RELPATH[] = "some_file.txt";
142 	const char *CONTENTS = "abcdefghijklmnopqrstuvwxyz";
143 	uint64_t ino = 42;
144 	uint64_t attr_valid, entry_valid;
145 	int fd;
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());
153 
154 	ASSERT_LE(osize, bufsize);
155 	ASSERT_LE(nsize, bufsize);
156 	if (cache_attrs)
157 		attr_valid = UINT64_MAX;
158 	else
159 		attr_valid = 0;
160 	if (cache_lookups)
161 		entry_valid = UINT64_MAX;
162 	else
163 		entry_valid = 0;
164 
165 	expect_lookup(RELPATH, ino, osize, entry_valid, attr_valid);
166 	expect_open(ino, 1);
167 	if (!cache_attrs)
168 		expect_getattr(ino, 2, osize, attr_valid);
169 	expect_read(ino, 0, osize, osize, CONTENTS);
170 
171 	fd = open(FULLPATH, O_RDONLY);
172 	ASSERT_LE(0, fd) << strerror(errno);
173 
174 	ASSERT_EQ(osize, read(fd, buf, bufsize)) << strerror(errno);
175 	ASSERT_EQ(0, memcmp(buf, CONTENTS, osize));
176 
177 	// Now truncate the file behind the kernel's back.  The next read
178 	// should discard cache and fetch from disk again.
179 	if (reopen) {
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);
185 		expect_open(ino, 1);
186 		fd = open(FULLPATH, O_RDONLY);
187 		ASSERT_LE(0, fd) << strerror(errno);
188 	}
189 
190 	if (!cache_attrs)
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));
196 
197 	leak(fd);
198 }
199 
200 INSTANTIATE_TEST_CASE_P(Cache, Cache,
201 	Combine(
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.
207 		 */
208 		Values(
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)
214 		),
215 		Values(Writethrough, Writeback),
216 		/* Test both reductions and extensions to file size */
217 		Values(20),
218 		Values(10, 25)
219 	)
220 );
221