2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
41 using namespace testing;
43 class Flush: public FuseTest {
47 expect_flush(uint64_t ino, int times, pid_t lo, ProcessMockerT r)
49 EXPECT_CALL(*m_mock, process(
50 ResultOf([=](auto in) {
51 return (in.header.opcode == FUSE_FLUSH &&
52 in.header.nodeid == ino &&
53 in.body.flush.lock_owner == (uint64_t)lo &&
54 in.body.flush.fh == FH);
58 .WillRepeatedly(Invoke(r));
61 void expect_lookup(const char *relpath, uint64_t ino, int times)
63 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
67 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This
68 * expectation will silence googlemock warnings
72 EXPECT_CALL(*m_mock, process(
73 ResultOf([=](auto in) {
74 return (in.header.opcode == FUSE_RELEASE);
77 ).WillRepeatedly(Invoke(ReturnErrno(0)));
81 class FlushWithLocks: public Flush {
82 virtual void SetUp() {
83 m_init_flags = FUSE_POSIX_LOCKS;
89 * If multiple file descriptors refer to the same file handle, closing each
90 * should send FUSE_FLUSH
92 TEST_F(Flush, open_twice)
94 const char FULLPATH[] = "mountpoint/some_file.txt";
95 const char RELPATH[] = "some_file.txt";
99 expect_lookup(RELPATH, ino, 2);
100 expect_open(ino, 0, 1);
101 expect_flush(ino, 2, getpid(), ReturnErrno(0));
104 fd = open(FULLPATH, O_WRONLY);
105 EXPECT_LE(0, fd) << strerror(errno);
107 fd2 = open(FULLPATH, O_WRONLY);
108 EXPECT_LE(0, fd2) << strerror(errno);
110 EXPECT_EQ(0, close(fd2)) << strerror(errno);
111 EXPECT_EQ(0, close(fd)) << strerror(errno);
115 * Some FUSE filesystem cache data internally and flush it on release. Such
116 * filesystems may generate errors during release. On Linux, these get
117 * returned by close(2). However, POSIX does not require close(2) to return
118 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at
121 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
124 const char FULLPATH[] = "mountpoint/some_file.txt";
125 const char RELPATH[] = "some_file.txt";
129 expect_lookup(RELPATH, ino, 1);
130 expect_open(ino, 0, 1);
131 expect_flush(ino, 1, getpid(), ReturnErrno(EIO));
134 fd = open(FULLPATH, O_WRONLY);
135 EXPECT_LE(0, fd) << strerror(errno);
137 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
141 * If the filesystem returns ENOSYS, it will be treated as success and
142 * no more FUSE_FLUSH operations will be sent to the daemon
144 TEST_F(Flush, enosys)
146 const char FULLPATH0[] = "mountpoint/some_file.txt";
147 const char RELPATH0[] = "some_file.txt";
148 const char FULLPATH1[] = "mountpoint/other_file.txt";
149 const char RELPATH1[] = "other_file.txt";
154 expect_lookup(RELPATH0, ino0, 1);
155 expect_open(ino0, 0, 1);
156 /* On the 2nd close, FUSE_FLUSH won't be sent at all */
157 expect_flush(ino0, 1, getpid(), ReturnErrno(ENOSYS));
160 expect_lookup(RELPATH1, ino1, 1);
161 expect_open(ino1, 0, 1);
162 /* On the 2nd close, FUSE_FLUSH won't be sent at all */
165 fd0 = open(FULLPATH0, O_WRONLY);
166 ASSERT_LE(0, fd0) << strerror(errno);
168 fd1 = open(FULLPATH1, O_WRONLY);
169 ASSERT_LE(0, fd1) << strerror(errno);
171 EXPECT_EQ(0, close(fd0)) << strerror(errno);
172 EXPECT_EQ(0, close(fd1)) << strerror(errno);
175 /* A FUSE_FLUSH should be sent on close(2) */
178 const char FULLPATH[] = "mountpoint/some_file.txt";
179 const char RELPATH[] = "some_file.txt";
183 expect_lookup(RELPATH, ino, 1);
184 expect_open(ino, 0, 1);
185 expect_flush(ino, 1, getpid(), ReturnErrno(0));
188 fd = open(FULLPATH, O_WRONLY);
189 EXPECT_LE(0, fd) << strerror(errno);
191 ASSERT_TRUE(0 == close(fd)) << strerror(errno);
195 * When closing a file with a POSIX file lock, flush should release the lock,
196 * _even_if_ it's not the process's last file descriptor for this file.
198 TEST_F(FlushWithLocks, unlock_on_close)
200 const char FULLPATH[] = "mountpoint/some_file.txt";
201 const char RELPATH[] = "some_file.txt";
205 pid_t pid = getpid();
207 expect_lookup(RELPATH, ino, 2);
208 expect_open(ino, 0, 1);
209 EXPECT_CALL(*m_mock, process(
210 ResultOf([=](auto in) {
211 return (in.header.opcode == FUSE_SETLK &&
212 in.header.nodeid == ino &&
213 in.body.setlk.fh == FH);
216 ).WillOnce(Invoke(ReturnErrno(0)));
217 expect_flush(ino, 1, pid, ReturnErrno(0));
219 fd = open(FULLPATH, O_RDWR);
220 ASSERT_LE(0, fd) << strerror(errno);
225 fl.l_whence = SEEK_SET;
227 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
229 fd2 = open(FULLPATH, O_WRONLY);
230 ASSERT_LE(0, fd2) << strerror(errno);
231 ASSERT_EQ(0, close(fd2)) << strerror(errno);