xref: /freebsd/tests/sys/fs/fusefs/lookup.cc (revision 690b7ea081790eef2c890f63a4fe7e195cf51df0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 
37 #include <fcntl.h>
38 #include <unistd.h>
39 }
40 
41 #include "mockfs.hh"
42 #include "utils.hh"
43 
44 using namespace testing;
45 
46 class Lookup: public FuseTest {};
47 
48 class Lookup_7_8: public Lookup {
49 public:
50 virtual void SetUp() {
51 	m_kernel_minor_version = 8;
52 	Lookup::SetUp();
53 }
54 };
55 
56 class LookupExportable: public Lookup {
57 public:
58 virtual void SetUp() {
59 	m_init_flags = FUSE_EXPORT_SUPPORT;
60 	Lookup::SetUp();
61 }
62 };
63 
64 /*
65  * If lookup returns a non-zero cache timeout, then subsequent VOP_GETATTRs
66  * should use the cached attributes, rather than query the daemon
67  */
68 TEST_F(Lookup, attr_cache)
69 {
70 	const char FULLPATH[] = "mountpoint/some_file.txt";
71 	const char RELPATH[] = "some_file.txt";
72 	const uint64_t ino = 42;
73 	const uint64_t generation = 13;
74 	struct stat sb;
75 
76 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
77 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
78 		SET_OUT_HEADER_LEN(out, entry);
79 		out.body.entry.nodeid = ino;
80 		out.body.entry.attr_valid = UINT64_MAX;
81 		out.body.entry.attr.ino = ino;	// Must match nodeid
82 		out.body.entry.attr.mode = S_IFREG | 0644;
83 		out.body.entry.attr.size = 1;
84 		out.body.entry.attr.blocks = 2;
85 		out.body.entry.attr.atime = 3;
86 		out.body.entry.attr.mtime = 4;
87 		out.body.entry.attr.ctime = 5;
88 		out.body.entry.attr.atimensec = 6;
89 		out.body.entry.attr.mtimensec = 7;
90 		out.body.entry.attr.ctimensec = 8;
91 		out.body.entry.attr.nlink = 9;
92 		out.body.entry.attr.uid = 10;
93 		out.body.entry.attr.gid = 11;
94 		out.body.entry.attr.rdev = 12;
95 		out.body.entry.generation = generation;
96 	})));
97 	/* stat(2) issues a VOP_LOOKUP followed by a VOP_GETATTR */
98 	ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
99 	EXPECT_EQ(1, sb.st_size);
100 	EXPECT_EQ(2, sb.st_blocks);
101 	EXPECT_EQ(3, sb.st_atim.tv_sec);
102 	EXPECT_EQ(6, sb.st_atim.tv_nsec);
103 	EXPECT_EQ(4, sb.st_mtim.tv_sec);
104 	EXPECT_EQ(7, sb.st_mtim.tv_nsec);
105 	EXPECT_EQ(5, sb.st_ctim.tv_sec);
106 	EXPECT_EQ(8, sb.st_ctim.tv_nsec);
107 	EXPECT_EQ(9ull, sb.st_nlink);
108 	EXPECT_EQ(10ul, sb.st_uid);
109 	EXPECT_EQ(11ul, sb.st_gid);
110 	EXPECT_EQ(12ul, sb.st_rdev);
111 	EXPECT_EQ(ino, sb.st_ino);
112 	EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
113 
114 	// fuse(4) does not _yet_ support inode generations
115 	//EXPECT_EQ(generation, sb.st_gen);
116 
117 	//st_birthtim and st_flags are not supported by protocol 7.8.  They're
118 	//only supported as OS-specific extensions to OSX.
119 	//EXPECT_EQ(, sb.st_birthtim);
120 	//EXPECT_EQ(, sb.st_flags);
121 
122 	//FUSE can't set st_blksize until protocol 7.9
123 }
124 
125 /*
126  * If lookup returns a finite but non-zero cache timeout, then we should discard
127  * the cached attributes and requery the daemon.
128  */
129 TEST_F(Lookup, attr_cache_timeout)
130 {
131 	const char FULLPATH[] = "mountpoint/some_file.txt";
132 	const char RELPATH[] = "some_file.txt";
133 	const uint64_t ino = 42;
134 	struct stat sb;
135 
136 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
137 	.Times(2)
138 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
139 		SET_OUT_HEADER_LEN(out, entry);
140 		out.body.entry.nodeid = ino;
141 		out.body.entry.attr_valid_nsec = NAP_NS / 2;
142 		out.body.entry.attr.ino = ino;	// Must match nodeid
143 		out.body.entry.attr.mode = S_IFREG | 0644;
144 	})));
145 
146 	/* access(2) will issue a VOP_LOOKUP and fill the attr cache */
147 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
148 	/* Next access(2) will use the cached attributes */
149 	nap();
150 	/* The cache has timed out; VOP_GETATTR should query the daemon*/
151 	ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
152 }
153 
154 TEST_F(Lookup, dot)
155 {
156 	const char FULLPATH[] = "mountpoint/some_dir/.";
157 	const char RELDIRPATH[] = "some_dir";
158 	uint64_t ino = 42;
159 
160 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH)
161 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
162 		SET_OUT_HEADER_LEN(out, entry);
163 		out.body.entry.attr.mode = S_IFDIR | 0755;
164 		out.body.entry.nodeid = ino;
165 		out.body.entry.attr_valid = UINT64_MAX;
166 		out.body.entry.entry_valid = UINT64_MAX;
167 	})));
168 
169 	/*
170 	 * access(2) is one of the few syscalls that will not (always) follow
171 	 * up a successful VOP_LOOKUP with another VOP.
172 	 */
173 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
174 }
175 
176 TEST_F(Lookup, dotdot)
177 {
178 	const char FULLPATH[] = "mountpoint/some_dir/..";
179 	const char RELDIRPATH[] = "some_dir";
180 
181 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH)
182 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
183 		SET_OUT_HEADER_LEN(out, entry);
184 		out.body.entry.attr.mode = S_IFDIR | 0755;
185 		out.body.entry.nodeid = 14;
186 		out.body.entry.attr_valid = UINT64_MAX;
187 		out.body.entry.entry_valid = UINT64_MAX;
188 	})));
189 
190 	/*
191 	 * access(2) is one of the few syscalls that will not (always) follow
192 	 * up a successful VOP_LOOKUP with another VOP.
193 	 */
194 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
195 }
196 
197 /*
198  * Lookup ".." when that vnode's entry cache has timed out, but its child's
199  * hasn't.  Since this file system doesn't set FUSE_EXPORT_SUPPORT, we have no
200  * choice but to use the cached entry, even though it expired.
201  */
202 TEST_F(Lookup, dotdot_entry_cache_timeout)
203 {
204 	uint64_t foo_ino = 42;
205 	uint64_t bar_ino = 43;
206 
207 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
208 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
209 		SET_OUT_HEADER_LEN(out, entry);
210 		out.body.entry.attr.mode = S_IFDIR | 0755;
211 		out.body.entry.nodeid = foo_ino;
212 		out.body.entry.attr_valid = UINT64_MAX;
213 		out.body.entry.entry_valid = 0;	// immediate timeout
214 	})));
215 	EXPECT_LOOKUP(foo_ino, "bar")
216 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
217 		SET_OUT_HEADER_LEN(out, entry);
218 		out.body.entry.attr.mode = S_IFDIR | 0755;
219 		out.body.entry.nodeid = bar_ino;
220 		out.body.entry.attr_valid = UINT64_MAX;
221 		out.body.entry.entry_valid = UINT64_MAX;
222 	})));
223 	expect_opendir(bar_ino);
224 
225 	int fd = open("mountpoint/foo/bar", O_EXEC| O_DIRECTORY);
226 	ASSERT_LE(0, fd) << strerror(errno);
227 	EXPECT_EQ(0, faccessat(fd, "../..", F_OK, 0)) << strerror(errno);
228 }
229 
230 /*
231  * Lookup ".." for a vnode with no valid parent nid
232  * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=259974
233  * Since the file system is not exportable, we have no choice but to return an
234  * error.
235  */
236 TEST_F(Lookup, dotdot_no_parent_nid)
237 {
238 	uint64_t foo_ino = 42;
239 	uint64_t bar_ino = 43;
240 	int fd;
241 
242 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
243 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
244 		SET_OUT_HEADER_LEN(out, entry);
245 		out.body.entry.attr.mode = S_IFDIR | 0755;
246 		out.body.entry.nodeid = foo_ino;
247 		out.body.entry.attr_valid = UINT64_MAX;
248 		out.body.entry.entry_valid = UINT64_MAX;
249 	})));
250 	EXPECT_LOOKUP(foo_ino, "bar")
251 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
252 		SET_OUT_HEADER_LEN(out, entry);
253 		out.body.entry.attr.mode = S_IFDIR | 0755;
254 		out.body.entry.nodeid = bar_ino;
255 		out.body.entry.attr_valid = UINT64_MAX;
256 		out.body.entry.entry_valid = UINT64_MAX;
257 	})));
258 	EXPECT_CALL(*m_mock, process(
259 		ResultOf([=](auto in) {
260 			return (in.header.opcode == FUSE_OPENDIR);
261 		}, Eq(true)),
262 		_)
263 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
264 		SET_OUT_HEADER_LEN(out, open);
265 	})));
266 	expect_forget(foo_ino, 1, NULL);
267 
268 	fd = open("mountpoint/foo/bar", O_EXEC| O_DIRECTORY);
269 	ASSERT_LE(0, fd) << strerror(errno);
270 	// Try (and fail) to unmount the file system, to reclaim the mountpoint
271 	// and foo vnodes.
272 	ASSERT_NE(0, unmount("mountpoint", 0));
273 	EXPECT_EQ(EBUSY, errno);
274 	nap();		// Because vnode reclamation is asynchronous
275 	EXPECT_NE(0, faccessat(fd, "../..", F_OK, 0));
276 	EXPECT_EQ(ESTALE, errno);
277 }
278 
279 /*
280  * A daemon that returns an illegal error value should be handled gracefully.
281  * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263220
282  */
283 TEST_F(Lookup, ejustreturn)
284 {
285 	const char FULLPATH[] = "mountpoint/does_not_exist";
286 	const char RELPATH[] = "does_not_exist";
287 
288 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
289 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
290 		out.header.len = sizeof(out.header);
291 		out.header.error = 2;
292 		m_mock->m_expected_write_errno = EINVAL;
293 	})));
294 
295 	EXPECT_NE(0, access(FULLPATH, F_OK));
296 
297 	EXPECT_EQ(EIO, errno);
298 }
299 
300 TEST_F(Lookup, enoent)
301 {
302 	const char FULLPATH[] = "mountpoint/does_not_exist";
303 	const char RELPATH[] = "does_not_exist";
304 
305 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
306 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
307 	EXPECT_NE(0, access(FULLPATH, F_OK));
308 	EXPECT_EQ(ENOENT, errno);
309 }
310 
311 TEST_F(Lookup, enotdir)
312 {
313 	const char FULLPATH[] = "mountpoint/not_a_dir/some_file.txt";
314 	const char RELPATH[] = "not_a_dir";
315 
316 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
317 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
318 		SET_OUT_HEADER_LEN(out, entry);
319 		out.body.entry.entry_valid = UINT64_MAX;
320 		out.body.entry.attr.mode = S_IFREG | 0644;
321 		out.body.entry.nodeid = 42;
322 	})));
323 
324 	ASSERT_EQ(-1, access(FULLPATH, F_OK));
325 	ASSERT_EQ(ENOTDIR, errno);
326 }
327 
328 /*
329  * If lookup returns a non-zero entry timeout, then subsequent VOP_LOOKUPs
330  * should use the cached inode rather than requery the daemon
331  */
332 TEST_F(Lookup, entry_cache)
333 {
334 	const char FULLPATH[] = "mountpoint/some_file.txt";
335 	const char RELPATH[] = "some_file.txt";
336 
337 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
338 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
339 		SET_OUT_HEADER_LEN(out, entry);
340 		out.body.entry.entry_valid = UINT64_MAX;
341 		out.body.entry.attr.mode = S_IFREG | 0644;
342 		out.body.entry.nodeid = 14;
343 	})));
344 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
345 	/* The second access(2) should use the cache */
346 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
347 }
348 
349 /*
350  * If the daemon returns an error of 0 and an inode of 0, that's a flag for
351  * "ENOENT and cache it" with the given entry_timeout
352  */
353 TEST_F(Lookup, entry_cache_negative)
354 {
355 	struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
356 
357 	EXPECT_LOOKUP(FUSE_ROOT_ID, "does_not_exist")
358 	.Times(1)
359 	.WillOnce(Invoke(ReturnNegativeCache(&entry_valid)));
360 
361 	EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
362 	EXPECT_EQ(ENOENT, errno);
363 	EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
364 	EXPECT_EQ(ENOENT, errno);
365 }
366 
367 /* Negative entry caches should timeout, too */
368 TEST_F(Lookup, entry_cache_negative_timeout)
369 {
370 	const char *RELPATH = "does_not_exist";
371 	const char *FULLPATH = "mountpoint/does_not_exist";
372 	struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = NAP_NS / 2};
373 
374 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
375 	.Times(2)
376 	.WillRepeatedly(Invoke(ReturnNegativeCache(&entry_valid)));
377 
378 	EXPECT_NE(0, access(FULLPATH, F_OK));
379 	EXPECT_EQ(ENOENT, errno);
380 
381 	nap();
382 
383 	/* The cache has timed out; VOP_LOOKUP should requery the daemon*/
384 	EXPECT_NE(0, access(FULLPATH, F_OK));
385 	EXPECT_EQ(ENOENT, errno);
386 }
387 
388 /*
389  * If lookup returns a finite but non-zero entry cache timeout, then we should
390  * discard the cached inode and requery the daemon
391  */
392 TEST_F(Lookup, entry_cache_timeout)
393 {
394 	const char FULLPATH[] = "mountpoint/some_file.txt";
395 	const char RELPATH[] = "some_file.txt";
396 
397 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
398 	.Times(2)
399 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
400 		SET_OUT_HEADER_LEN(out, entry);
401 		out.body.entry.entry_valid_nsec = NAP_NS / 2;
402 		out.body.entry.attr.mode = S_IFREG | 0644;
403 		out.body.entry.nodeid = 14;
404 	})));
405 
406 	/* access(2) will issue a VOP_LOOKUP and fill the entry cache */
407 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
408 	/* Next access(2) will use the cached entry */
409 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
410 	nap();
411 	/* The cache has timed out; VOP_LOOKUP should requery the daemon*/
412 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
413 }
414 
415 TEST_F(Lookup, ok)
416 {
417 	const char FULLPATH[] = "mountpoint/some_file.txt";
418 	const char RELPATH[] = "some_file.txt";
419 
420 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
421 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
422 		SET_OUT_HEADER_LEN(out, entry);
423 		out.body.entry.attr.mode = S_IFREG | 0644;
424 		out.body.entry.nodeid = 14;
425 	})));
426 	/*
427 	 * access(2) is one of the few syscalls that will not (always) follow
428 	 * up a successful VOP_LOOKUP with another VOP.
429 	 */
430 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
431 }
432 
433 // Lookup in a subdirectory of the fuse mount
434 TEST_F(Lookup, subdir)
435 {
436 	const char FULLPATH[] = "mountpoint/some_dir/some_file.txt";
437 	const char DIRPATH[] = "some_dir";
438 	const char RELPATH[] = "some_file.txt";
439 	uint64_t dir_ino = 2;
440 	uint64_t file_ino = 3;
441 
442 	EXPECT_LOOKUP(FUSE_ROOT_ID, DIRPATH)
443 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
444 		SET_OUT_HEADER_LEN(out, entry);
445 		out.body.entry.attr.mode = S_IFDIR | 0755;
446 		out.body.entry.nodeid = dir_ino;
447 	})));
448 	EXPECT_LOOKUP(dir_ino, RELPATH)
449 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
450 		SET_OUT_HEADER_LEN(out, entry);
451 		out.body.entry.attr.mode = S_IFREG | 0644;
452 		out.body.entry.nodeid = file_ino;
453 	})));
454 	/*
455 	 * access(2) is one of the few syscalls that will not (always) follow
456 	 * up a successful VOP_LOOKUP with another VOP.
457 	 */
458 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
459 }
460 
461 /*
462  * The server returns two different vtypes for the same nodeid.  This is
463  * technically allowed if the entry's cache has already expired.
464  * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=258022
465  */
466 TEST_F(Lookup, vtype_conflict)
467 {
468 	const char FIRSTFULLPATH[] = "mountpoint/foo";
469 	const char SECONDFULLPATH[] = "mountpoint/bar";
470 	const char FIRSTRELPATH[] = "foo";
471 	const char SECONDRELPATH[] = "bar";
472 	uint64_t ino = 42;
473 
474 	EXPECT_LOOKUP(FUSE_ROOT_ID, FIRSTRELPATH)
475 	.WillOnce(Invoke(
476 		ReturnImmediate([=](auto in __unused, auto& out) {
477 		SET_OUT_HEADER_LEN(out, entry);
478 		out.body.entry.attr.mode = S_IFDIR | 0644;
479 		out.body.entry.nodeid = ino;
480 		out.body.entry.attr.nlink = 1;
481 	})));
482 	expect_lookup(SECONDRELPATH, ino, S_IFREG | 0755, 0, 1, UINT64_MAX);
483 	// VOP_FORGET happens asynchronously, so it may or may not arrive
484 	// before the test completes.
485 	EXPECT_CALL(*m_mock, process(
486 		ResultOf([=](auto in) {
487 			return (in.header.opcode == FUSE_FORGET &&
488 				in.header.nodeid == ino &&
489 				in.body.forget.nlookup == 1);
490 		}, Eq(true)),
491 		_)
492 	).Times(AtMost(1))
493 	.WillOnce(Invoke([=](auto in __unused, auto &out __unused) { }));
494 
495 	ASSERT_EQ(0, access(FIRSTFULLPATH, F_OK)) << strerror(errno);
496 	EXPECT_EQ(0, access(SECONDFULLPATH, F_OK)) << strerror(errno);
497 }
498 
499 TEST_F(Lookup_7_8, ok)
500 {
501 	const char FULLPATH[] = "mountpoint/some_file.txt";
502 	const char RELPATH[] = "some_file.txt";
503 
504 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
505 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
506 		SET_OUT_HEADER_LEN(out, entry_7_8);
507 		out.body.entry.attr.mode = S_IFREG | 0644;
508 		out.body.entry.nodeid = 14;
509 	})));
510 	/*
511 	 * access(2) is one of the few syscalls that will not (always) follow
512 	 * up a successful VOP_LOOKUP with another VOP.
513 	 */
514 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
515 }
516 
517 /*
518  * Lookup ".." when that vnode's entry cache has timed out, but its child's
519  * hasn't.
520  */
521 TEST_F(LookupExportable, dotdot_entry_cache_timeout)
522 {
523 	uint64_t foo_ino = 42;
524 	uint64_t bar_ino = 43;
525 
526 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
527 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
528 		SET_OUT_HEADER_LEN(out, entry);
529 		out.body.entry.attr.mode = S_IFDIR | 0755;
530 		out.body.entry.nodeid = foo_ino;
531 		out.body.entry.attr_valid = UINT64_MAX;
532 		out.body.entry.entry_valid = 0;	// immediate timeout
533 	})));
534 	EXPECT_LOOKUP(foo_ino, "bar")
535 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
536 		SET_OUT_HEADER_LEN(out, entry);
537 		out.body.entry.attr.mode = S_IFDIR | 0755;
538 		out.body.entry.nodeid = bar_ino;
539 		out.body.entry.attr_valid = UINT64_MAX;
540 		out.body.entry.entry_valid = UINT64_MAX;
541 	})));
542 	expect_opendir(bar_ino);
543 	EXPECT_LOOKUP(foo_ino, "..")
544 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
545 		SET_OUT_HEADER_LEN(out, entry);
546 		out.body.entry.attr.mode = S_IFDIR | 0755;
547 		out.body.entry.nodeid = FUSE_ROOT_ID;
548 		out.body.entry.attr_valid = UINT64_MAX;
549 		out.body.entry.entry_valid = UINT64_MAX;
550 	})));
551 
552 	int fd = open("mountpoint/foo/bar", O_EXEC| O_DIRECTORY);
553 	ASSERT_LE(0, fd) << strerror(errno);
554 	/* FreeBSD's fusefs driver always uses the same cache expiration time
555 	 * for ".." as for the directory itself.  So we need to look up two
556 	 * levels to find an expired ".." cache entry.
557 	 */
558 	EXPECT_EQ(0, faccessat(fd, "../..", F_OK, 0)) << strerror(errno);
559 }
560 
561 /*
562  * Lookup ".." for a vnode with no valid parent nid
563  * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=259974
564  * Since the file system is exportable, we should resolve the problem by
565  * sending a FUSE_LOOKUP for "..".
566  */
567 TEST_F(LookupExportable, dotdot_no_parent_nid)
568 {
569 	uint64_t foo_ino = 42;
570 	uint64_t bar_ino = 43;
571 	int fd;
572 
573 	EXPECT_LOOKUP(FUSE_ROOT_ID, "foo")
574 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
575 		SET_OUT_HEADER_LEN(out, entry);
576 		out.body.entry.attr.mode = S_IFDIR | 0755;
577 		out.body.entry.nodeid = foo_ino;
578 		out.body.entry.attr_valid = UINT64_MAX;
579 		out.body.entry.entry_valid = UINT64_MAX;
580 	})));
581 	EXPECT_LOOKUP(foo_ino, "bar")
582 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
583 		SET_OUT_HEADER_LEN(out, entry);
584 		out.body.entry.attr.mode = S_IFDIR | 0755;
585 		out.body.entry.nodeid = bar_ino;
586 		out.body.entry.attr_valid = UINT64_MAX;
587 		out.body.entry.entry_valid = UINT64_MAX;
588 	})));
589 	EXPECT_CALL(*m_mock, process(
590 		ResultOf([=](auto in) {
591 			return (in.header.opcode == FUSE_OPENDIR);
592 		}, Eq(true)),
593 		_)
594 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
595 		SET_OUT_HEADER_LEN(out, open);
596 	})));
597 	expect_forget(foo_ino, 1, NULL);
598 	EXPECT_LOOKUP(bar_ino, "..")
599 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
600 		SET_OUT_HEADER_LEN(out, entry);
601 		out.body.entry.attr.mode = S_IFDIR | 0755;
602 		out.body.entry.nodeid = foo_ino;
603 		out.body.entry.attr_valid = UINT64_MAX;
604 		out.body.entry.entry_valid = UINT64_MAX;
605 	})));
606 	EXPECT_LOOKUP(foo_ino, "..")
607 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
608 		SET_OUT_HEADER_LEN(out, entry);
609 		out.body.entry.attr.mode = S_IFDIR | 0755;
610 		out.body.entry.nodeid = FUSE_ROOT_ID;
611 		out.body.entry.attr_valid = UINT64_MAX;
612 		out.body.entry.entry_valid = UINT64_MAX;
613 	})));
614 
615 	fd = open("mountpoint/foo/bar", O_EXEC| O_DIRECTORY);
616 	ASSERT_LE(0, fd) << strerror(errno);
617 	// Try (and fail) to unmount the file system, to reclaim the mountpoint
618 	// and foo vnodes.
619 	ASSERT_NE(0, unmount("mountpoint", 0));
620 	EXPECT_EQ(EBUSY, errno);
621 	nap();		// Because vnode reclamation is asynchronous
622 	EXPECT_EQ(0, faccessat(fd, "../..", F_OK, 0)) << strerror(errno);
623 }
624