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/types.h> 35 #include <sys/sysctl.h> 36 37 #include <fcntl.h> 38 #include <semaphore.h> 39 #include <unistd.h> 40 } 41 42 #include "mockfs.hh" 43 #include "utils.hh" 44 45 using namespace testing; 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 FuseTest::SetUp(); 54 } 55 56 }; 57 58 /* 59 * When a fusefs vnode is reclaimed, it should send a FUSE_FORGET operation. 60 */ 61 TEST_F(Forget, ok) 62 { 63 const char FULLPATH[] = "mountpoint/some_file.txt"; 64 const char RELPATH[] = "some_file.txt"; 65 uint64_t ino = 42; 66 mode_t mode = S_IFREG | 0755; 67 sem_t sem; 68 69 ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno); 70 71 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 72 .Times(3) 73 .WillRepeatedly(Invoke( 74 ReturnImmediate([=](auto in __unused, auto& out) { 75 SET_OUT_HEADER_LEN(out, entry); 76 out.body.entry.attr.mode = mode; 77 out.body.entry.nodeid = ino; 78 out.body.entry.attr.nlink = 1; 79 out.body.entry.attr_valid = UINT64_MAX; 80 }))); 81 expect_forget(ino, 3, &sem); 82 83 /* 84 * access(2) the file to force a lookup. Access it twice to double its 85 * lookup count. 86 */ 87 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 88 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno); 89 90 reclaim_vnode(FULLPATH); 91 92 sem_wait(&sem); 93 sem_destroy(&sem); 94 } 95 96 /* 97 * When a directory is reclaimed, the names of its entries vanish from the 98 * namecache 99 */ 100 TEST_F(Forget, invalidate_names) 101 { 102 const char FULLFPATH[] = "mountpoint/some_dir/some_file.txt"; 103 const char FULLDPATH[] = "mountpoint/some_dir"; 104 const char DNAME[] = "some_dir"; 105 const char FNAME[] = "some_file.txt"; 106 uint64_t dir_ino = 42; 107 uint64_t file_ino = 43; 108 109 EXPECT_LOOKUP(FUSE_ROOT_ID, DNAME) 110 .Times(2) 111 .WillRepeatedly(Invoke( 112 ReturnImmediate([=](auto in __unused, auto& out) { 113 SET_OUT_HEADER_LEN(out, entry); 114 out.body.entry.attr.mode = S_IFDIR | 0755; 115 out.body.entry.nodeid = dir_ino; 116 out.body.entry.attr.nlink = 2; 117 out.body.entry.attr_valid = UINT64_MAX; 118 out.body.entry.entry_valid = UINT64_MAX; 119 }))); 120 121 /* 122 * Even though we don't reclaim FNAME and its entry is cacheable, we 123 * should get two lookups because the reclaim of DNAME will invalidate 124 * the cached FNAME entry. 125 */ 126 EXPECT_LOOKUP(dir_ino, FNAME) 127 .Times(2) 128 .WillRepeatedly(Invoke( 129 ReturnImmediate([=](auto in __unused, auto& out) { 130 SET_OUT_HEADER_LEN(out, entry); 131 out.body.entry.attr.mode = S_IFREG | 0644; 132 out.body.entry.nodeid = file_ino; 133 out.body.entry.attr.nlink = 1; 134 out.body.entry.attr_valid = UINT64_MAX; 135 out.body.entry.entry_valid = UINT64_MAX; 136 }))); 137 expect_forget(dir_ino, 1); 138 139 /* Access the file to cache its name */ 140 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 141 142 /* Reclaim the directory, invalidating its children from namecache */ 143 reclaim_vnode(FULLDPATH); 144 145 /* Access the file again, causing another lookup */ 146 ASSERT_EQ(0, access(FULLFPATH, F_OK)) << strerror(errno); 147 } 148