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
39 using namespace testing;
41 class Flush: public FuseTest {
45 expect_flush(uint64_t ino, int times, pid_t lo, ProcessMockerT r)
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);
56 .WillRepeatedly(Invoke(r));
59 void expect_lookup(const char *relpath, uint64_t ino, int times)
61 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
65 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This
66 * expectation will silence googlemock warnings
70 EXPECT_CALL(*m_mock, process(
71 ResultOf([=](auto in) {
72 return (in.header.opcode == FUSE_RELEASE);
75 ).WillRepeatedly(Invoke(ReturnErrno(0)));
79 class FlushWithLocks: public Flush {
80 virtual void SetUp() {
81 m_init_flags = FUSE_POSIX_LOCKS;
87 * If multiple file descriptors refer to the same file handle, closing each
88 * should send FUSE_FLUSH
90 TEST_F(Flush, open_twice)
92 const char FULLPATH[] = "mountpoint/some_file.txt";
93 const char RELPATH[] = "some_file.txt";
97 expect_lookup(RELPATH, ino, 2);
98 expect_open(ino, 0, 1);
99 expect_flush(ino, 2, getpid(), ReturnErrno(0));
102 fd = open(FULLPATH, O_WRONLY);
103 EXPECT_LE(0, fd) << strerror(errno);
105 fd2 = open(FULLPATH, O_WRONLY);
106 EXPECT_LE(0, fd2) << strerror(errno);
108 EXPECT_EQ(0, close(fd2)) << strerror(errno);
109 EXPECT_EQ(0, close(fd)) << strerror(errno);
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
119 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
122 const char FULLPATH[] = "mountpoint/some_file.txt";
123 const char RELPATH[] = "some_file.txt";
127 expect_lookup(RELPATH, ino, 1);
128 expect_open(ino, 0, 1);
129 expect_flush(ino, 1, getpid(), ReturnErrno(EIO));
132 fd = open(FULLPATH, O_WRONLY);
133 EXPECT_LE(0, fd) << strerror(errno);
135 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
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
142 TEST_F(Flush, enosys)
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";
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));
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 */
163 fd0 = open(FULLPATH0, O_WRONLY);
164 ASSERT_LE(0, fd0) << strerror(errno);
166 fd1 = open(FULLPATH1, O_WRONLY);
167 ASSERT_LE(0, fd1) << strerror(errno);
169 EXPECT_EQ(0, close(fd0)) << strerror(errno);
170 EXPECT_EQ(0, close(fd1)) << strerror(errno);
173 /* A FUSE_FLUSH should be sent on close(2) */
176 const char FULLPATH[] = "mountpoint/some_file.txt";
177 const char RELPATH[] = "some_file.txt";
181 expect_lookup(RELPATH, ino, 1);
182 expect_open(ino, 0, 1);
183 expect_flush(ino, 1, getpid(), ReturnErrno(0));
186 fd = open(FULLPATH, O_WRONLY);
187 EXPECT_LE(0, fd) << strerror(errno);
189 ASSERT_TRUE(0 == close(fd)) << strerror(errno);
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.
196 TEST_F(FlushWithLocks, unlock_on_close)
198 const char FULLPATH[] = "mountpoint/some_file.txt";
199 const char RELPATH[] = "some_file.txt";
203 pid_t pid = getpid();
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.fh == FH);
214 ).WillOnce(Invoke(ReturnErrno(0)));
215 expect_flush(ino, 1, pid, ReturnErrno(0));
217 fd = open(FULLPATH, O_RDWR);
218 ASSERT_LE(0, fd) << strerror(errno);
223 fl.l_whence = SEEK_SET;
225 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
227 fd2 = open(FULLPATH, O_WRONLY);
228 ASSERT_LE(0, fd2) << strerror(errno);
229 ASSERT_EQ(0, close(fd2)) << strerror(errno);
230 /* Deliberately leak fd */