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
34 #include <sys/types.h>
36 #include <sys/sysctl.h>
47 * For testing I/O like fsx does, but deterministically and without a real
48 * underlying file system
51 using namespace testing;
53 const char FULLPATH[] = "mountpoint/some_file.txt";
54 const char RELPATH[] = "some_file.txt";
55 const uint64_t ino = 42;
57 static void compare(const void *tbuf, const void *controlbuf, off_t baseofs,
62 for (i = 0; i < size; i++) {
63 if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) {
64 off_t ofs = baseofs + i;
65 FAIL() << "miscompare at offset "
71 << (unsigned)((const uint8_t*)controlbuf)[i]
73 << (unsigned)((const uint8_t*)tbuf)[i];
78 typedef tuple<bool, uint32_t, cache_mode> IoParam;
80 class Io: public FuseTest, public WithParamInterface<IoParam> {
82 int m_backing_fd, m_control_fd, m_test_fd;
86 Io(): m_backing_fd(-1), m_control_fd(-1), m_test_fd(-1), m_filesize(0),
87 m_direct_io(false) {};
91 m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
93 FAIL() << strerror(errno);
94 m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
96 FAIL() << strerror(errno);
97 srandom(22'9'1982); // Seed with my birthday
99 if (get<0>(GetParam()))
100 m_init_flags |= FUSE_ASYNC_READ;
101 m_maxwrite = get<1>(GetParam());
102 switch (get<2>(GetParam())) {
110 m_init_flags |= FUSE_WRITEBACK_CACHE;
115 FAIL() << "Unknown cache mode";
123 printf("Test Parameters: init_flags=%#x maxwrite=%#x "
124 "%sasync cache=%s\n",
125 m_init_flags, m_maxwrite, m_async? "" : "no",
126 cache_mode_to_s(get<2>(GetParam())));
129 expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
130 expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO : 0, 1);
131 EXPECT_CALL(*m_mock, process(
132 ResultOf([=](auto in) {
133 return (in.header.opcode == FUSE_WRITE &&
134 in.header.nodeid == ino);
137 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
138 const char *buf = (const char*)in.body.bytes +
139 sizeof(struct fuse_write_in);
140 ssize_t isize = in.body.write.size;
141 off_t iofs = in.body.write.offset;
143 ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
145 SET_OUT_HEADER_LEN(out, write);
146 out.body.write.size = isize;
148 EXPECT_CALL(*m_mock, process(
149 ResultOf([=](auto in) {
150 return (in.header.opcode == FUSE_READ &&
151 in.header.nodeid == ino);
154 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
155 ssize_t isize = in.body.write.size;
156 off_t iofs = in.body.write.offset;
157 void *buf = out.body.bytes;
160 osize = pread(m_backing_fd, buf, isize, iofs);
161 ASSERT_LE(0, osize) << strerror(errno);
162 out.header.len = sizeof(struct fuse_out_header) + osize;
164 EXPECT_CALL(*m_mock, process(
165 ResultOf([=](auto in) {
166 return (in.header.opcode == FUSE_SETATTR &&
167 in.header.nodeid == ino &&
168 (in.body.setattr.valid & FATTR_SIZE));
172 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
173 ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
175 SET_OUT_HEADER_LEN(out, attr);
176 out.body.attr.attr.ino = ino;
177 out.body.attr.attr.mode = S_IFREG | 0755;
178 out.body.attr.attr.size = in.body.setattr.size;
179 out.body.attr.attr_valid = UINT64_MAX;
181 /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
182 EXPECT_CALL(*m_mock, process(
183 ResultOf([=](auto in) {
184 return (in.header.opcode == FUSE_FLUSH &&
185 in.header.nodeid == ino);
188 ).WillRepeatedly(Invoke(ReturnErrno(0)));
189 EXPECT_CALL(*m_mock, process(
190 ResultOf([=](auto in) {
191 return (in.header.opcode == FUSE_RELEASE &&
192 in.header.nodeid == ino);
195 ).WillRepeatedly(Invoke(ReturnErrno(0)));
197 m_test_fd = open(FULLPATH, O_RDWR );
198 EXPECT_LE(0, m_test_fd) << strerror(errno);
205 if (m_backing_fd >= 0)
207 if (m_control_fd >= 0)
209 FuseTest::TearDown();
215 ASSERT_EQ(0, close(m_test_fd)) << strerror(errno);
216 m_test_fd = open("backing_file", O_RDWR);
217 ASSERT_LE(0, m_test_fd) << strerror(errno);
219 ASSERT_EQ(0, close(m_control_fd)) << strerror(errno);
220 m_control_fd = open("control", O_RDWR);
221 ASSERT_LE(0, m_control_fd) << strerror(errno);
224 void do_ftruncate(off_t offs)
226 ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
227 ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
231 void do_mapread(ssize_t size, off_t offs)
233 void *control_buf, *p;
234 off_t pg_offset, page_mask;
237 page_mask = getpagesize() - 1;
238 pg_offset = offs & page_mask;
239 map_size = pg_offset + size;
241 p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd,
243 ASSERT_NE(p, MAP_FAILED) << strerror(errno);
245 control_buf = malloc(size);
246 ASSERT_NE(nullptr, control_buf) << strerror(errno);
248 ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
251 compare((void*)((char*)p + pg_offset), control_buf, offs, size);
253 ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
257 void do_read(ssize_t size, off_t offs)
259 void *test_buf, *control_buf;
262 test_buf = malloc(size);
263 ASSERT_NE(nullptr, test_buf) << strerror(errno);
264 control_buf = malloc(size);
265 ASSERT_NE(nullptr, control_buf) << strerror(errno);
268 r = pread(m_test_fd, test_buf, size, offs);
269 ASSERT_NE(-1, r) << strerror(errno);
270 ASSERT_EQ(size, r) << "unexpected short read";
271 r = pread(m_control_fd, control_buf, size, offs);
272 ASSERT_NE(-1, r) << strerror(errno);
273 ASSERT_EQ(size, r) << "unexpected short read";
275 compare(test_buf, control_buf, offs, size);
281 void do_mapwrite(ssize_t size, off_t offs)
285 off_t pg_offset, page_mask;
289 page_mask = getpagesize() - 1;
290 pg_offset = offs & page_mask;
291 map_size = pg_offset + size;
293 buf = (char*)malloc(size);
294 ASSERT_NE(nullptr, buf) << strerror(errno);
295 for (i=0; i < size; i++)
298 if (offs + size > m_filesize) {
300 * Must manually extend. vm_mmap_vnode will not implicitly
303 do_ftruncate(offs + size);
306 p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
307 MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset);
308 ASSERT_NE(p, MAP_FAILED) << strerror(errno);
310 bcopy(buf, (char*)p + pg_offset, size);
311 ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
315 ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
318 void do_write(ssize_t size, off_t offs)
323 buf = (char*)malloc(size);
324 ASSERT_NE(nullptr, buf) << strerror(errno);
325 for (i=0; i < size; i++)
328 ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
330 ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
332 m_filesize = std::max(m_filesize, offs + size);
339 class IoCacheable: public Io {
341 virtual void SetUp() {
347 * Extend a file with dirty data in the last page of the last block.
349 * fsx -WR -P /tmp -S8 -N3 fsx.bin
351 TEST_P(Io, extend_from_dirty_page)
354 ssize_t wsize = 0xf0a8;
356 ssize_t rsize = 0x9b22;
357 off_t truncsize = 0x28702;
359 do_write(wsize, wofs);
360 do_ftruncate(truncsize);
361 do_read(rsize, rofs);
365 * mapwrite into a newly extended part of a file.
367 * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin
369 TEST_P(IoCacheable, extend_by_mapwrite)
371 do_mapwrite(0x849e, 0x29a3a); /* [0x29a3a, 0x31ed7] */
372 do_mapwrite(0x3994, 0x3c7d8); /* [0x3c7d8, 0x4016b] */
373 do_read(0xf556, 0x30c16); /* [0x30c16, 0x4016b] */
377 * When writing the last page of a file, it must be written synchronously.
378 * Otherwise the cached page can become invalid by a subsequent extend
381 * fsx -WR -P /tmp -S642 -N3 fsx.bin
383 TEST_P(Io, last_page)
385 do_write(0xcc77, 0x1134f); /* [0x1134f, 0x1dfc5] */
386 do_write(0xdfa7, 0x2096a); /* [0x2096a, 0x2e910] */
387 do_read(0xb5b7, 0x1a3aa); /* [0x1a3aa, 0x25960] */
391 * Read a hole using mmap
393 * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp -S14 fsx.bin
395 TEST_P(IoCacheable, mapread_hole)
397 do_write(0x123b7, 0xf205); /* [0xf205, 0x215bb] */
398 do_mapread(0xeeea, 0x2f4c); /* [0x2f4c, 0x11e35] */
402 * Read a hole from a block that contains some cached data.
404 * fsx -WR -P /tmp -S55 fsx.bin
406 TEST_P(Io, read_hole_from_cached_block)
408 off_t wofs = 0x160c5;
409 ssize_t wsize = 0xa996;
411 ssize_t rsize = 0xd8d5;
413 do_write(wsize, wofs);
414 do_read(rsize, rofs);
418 * Truncating a file into a dirty buffer should not causing anything untoward
419 * to happen when that buffer is eventually flushed.
421 * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
423 TEST_P(Io, truncate_into_dirty_buffer)
425 off_t wofs0 = 0x3bad7;
426 ssize_t wsize0 = 0x4529;
427 off_t wofs1 = 0xc30d;
428 ssize_t wsize1 = 0x5f77;
429 off_t truncsize0 = 0x10916;
431 ssize_t rsize = 0x29ff;
432 off_t truncsize1 = 0x152b4;
434 do_write(wsize0, wofs0);
435 do_write(wsize1, wofs1);
436 do_ftruncate(truncsize0);
437 do_read(rsize, rofs);
438 do_ftruncate(truncsize1);
443 * Truncating a file into a dirty buffer should not causing anything untoward
444 * to happen when that buffer is eventually flushed, even when the buffer's
447 * Based on this command with a few steps removed:
448 * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
450 TEST_P(Io, truncate_into_dirty_buffer2)
452 off_t truncsize0 = 0x344f3;
453 off_t wofs = 0x2790c;
454 ssize_t wsize = 0xd86a;
455 off_t truncsize1 = 0x2de38;
456 off_t rofs2 = 0x1fd7a;
457 ssize_t rsize2 = 0xc594;
458 off_t truncsize2 = 0x31e71;
460 /* Sets the file size to something larger than the next write */
461 do_ftruncate(truncsize0);
463 * Creates a dirty buffer. The part in lbn 2 doesn't flush
466 do_write(wsize, wofs);
467 /* Truncates part of the dirty buffer created in step 2 */
468 do_ftruncate(truncsize1);
469 /* XXX ?I don't know why this is necessary? */
470 do_read(rsize2, rofs2);
471 /* Truncates the dirty buffer */
472 do_ftruncate(truncsize2);
477 * Regression test for a bug introduced in r348931
479 * Sequence of operations:
480 * 1) The first write reads lbn so it can modify it
481 * 2) The first write flushes lbn 3 immediately because it's the end of file
482 * 3) The first write then flushes lbn 4 because it's the end of the file
483 * 4) The second write modifies the cached versions of lbn 3 and 4
484 * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's
485 * extending the buffer. Then it flushes lbn 4 because B_DELWRI was set but
487 * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the
488 * buffer into a weird write-only state. All read operations would return
489 * 0. Writes were apparently still processed, because the buffer's contents
490 * were correct when examined in a core dump.
491 * 7) The third write reads lbn 4 because cache is clear
492 * 9) uiomove dutifully copies new data into the buffer
493 * 10) The buffer's dirty is flushed to lbn 4
494 * 11) The read returns all zeros because of step 6.
497 * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q fsx.bin
499 TEST_P(Io, resize_a_valid_buffer_while_extending)
501 do_write(0x14530, 0x36ee6); /* [0x36ee6, 0x4b415] */
502 do_write(0x1507c, 0x33256); /* [0x33256, 0x482d1] */
503 do_write(0x175c, 0x4c03d); /* [0x4c03d, 0x4d798] */
504 do_read(0xe277, 0x3599c); /* [0x3599c, 0x43c12] */
508 INSTANTIATE_TEST_CASE_P(Io, Io,
509 Combine(Bool(), /* async read */
510 Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */
511 Values(Uncached, Writethrough, Writeback, WritebackAsync)
515 INSTANTIATE_TEST_CASE_P(Io, IoCacheable,
516 Combine(Bool(), /* async read */
517 Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */
518 Values(Writethrough, Writeback, WritebackAsync)