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 {
44 void expect_flush(uint64_t ino, int times, pid_t lo, ProcessMockerT r)
46 EXPECT_CALL(*m_mock, process(
47 ResultOf([=](auto in) {
48 return (in->header.opcode == FUSE_FLUSH &&
49 in->header.nodeid == ino &&
50 in->body.flush.lock_owner == (uint64_t)lo &&
51 in->body.flush.fh == FH);
55 .WillRepeatedly(Invoke(r));
58 void expect_lookup(const char *relpath, uint64_t ino)
60 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
64 * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting. This
65 * expectation will silence googlemock warnings
69 EXPECT_CALL(*m_mock, process(
70 ResultOf([=](auto in) {
71 return (in->header.opcode == FUSE_RELEASE);
74 ).WillRepeatedly(Invoke(ReturnErrno(0)));
78 class FlushWithLocks: public Flush {
79 virtual void SetUp() {
80 m_init_flags = FUSE_POSIX_LOCKS;
85 /* If a file descriptor is duplicated, every close causes FLUSH */
86 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
87 TEST_F(Flush, DISABLED_dup)
89 const char FULLPATH[] = "mountpoint/some_file.txt";
90 const char RELPATH[] = "some_file.txt";
94 expect_lookup(RELPATH, ino);
95 expect_open(ino, 0, 1);
96 expect_getattr(ino, 0);
97 expect_flush(ino, 2, 0, ReturnErrno(0));
100 fd = open(FULLPATH, O_WRONLY);
101 EXPECT_LE(0, fd) << strerror(errno);
105 ASSERT_EQ(0, close(fd2)) << strerror(errno);
106 ASSERT_EQ(0, close(fd)) << strerror(errno);
110 * Some FUSE filesystem cache data internally and flush it on release. Such
111 * filesystems may generate errors during release. On Linux, these get
112 * returned by close(2). However, POSIX does not require close(2) to return
113 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at
116 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
117 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
118 TEST_F(Flush, DISABLED_eio)
120 const char FULLPATH[] = "mountpoint/some_file.txt";
121 const char RELPATH[] = "some_file.txt";
125 expect_lookup(RELPATH, ino);
126 expect_open(ino, 0, 1);
127 expect_getattr(ino, 0);
128 expect_flush(ino, 1, 0, ReturnErrno(EIO));
131 fd = open(FULLPATH, O_WRONLY);
132 EXPECT_LE(0, fd) << strerror(errno);
134 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
138 * If the filesystem returns ENOSYS, it will be treated as success and
139 * no more FUSE_FLUSH operations will be sent to the daemon
141 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
142 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
143 TEST_F(Flush, DISABLED_enosys)
145 const char FULLPATH[] = "mountpoint/some_file.txt";
146 const char RELPATH[] = "some_file.txt";
150 expect_lookup(RELPATH, ino);
151 expect_open(ino, 0, 1);
152 expect_getattr(ino, 0);
153 /* On the 2nd close, FUSE_FLUSH won't be sent at all */
154 expect_flush(ino, 1, 0, ReturnErrno(ENOSYS));
157 fd = open(FULLPATH, O_WRONLY);
158 EXPECT_LE(0, fd) << strerror(errno);
162 EXPECT_EQ(0, close(fd2)) << strerror(errno);
163 EXPECT_EQ(0, close(fd)) << strerror(errno);
166 /* A FUSE_FLUSH should be sent on close(2) */
167 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
168 TEST_F(Flush, DISABLED_flush)
170 const char FULLPATH[] = "mountpoint/some_file.txt";
171 const char RELPATH[] = "some_file.txt";
175 expect_lookup(RELPATH, ino);
176 expect_open(ino, 0, 1);
177 expect_getattr(ino, 0);
178 expect_flush(ino, 1, 0, ReturnErrno(0));
181 fd = open(FULLPATH, O_WRONLY);
182 EXPECT_LE(0, fd) << strerror(errno);
184 ASSERT_TRUE(0 == close(fd)) << strerror(errno);
188 * When closing a file with a POSIX file lock, flush should release the lock,
189 * _even_if_ it's not the process's last file descriptor for this file.
191 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
192 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
193 TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
195 const char FULLPATH[] = "mountpoint/some_file.txt";
196 const char RELPATH[] = "some_file.txt";
200 pid_t pid = getpid();
202 expect_lookup(RELPATH, ino);
203 expect_open(ino, 0, 1);
204 expect_getattr(ino, 0);
205 EXPECT_CALL(*m_mock, process(
206 ResultOf([=](auto in) {
207 return (in->header.opcode == FUSE_SETLK &&
208 in->header.nodeid == ino &&
209 in->body.setlk.fh == FH);
212 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
213 SET_OUT_HEADER_LEN(out, setlk);
214 out->body.setlk.lk = in->body.setlk.lk;
216 expect_flush(ino, 1, pid, ReturnErrno(0));
218 fd = open(FULLPATH, O_RDWR);
219 ASSERT_LE(0, fd) << strerror(errno);
224 fl.l_whence = SEEK_SET;
226 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
229 ASSERT_EQ(0, close(fd2)) << strerror(errno);
230 /* Deliberately leak fd */