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 * Test for FOPEN_NOFLUSH: we expect that zero flush calls will be performed. 114 */ 115 TEST_F(Flush, open_noflush) 116 { 117 const char FULLPATH[] = "mountpoint/some_file.txt"; 118 const char RELPATH[] = "some_file.txt"; 119 uint64_t ino = 42; 120 uint64_t pid = (uint64_t)getpid(); 121 int fd; 122 123 expect_lookup(RELPATH, ino, 1); 124 expect_open(ino, FOPEN_NOFLUSH, 1); 125 EXPECT_CALL(*m_mock, process( 126 ResultOf([=](auto in) { 127 return (in.header.opcode == FUSE_FLUSH && 128 in.header.nodeid == ino && 129 in.body.flush.lock_owner == pid && 130 in.body.flush.fh == FH); 131 }, Eq(true)), 132 _) 133 ).Times(0); 134 expect_release(); 135 136 fd = open(FULLPATH, O_WRONLY); 137 ASSERT_LE(0, fd) << strerror(errno); 138 // close MUST not flush 139 EXPECT_EQ(0, close(fd)) << strerror(errno); 140 } 141 142 /* 143 * Some FUSE filesystem cache data internally and flush it on release. Such 144 * filesystems may generate errors during release. On Linux, these get 145 * returned by close(2). However, POSIX does not require close(2) to return 146 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at 147 * all. 148 */ 149 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */ 150 TEST_F(Flush, eio) 151 { 152 const char FULLPATH[] = "mountpoint/some_file.txt"; 153 const char RELPATH[] = "some_file.txt"; 154 uint64_t ino = 42; 155 int fd; 156 157 expect_lookup(RELPATH, ino, 1); 158 expect_open(ino, 0, 1); 159 expect_flush(ino, 1, getpid(), ReturnErrno(EIO)); 160 expect_release(); 161 162 fd = open(FULLPATH, O_WRONLY); 163 ASSERT_LE(0, fd) << strerror(errno); 164 165 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno); 166 } 167 168 /* 169 * If the filesystem returns ENOSYS, it will be treated as success and 170 * no more FUSE_FLUSH operations will be sent to the daemon 171 */ 172 TEST_F(Flush, enosys) 173 { 174 const char FULLPATH0[] = "mountpoint/some_file.txt"; 175 const char RELPATH0[] = "some_file.txt"; 176 const char FULLPATH1[] = "mountpoint/other_file.txt"; 177 const char RELPATH1[] = "other_file.txt"; 178 uint64_t ino0 = 42; 179 uint64_t ino1 = 43; 180 int fd0, fd1; 181 182 expect_lookup(RELPATH0, ino0, 1); 183 expect_open(ino0, 0, 1); 184 /* On the 2nd close, FUSE_FLUSH won't be sent at all */ 185 expect_flush(ino0, 1, getpid(), ReturnErrno(ENOSYS)); 186 expect_release(); 187 188 expect_lookup(RELPATH1, ino1, 1); 189 expect_open(ino1, 0, 1); 190 /* On the 2nd close, FUSE_FLUSH won't be sent at all */ 191 expect_release(); 192 193 fd0 = open(FULLPATH0, O_WRONLY); 194 ASSERT_LE(0, fd0) << strerror(errno); 195 196 fd1 = open(FULLPATH1, O_WRONLY); 197 ASSERT_LE(0, fd1) << strerror(errno); 198 199 EXPECT_EQ(0, close(fd0)) << strerror(errno); 200 EXPECT_EQ(0, close(fd1)) << strerror(errno); 201 } 202 203 /* A FUSE_FLUSH should be sent on close(2) */ 204 TEST_F(Flush, flush) 205 { 206 const char FULLPATH[] = "mountpoint/some_file.txt"; 207 const char RELPATH[] = "some_file.txt"; 208 uint64_t ino = 42; 209 int fd; 210 211 expect_lookup(RELPATH, ino, 1); 212 expect_open(ino, 0, 1); 213 expect_flush(ino, 1, getpid(), ReturnErrno(0)); 214 expect_release(); 215 216 fd = open(FULLPATH, O_WRONLY); 217 ASSERT_LE(0, fd) << strerror(errno); 218 219 ASSERT_TRUE(0 == close(fd)) << strerror(errno); 220 } 221 222 /* 223 * When closing a file with a POSIX file lock, flush should release the lock, 224 * _even_if_ it's not the process's last file descriptor for this file. 225 */ 226 TEST_F(FlushWithLocks, unlock_on_close) 227 { 228 const char FULLPATH[] = "mountpoint/some_file.txt"; 229 const char RELPATH[] = "some_file.txt"; 230 uint64_t ino = 42; 231 int fd, fd2; 232 struct flock fl; 233 pid_t pid = getpid(); 234 235 expect_lookup(RELPATH, ino, 2); 236 expect_open(ino, 0, 1); 237 EXPECT_CALL(*m_mock, process( 238 ResultOf([=](auto in) { 239 return (in.header.opcode == FUSE_SETLK && 240 in.header.nodeid == ino && 241 in.body.setlk.lk.type == F_RDLCK && 242 in.body.setlk.fh == FH); 243 }, Eq(true)), 244 _) 245 ).WillOnce(Invoke(ReturnErrno(0))); 246 EXPECT_CALL(*m_mock, process( 247 ResultOf([=](auto in) { 248 return (in.header.opcode == FUSE_SETLK && 249 in.header.nodeid == ino && 250 in.body.setlk.lk.type == F_UNLCK && 251 in.body.setlk.fh == FH); 252 }, Eq(true)), 253 _) 254 ).WillOnce(Invoke(ReturnErrno(0))); 255 expect_flush(ino, 1, pid, ReturnErrno(0)); 256 257 fd = open(FULLPATH, O_RDWR); 258 ASSERT_LE(0, fd) << strerror(errno); 259 fl.l_start = 0; 260 fl.l_len = 0; 261 fl.l_pid = pid; 262 fl.l_type = F_RDLCK; 263 fl.l_whence = SEEK_SET; 264 fl.l_sysid = 0; 265 ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno); 266 267 fd2 = open(FULLPATH, O_WRONLY); 268 ASSERT_LE(0, fd2) << strerror(errno); 269 ASSERT_EQ(0, close(fd2)) << strerror(errno); 270 leak(fd); 271 leak(fd2); 272 } 273