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
41 * For testing I/O like fsx does, but deterministically and without a real
42 * underlying file system
44 * TODO: after fusefs gains the options to select cache mode for each mount
45 * point, run each of these tests for all cache modes.
48 using namespace testing;
50 const char FULLPATH[] = "mountpoint/some_file.txt";
51 const char RELPATH[] = "some_file.txt";
52 const uint64_t ino = 42;
54 class Io: public FuseTest {
56 int m_backing_fd, m_control_fd, m_test_fd;
58 Io(): m_backing_fd(-1), m_control_fd(-1) {};
62 m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
64 FAIL() << strerror(errno);
65 m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
67 FAIL() << strerror(errno);
68 srandom(22'9'1982); // Seed with my birthday
73 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
74 expect_open(ino, 0, 1);
75 EXPECT_CALL(*m_mock, process(
76 ResultOf([=](auto in) {
77 return (in.header.opcode == FUSE_WRITE &&
78 in.header.nodeid == ino);
81 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
82 const char *buf = (const char*)in.body.bytes +
83 sizeof(struct fuse_write_in);
84 ssize_t isize = in.body.write.size;
85 off_t iofs = in.body.write.offset;
87 ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
89 SET_OUT_HEADER_LEN(out, write);
90 out.body.write.size = isize;
92 EXPECT_CALL(*m_mock, process(
93 ResultOf([=](auto in) {
94 return (in.header.opcode == FUSE_READ &&
95 in.header.nodeid == ino);
98 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
99 ssize_t isize = in.body.write.size;
100 off_t iofs = in.body.write.offset;
101 void *buf = out.body.bytes;
103 ASSERT_LE(0, pread(m_backing_fd, buf, isize, iofs))
105 out.header.len = sizeof(struct fuse_out_header) + isize;
107 EXPECT_CALL(*m_mock, process(
108 ResultOf([=](auto in) {
109 uint32_t valid = FATTR_SIZE | FATTR_FH;
110 return (in.header.opcode == FUSE_SETATTR &&
111 in.header.nodeid == ino &&
112 in.body.setattr.valid == valid);
115 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
116 ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
118 SET_OUT_HEADER_LEN(out, attr);
119 out.body.attr.attr.ino = ino;
120 out.body.attr.attr.mode = S_IFREG | 0755;
121 out.body.attr.attr.size = in.body.setattr.size;
122 out.body.attr.attr_valid = UINT64_MAX;
124 /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
125 EXPECT_CALL(*m_mock, process(
126 ResultOf([=](auto in) {
127 return (in.header.opcode == FUSE_FLUSH &&
128 in.header.nodeid == ino);
131 ).WillRepeatedly(Invoke(ReturnErrno(0)));
132 EXPECT_CALL(*m_mock, process(
133 ResultOf([=](auto in) {
134 return (in.header.opcode == FUSE_RELEASE &&
135 in.header.nodeid == ino);
138 ).WillRepeatedly(Invoke(ReturnErrno(0)));
140 m_test_fd = open(FULLPATH, O_RDWR );
141 EXPECT_LE(0, m_test_fd) << strerror(errno);
146 if (m_backing_fd >= 0)
148 if (m_control_fd >= 0)
150 FuseTest::TearDown();
151 /* Deliberately leak test_fd */
154 void do_ftruncate(off_t offs)
156 ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
157 ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
160 void do_read(ssize_t size, off_t offs)
162 void *test_buf, *control_buf;
164 test_buf = malloc(size);
165 ASSERT_NE(NULL, test_buf) << strerror(errno);
166 control_buf = malloc(size);
167 ASSERT_NE(NULL, control_buf) << strerror(errno);
169 ASSERT_EQ(size, pread(m_test_fd, test_buf, size, offs))
171 ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
174 ASSERT_EQ(0, memcmp(test_buf, control_buf, size));
180 void do_write(ssize_t size, off_t offs)
185 buf = (char*)malloc(size);
186 ASSERT_NE(NULL, buf) << strerror(errno);
187 for (i=0; i < size; i++)
190 ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
192 ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
199 * Extend a file with dirty data in the last page of the last block.
201 * fsx -WR -P /tmp -S8 -N3 fsx.bin
203 TEST_F(Io, extend_from_dirty_page)
206 ssize_t wsize = 0xf0a8;
208 ssize_t rsize = 0x9b22;
209 off_t truncsize = 0x28702;
211 do_write(wsize, wofs);
212 do_ftruncate(truncsize);
213 do_read(rsize, rofs);
217 * When writing the last page of a file, it must be written synchronously.
218 * Otherwise the cached page can become invalid by a subsequent extend
221 * fsx -WR -P /tmp -S642 -N3 fsx.bin
223 TEST_F(Io, last_page)
225 off_t wofs0 = 0x1134f;
226 ssize_t wsize0 = 0xcc77;
227 off_t wofs1 = 0x2096a;
228 ssize_t wsize1 = 0xdfa7;
229 off_t rofs = 0x1a3aa;
230 ssize_t rsize = 0xb5b7;
232 do_write(wsize0, wofs0);
233 do_write(wsize1, wofs1);
234 do_read(rsize, rofs);
238 * Read a hole from a block that contains some cached data.
240 * fsx -WR -P /tmp -S55 fsx.bin
242 TEST_F(Io, read_hole_from_cached_block)
244 off_t wofs = 0x160c5;
245 ssize_t wsize = 0xa996;
247 ssize_t rsize = 0xd8d5;
249 do_write(wsize, wofs);
250 do_read(rsize, rofs);
254 * Truncating a file into a dirty buffer should not causing anything untoward
255 * to happen when that buffer is eventually flushed.
257 * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
259 TEST_F(Io, truncate_into_dirty_buffer)
261 off_t wofs0 = 0x3bad7;
262 ssize_t wsize0 = 0x4529;
263 off_t wofs1 = 0xc30d;
264 ssize_t wsize1 = 0x5f77;
265 off_t truncsize0 = 0x10916;
267 ssize_t rsize = 0x29ff;
268 off_t truncsize1 = 0x152b4;
270 do_write(wsize0, wofs0);
271 do_write(wsize1, wofs1);
272 do_ftruncate(truncsize0);
273 do_read(rsize, rofs);
274 do_ftruncate(truncsize1);
279 * Truncating a file into a dirty buffer should not causing anything untoward
280 * to happen when that buffer is eventually flushed, even when the buffer's
283 * Based on this command with a few steps removed:
284 * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
286 TEST_F(Io, truncate_into_dirty_buffer2)
288 off_t truncsize0 = 0x344f3;
289 off_t wofs = 0x2790c;
290 ssize_t wsize = 0xd86a;
291 off_t truncsize1 = 0x2de38;
292 off_t rofs2 = 0x1fd7a;
293 ssize_t rsize2 = 0xc594;
294 off_t truncsize2 = 0x31e71;
296 /* Sets the file size to something larger than the next write */
297 do_ftruncate(truncsize0);
299 * Creates a dirty buffer. The part in lbn 2 doesn't flush
302 do_write(wsize, wofs);
303 /* Truncates part of the dirty buffer created in step 2 */
304 do_ftruncate(truncsize1);
305 /* XXX ?I don't know why this is necessary? */
306 do_read(rsize2, rofs2);
307 /* Truncates the dirty buffer */
308 do_ftruncate(truncsize2);