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
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
36 #include <semaphore.h>
43 using namespace testing;
45 const char reclaim_mib[] = "debug.try_reclaim_vnode";
47 class Forget: public FuseTest {
51 GTEST_SKIP() << "Only root may use " << reclaim_mib;
53 if (-1 == sysctlbyname(reclaim_mib, NULL, 0, NULL, 0) &&
55 GTEST_SKIP() << reclaim_mib << " is not available";
63 * When a fusefs vnode is reclaimed, it should send a FUSE_FORGET operation.
67 const char FULLPATH[] = "mountpoint/some_file.txt";
68 const char RELPATH[] = "some_file.txt";
70 mode_t mode = S_IFREG | 0755;
74 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
76 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
78 .WillRepeatedly(Invoke(
79 ReturnImmediate([=](auto in __unused, auto& out) {
80 SET_OUT_HEADER_LEN(out, entry);
81 out.body.entry.attr.mode = mode;
82 out.body.entry.nodeid = ino;
83 out.body.entry.attr.nlink = 1;
84 out.body.entry.attr_valid = UINT64_MAX;
86 expect_forget(ino, 3, &sem);
89 * access(2) the file to force a lookup. Access it twice to double its
92 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
93 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
95 err = sysctlbyname(reclaim_mib, NULL, 0, FULLPATH, sizeof(FULLPATH));
96 ASSERT_EQ(0, err) << strerror(errno);
103 * When a directory is reclaimed, the names of its entries vanish from the
106 TEST_F(Forget, invalidate_names)
108 const char FULLFPATH[] = "mountpoint/some_dir/some_file.txt";
109 const char FULLDPATH[] = "mountpoint/some_dir";
110 const char DNAME[] = "some_dir";
111 const char FNAME[] = "some_file.txt";
112 uint64_t dir_ino = 42;
113 uint64_t file_ino = 43;
116 EXPECT_LOOKUP(FUSE_ROOT_ID, DNAME)
117 .WillRepeatedly(Invoke(
118 ReturnImmediate([=](auto in __unused, auto& out) {
119 SET_OUT_HEADER_LEN(out, entry);
120 out.body.entry.attr.mode = S_IFDIR | 0755;
121 out.body.entry.nodeid = dir_ino;
122 out.body.entry.attr.nlink = 2;
123 out.body.entry.attr_valid = UINT64_MAX;
124 out.body.entry.entry_valid = UINT64_MAX;
128 * Even though we don't reclaim FNAME and its entry is cacheable, we
129 * should get two lookups because the reclaim of DNAME will invalidate
130 * the cached FNAME entry.
132 EXPECT_LOOKUP(dir_ino, FNAME)
134 .WillRepeatedly(Invoke(
135 ReturnImmediate([=](auto in __unused, auto& out) {
136 SET_OUT_HEADER_LEN(out, entry);
137 out.body.entry.attr.mode = S_IFREG | 0644;
138 out.body.entry.nodeid = file_ino;
139 out.body.entry.attr.nlink = 1;
140 out.body.entry.attr_valid = UINT64_MAX;
141 out.body.entry.entry_valid = UINT64_MAX;
143 expect_forget(dir_ino, 2);
145 /* Access the file to cache its name */
146 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno);
148 /* Reclaim the directory, invalidating its children from namecache */
149 err = sysctlbyname(reclaim_mib, NULL, 0, FULLDPATH, sizeof(FULLDPATH));
150 ASSERT_EQ(0, err) << strerror(errno);
152 /* Access the file again, causing another lookup */
153 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno);