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 Release: public FuseTest {
46 void expect_lookup(const char *relpath, uint64_t ino, int times)
48 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
51 void expect_release(uint64_t ino, uint64_t lock_owner,
52 uint32_t flags, int error)
54 EXPECT_CALL(*m_mock, process(
55 ResultOf([=](auto in) {
56 return (in.header.opcode == FUSE_RELEASE &&
57 in.header.nodeid == ino &&
58 in.body.release.lock_owner == lock_owner &&
59 in.body.release.fh == FH &&
60 in.body.release.flags == flags);
63 ).WillOnce(Invoke(ReturnErrno(error)))
64 .RetiresOnSaturation();
68 class ReleaseWithLocks: public Release {
69 virtual void SetUp() {
70 m_init_flags = FUSE_POSIX_LOCKS;
76 /* If a file descriptor is duplicated, only the last close causes RELEASE */
79 const char FULLPATH[] = "mountpoint/some_file.txt";
80 const char RELPATH[] = "some_file.txt";
84 expect_lookup(RELPATH, ino, 1);
85 expect_open(ino, 0, 1);
86 expect_flush(ino, 1, ReturnErrno(0));
87 expect_release(ino, getpid(), O_RDONLY, 0);
89 fd = open(FULLPATH, O_RDONLY);
90 EXPECT_LE(0, fd) << strerror(errno);
94 ASSERT_EQ(0, close(fd2)) << strerror(errno);
95 ASSERT_EQ(0, close(fd)) << strerror(errno);
99 * Some FUSE filesystem cache data internally and flush it on release. Such
100 * filesystems may generate errors during release. On Linux, these get
101 * returned by close(2). However, POSIX does not require close(2) to return
102 * this error. FreeBSD's fuse(4) should return EIO if it returns an error at
105 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
108 const char FULLPATH[] = "mountpoint/some_file.txt";
109 const char RELPATH[] = "some_file.txt";
113 expect_lookup(RELPATH, ino, 1);
114 expect_open(ino, 0, 1);
115 expect_flush(ino, 1, ReturnErrno(0));
116 expect_release(ino, getpid(), O_WRONLY, EIO);
118 fd = open(FULLPATH, O_WRONLY);
119 EXPECT_LE(0, fd) << strerror(errno);
121 ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
125 * FUSE_RELEASE should contain the same flags used for FUSE_OPEN
127 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
128 TEST_F(Release, DISABLED_flags)
130 const char FULLPATH[] = "mountpoint/some_file.txt";
131 const char RELPATH[] = "some_file.txt";
135 expect_lookup(RELPATH, ino, 1);
136 expect_open(ino, 0, 1);
137 expect_flush(ino, 1, ReturnErrno(0));
138 expect_release(ino, getpid(), O_RDWR | O_APPEND, 0);
140 fd = open(FULLPATH, O_RDWR | O_APPEND);
141 EXPECT_LE(0, fd) << strerror(errno);
143 ASSERT_EQ(0, close(fd)) << strerror(errno);
147 * fuse(4) will issue multiple FUSE_OPEN operations for the same file if it's
148 * opened with different modes. Each FUSE_OPEN should get its own
151 TEST_F(Release, multiple_opens)
153 const char FULLPATH[] = "mountpoint/some_file.txt";
154 const char RELPATH[] = "some_file.txt";
158 expect_lookup(RELPATH, ino, 2);
159 expect_open(ino, 0, 2);
160 expect_flush(ino, 2, ReturnErrno(0));
161 expect_release(ino, getpid(), O_RDONLY, 0);
163 fd = open(FULLPATH, O_RDONLY);
164 EXPECT_LE(0, fd) << strerror(errno);
166 expect_release(ino, getpid(), O_WRONLY, 0);
167 fd2 = open(FULLPATH, O_WRONLY);
168 EXPECT_LE(0, fd2) << strerror(errno);
170 ASSERT_EQ(0, close(fd2)) << strerror(errno);
171 ASSERT_EQ(0, close(fd)) << strerror(errno);
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, ReturnErrno(0));
184 expect_release(ino, getpid(), O_RDONLY, 0);
186 fd = open(FULLPATH, O_RDONLY);
187 EXPECT_LE(0, fd) << strerror(errno);
189 ASSERT_EQ(0, close(fd)) << strerror(errno);
192 /* When closing a file with a POSIX file lock, release should release the lock*/
193 TEST_F(ReleaseWithLocks, 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, 1);
203 expect_open(ino, 0, 1);
204 EXPECT_CALL(*m_mock, process(
205 ResultOf([=](auto in) {
206 return (in.header.opcode == FUSE_SETLK &&
207 in.header.nodeid == ino &&
208 in.body.setlk.fh == FH);
211 ).WillOnce(Invoke(ReturnErrno(0)));
212 expect_flush(ino, 1, ReturnErrno(0));
213 expect_release(ino, static_cast<uint64_t>(pid), O_RDWR, 0);
215 fd = open(FULLPATH, O_RDWR);
216 ASSERT_LE(0, fd) << strerror(errno);
221 fl.l_whence = SEEK_SET;
223 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
225 ASSERT_EQ(0, close(fd)) << strerror(errno);