]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/flush.cc
MFHead @345353
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / flush.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 #include <unistd.h>
34 }
35
36 #include "mockfs.hh"
37 #include "utils.hh"
38
39 using namespace testing;
40
41 class Flush: public FuseTest {
42
43 public:
44 void expect_flush(uint64_t ino, int times, pid_t lo, ProcessMockerT r)
45 {
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);
52                 }, Eq(true)),
53                 _)
54         ).Times(times)
55         .WillRepeatedly(Invoke(r));
56 }
57
58 void expect_lookup(const char *relpath, uint64_t ino)
59 {
60         FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
61 }
62
63 /*
64  * When testing FUSE_FLUSH, the FUSE_RELEASE calls are uninteresting.  This
65  * expectation will silence googlemock warnings
66  */
67 void expect_release()
68 {
69         EXPECT_CALL(*m_mock, process(
70                 ResultOf([=](auto in) {
71                         return (in->header.opcode == FUSE_RELEASE);
72                 }, Eq(true)),
73                 _)
74         ).WillRepeatedly(Invoke(ReturnErrno(0)));
75 }
76 };
77
78 class FlushWithLocks: public Flush {
79         virtual void SetUp() {
80                 m_init_flags = FUSE_POSIX_LOCKS;
81                 Flush::SetUp();
82         }
83 };
84
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)
88 {
89         const char FULLPATH[] = "mountpoint/some_file.txt";
90         const char RELPATH[] = "some_file.txt";
91         uint64_t ino = 42;
92         int fd, fd2;
93
94         expect_lookup(RELPATH, ino);
95         expect_open(ino, 0, 1);
96         expect_getattr(ino, 0);
97         expect_flush(ino, 2, 0, ReturnErrno(0));
98         expect_release();
99
100         fd = open(FULLPATH, O_WRONLY);
101         EXPECT_LE(0, fd) << strerror(errno);
102
103         fd2 = dup(fd);
104
105         ASSERT_EQ(0, close(fd2)) << strerror(errno);
106         ASSERT_EQ(0, close(fd)) << strerror(errno);
107 }
108
109 /*
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
114  * all.
115  */
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)
119 {
120         const char FULLPATH[] = "mountpoint/some_file.txt";
121         const char RELPATH[] = "some_file.txt";
122         uint64_t ino = 42;
123         int fd;
124
125         expect_lookup(RELPATH, ino);
126         expect_open(ino, 0, 1);
127         expect_getattr(ino, 0);
128         expect_flush(ino, 1, 0, ReturnErrno(EIO));
129         expect_release();
130
131         fd = open(FULLPATH, O_WRONLY);
132         EXPECT_LE(0, fd) << strerror(errno);
133
134         ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
135 }
136
137 /*
138  * If the filesystem returns ENOSYS, it will be treated as success and
139  * no more FUSE_FLUSH operations will be sent to the daemon
140  */
141 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
142 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
143 TEST_F(Flush, DISABLED_enosys)
144 {
145         const char FULLPATH[] = "mountpoint/some_file.txt";
146         const char RELPATH[] = "some_file.txt";
147         uint64_t ino = 42;
148         int fd, fd2;
149
150         expect_lookup(RELPATH, ino);
151         expect_open(ino, 0, 1);
152         expect_getattr(ino, 0);
153         /* On the 2nd close, FUSE_FLUSH won't be sent at all */
154         expect_flush(ino, 1, 0, ReturnErrno(ENOSYS));
155         expect_release();
156
157         fd = open(FULLPATH, O_WRONLY);
158         EXPECT_LE(0, fd) << strerror(errno);
159
160         fd2 = dup(fd);
161
162         EXPECT_EQ(0, close(fd2)) << strerror(errno);
163         EXPECT_EQ(0, close(fd)) << strerror(errno);
164 }
165
166 /* A FUSE_FLUSH should be sent on close(2) */
167 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
168 TEST_F(Flush, DISABLED_flush)
169 {
170         const char FULLPATH[] = "mountpoint/some_file.txt";
171         const char RELPATH[] = "some_file.txt";
172         uint64_t ino = 42;
173         int fd;
174
175         expect_lookup(RELPATH, ino);
176         expect_open(ino, 0, 1);
177         expect_getattr(ino, 0);
178         expect_flush(ino, 1, 0, ReturnErrno(0));
179         expect_release();
180
181         fd = open(FULLPATH, O_WRONLY);
182         EXPECT_LE(0, fd) << strerror(errno);
183
184         ASSERT_TRUE(0 == close(fd)) << strerror(errno);
185 }
186
187 /*
188  * When closing a file with a POSIX file lock, flush should release the lock,
189  * _even_if_ it's not the process's last file descriptor for this file.
190  */
191 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236405 */
192 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=234581 */
193 TEST_F(FlushWithLocks, DISABLED_unlock_on_close)
194 {
195         const char FULLPATH[] = "mountpoint/some_file.txt";
196         const char RELPATH[] = "some_file.txt";
197         uint64_t ino = 42;
198         int fd, fd2;
199         struct flock fl;
200         pid_t pid = getpid();
201
202         expect_lookup(RELPATH, ino);
203         expect_open(ino, 0, 1);
204         expect_getattr(ino, 0);
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.fh == FH);
210                 }, Eq(true)),
211                 _)
212         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
213                 SET_OUT_HEADER_LEN(out, setlk);
214                 out->body.setlk.lk = in->body.setlk.lk;
215         })));
216         expect_flush(ino, 1, pid, ReturnErrno(0));
217
218         fd = open(FULLPATH, O_RDWR);
219         ASSERT_LE(0, fd) << strerror(errno);
220         fl.l_start = 0;
221         fl.l_len = 0;
222         fl.l_pid = pid;
223         fl.l_type = F_RDLCK;
224         fl.l_whence = SEEK_SET;
225         fl.l_sysid = 0;
226         ASSERT_NE(-1, fcntl(fd, F_SETLKW, &fl)) << strerror(errno);
227
228         fd2 = dup(fd);
229         ASSERT_EQ(0, close(fd2)) << strerror(errno);
230         /* Deliberately leak fd */
231 }