xref: /freebsd/tests/sys/fs/fusefs/cache.cc (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 
79 	FuseTest::SetUp();
80 	if (IsSkipped())
81 		return;
82 }
83 
84 void expect_getattr(uint64_t ino, int times, uint64_t size, uint64_t attr_valid)
85 {
86 	EXPECT_CALL(*m_mock, process(
87 		ResultOf([=](auto in) {
88 			return (in.header.opcode == FUSE_GETATTR &&
89 				in.header.nodeid == ino);
90 		}, Eq(true)),
91 		_)
92 	).Times(times)
93 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
94 		SET_OUT_HEADER_LEN(out, attr);
95 		out.body.attr.attr_valid = attr_valid;
96 		out.body.attr.attr.ino = ino;
97 		out.body.attr.attr.mode = S_IFREG | 0644;
98 		out.body.attr.attr.size = size;
99 	})));
100 }
101 
102 void expect_lookup(const char *relpath, uint64_t ino,
103 	uint64_t size, uint64_t entry_valid, uint64_t attr_valid)
104 {
105 	EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
106 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
107 		SET_OUT_HEADER_LEN(out, entry);
108 		out.body.entry.attr.mode = S_IFREG | 0644;
109 		out.body.entry.nodeid = ino;
110 		out.body.entry.attr.nlink = 1;
111 		out.body.entry.attr_valid = attr_valid;
112 		out.body.entry.attr.size = size;
113 		out.body.entry.entry_valid = entry_valid;
114 	})));
115 }
116 
117 void expect_open(uint64_t ino, int times)
118 {
119 	FuseTest::expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO: 0, times);
120 }
121 
122 void expect_release(uint64_t ino, ProcessMockerT r)
123 {
124 	EXPECT_CALL(*m_mock, process(
125 		ResultOf([=](auto in) {
126 			return (in.header.opcode == FUSE_RELEASE &&
127 				in.header.nodeid == ino);
128 		}, Eq(true)),
129 		_)
130 	).WillRepeatedly(Invoke(r));
131 }
132 
133 };
134 
135 // If the server truncates the file behind the kernel's back, the kernel should
136 // invalidate cached pages beyond the new EOF
137 TEST_P(Cache, truncate_by_surprise_invalidates_cache)
138 {
139 	const char FULLPATH[] = "mountpoint/some_file.txt";
140 	const char RELPATH[] = "some_file.txt";
141 	const char *CONTENTS = "abcdefghijklmnopqrstuvwxyz";
142 	uint64_t ino = 42;
143 	uint64_t attr_valid, entry_valid;
144 	int fd;
145 	ssize_t bufsize = strlen(CONTENTS);
146 	uint8_t buf[bufsize];
147 	bool reopen = get<0>(get<0>(GetParam()));
148 	bool cache_lookups = get<1>(get<0>(GetParam()));
149 	bool cache_attrs = get<2>(get<0>(GetParam()));
150 	ssize_t osize = get<2>(GetParam());
151 	ssize_t nsize = get<3>(GetParam());
152 
153 	ASSERT_LE(osize, bufsize);
154 	ASSERT_LE(nsize, bufsize);
155 	if (cache_attrs)
156 		attr_valid = UINT64_MAX;
157 	else
158 		attr_valid = 0;
159 	if (cache_lookups)
160 		entry_valid = UINT64_MAX;
161 	else
162 		entry_valid = 0;
163 
164 	expect_lookup(RELPATH, ino, osize, entry_valid, attr_valid);
165 	expect_open(ino, 1);
166 	if (!cache_attrs)
167 		expect_getattr(ino, 2, osize, attr_valid);
168 	expect_read(ino, 0, osize, osize, CONTENTS);
169 
170 	fd = open(FULLPATH, O_RDONLY);
171 	ASSERT_LE(0, fd) << strerror(errno);
172 
173 	ASSERT_EQ(osize, read(fd, buf, bufsize)) << strerror(errno);
174 	ASSERT_EQ(0, memcmp(buf, CONTENTS, osize));
175 
176 	// Now truncate the file behind the kernel's back.  The next read
177 	// should discard cache and fetch from disk again.
178 	if (reopen) {
179 		// Close and reopen the file
180 		expect_flush(ino, 1, ReturnErrno(ENOSYS));
181 		expect_release(ino, ReturnErrno(0));
182 		ASSERT_EQ(0, close(fd));
183 		expect_lookup(RELPATH, ino, nsize, entry_valid, attr_valid);
184 		expect_open(ino, 1);
185 		fd = open(FULLPATH, O_RDONLY);
186 		ASSERT_LE(0, fd) << strerror(errno);
187 	}
188 
189 	if (!cache_attrs)
190 		expect_getattr(ino, 1, nsize, attr_valid);
191 	expect_read(ino, 0, nsize, nsize, CONTENTS);
192 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
193 	ASSERT_EQ(nsize, read(fd, buf, bufsize)) << strerror(errno);
194 	ASSERT_EQ(0, memcmp(buf, CONTENTS, nsize));
195 
196 	leak(fd);
197 }
198 
199 INSTANTIATE_TEST_CASE_P(Cache, Cache,
200 	Combine(
201 		/* Test every combination that:
202 		 * - does not cache at least one of entries and attrs
203 		 * - either doesn't cache attrs, or reopens the file
204 		 * In the other combinations, the kernel will never learn that
205 		 * the file's size has changed.
206 		 */
207 		Values(
208 			std::make_tuple(false, false, false),
209 			std::make_tuple(false, true, false),
210 			std::make_tuple(true, false, false),
211 			std::make_tuple(true, false, true),
212 			std::make_tuple(true, true, false)
213 		),
214 		Values(Writethrough, Writeback),
215 		/* Test both reductions and extensions to file size */
216 		Values(20),
217 		Values(10, 25)
218 	)
219 );
220