1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 <aio.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 }
36
37 #include "mockfs.hh"
38 #include "utils.hh"
39
40 using namespace testing;
41
42 /*
43 * TODO: remove FUSE_FSYNC_FDATASYNC definition when upgrading to protocol 7.28.
44 * This bit was actually part of kernel protocol version 5.2, but never
45 * documented until after 7.28
46 */
47 #ifndef FUSE_FSYNC_FDATASYNC
48 #define FUSE_FSYNC_FDATASYNC 1
49 #endif
50
51 class Fsync: public FuseTest {
52 public:
expect_fsync(uint64_t ino,uint32_t flags,int error,int times=1)53 void expect_fsync(uint64_t ino, uint32_t flags, int error, int times = 1)
54 {
55 EXPECT_CALL(*m_mock, process(
56 ResultOf([=](auto in) {
57 return (in.header.opcode == FUSE_FSYNC &&
58 in.header.nodeid == ino &&
59 /*
60 * TODO: reenable pid check after fixing
61 * bug 236379
62 */
63 //(pid_t)in.header.pid == getpid() &&
64 in.body.fsync.fh == FH &&
65 in.body.fsync.fsync_flags == flags);
66 }, Eq(true)),
67 _)
68 ).Times(times)
69 .WillRepeatedly(Invoke(ReturnErrno(error)));
70 }
71
expect_lookup(const char * relpath,uint64_t ino,int times=1)72 void expect_lookup(const char *relpath, uint64_t ino, int times = 1)
73 {
74 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
75 }
76
expect_write(uint64_t ino,uint64_t size,const void * contents)77 void expect_write(uint64_t ino, uint64_t size, const void *contents)
78 {
79 FuseTest::expect_write(ino, 0, size, size, 0, 0, contents);
80 }
81
82 };
83
84 class AioFsync: public Fsync {
SetUp()85 virtual void SetUp() {
86 if (!is_unsafe_aio_enabled())
87 GTEST_SKIP() <<
88 "vfs.aio.enable_unsafe must be set for this test";
89 FuseTest::SetUp();
90 }
91 };
92
93 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
TEST_F(AioFsync,aio_fsync)94 TEST_F(AioFsync, aio_fsync)
95 {
96 const char FULLPATH[] = "mountpoint/some_file.txt";
97 const char RELPATH[] = "some_file.txt";
98 const char *CONTENTS = "abcdefgh";
99 ssize_t bufsize = strlen(CONTENTS);
100 uint64_t ino = 42;
101 struct aiocb iocb, *piocb;
102 int fd;
103
104 expect_lookup(RELPATH, ino);
105 expect_open(ino, 0, 1);
106 expect_write(ino, bufsize, CONTENTS);
107 expect_fsync(ino, 0, 0);
108
109 fd = open(FULLPATH, O_RDWR);
110 ASSERT_LE(0, fd) << strerror(errno);
111 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
112
113 bzero(&iocb, sizeof(iocb));
114 iocb.aio_fildes = fd;
115
116 ASSERT_EQ(0, aio_fsync(O_SYNC, &iocb)) << strerror(errno);
117 ASSERT_EQ(0, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
118
119 leak(fd);
120 }
121
122 /*
123 * fuse(4) should NOT fsync during VOP_RELEASE or VOP_INACTIVE
124 *
125 * This test only really make sense in writeback caching mode, but it should
126 * still pass in any cache mode.
127 */
TEST_F(Fsync,close)128 TEST_F(Fsync, close)
129 {
130 const char FULLPATH[] = "mountpoint/some_file.txt";
131 const char RELPATH[] = "some_file.txt";
132 const char *CONTENTS = "abcdefgh";
133 ssize_t bufsize = strlen(CONTENTS);
134 uint64_t ino = 42;
135 int fd;
136
137 expect_lookup(RELPATH, ino);
138 expect_open(ino, 0, 1);
139 expect_write(ino, bufsize, CONTENTS);
140 EXPECT_CALL(*m_mock, process(
141 ResultOf([=](auto in) {
142 return (in.header.opcode == FUSE_SETATTR);
143 }, Eq(true)),
144 _)
145 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
146 SET_OUT_HEADER_LEN(out, attr);
147 out.body.attr.attr.ino = ino; // Must match nodeid
148 })));
149 EXPECT_CALL(*m_mock, process(
150 ResultOf([=](auto in) {
151 return (in.header.opcode == FUSE_FSYNC);
152 }, Eq(true)),
153 _)
154 ).Times(0);
155 expect_flush(ino, 1, ReturnErrno(0));
156 expect_release(ino, FH);
157
158 fd = open(FULLPATH, O_RDWR);
159 ASSERT_LE(0, fd) << strerror(errno);
160 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
161 close(fd);
162 }
163
TEST_F(Fsync,eio)164 TEST_F(Fsync, eio)
165 {
166 const char FULLPATH[] = "mountpoint/some_file.txt";
167 const char RELPATH[] = "some_file.txt";
168 const char *CONTENTS = "abcdefgh";
169 ssize_t bufsize = strlen(CONTENTS);
170 uint64_t ino = 42;
171 int fd;
172
173 expect_lookup(RELPATH, ino);
174 expect_open(ino, 0, 1);
175 expect_write(ino, bufsize, CONTENTS);
176 expect_fsync(ino, FUSE_FSYNC_FDATASYNC, EIO);
177
178 fd = open(FULLPATH, O_RDWR);
179 ASSERT_LE(0, fd) << strerror(errno);
180 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
181 ASSERT_NE(0, fdatasync(fd));
182 ASSERT_EQ(EIO, errno);
183
184 leak(fd);
185 }
186
187 /*
188 * If the filesystem returns ENOSYS, it will be treated as success and
189 * subsequent calls to VOP_FSYNC will succeed automatically without being sent
190 * to the filesystem daemon
191 */
TEST_F(Fsync,enosys)192 TEST_F(Fsync, enosys)
193 {
194 const char FULLPATH[] = "mountpoint/some_file.txt";
195 const char RELPATH[] = "some_file.txt";
196 const char *CONTENTS = "abcdefgh";
197 ssize_t bufsize = strlen(CONTENTS);
198 uint64_t ino = 42;
199 int fd;
200
201 expect_lookup(RELPATH, ino);
202 expect_open(ino, 0, 1);
203 expect_write(ino, bufsize, CONTENTS);
204 expect_fsync(ino, FUSE_FSYNC_FDATASYNC, ENOSYS);
205
206 fd = open(FULLPATH, O_RDWR);
207 ASSERT_LE(0, fd) << strerror(errno);
208 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
209 EXPECT_EQ(0, fdatasync(fd));
210
211 /* Subsequent calls shouldn't query the daemon*/
212 EXPECT_EQ(0, fdatasync(fd));
213 leak(fd);
214 }
215
216
TEST_F(Fsync,fdatasync)217 TEST_F(Fsync, fdatasync)
218 {
219 const char FULLPATH[] = "mountpoint/some_file.txt";
220 const char RELPATH[] = "some_file.txt";
221 const char *CONTENTS = "abcdefgh";
222 ssize_t bufsize = strlen(CONTENTS);
223 uint64_t ino = 42;
224 int fd;
225
226 expect_lookup(RELPATH, ino);
227 expect_open(ino, 0, 1);
228 expect_write(ino, bufsize, CONTENTS);
229 expect_fsync(ino, FUSE_FSYNC_FDATASYNC, 0);
230
231 fd = open(FULLPATH, O_RDWR);
232 ASSERT_LE(0, fd) << strerror(errno);
233 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
234 ASSERT_EQ(0, fdatasync(fd)) << strerror(errno);
235
236 leak(fd);
237 }
238
TEST_F(Fsync,fsync)239 TEST_F(Fsync, fsync)
240 {
241 const char FULLPATH[] = "mountpoint/some_file.txt";
242 const char RELPATH[] = "some_file.txt";
243 const char *CONTENTS = "abcdefgh";
244 ssize_t bufsize = strlen(CONTENTS);
245 uint64_t ino = 42;
246 int fd;
247
248 expect_lookup(RELPATH, ino);
249 expect_open(ino, 0, 1);
250 expect_write(ino, bufsize, CONTENTS);
251 expect_fsync(ino, 0, 0);
252
253 fd = open(FULLPATH, O_RDWR);
254 ASSERT_LE(0, fd) << strerror(errno);
255 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
256 ASSERT_EQ(0, fsync(fd)) << strerror(errno);
257
258 leak(fd);
259 }
260
261 /* If multiple FUSE file handles are active, we must fsync them all */
TEST_F(Fsync,two_handles)262 TEST_F(Fsync, two_handles)
263 {
264 const char FULLPATH[] = "mountpoint/some_file.txt";
265 const char RELPATH[] = "some_file.txt";
266 const char *CONTENTS = "abcdefgh";
267 ssize_t bufsize = strlen(CONTENTS);
268 uint64_t ino = 42;
269 int fd1, fd2;
270
271 expect_lookup(RELPATH, ino, 2);
272 expect_open(ino, 0, 2);
273 expect_write(ino, bufsize, CONTENTS);
274 expect_fsync(ino, 0, 0, 2);
275
276 fd1 = open(FULLPATH, O_WRONLY);
277 ASSERT_LE(0, fd1) << strerror(errno);
278 fd2 = open(FULLPATH, O_RDONLY);
279 ASSERT_LE(0, fd2) << strerror(errno);
280 ASSERT_EQ(bufsize, write(fd1, CONTENTS, bufsize)) << strerror(errno);
281 ASSERT_EQ(0, fsync(fd1)) << strerror(errno);
282
283 leak(fd1);
284 leak(fd2);
285 }
286