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