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 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
47 TEST_F(Lookup, DISABLED_attr_cache)
49 const char FULLPATH[] = "mountpoint/some_file.txt";
50 const char RELPATH[] = "some_file.txt";
51 const uint64_t ino = 42;
52 const uint64_t generation = 13;
55 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
56 out->header.unique = in->header.unique;
57 SET_OUT_HEADER_LEN(out, entry);
58 out->body.entry.nodeid = ino;
59 out->body.entry.attr_valid = UINT64_MAX;
60 out->body.entry.attr.ino = ino; // Must match nodeid
61 out->body.entry.attr.mode = S_IFREG | 0644;
62 out->body.entry.attr.size = 1;
63 out->body.entry.attr.blocks = 2;
64 out->body.entry.attr.atime = 3;
65 out->body.entry.attr.mtime = 4;
66 out->body.entry.attr.ctime = 5;
67 out->body.entry.attr.atimensec = 6;
68 out->body.entry.attr.mtimensec = 7;
69 out->body.entry.attr.ctimensec = 8;
70 out->body.entry.attr.nlink = 9;
71 out->body.entry.attr.uid = 10;
72 out->body.entry.attr.gid = 11;
73 out->body.entry.attr.rdev = 12;
74 out->body.entry.generation = generation;
76 /* stat(2) issues a VOP_LOOKUP followed by a VOP_GETATTR */
77 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
78 EXPECT_EQ(1, sb.st_size);
79 EXPECT_EQ(2, sb.st_blocks);
80 EXPECT_EQ(3, sb.st_atim.tv_sec);
81 EXPECT_EQ(6, sb.st_atim.tv_nsec);
82 EXPECT_EQ(4, sb.st_mtim.tv_sec);
83 EXPECT_EQ(7, sb.st_mtim.tv_nsec);
84 EXPECT_EQ(5, sb.st_ctim.tv_sec);
85 EXPECT_EQ(8, sb.st_ctim.tv_nsec);
86 EXPECT_EQ(9ull, sb.st_nlink);
87 EXPECT_EQ(10ul, sb.st_uid);
88 EXPECT_EQ(11ul, sb.st_gid);
89 EXPECT_EQ(12ul, sb.st_rdev);
90 EXPECT_EQ(ino, sb.st_ino);
91 EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
93 // fuse(4) does not _yet_ support inode generations
94 //EXPECT_EQ(generation, sb.st_gen);
96 //st_birthtim and st_flags are not supported by protocol 7.8. They're
97 //only supported as OS-specific extensions to OSX.
98 //EXPECT_EQ(, sb.st_birthtim);
99 //EXPECT_EQ(, sb.st_flags);
101 //FUSE can't set st_blksize until protocol 7.9
105 * If lookup returns a finite but non-zero cache timeout, then we should discard
106 * the cached attributes and requery the daemon.
108 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
109 TEST_F(Lookup, attr_cache_timeout)
111 const char FULLPATH[] = "mountpoint/some_file.txt";
112 const char RELPATH[] = "some_file.txt";
113 const uint64_t ino = 42;
116 * The timeout should be longer than the longest plausible time the
117 * daemon would take to complete a write(2) to /dev/fuse, but no longer.
119 long timeout_ns = 250'000'000;
121 EXPECT_LOOKUP(1, RELPATH).WillRepeatedly(Invoke([=](auto in, auto out) {
122 out->header.unique = in->header.unique;
123 SET_OUT_HEADER_LEN(out, entry);
124 out->body.entry.nodeid = ino;
125 out->body.entry.attr_valid_nsec = timeout_ns;
126 out->body.entry.attr.ino = ino; // Must match nodeid
127 out->body.entry.attr.mode = S_IFREG | 0644;
129 expect_getattr(ino, 0);
131 /* access(2) will issue a VOP_LOOKUP but not a VOP_GETATTR */
132 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
133 usleep(2 * timeout_ns / 1000);
134 /* The cache has timed out; VOP_GETATTR should query the daemon*/
135 ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
138 TEST_F(Lookup, enoent)
140 const char FULLPATH[] = "mountpoint/does_not_exist";
141 const char RELPATH[] = "does_not_exist";
143 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
144 EXPECT_NE(0, access(FULLPATH, F_OK));
145 EXPECT_EQ(ENOENT, errno);
149 * If lookup returns a non-zero entry timeout, then subsequent VOP_LOOKUPs
150 * should use the cached inode rather than requery the daemon
152 TEST_F(Lookup, entry_cache)
154 const char FULLPATH[] = "mountpoint/some_file.txt";
155 const char RELPATH[] = "some_file.txt";
157 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
158 out->header.unique = in->header.unique;
159 SET_OUT_HEADER_LEN(out, entry);
160 out->body.entry.entry_valid = UINT64_MAX;
161 out->body.entry.attr.mode = S_IFREG | 0644;
162 out->body.entry.nodeid = 14;
164 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
165 /* The second access(2) should use the cache */
166 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
170 * If the daemon returns an error of 0 and an inode of 0, that's a flag for
171 * "ENOENT and cache it" with the given entry_timeout
173 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236226 */
174 TEST_F(Lookup, DISABLED_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 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
216 TEST_F(Lookup, DISABLED_entry_cache_timeout)
218 const char FULLPATH[] = "mountpoint/some_file.txt";
219 const char RELPATH[] = "some_file.txt";
221 * The timeout should be longer than the longest plausible time the
222 * daemon would take to complete a write(2) to /dev/fuse, but no longer.
224 long timeout_ns = 250'000'000;
226 EXPECT_LOOKUP(1, RELPATH).Times(2)
227 .WillRepeatedly(Invoke([=](auto in, auto out) {
228 out->header.unique = in->header.unique;
229 SET_OUT_HEADER_LEN(out, entry);
230 out->body.entry.entry_valid_nsec = timeout_ns;
231 out->body.entry.attr.mode = S_IFREG | 0644;
232 out->body.entry.nodeid = 14;
234 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
235 usleep(2 * timeout_ns / 1000);
236 /* The cache has timed out; VOP_LOOKUP should query the daemon*/
237 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
240 // TODO: export_support
241 // After upgrading the protocol to 7.10, check that the kernel will only
242 // attempt to lookup "." and ".." if the filesystem sets FUSE_EXPORT_SUPPORT in
243 // the init flags. If not, then all lookups for those entries will return
248 const char FULLPATH[] = "mountpoint/some_file.txt";
249 const char RELPATH[] = "some_file.txt";
251 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
252 out->header.unique = in->header.unique;
253 SET_OUT_HEADER_LEN(out, entry);
254 out->body.entry.attr.mode = S_IFREG | 0644;
255 out->body.entry.nodeid = 14;
258 * access(2) is one of the few syscalls that will not (always) follow
259 * up a successful VOP_LOOKUP with another VOP.
261 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
264 // Lookup in a subdirectory of the fuse mount
265 TEST_F(Lookup, subdir)
267 const char FULLPATH[] = "mountpoint/some_dir/some_file.txt";
268 const char DIRPATH[] = "some_dir";
269 const char RELPATH[] = "some_file.txt";
270 uint64_t dir_ino = 2;
271 uint64_t file_ino = 3;
273 EXPECT_LOOKUP(1, DIRPATH).WillOnce(Invoke([=](auto in, auto out) {
274 out->header.unique = in->header.unique;
275 SET_OUT_HEADER_LEN(out, entry);
276 out->body.entry.attr.mode = S_IFDIR | 0755;
277 out->body.entry.nodeid = dir_ino;
279 EXPECT_LOOKUP(dir_ino, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
280 out->header.unique = in->header.unique;
281 SET_OUT_HEADER_LEN(out, entry);
282 out->body.entry.attr.mode = S_IFREG | 0644;
283 out->body.entry.nodeid = file_ino;
286 * access(2) is one of the few syscalls that will not (always) follow
287 * up a successful VOP_LOOKUP with another VOP.
289 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);