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 <fcntl.h> 33 #include <unistd.h> 34 } 35 36 #include "mockfs.hh" 37 #include "utils.hh" 38 39 using namespace testing; 40 41 class Flush: public FuseTest { 42 43 public: 44 void 45 expect_flush(uint64_t ino, int times, pid_t lo, ProcessMockerT r) 46 { 47 EXPECT_CALL(*m_mock, process( 48 ResultOf([=](auto in) { 49 return (in.header.opcode == FUSE_FLUSH && 50 in.header.nodeid == ino && 51 in.body.flush.lock_owner == (uint64_t)lo && 52 in.body.flush.fh == FH); 53 }, Eq(true)), 54 _) 55 ).Times(times) 56 .WillRepeatedly(Invoke(r)); 57 } 58 59 void expect_lookup(const char *relpath, uint64_t ino, int times) 60 { 61 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times); 62 } 63 64 /* 65 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This 66 * expectation will silence googlemock warnings 67 */ 68 void expect_release() 69 { 70 EXPECT_CALL(*m_mock, process( 71 ResultOf([=](auto in) { 72 return (in.header.opcode == FUSE_RELEASE); 73 }, Eq(true)), 74 _) 75 ).WillRepeatedly(Invoke(ReturnErrno(0))); 76 } 77 }; 78 79 class FlushWithLocks: public Flush { 80 virtual void SetUp() { 81 m_init_flags = FUSE_POSIX_LOCKS; 82 Flush::SetUp(); 83 } 84 }; 85 86 /* 87 * If multiple file descriptors refer to the same file handle, closing each 88 * should send FUSE_FLUSH 89 */ 90 TEST_F(Flush, open_twice) 91 { 92 const char FULLPATH[] = "mountpoint/some_file.txt"; 93 const char RELPATH[] = "some_file.txt"; 94 uint64_t ino = 42; 95 int fd, fd2; 96 97 expect_lookup(RELPATH, ino, 2); 98 expect_open(ino, 0, 1); 99 expect_flush(ino, 2, getpid(), ReturnErrno(0)); 100 expect_release(); 101 102 fd = open(FULLPATH, O_WRONLY); 103 ASSERT_LE(0, fd) << strerror(errno); 104 105 fd2 = open(FULLPATH, O_WRONLY); 106 ASSERT_LE(0, fd2) << strerror(errno); 107 108 EXPECT_EQ(0, close(fd2)) << strerror(errno); 109 EXPECT_EQ(0, close(fd)) << strerror(errno); 110 } 111 112 /* 113 * Some FUSE filesystem cache data internally and flush it on release. Such 114 * filesystems may generate errors during release. On Linux, these get 115 * returned by close(2). However, POSIX does not require close(2) to return 116 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at 117 * all. 118 */ 119 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */ 120 TEST_F(Flush, eio) 121 { 122 const char FULLPATH[] = "mountpoint/some_file.txt"; 123 const char RELPATH[] = "some_file.txt"; 124 uint64_t ino = 42; 125 int fd; 126 127 expect_lookup(RELPATH, ino, 1); 128 expect_open(ino, 0, 1); 129 expect_flush(ino, 1, getpid(), ReturnErrno(EIO)); 130 expect_release(); 131 132 fd = open(FULLPATH, O_WRONLY); 133 ASSERT_LE(0, fd) << strerror(errno); 134 135 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno); 136 } 137 138 /* 139 * If the filesystem returns ENOSYS, it will be treated as success and 140 * no more FUSE_FLUSH operations will be sent to the daemon 141 */ 142 TEST_F(Flush, enosys) 143 { 144 const char FULLPATH0[] = "mountpoint/some_file.txt"; 145 const char RELPATH0[] = "some_file.txt"; 146 const char FULLPATH1[] = "mountpoint/other_file.txt"; 147 const char RELPATH1[] = "other_file.txt"; 148 uint64_t ino0 = 42; 149 uint64_t ino1 = 43; 150 int fd0, fd1; 151 152 expect_lookup(RELPATH0, ino0, 1); 153 expect_open(ino0, 0, 1); 154 /* On the 2nd close, FUSE_FLUSH won't be sent at all */ 155 expect_flush(ino0, 1, getpid(), ReturnErrno(ENOSYS)); 156 expect_release(); 157 158 expect_lookup(RELPATH1, ino1, 1); 159 expect_open(ino1, 0, 1); 160 /* On the 2nd close, FUSE_FLUSH won't be sent at all */ 161 expect_release(); 162 163 fd0 = open(FULLPATH0, O_WRONLY); 164 ASSERT_LE(0, fd0) << strerror(errno); 165 166 fd1 = open(FULLPATH1, O_WRONLY); 167 ASSERT_LE(0, fd1) << strerror(errno); 168 169 EXPECT_EQ(0, close(fd0)) << strerror(errno); 170 EXPECT_EQ(0, close(fd1)) << strerror(errno); 171 } 172 173 /* A FUSE_FLUSH should be sent on close(2) */ 174 TEST_F(Flush, flush) 175 { 176 const char FULLPATH[] = "mountpoint/some_file.txt"; 177 const char RELPATH[] = "some_file.txt"; 178 uint64_t ino = 42; 179 int fd; 180 181 expect_lookup(RELPATH, ino, 1); 182 expect_open(ino, 0, 1); 183 expect_flush(ino, 1, getpid(), ReturnErrno(0)); 184 expect_release(); 185 186 fd = open(FULLPATH, O_WRONLY); 187 ASSERT_LE(0, fd) << strerror(errno); 188 189 ASSERT_TRUE(0 == close(fd)) << strerror(errno); 190 } 191 192 /* 193 * When closing a file with a POSIX file lock, flush should release the lock, 194 * _even_if_ it's not the process's last file descriptor for this file. 195 */ 196 TEST_F(FlushWithLocks, unlock_on_close) 197 { 198 const char FULLPATH[] = "mountpoint/some_file.txt"; 199 const char RELPATH[] = "some_file.txt"; 200 uint64_t ino = 42; 201 int fd, fd2; 202 struct flock fl; 203 pid_t pid = getpid(); 204 205 expect_lookup(RELPATH, ino, 2); 206 expect_open(ino, 0, 1); 207 EXPECT_CALL(*m_mock, process( 208 ResultOf([=](auto in) { 209 return (in.header.opcode == FUSE_SETLK && 210 in.header.nodeid == ino && 211 in.body.setlk.lk.type == F_RDLCK && 212 in.body.setlk.fh == FH); 213 }, Eq(true)), 214 _) 215 ).WillOnce(Invoke(ReturnErrno(0))); 216 EXPECT_CALL(*m_mock, process( 217 ResultOf([=](auto in) { 218 return (in.header.opcode == FUSE_SETLK && 219 in.header.nodeid == ino && 220 in.body.setlk.lk.type == F_UNLCK && 221 in.body.setlk.fh == FH); 222 }, Eq(true)), 223 _) 224 ).WillOnce(Invoke(ReturnErrno(0))); 225 expect_flush(ino, 1, pid, ReturnErrno(0)); 226 227 fd = open(FULLPATH, O_RDWR); 228 ASSERT_LE(0, fd) << strerror(errno); 229 fl.l_start = 0; 230 fl.l_len = 0; 231 fl.l_pid = pid; 232 fl.l_type = F_RDLCK; 233 fl.l_whence = SEEK_SET; 234 fl.l_sysid = 0; 235 ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno); 236 237 fd2 = open(FULLPATH, O_WRONLY); 238 ASSERT_LE(0, fd2) << strerror(errno); 239 ASSERT_EQ(0, close(fd2)) << strerror(errno); 240 leak(fd); 241 leak(fd2); 242 } 243