1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/mount.h> 34 #include <sys/sysctl.h> 35 36 #include <fcntl.h> 37 #include <semaphore.h> 38 #include <unistd.h> 39 } 40 41 #include "mockfs.hh" 42 #include "utils.hh" 43 44 using namespace testing; 45 46 class Forget: public FuseTest { 47 public: 48 void SetUp() { 49 if (geteuid() != 0) 50 GTEST_SKIP() << "Only root may use " << reclaim_mib; 51 52 FuseTest::SetUp(); 53 } 54 55 }; 56 57 /* 58 * When a fusefs vnode is reclaimed, it should send a FUSE_FORGET operation. 59 */ 60 TEST_F(Forget, ok) 61 { 62 const char FULLPATH[] = "mountpoint/some_file.txt"; 63 const char RELPATH[] = "some_file.txt"; 64 uint64_t ino = 42; 65 mode_t mode = S_IFREG | 0755; 66 sem_t sem; 67 68 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno); 69 70 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 71 .Times(3) 72 .WillRepeatedly(Invoke( 73 ReturnImmediate([=](auto in __unused, auto& out) { 74 SET_OUT_HEADER_LEN(out, entry); 75 out.body.entry.attr.mode = mode; 76 out.body.entry.nodeid = ino; 77 out.body.entry.attr.nlink = 1; 78 out.body.entry.attr_valid = UINT64_MAX; 79 }))); 80 expect_forget(ino, 3, &sem); 81 82 /* 83 * access(2) the file to force a lookup. Access it twice to double its 84 * lookup count. 85 */ 86 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 87 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 88 89 reclaim_vnode(FULLPATH); 90 91 sem_wait(&sem); 92 sem_destroy(&sem); 93 } 94 95 /* 96 * When a directory is reclaimed, the names of its entries vanish from the 97 * namecache 98 */ 99 TEST_F(Forget, invalidate_names) 100 { 101 const char FULLFPATH[] = "mountpoint/some_dir/some_file.txt"; 102 const char FULLDPATH[] = "mountpoint/some_dir"; 103 const char DNAME[] = "some_dir"; 104 const char FNAME[] = "some_file.txt"; 105 uint64_t dir_ino = 42; 106 uint64_t file_ino = 43; 107 108 EXPECT_LOOKUP(FUSE_ROOT_ID, DNAME) 109 .Times(2) 110 .WillRepeatedly(Invoke( 111 ReturnImmediate([=](auto in __unused, auto& out) { 112 SET_OUT_HEADER_LEN(out, entry); 113 out.body.entry.attr.mode = S_IFDIR | 0755; 114 out.body.entry.nodeid = dir_ino; 115 out.body.entry.attr.nlink = 2; 116 out.body.entry.attr_valid = UINT64_MAX; 117 out.body.entry.entry_valid = UINT64_MAX; 118 }))); 119 120 /* 121 * Even though we don't reclaim FNAME and its entry is cacheable, we 122 * should get two lookups because the reclaim of DNAME will invalidate 123 * the cached FNAME entry. 124 */ 125 EXPECT_LOOKUP(dir_ino, FNAME) 126 .Times(2) 127 .WillRepeatedly(Invoke( 128 ReturnImmediate([=](auto in __unused, auto& out) { 129 SET_OUT_HEADER_LEN(out, entry); 130 out.body.entry.attr.mode = S_IFREG | 0644; 131 out.body.entry.nodeid = file_ino; 132 out.body.entry.attr.nlink = 1; 133 out.body.entry.attr_valid = UINT64_MAX; 134 out.body.entry.entry_valid = UINT64_MAX; 135 }))); 136 expect_forget(dir_ino, 1); 137 138 /* Access the file to cache its name */ 139 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 140 141 /* Reclaim the directory, invalidating its children from namecache */ 142 reclaim_vnode(FULLDPATH); 143 144 /* Access the file again, causing another lookup */ 145 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 146 } 147 148 /* 149 * Reclaiming the root inode should not send a FUSE_FORGET request, nor should 150 * it interfere with further lookup operations. 151 */ 152 TEST_F(Forget, root) 153 { 154 const char FULLPATH[] = "mountpoint/some_file.txt"; 155 const char RELPATH[] = "some_file.txt"; 156 uint64_t ino = 42; 157 mode_t mode = S_IFREG | 0755; 158 159 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 160 .WillRepeatedly(Invoke( 161 ReturnImmediate([=](auto in __unused, auto& out) { 162 SET_OUT_HEADER_LEN(out, entry); 163 out.body.entry.attr.mode = mode; 164 out.body.entry.nodeid = ino; 165 out.body.entry.attr.nlink = 1; 166 out.body.entry.attr_valid = UINT64_MAX; 167 out.body.entry.entry_valid = UINT64_MAX; 168 }))); 169 170 /* access(2) the file to force a lookup. */ 171 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 172 173 reclaim_vnode("mountpoint"); 174 nap(); 175 176 /* Access it again, to make sure it's still possible. */ 177 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 178 } 179