xref: /freebsd/tests/sys/fs/fusefs/release.cc (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <fcntl.h>
35 #include <unistd.h>
36 }
37 
38 #include "mockfs.hh"
39 #include "utils.hh"
40 
41 using namespace testing;
42 
43 class Release: public FuseTest {
44 
45 public:
46 void expect_lookup(const char *relpath, uint64_t ino, int times)
47 {
48 	FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
49 }
50 
51 void expect_release(uint64_t ino, uint64_t lock_owner,
52 	uint32_t flags, int error)
53 {
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);
61 		}, Eq(true)),
62 		_)
63 	).WillOnce(Invoke(ReturnErrno(error)))
64 	.RetiresOnSaturation();
65 }
66 };
67 
68 class ReleaseWithLocks: public Release {
69 	virtual void SetUp() {
70 		m_init_flags = FUSE_POSIX_LOCKS;
71 		Release::SetUp();
72 	}
73 };
74 
75 
76 /* If a file descriptor is duplicated, only the last close causes RELEASE */
77 TEST_F(Release, dup)
78 {
79 	const char FULLPATH[] = "mountpoint/some_file.txt";
80 	const char RELPATH[] = "some_file.txt";
81 	uint64_t ino = 42;
82 	int fd, fd2;
83 
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);
88 
89 	fd = open(FULLPATH, O_RDONLY);
90 	ASSERT_LE(0, fd) << strerror(errno);
91 
92 	fd2 = dup(fd);
93 	ASSERT_LE(0, fd2) << strerror(errno);
94 
95 	ASSERT_EQ(0, close(fd2)) << strerror(errno);
96 	ASSERT_EQ(0, close(fd)) << strerror(errno);
97 }
98 
99 /*
100  * Some FUSE filesystem cache data internally and flush it on release.  Such
101  * filesystems may generate errors during release.  On Linux, these get
102  * returned by close(2).  However, POSIX does not require close(2) to return
103  * this error.  FreeBSD's fuse(4) should return EIO if it returns an error at
104  * all.
105  */
106 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html */
107 TEST_F(Release, eio)
108 {
109 	const char FULLPATH[] = "mountpoint/some_file.txt";
110 	const char RELPATH[] = "some_file.txt";
111 	uint64_t ino = 42;
112 	int fd;
113 
114 	expect_lookup(RELPATH, ino, 1);
115 	expect_open(ino, 0, 1);
116 	expect_flush(ino, 1, ReturnErrno(0));
117 	expect_release(ino, getpid(), O_WRONLY, EIO);
118 
119 	fd = open(FULLPATH, O_WRONLY);
120 	ASSERT_LE(0, fd) << strerror(errno);
121 
122 	ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
123 }
124 
125 /*
126  * FUSE_RELEASE should contain the same flags used for FUSE_OPEN
127  */
128 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
129 TEST_F(Release, DISABLED_flags)
130 {
131 	const char FULLPATH[] = "mountpoint/some_file.txt";
132 	const char RELPATH[] = "some_file.txt";
133 	uint64_t ino = 42;
134 	int fd;
135 
136 	expect_lookup(RELPATH, ino, 1);
137 	expect_open(ino, 0, 1);
138 	expect_flush(ino, 1, ReturnErrno(0));
139 	expect_release(ino, getpid(), O_RDWR | O_APPEND, 0);
140 
141 	fd = open(FULLPATH, O_RDWR | O_APPEND);
142 	ASSERT_LE(0, fd) << strerror(errno);
143 
144 	ASSERT_EQ(0, close(fd)) << strerror(errno);
145 }
146 
147 /*
148  * fuse(4) will issue multiple FUSE_OPEN operations for the same file if it's
149  * opened with different modes.  Each FUSE_OPEN should get its own
150  * FUSE_RELEASE.
151  */
152 TEST_F(Release, multiple_opens)
153 {
154 	const char FULLPATH[] = "mountpoint/some_file.txt";
155 	const char RELPATH[] = "some_file.txt";
156 	uint64_t ino = 42;
157 	int fd, fd2;
158 
159 	expect_lookup(RELPATH, ino, 2);
160 	expect_open(ino, 0, 2);
161 	expect_flush(ino, 2, ReturnErrno(0));
162 	expect_release(ino, getpid(), O_RDONLY, 0);
163 
164 	fd = open(FULLPATH, O_RDONLY);
165 	ASSERT_LE(0, fd) << strerror(errno);
166 
167 	expect_release(ino, getpid(), O_WRONLY, 0);
168 	fd2 = open(FULLPATH, O_WRONLY);
169 	ASSERT_LE(0, fd2) << strerror(errno);
170 
171 	ASSERT_EQ(0, close(fd2)) << strerror(errno);
172 	ASSERT_EQ(0, close(fd)) << strerror(errno);
173 }
174 
175 TEST_F(Release, ok)
176 {
177 	const char FULLPATH[] = "mountpoint/some_file.txt";
178 	const char RELPATH[] = "some_file.txt";
179 	uint64_t ino = 42;
180 	int fd;
181 
182 	expect_lookup(RELPATH, ino, 1);
183 	expect_open(ino, 0, 1);
184 	expect_flush(ino, 1, ReturnErrno(0));
185 	expect_release(ino, getpid(), O_RDONLY, 0);
186 
187 	fd = open(FULLPATH, O_RDONLY);
188 	ASSERT_LE(0, fd) << strerror(errno);
189 
190 	ASSERT_EQ(0, close(fd)) << strerror(errno);
191 }
192 
193 /* When closing a file with a POSIX file lock, release should release the lock*/
194 TEST_F(ReleaseWithLocks, unlock_on_close)
195 {
196 	const char FULLPATH[] = "mountpoint/some_file.txt";
197 	const char RELPATH[] = "some_file.txt";
198 	uint64_t ino = 42;
199 	int fd;
200 	struct flock fl;
201 	pid_t pid = getpid();
202 
203 	expect_lookup(RELPATH, ino, 1);
204 	expect_open(ino, 0, 1);
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.lk.type == F_RDLCK &&
210 				in.body.setlk.fh == FH);
211 		}, Eq(true)),
212 		_)
213 	).WillOnce(Invoke(ReturnErrno(0)));
214 	EXPECT_CALL(*m_mock, process(
215 		ResultOf([=](auto in) {
216 			return (in.header.opcode == FUSE_SETLK &&
217 				in.header.nodeid == ino &&
218 				in.body.setlk.lk.type == F_UNLCK &&
219 				in.body.setlk.fh == FH);
220 		}, Eq(true)),
221 		_)
222 	).WillOnce(Invoke(ReturnErrno(0)));
223 	expect_flush(ino, 1, ReturnErrno(0));
224 	expect_release(ino, static_cast<uint64_t>(pid), O_RDWR, 0);
225 
226 	fd = open(FULLPATH, O_RDWR);
227 	ASSERT_LE(0, fd) << strerror(errno);
228 	fl.l_start = 0;
229 	fl.l_len = 0;
230 	fl.l_pid = pid;
231 	fl.l_type = F_RDLCK;
232 	fl.l_whence = SEEK_SET;
233 	fl.l_sysid = 0;
234 	ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
235 
236 	ASSERT_EQ(0, close(fd)) << strerror(errno);
237 }
238