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
38 /* This flag value should probably be defined in fuse_kernel.h */
39 #define OFFSET_MAX 0x7fffffffffffffffLL
41 using namespace testing;
43 /* For testing filesystems without posix locking support */
44 class Fallback: public FuseTest {
47 void expect_lookup(const char *relpath, uint64_t ino)
49 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 1);
54 /* For testing filesystems with posix locking support */
55 class Locks: public Fallback {
56 virtual void SetUp() {
57 m_init_flags = FUSE_POSIX_LOCKS;
62 class GetlkFallback: public Fallback {};
63 class Getlk: public Locks {};
64 class SetlkFallback: public Fallback {};
65 class Setlk: public Locks {};
66 class SetlkwFallback: public Fallback {};
67 class Setlkw: public Locks {};
70 * If the fuse filesystem does not support posix file locks, then the kernel
71 * should fall back to local locks.
73 TEST_F(GetlkFallback, local)
75 const char FULLPATH[] = "mountpoint/some_file.txt";
76 const char RELPATH[] = "some_file.txt";
81 expect_lookup(RELPATH, ino);
82 expect_open(ino, 0, 1);
83 expect_getattr(ino, 0);
85 fd = open(FULLPATH, O_RDWR);
86 ASSERT_LE(0, fd) << strerror(errno);
91 fl.l_whence = SEEK_SET;
93 ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
94 /* Deliberately leak fd. close(2) will be tested in release.cc */
98 * If the filesystem has no locks that fit the description, the filesystem
99 * should return F_UNLCK
101 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
102 TEST_F(Getlk, DISABLED_no_locks)
104 const char FULLPATH[] = "mountpoint/some_file.txt";
105 const char RELPATH[] = "some_file.txt";
111 expect_lookup(RELPATH, ino);
112 expect_open(ino, 0, 1);
113 expect_getattr(ino, 0);
114 EXPECT_CALL(*m_mock, process(
115 ResultOf([=](auto in) {
116 return (in->header.opcode == FUSE_GETLK &&
117 in->header.nodeid == ino &&
118 in->body.getlk.fh == FH &&
119 in->body.getlk.owner == (uint32_t)pid &&
120 in->body.getlk.lk.start == 10 &&
121 in->body.getlk.lk.end == 1009 &&
122 in->body.getlk.lk.type == F_RDLCK &&
123 in->body.getlk.lk.pid == (uint64_t)pid);
126 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
127 SET_OUT_HEADER_LEN(out, getlk);
128 out->body.getlk.lk = in->body.getlk.lk;
129 out->body.getlk.lk.type = F_UNLCK;
132 fd = open(FULLPATH, O_RDWR);
133 ASSERT_LE(0, fd) << strerror(errno);
138 fl.l_whence = SEEK_SET;
140 ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
141 ASSERT_EQ(F_UNLCK, fl.l_type);
142 /* Deliberately leak fd. close(2) will be tested in release.cc */
145 /* A different pid does have a lock */
146 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
147 TEST_F(Getlk, DISABLED_lock_exists)
149 const char FULLPATH[] = "mountpoint/some_file.txt";
150 const char RELPATH[] = "some_file.txt";
157 expect_lookup(RELPATH, ino);
158 expect_open(ino, 0, 1);
159 expect_getattr(ino, 0);
160 EXPECT_CALL(*m_mock, process(
161 ResultOf([=](auto in) {
162 return (in->header.opcode == FUSE_GETLK &&
163 in->header.nodeid == ino &&
164 in->body.getlk.fh == FH &&
165 in->body.getlk.owner == (uint32_t)pid &&
166 in->body.getlk.lk.start == 10 &&
167 in->body.getlk.lk.end == 1009 &&
168 in->body.getlk.lk.type == F_RDLCK &&
169 in->body.getlk.lk.pid == (uint64_t)pid);
172 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
173 SET_OUT_HEADER_LEN(out, getlk);
174 out->body.getlk.lk.start = 100;
175 out->body.getlk.lk.end = 199;
176 out->body.getlk.lk.type = F_WRLCK;
177 out->body.getlk.lk.pid = (uint32_t)pid2;;
180 fd = open(FULLPATH, O_RDWR);
181 ASSERT_LE(0, fd) << strerror(errno);
186 fl.l_whence = SEEK_SET;
188 ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
189 EXPECT_EQ(100, fl.l_start);
190 EXPECT_EQ(100, fl.l_len);
191 EXPECT_EQ(pid2, fl.l_pid);
192 EXPECT_EQ(F_WRLCK, fl.l_type);
193 EXPECT_EQ(SEEK_SET, fl.l_whence);
194 EXPECT_EQ(0, fl.l_sysid);
195 /* Deliberately leak fd. close(2) will be tested in release.cc */
199 * If the fuse filesystem does not support posix file locks, then the kernel
200 * should fall back to local locks.
202 TEST_F(SetlkFallback, local)
204 const char FULLPATH[] = "mountpoint/some_file.txt";
205 const char RELPATH[] = "some_file.txt";
210 expect_lookup(RELPATH, ino);
211 expect_open(ino, 0, 1);
212 expect_getattr(ino, 0);
214 fd = open(FULLPATH, O_RDWR);
215 ASSERT_LE(0, fd) << strerror(errno);
220 fl.l_whence = SEEK_SET;
222 ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
223 /* Deliberately leak fd. close(2) will be tested in release.cc */
226 /* Set a new lock with FUSE_SETLK */
227 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
228 TEST_F(Setlk, DISABLED_set)
230 const char FULLPATH[] = "mountpoint/some_file.txt";
231 const char RELPATH[] = "some_file.txt";
237 expect_lookup(RELPATH, ino);
238 expect_open(ino, 0, 1);
239 expect_getattr(ino, 0);
240 EXPECT_CALL(*m_mock, process(
241 ResultOf([=](auto in) {
242 return (in->header.opcode == FUSE_SETLK &&
243 in->header.nodeid == ino &&
244 in->body.setlk.fh == FH &&
245 in->body.setlk.owner == (uint32_t)pid &&
246 in->body.setlk.lk.start == 10 &&
247 in->body.setlk.lk.end == 1009 &&
248 in->body.setlk.lk.type == F_RDLCK &&
249 in->body.setlk.lk.pid == (uint64_t)pid);
252 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
253 SET_OUT_HEADER_LEN(out, setlk);
254 out->body.setlk.lk = in->body.setlk.lk;
257 fd = open(FULLPATH, O_RDWR);
258 ASSERT_LE(0, fd) << strerror(errno);
263 fl.l_whence = SEEK_SET;
265 ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
266 /* Deliberately leak fd. close(2) will be tested in release.cc */
269 /* l_len = 0 is a flag value that means to lock until EOF */
270 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
271 TEST_F(Setlk, DISABLED_set_eof)
273 const char FULLPATH[] = "mountpoint/some_file.txt";
274 const char RELPATH[] = "some_file.txt";
280 expect_lookup(RELPATH, ino);
281 expect_open(ino, 0, 1);
282 expect_getattr(ino, 0);
283 EXPECT_CALL(*m_mock, process(
284 ResultOf([=](auto in) {
285 return (in->header.opcode == FUSE_SETLK &&
286 in->header.nodeid == ino &&
287 in->body.setlk.fh == FH &&
288 in->body.setlk.owner == (uint32_t)pid &&
289 in->body.setlk.lk.start == 10 &&
290 in->body.setlk.lk.end == OFFSET_MAX &&
291 in->body.setlk.lk.type == F_RDLCK &&
292 in->body.setlk.lk.pid == (uint64_t)pid);
295 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
296 SET_OUT_HEADER_LEN(out, setlk);
297 out->body.setlk.lk = in->body.setlk.lk;
300 fd = open(FULLPATH, O_RDWR);
301 ASSERT_LE(0, fd) << strerror(errno);
306 fl.l_whence = SEEK_SET;
308 ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
309 /* Deliberately leak fd. close(2) will be tested in release.cc */
312 /* Fail to set a new lock with FUSE_SETLK due to a conflict */
313 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
314 TEST_F(Setlk, DISABLED_eagain)
316 const char FULLPATH[] = "mountpoint/some_file.txt";
317 const char RELPATH[] = "some_file.txt";
323 expect_lookup(RELPATH, ino);
324 expect_open(ino, 0, 1);
325 expect_getattr(ino, 0);
326 EXPECT_CALL(*m_mock, process(
327 ResultOf([=](auto in) {
328 return (in->header.opcode == FUSE_SETLK &&
329 in->header.nodeid == ino &&
330 in->body.setlk.fh == FH &&
331 in->body.setlk.owner == (uint32_t)pid &&
332 in->body.setlk.lk.start == 10 &&
333 in->body.setlk.lk.end == 1009 &&
334 in->body.setlk.lk.type == F_RDLCK &&
335 in->body.setlk.lk.pid == (uint64_t)pid);
338 ).WillOnce(Invoke(ReturnErrno(EAGAIN)));
340 fd = open(FULLPATH, O_RDWR);
341 ASSERT_LE(0, fd) << strerror(errno);
346 fl.l_whence = SEEK_SET;
348 ASSERT_EQ(-1, fcntl(fd, F_SETLK, &fl));
349 ASSERT_EQ(EAGAIN, errno);
350 /* Deliberately leak fd. close(2) will be tested in release.cc */
354 * If the fuse filesystem does not support posix file locks, then the kernel
355 * should fall back to local locks.
357 TEST_F(SetlkwFallback, local)
359 const char FULLPATH[] = "mountpoint/some_file.txt";
360 const char RELPATH[] = "some_file.txt";
365 expect_lookup(RELPATH, ino);
366 expect_open(ino, 0, 1);
367 expect_getattr(ino, 0);
369 fd = open(FULLPATH, O_RDWR);
370 ASSERT_LE(0, fd) << strerror(errno);
375 fl.l_whence = SEEK_SET;
377 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
378 /* Deliberately leak fd. close(2) will be tested in release.cc */
382 * Set a new lock with FUSE_SETLK. If the lock is not available, then the
383 * command should block. But to the kernel, that's the same as just being
384 * slow, so we don't need a separate test method
386 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
387 TEST_F(Setlkw, DISABLED_set)
389 const char FULLPATH[] = "mountpoint/some_file.txt";
390 const char RELPATH[] = "some_file.txt";
396 expect_lookup(RELPATH, ino);
397 expect_open(ino, 0, 1);
398 expect_getattr(ino, 0);
399 EXPECT_CALL(*m_mock, process(
400 ResultOf([=](auto in) {
401 return (in->header.opcode == FUSE_SETLK &&
402 in->header.nodeid == ino &&
403 in->body.setlkw.fh == FH &&
404 in->body.setlkw.owner == (uint32_t)pid &&
405 in->body.setlkw.lk.start == 10 &&
406 in->body.setlkw.lk.end == 1009 &&
407 in->body.setlkw.lk.type == F_RDLCK &&
408 in->body.setlkw.lk.pid == (uint64_t)pid);
411 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
412 SET_OUT_HEADER_LEN(out, setlkw);
413 out->body.setlkw.lk = in->body.setlkw.lk;
416 fd = open(FULLPATH, O_RDWR);
417 ASSERT_LE(0, fd) << strerror(errno);
422 fl.l_whence = SEEK_SET;
424 ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
425 /* Deliberately leak fd. close(2) will be tested in release.cc */