2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 using namespace testing;
40 class Lookup: public FuseTest {};
43 * If lookup returns a non-zero cache timeout, then subsequent VOP_GETATTRs
44 * should use the cached attributes, rather than query the daemon
46 TEST_F(Lookup, attr_cache)
48 const char FULLPATH[] = "mountpoint/some_file.txt";
49 const char RELPATH[] = "some_file.txt";
50 const uint64_t ino = 42;
51 const uint64_t generation = 13;
54 EXPECT_LOOKUP(1, RELPATH)
55 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
56 SET_OUT_HEADER_LEN(out, entry);
57 out->body.entry.nodeid = ino;
58 out->body.entry.attr_valid = UINT64_MAX;
59 out->body.entry.attr.ino = ino; // Must match nodeid
60 out->body.entry.attr.mode = S_IFREG | 0644;
61 out->body.entry.attr.size = 1;
62 out->body.entry.attr.blocks = 2;
63 out->body.entry.attr.atime = 3;
64 out->body.entry.attr.mtime = 4;
65 out->body.entry.attr.ctime = 5;
66 out->body.entry.attr.atimensec = 6;
67 out->body.entry.attr.mtimensec = 7;
68 out->body.entry.attr.ctimensec = 8;
69 out->body.entry.attr.nlink = 9;
70 out->body.entry.attr.uid = 10;
71 out->body.entry.attr.gid = 11;
72 out->body.entry.attr.rdev = 12;
73 out->body.entry.generation = generation;
75 /* stat(2) issues a VOP_LOOKUP followed by a VOP_GETATTR */
76 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
77 EXPECT_EQ(1, sb.st_size);
78 EXPECT_EQ(2, sb.st_blocks);
79 EXPECT_EQ(3, sb.st_atim.tv_sec);
80 EXPECT_EQ(6, sb.st_atim.tv_nsec);
81 EXPECT_EQ(4, sb.st_mtim.tv_sec);
82 EXPECT_EQ(7, sb.st_mtim.tv_nsec);
83 EXPECT_EQ(5, sb.st_ctim.tv_sec);
84 EXPECT_EQ(8, sb.st_ctim.tv_nsec);
85 EXPECT_EQ(9ull, sb.st_nlink);
86 EXPECT_EQ(10ul, sb.st_uid);
87 EXPECT_EQ(11ul, sb.st_gid);
88 EXPECT_EQ(12ul, sb.st_rdev);
89 EXPECT_EQ(ino, sb.st_ino);
90 EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
92 // fuse(4) does not _yet_ support inode generations
93 //EXPECT_EQ(generation, sb.st_gen);
95 //st_birthtim and st_flags are not supported by protocol 7.8. They're
96 //only supported as OS-specific extensions to OSX.
97 //EXPECT_EQ(, sb.st_birthtim);
98 //EXPECT_EQ(, sb.st_flags);
100 //FUSE can't set st_blksize until protocol 7.9
104 * If lookup returns a finite but non-zero cache timeout, then we should discard
105 * the cached attributes and requery the daemon.
107 TEST_F(Lookup, attr_cache_timeout)
109 const char FULLPATH[] = "mountpoint/some_file.txt";
110 const char RELPATH[] = "some_file.txt";
111 const uint64_t ino = 42;
114 * The timeout should be longer than the longest plausible time the
115 * daemon would take to complete a write(2) to /dev/fuse, but no longer.
117 long timeout_ns = 250'000'000;
119 EXPECT_LOOKUP(1, RELPATH)
121 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
122 SET_OUT_HEADER_LEN(out, entry);
123 out->body.entry.nodeid = ino;
124 out->body.entry.attr_valid_nsec = timeout_ns;
125 out->body.entry.attr.ino = ino; // Must match nodeid
126 out->body.entry.attr.mode = S_IFREG | 0644;
129 /* access(2) will issue a VOP_LOOKUP and fill the attr cache */
130 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
131 /* Next access(2) will use the cached attributes */
132 usleep(2 * timeout_ns / 1000);
133 /* The cache has timed out; VOP_GETATTR should query the daemon*/
134 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
137 TEST_F(Lookup, enoent)
139 const char FULLPATH[] = "mountpoint/does_not_exist";
140 const char RELPATH[] = "does_not_exist";
142 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
143 EXPECT_NE(0, access(FULLPATH, F_OK));
144 EXPECT_EQ(ENOENT, errno);
150 * If lookup returns a non-zero entry timeout, then subsequent VOP_LOOKUPs
151 * should use the cached inode rather than requery the daemon
153 TEST_F(Lookup, entry_cache)
155 const char FULLPATH[] = "mountpoint/some_file.txt";
156 const char RELPATH[] = "some_file.txt";
158 EXPECT_LOOKUP(1, RELPATH)
159 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
160 SET_OUT_HEADER_LEN(out, entry);
161 out->body.entry.entry_valid = UINT64_MAX;
162 out->body.entry.attr.mode = S_IFREG | 0644;
163 out->body.entry.nodeid = 14;
165 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
166 /* The second access(2) should use the cache */
167 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
171 * If the daemon returns an error of 0 and an inode of 0, that's a flag for
172 * "ENOENT and cache it" with the given entry_timeout
174 TEST_F(Lookup, entry_cache_negative)
176 struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
178 EXPECT_LOOKUP(1, "does_not_exist").Times(1)
179 .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)));
181 EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
182 EXPECT_EQ(ENOENT, errno);
183 EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
184 EXPECT_EQ(ENOENT, errno);
187 /* Negative entry caches should timeout, too */
188 TEST_F(Lookup, entry_cache_negative_timeout)
190 const char *RELPATH = "does_not_exist";
191 const char *FULLPATH = "mountpoint/does_not_exist";
193 * The timeout should be longer than the longest plausible time the
194 * daemon would take to complete a write(2) to /dev/fuse, but no longer.
196 struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 250'000'000};
198 EXPECT_LOOKUP(1, RELPATH).Times(2)
199 .WillRepeatedly(Invoke(ReturnNegativeCache(&entry_valid)));
201 EXPECT_NE(0, access(FULLPATH, F_OK));
202 EXPECT_EQ(ENOENT, errno);
204 usleep(2 * entry_valid.tv_nsec / 1000);
206 /* The cache has timed out; VOP_LOOKUP should requery the daemon*/
207 EXPECT_NE(0, access(FULLPATH, F_OK));
208 EXPECT_EQ(ENOENT, errno);
212 * If lookup returns a finite but non-zero entry cache timeout, then we should
213 * discard the cached inode and requery the daemon
215 TEST_F(Lookup, entry_cache_timeout)
217 const char FULLPATH[] = "mountpoint/some_file.txt";
218 const char RELPATH[] = "some_file.txt";
220 * The timeout should be longer than the longest plausible time the
221 * daemon would take to complete a write(2) to /dev/fuse, but no longer.
223 long timeout_ns = 250'000'000;
225 EXPECT_LOOKUP(1, RELPATH)
227 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
228 SET_OUT_HEADER_LEN(out, entry);
229 out->body.entry.entry_valid_nsec = timeout_ns;
230 out->body.entry.attr.mode = S_IFREG | 0644;
231 out->body.entry.nodeid = 14;
234 /* access(2) will issue a VOP_LOOKUP and fill the entry cache */
235 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
236 /* Next access(2) will use the cached entry */
237 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
238 usleep(2 * timeout_ns / 1000);
239 /* The cache has timed out; VOP_LOOKUP should requery the daemon*/
240 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
243 // TODO: export_support
244 // After upgrading the protocol to 7.10, check that the kernel will only
245 // attempt to lookup "." and ".." if the filesystem sets FUSE_EXPORT_SUPPORT in
246 // the init flags. If not, then all lookups for those entries will return
251 const char FULLPATH[] = "mountpoint/some_file.txt";
252 const char RELPATH[] = "some_file.txt";
254 EXPECT_LOOKUP(1, RELPATH)
255 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
256 SET_OUT_HEADER_LEN(out, entry);
257 out->body.entry.attr.mode = S_IFREG | 0644;
258 out->body.entry.nodeid = 14;
261 * access(2) is one of the few syscalls that will not (always) follow
262 * up a successful VOP_LOOKUP with another VOP.
264 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
267 // Lookup in a subdirectory of the fuse mount
268 TEST_F(Lookup, subdir)
270 const char FULLPATH[] = "mountpoint/some_dir/some_file.txt";
271 const char DIRPATH[] = "some_dir";
272 const char RELPATH[] = "some_file.txt";
273 uint64_t dir_ino = 2;
274 uint64_t file_ino = 3;
276 EXPECT_LOOKUP(1, DIRPATH)
277 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
278 SET_OUT_HEADER_LEN(out, entry);
279 out->body.entry.attr.mode = S_IFDIR | 0755;
280 out->body.entry.nodeid = dir_ino;
282 EXPECT_LOOKUP(dir_ino, RELPATH)
283 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
284 SET_OUT_HEADER_LEN(out, entry);
285 out->body.entry.attr.mode = S_IFREG | 0644;
286 out->body.entry.nodeid = file_ino;
289 * access(2) is one of the few syscalls that will not (always) follow
290 * up a successful VOP_LOOKUP with another VOP.
292 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);