]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/locks.cc
fuse(4): combine common code in the tests
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / locks.cc
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
31 extern "C" {
32 #include <fcntl.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 /* This flag value should probably be defined in fuse_kernel.h */
39 #define OFFSET_MAX 0x7fffffffffffffffLL
40
41 using namespace testing;
42
43 /* For testing filesystems without posix locking support */
44 class Fallback: public FuseTest {
45 public:
46
47 void expect_lookup(const char *relpath, uint64_t ino)
48 {
49         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 1);
50 }
51
52 };
53
54 /* For testing filesystems with posix locking support */
55 class Locks: public Fallback {
56         virtual void SetUp() {
57                 m_init_flags = FUSE_POSIX_LOCKS;
58                 Fallback::SetUp();
59         }
60 };
61
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 {};
68
69 /*
70  * If the fuse filesystem does not support posix file locks, then the kernel
71  * should fall back to local locks.
72  */
73 TEST_F(GetlkFallback, local)
74 {
75         const char FULLPATH[] = "mountpoint/some_file.txt";
76         const char RELPATH[] = "some_file.txt";
77         uint64_t ino = 42;
78         struct flock fl;
79         int fd;
80
81         expect_lookup(RELPATH, ino);
82         expect_open(ino, 0, 1);
83         expect_getattr(ino, 0);
84
85         fd = open(FULLPATH, O_RDWR);
86         ASSERT_LE(0, fd) << strerror(errno);
87         fl.l_start = 10;
88         fl.l_len = 1000;
89         fl.l_pid = getpid();
90         fl.l_type = F_RDLCK;
91         fl.l_whence = SEEK_SET;
92         fl.l_sysid = 0;
93         ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
94         /* Deliberately leak fd.  close(2) will be tested in release.cc */
95 }
96
97 /* 
98  * If the filesystem has no locks that fit the description, the filesystem
99  * should return F_UNLCK
100  */
101 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
102 TEST_F(Getlk, DISABLED_no_locks)
103 {
104         const char FULLPATH[] = "mountpoint/some_file.txt";
105         const char RELPATH[] = "some_file.txt";
106         uint64_t ino = 42;
107         struct flock fl;
108         int fd;
109         pid_t pid = 1234;
110
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);
124                 }, Eq(true)),
125                 _)
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;
130         })));
131
132         fd = open(FULLPATH, O_RDWR);
133         ASSERT_LE(0, fd) << strerror(errno);
134         fl.l_start = 10;
135         fl.l_len = 1000;
136         fl.l_pid = pid;
137         fl.l_type = F_RDLCK;
138         fl.l_whence = SEEK_SET;
139         fl.l_sysid = 0;
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 */
143 }
144
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)
148 {
149         const char FULLPATH[] = "mountpoint/some_file.txt";
150         const char RELPATH[] = "some_file.txt";
151         uint64_t ino = 42;
152         struct flock fl;
153         int fd;
154         pid_t pid = 1234;
155         pid_t pid2 = 1235;
156
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);
170                 }, Eq(true)),
171                 _)
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;;
178         })));
179
180         fd = open(FULLPATH, O_RDWR);
181         ASSERT_LE(0, fd) << strerror(errno);
182         fl.l_start = 10;
183         fl.l_len = 1000;
184         fl.l_pid = pid;
185         fl.l_type = F_RDLCK;
186         fl.l_whence = SEEK_SET;
187         fl.l_sysid = 0;
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 */
196 }
197
198 /*
199  * If the fuse filesystem does not support posix file locks, then the kernel
200  * should fall back to local locks.
201  */
202 TEST_F(SetlkFallback, local)
203 {
204         const char FULLPATH[] = "mountpoint/some_file.txt";
205         const char RELPATH[] = "some_file.txt";
206         uint64_t ino = 42;
207         struct flock fl;
208         int fd;
209
210         expect_lookup(RELPATH, ino);
211         expect_open(ino, 0, 1);
212         expect_getattr(ino, 0);
213
214         fd = open(FULLPATH, O_RDWR);
215         ASSERT_LE(0, fd) << strerror(errno);
216         fl.l_start = 10;
217         fl.l_len = 1000;
218         fl.l_pid = getpid();
219         fl.l_type = F_RDLCK;
220         fl.l_whence = SEEK_SET;
221         fl.l_sysid = 0;
222         ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
223         /* Deliberately leak fd.  close(2) will be tested in release.cc */
224 }
225
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)
229 {
230         const char FULLPATH[] = "mountpoint/some_file.txt";
231         const char RELPATH[] = "some_file.txt";
232         uint64_t ino = 42;
233         struct flock fl;
234         int fd;
235         pid_t pid = 1234;
236
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);
250                 }, Eq(true)),
251                 _)
252         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
253                 SET_OUT_HEADER_LEN(out, setlk);
254                 out->body.setlk.lk = in->body.setlk.lk;
255         })));
256
257         fd = open(FULLPATH, O_RDWR);
258         ASSERT_LE(0, fd) << strerror(errno);
259         fl.l_start = 10;
260         fl.l_len = 1000;
261         fl.l_pid = pid;
262         fl.l_type = F_RDLCK;
263         fl.l_whence = SEEK_SET;
264         fl.l_sysid = 0;
265         ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
266         /* Deliberately leak fd.  close(2) will be tested in release.cc */
267 }
268
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)
272 {
273         const char FULLPATH[] = "mountpoint/some_file.txt";
274         const char RELPATH[] = "some_file.txt";
275         uint64_t ino = 42;
276         struct flock fl;
277         int fd;
278         pid_t pid = 1234;
279
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);
293                 }, Eq(true)),
294                 _)
295         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
296                 SET_OUT_HEADER_LEN(out, setlk);
297                 out->body.setlk.lk = in->body.setlk.lk;
298         })));
299
300         fd = open(FULLPATH, O_RDWR);
301         ASSERT_LE(0, fd) << strerror(errno);
302         fl.l_start = 10;
303         fl.l_len = 0;
304         fl.l_pid = pid;
305         fl.l_type = F_RDLCK;
306         fl.l_whence = SEEK_SET;
307         fl.l_sysid = 0;
308         ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
309         /* Deliberately leak fd.  close(2) will be tested in release.cc */
310 }
311
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)
315 {
316         const char FULLPATH[] = "mountpoint/some_file.txt";
317         const char RELPATH[] = "some_file.txt";
318         uint64_t ino = 42;
319         struct flock fl;
320         int fd;
321         pid_t pid = 1234;
322
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);
336                 }, Eq(true)),
337                 _)
338         ).WillOnce(Invoke(ReturnErrno(EAGAIN)));
339
340         fd = open(FULLPATH, O_RDWR);
341         ASSERT_LE(0, fd) << strerror(errno);
342         fl.l_start = 10;
343         fl.l_len = 1000;
344         fl.l_pid = pid;
345         fl.l_type = F_RDLCK;
346         fl.l_whence = SEEK_SET;
347         fl.l_sysid = 0;
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 */
351 }
352
353 /*
354  * If the fuse filesystem does not support posix file locks, then the kernel
355  * should fall back to local locks.
356  */
357 TEST_F(SetlkwFallback, local)
358 {
359         const char FULLPATH[] = "mountpoint/some_file.txt";
360         const char RELPATH[] = "some_file.txt";
361         uint64_t ino = 42;
362         struct flock fl;
363         int fd;
364
365         expect_lookup(RELPATH, ino);
366         expect_open(ino, 0, 1);
367         expect_getattr(ino, 0);
368
369         fd = open(FULLPATH, O_RDWR);
370         ASSERT_LE(0, fd) << strerror(errno);
371         fl.l_start = 10;
372         fl.l_len = 1000;
373         fl.l_pid = getpid();
374         fl.l_type = F_RDLCK;
375         fl.l_whence = SEEK_SET;
376         fl.l_sysid = 0;
377         ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
378         /* Deliberately leak fd.  close(2) will be tested in release.cc */
379 }
380
381 /*
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
385  */
386 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
387 TEST_F(Setlkw, DISABLED_set)
388 {
389         const char FULLPATH[] = "mountpoint/some_file.txt";
390         const char RELPATH[] = "some_file.txt";
391         uint64_t ino = 42;
392         struct flock fl;
393         int fd;
394         pid_t pid = 1234;
395
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);
409                 }, Eq(true)),
410                 _)
411         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
412                 SET_OUT_HEADER_LEN(out, setlkw);
413                 out->body.setlkw.lk = in->body.setlkw.lk;
414         })));
415
416         fd = open(FULLPATH, O_RDWR);
417         ASSERT_LE(0, fd) << strerror(errno);
418         fl.l_start = 10;
419         fl.l_len = 1000;
420         fl.l_pid = pid;
421         fl.l_type = F_RDLCK;
422         fl.l_whence = SEEK_SET;
423         fl.l_sysid = 0;
424         ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
425         /* Deliberately leak fd.  close(2) will be tested in release.cc */
426 }