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, 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);
137 /* A FUSE_FLUSH should be sent on close(2) */
138 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
139 TEST_F(Flush, DISABLED_flush)
141 const char FULLPATH[] = "mountpoint/some_file.txt";
142 const char RELPATH[] = "some_file.txt";
146 expect_lookup(RELPATH, ino);
147 expect_open(ino, 0, 1);
148 expect_getattr(ino, 0);
149 expect_flush(ino, 1, 0, ReturnErrno(0));
152 fd = open(FULLPATH, O_WRONLY);
153 EXPECT_LE(0, fd) << strerror(errno);
155 ASSERT_TRUE(0 == close(fd)) << strerror(errno);
159 * When closing a file with a POSIX file lock, flush should release the lock,
160 * _even_if_ it's not the process's last file descriptor for this file.
162 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
163 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
164 TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
166 const char FULLPATH[] = "mountpoint/some_file.txt";
167 const char RELPATH[] = "some_file.txt";
171 pid_t pid = getpid();
173 expect_lookup(RELPATH, ino);
174 expect_open(ino, 0, 1);
175 expect_getattr(ino, 0);
176 EXPECT_CALL(*m_mock, process(
177 ResultOf([=](auto in) {
178 return (in->header.opcode == FUSE_SETLK &&
179 in->header.nodeid == ino &&
180 in->body.setlk.fh == FH);
183 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
184 SET_OUT_HEADER_LEN(out, setlk);
185 out->body.setlk.lk = in->body.setlk.lk;
187 expect_flush(ino, 1, pid, ReturnErrno(0));
189 fd = open(FULLPATH, O_RDWR);
190 ASSERT_LE(0, fd) << strerror(errno);
195 fl.l_whence = SEEK_SET;
197 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
200 ASSERT_EQ(0, close(fd2)) << strerror(errno);
201 /* Deliberately leak fd */