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
43 * For testing I/O like fsx does, but deterministically and without a real
44 * underlying file system
46 * TODO: after fusefs gains the options to select cache mode for each mount
47 * point, run each of these tests for all cache modes.
50 using namespace testing;
52 const char FULLPATH[] = "mountpoint/some_file.txt";
53 const char RELPATH[] = "some_file.txt";
54 const uint64_t ino = 42;
56 static void compare(const void *tbuf, const void *controlbuf, off_t baseofs,
61 for (i = 0; i < size; i++) {
62 if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) {
63 off_t ofs = baseofs + i;
64 FAIL() << "miscompare at offset "
70 << (unsigned)((const uint8_t*)controlbuf)[i]
72 << (unsigned)((const uint8_t*)tbuf)[i];
77 class Io: public FuseTest,
78 public WithParamInterface<tuple<uint32_t, uint32_t, bool>> {
80 int m_backing_fd, m_control_fd, m_test_fd;
83 Io(): m_backing_fd(-1), m_control_fd(-1) {};
88 m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
90 FAIL() << strerror(errno);
91 m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
93 FAIL() << strerror(errno);
94 srandom(22'9'1982); // Seed with my birthday
96 m_init_flags = get<0>(GetParam());
97 m_maxwrite = get<1>(GetParam());
98 m_async = get<2>(GetParam());
104 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
105 expect_open(ino, 0, 1);
106 EXPECT_CALL(*m_mock, process(
107 ResultOf([=](auto in) {
108 return (in.header.opcode == FUSE_WRITE &&
109 in.header.nodeid == ino);
112 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
113 const char *buf = (const char*)in.body.bytes +
114 sizeof(struct fuse_write_in);
115 ssize_t isize = in.body.write.size;
116 off_t iofs = in.body.write.offset;
118 ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
120 SET_OUT_HEADER_LEN(out, write);
121 out.body.write.size = isize;
123 EXPECT_CALL(*m_mock, process(
124 ResultOf([=](auto in) {
125 return (in.header.opcode == FUSE_READ &&
126 in.header.nodeid == ino);
129 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
130 ssize_t isize = in.body.write.size;
131 off_t iofs = in.body.write.offset;
132 void *buf = out.body.bytes;
135 osize = pread(m_backing_fd, buf, isize, iofs);
136 ASSERT_LE(0, osize) << strerror(errno);
137 out.header.len = sizeof(struct fuse_out_header) + osize;
139 EXPECT_CALL(*m_mock, process(
140 ResultOf([=](auto in) {
141 uint32_t valid = FATTR_SIZE | FATTR_FH;
142 return (in.header.opcode == FUSE_SETATTR &&
143 in.header.nodeid == ino &&
144 in.body.setattr.valid == valid);
147 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
148 ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
150 SET_OUT_HEADER_LEN(out, attr);
151 out.body.attr.attr.ino = ino;
152 out.body.attr.attr.mode = S_IFREG | 0755;
153 out.body.attr.attr.size = in.body.setattr.size;
154 out.body.attr.attr_valid = UINT64_MAX;
156 /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
157 EXPECT_CALL(*m_mock, process(
158 ResultOf([=](auto in) {
159 return (in.header.opcode == FUSE_FLUSH &&
160 in.header.nodeid == ino);
163 ).WillRepeatedly(Invoke(ReturnErrno(0)));
164 EXPECT_CALL(*m_mock, process(
165 ResultOf([=](auto in) {
166 return (in.header.opcode == FUSE_RELEASE &&
167 in.header.nodeid == ino);
170 ).WillRepeatedly(Invoke(ReturnErrno(0)));
172 m_test_fd = open(FULLPATH, O_RDWR );
173 EXPECT_LE(0, m_test_fd) << strerror(errno);
178 if (m_backing_fd >= 0)
180 if (m_control_fd >= 0)
182 FuseTest::TearDown();
183 /* Deliberately leak test_fd */
186 void do_ftruncate(off_t offs)
188 ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
189 ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
193 void do_mapread(ssize_t size, off_t offs)
195 void *control_buf, *p;
196 off_t pg_offset, page_mask;
199 page_mask = getpagesize() - 1;
200 pg_offset = offs & page_mask;
201 map_size = pg_offset + size;
203 p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd,
205 ASSERT_NE(p, MAP_FAILED) << strerror(errno);
207 control_buf = malloc(size);
208 ASSERT_NE(NULL, control_buf) << strerror(errno);
210 ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
213 compare((void*)((char*)p + pg_offset), control_buf, offs, size);
215 ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
219 void do_read(ssize_t size, off_t offs)
221 void *test_buf, *control_buf;
224 test_buf = malloc(size);
225 ASSERT_NE(NULL, test_buf) << strerror(errno);
226 control_buf = malloc(size);
227 ASSERT_NE(NULL, control_buf) << strerror(errno);
230 r = pread(m_test_fd, test_buf, size, offs);
231 ASSERT_NE(-1, r) << strerror(errno);
232 ASSERT_EQ(size, r) << "unexpected short read";
233 r = pread(m_control_fd, control_buf, size, offs);
234 ASSERT_NE(-1, r) << strerror(errno);
235 ASSERT_EQ(size, r) << "unexpected short read";
237 compare(test_buf, control_buf, offs, size);
243 void do_mapwrite(ssize_t size, off_t offs)
247 off_t pg_offset, page_mask;
251 page_mask = getpagesize() - 1;
252 pg_offset = offs & page_mask;
253 map_size = pg_offset + size;
255 buf = (char*)malloc(size);
256 ASSERT_NE(NULL, buf) << strerror(errno);
257 for (i=0; i < size; i++)
260 if (offs + size > m_filesize) {
262 * Must manually extend. vm_mmap_vnode will not implicitly
265 do_ftruncate(offs + size);
268 p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
269 MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset);
270 ASSERT_NE(p, MAP_FAILED) << strerror(errno);
272 bcopy(buf, (char*)p + pg_offset, size);
273 ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
277 ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
280 void do_write(ssize_t size, off_t offs)
285 buf = (char*)malloc(size);
286 ASSERT_NE(NULL, buf) << strerror(errno);
287 for (i=0; i < size; i++)
290 ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
292 ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
294 m_filesize = std::max(m_filesize, offs + size);
302 * Extend a file with dirty data in the last page of the last block.
304 * fsx -WR -P /tmp -S8 -N3 fsx.bin
306 TEST_P(Io, extend_from_dirty_page)
309 ssize_t wsize = 0xf0a8;
311 ssize_t rsize = 0x9b22;
312 off_t truncsize = 0x28702;
314 do_write(wsize, wofs);
315 do_ftruncate(truncsize);
316 do_read(rsize, rofs);
320 * mapwrite into a newly extended part of a file.
322 * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin
324 TEST_P(Io, extend_by_mapwrite)
326 do_mapwrite(0x849e, 0x29a3a); /* [0x29a3a, 0x31ed7] */
327 do_mapwrite(0x3994, 0x3c7d8); /* [0x3c7d8, 0x4016b] */
328 do_read(0xf556, 0x30c16); /* [0x30c16, 0x4016b] */
332 * When writing the last page of a file, it must be written synchronously.
333 * Otherwise the cached page can become invalid by a subsequent extend
336 * fsx -WR -P /tmp -S642 -N3 fsx.bin
338 TEST_P(Io, last_page)
340 do_write(0xcc77, 0x1134f); /* [0x1134f, 0x1dfc5] */
341 do_write(0xdfa7, 0x2096a); /* [0x2096a, 0x2e910] */
342 do_read(0xb5b7, 0x1a3aa); /* [0x1a3aa, 0x25960] */
346 * Read a hole using mmap
348 * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp -S14 fsx.bin
350 TEST_P(Io, mapread_hole)
352 do_write(0x123b7, 0xf205); /* [0xf205, 0x215bb] */
353 do_mapread(0xeeea, 0x2f4c); /* [0x2f4c, 0x11e35] */
357 * Read a hole from a block that contains some cached data.
359 * fsx -WR -P /tmp -S55 fsx.bin
361 TEST_P(Io, read_hole_from_cached_block)
363 off_t wofs = 0x160c5;
364 ssize_t wsize = 0xa996;
366 ssize_t rsize = 0xd8d5;
368 do_write(wsize, wofs);
369 do_read(rsize, rofs);
373 * Truncating a file into a dirty buffer should not causing anything untoward
374 * to happen when that buffer is eventually flushed.
376 * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
378 TEST_P(Io, truncate_into_dirty_buffer)
380 off_t wofs0 = 0x3bad7;
381 ssize_t wsize0 = 0x4529;
382 off_t wofs1 = 0xc30d;
383 ssize_t wsize1 = 0x5f77;
384 off_t truncsize0 = 0x10916;
386 ssize_t rsize = 0x29ff;
387 off_t truncsize1 = 0x152b4;
389 do_write(wsize0, wofs0);
390 do_write(wsize1, wofs1);
391 do_ftruncate(truncsize0);
392 do_read(rsize, rofs);
393 do_ftruncate(truncsize1);
398 * Truncating a file into a dirty buffer should not causing anything untoward
399 * to happen when that buffer is eventually flushed, even when the buffer's
402 * Based on this command with a few steps removed:
403 * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
405 TEST_P(Io, truncate_into_dirty_buffer2)
407 off_t truncsize0 = 0x344f3;
408 off_t wofs = 0x2790c;
409 ssize_t wsize = 0xd86a;
410 off_t truncsize1 = 0x2de38;
411 off_t rofs2 = 0x1fd7a;
412 ssize_t rsize2 = 0xc594;
413 off_t truncsize2 = 0x31e71;
415 /* Sets the file size to something larger than the next write */
416 do_ftruncate(truncsize0);
418 * Creates a dirty buffer. The part in lbn 2 doesn't flush
421 do_write(wsize, wofs);
422 /* Truncates part of the dirty buffer created in step 2 */
423 do_ftruncate(truncsize1);
424 /* XXX ?I don't know why this is necessary? */
425 do_read(rsize2, rofs2);
426 /* Truncates the dirty buffer */
427 do_ftruncate(truncsize2);
432 * Regression test for a bug introduced in r348931
434 * Sequence of operations:
435 * 1) The first write reads lbn so it can modify it
436 * 2) The first write flushes lbn 3 immediately because it's the end of file
437 * 3) The first write then flushes lbn 4 because it's the end of the file
438 * 4) The second write modifies the cached versions of lbn 3 and 4
439 * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's
440 * extending the buffer. Then it flushes lbn 4 because B_DELWRI was set but
442 * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the
443 * buffer into a weird write-only state. All read operations would return
444 * 0. Writes were apparently still processed, because the buffer's contents
445 * were correct when examined in a core dump.
446 * 7) The third write reads lbn 4 because cache is clear
447 * 9) uiomove dutifully copies new data into the buffer
448 * 10) The buffer's dirty is flushed to lbn 4
449 * 11) The read returns all zeros because of step 6.
452 * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q fsx.bin
454 TEST_P(Io, resize_a_valid_buffer_while_extending)
456 do_write(0x14530, 0x36ee6); /* [0x36ee6, 0x4b415] */
457 do_write(0x1507c, 0x33256); /* [0x33256, 0x482d1] */
458 do_write(0x175c, 0x4c03d); /* [0x4c03d, 0x4d798] */
459 do_read(0xe277, 0x3599c); /* [0x3599c, 0x43c12] */
463 INSTANTIATE_TEST_CASE_P(Io, Io,
464 Combine(Values(0, FUSE_ASYNC_READ), /* m_init_flags */
465 Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */
466 Bool())); /* m_async */