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 31 extern "C" { 32 #include <sys/types.h> 33 #include <sys/sysctl.h> 34 35 #include <fcntl.h> 36 #include <semaphore.h> 37 #include <unistd.h> 38 } 39 40 #include "mockfs.hh" 41 #include "utils.hh" 42 43 using namespace testing; 44 45 const char reclaim_mib[] = "debug.try_reclaim_vnode"; 46 47 class Forget: public FuseTest { 48 public: 49 void SetUp() { 50 if (geteuid() != 0) 51 GTEST_SKIP() << "Only root may use " << reclaim_mib; 52 53 if (-1 == sysctlbyname(reclaim_mib, NULL, 0, NULL, 0) && 54 errno == ENOENT) 55 GTEST_SKIP() << reclaim_mib << " is not available"; 56 57 FuseTest::SetUp(); 58 } 59 60 }; 61 62 /* 63 * When a fusefs vnode is reclaimed, it should send a FUSE_FORGET operation. 64 */ 65 TEST_F(Forget, ok) 66 { 67 const char FULLPATH[] = "mountpoint/some_file.txt"; 68 const char RELPATH[] = "some_file.txt"; 69 uint64_t ino = 42; 70 mode_t mode = S_IFREG | 0755; 71 sem_t sem; 72 int err; 73 74 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno); 75 76 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 77 .Times(3) 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; 85 }))); 86 expect_forget(ino, 3, &sem); 87 88 /* 89 * access(2) the file to force a lookup. Access it twice to double its 90 * lookup count. 91 */ 92 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 93 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 94 95 err = sysctlbyname(reclaim_mib, NULL, 0, FULLPATH, sizeof(FULLPATH)); 96 ASSERT_EQ(0, err) << strerror(errno); 97 98 sem_wait(&sem); 99 sem_destroy(&sem); 100 } 101 102 /* 103 * When a directory is reclaimed, the names of its entries vanish from the 104 * namecache 105 */ 106 TEST_F(Forget, invalidate_names) 107 { 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; 114 int err; 115 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; 125 }))); 126 127 /* 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. 131 */ 132 EXPECT_LOOKUP(dir_ino, FNAME) 133 .Times(2) 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; 142 }))); 143 expect_forget(dir_ino, 2); 144 145 /* Access the file to cache its name */ 146 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 147 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); 151 152 /* Access the file again, causing another lookup */ 153 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 154 } 155