1a87e0831SAlan Somers /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3a87e0831SAlan Somers * 4a87e0831SAlan Somers * Copyright (c) 2019 The FreeBSD Foundation 5a87e0831SAlan Somers * 6a87e0831SAlan Somers * This software was developed by BFF Storage Systems, LLC under sponsorship 7a87e0831SAlan Somers * from the FreeBSD Foundation. 8a87e0831SAlan Somers * 9a87e0831SAlan Somers * Redistribution and use in source and binary forms, with or without 10a87e0831SAlan Somers * modification, are permitted provided that the following conditions 11a87e0831SAlan Somers * are met: 12a87e0831SAlan Somers * 1. Redistributions of source code must retain the above copyright 13a87e0831SAlan Somers * notice, this list of conditions and the following disclaimer. 14a87e0831SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright 15a87e0831SAlan Somers * notice, this list of conditions and the following disclaimer in the 16a87e0831SAlan Somers * documentation and/or other materials provided with the distribution. 17a87e0831SAlan Somers * 18a87e0831SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19a87e0831SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20a87e0831SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21a87e0831SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22a87e0831SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23a87e0831SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24a87e0831SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a87e0831SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26a87e0831SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27a87e0831SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28a87e0831SAlan Somers * SUCH DAMAGE. 29a87e0831SAlan Somers */ 30a87e0831SAlan Somers 31a87e0831SAlan Somers extern "C" { 3248417ae0SAlan Somers #include <sys/types.h> 3317575badSAlan Somers #include <sys/mman.h> 3417575badSAlan Somers 35a87e0831SAlan Somers #include <fcntl.h> 36a87e0831SAlan Somers #include <stdlib.h> 37a87e0831SAlan Somers #include <unistd.h> 38a87e0831SAlan Somers } 39a87e0831SAlan Somers 40*0077477fSEnji Cooper #include <iomanip> 41*0077477fSEnji Cooper 42a87e0831SAlan Somers #include "mockfs.hh" 43a87e0831SAlan Somers #include "utils.hh" 44a87e0831SAlan Somers 45a87e0831SAlan Somers /* 46a87e0831SAlan Somers * For testing I/O like fsx does, but deterministically and without a real 47a87e0831SAlan Somers * underlying file system 48a87e0831SAlan Somers */ 49a87e0831SAlan Somers 50a87e0831SAlan Somers using namespace testing; 51a87e0831SAlan Somers 52a87e0831SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt"; 53a87e0831SAlan Somers const char RELPATH[] = "some_file.txt"; 54a87e0831SAlan Somers const uint64_t ino = 42; 55a87e0831SAlan Somers 56dff3a6b4SAlan Somers static void compare(const void *tbuf, const void *controlbuf, off_t baseofs, 57dff3a6b4SAlan Somers ssize_t size) 58dff3a6b4SAlan Somers { 59dff3a6b4SAlan Somers int i; 60dff3a6b4SAlan Somers 61dff3a6b4SAlan Somers for (i = 0; i < size; i++) { 62dff3a6b4SAlan Somers if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) { 63dff3a6b4SAlan Somers off_t ofs = baseofs + i; 64dff3a6b4SAlan Somers FAIL() << "miscompare at offset " 65dff3a6b4SAlan Somers << std::hex 66dff3a6b4SAlan Somers << std::showbase 67dff3a6b4SAlan Somers << ofs 68dff3a6b4SAlan Somers << ". expected = " 69dff3a6b4SAlan Somers << std::setw(2) 70dff3a6b4SAlan Somers << (unsigned)((const uint8_t*)controlbuf)[i] 71dff3a6b4SAlan Somers << " got = " 72dff3a6b4SAlan Somers << (unsigned)((const uint8_t*)tbuf)[i]; 73dff3a6b4SAlan Somers } 74dff3a6b4SAlan Somers } 75dff3a6b4SAlan Somers } 76dff3a6b4SAlan Somers 771c909c30SAlan Somers typedef tuple<bool, uint32_t, cache_mode, uint32_t> IoParam; 78c51f519bSAlan Somers 79c51f519bSAlan Somers class Io: public FuseTest, public WithParamInterface<IoParam> { 80a87e0831SAlan Somers public: 81a87e0831SAlan Somers int m_backing_fd, m_control_fd, m_test_fd; 8217575badSAlan Somers off_t m_filesize; 83c51f519bSAlan Somers bool m_direct_io; 84a87e0831SAlan Somers 859c9634d1SAlan Somers Io(): m_backing_fd(-1), m_control_fd(-1), m_test_fd(-1), m_filesize(0), 869c9634d1SAlan Somers m_direct_io(false) {}; 87a87e0831SAlan Somers 88a87e0831SAlan Somers void SetUp() 89a87e0831SAlan Somers { 9093c0c1d4SAlan Somers m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644); 91a87e0831SAlan Somers if (m_backing_fd < 0) 92a87e0831SAlan Somers FAIL() << strerror(errno); 9393c0c1d4SAlan Somers m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644); 94a87e0831SAlan Somers if (m_control_fd < 0) 95a87e0831SAlan Somers FAIL() << strerror(errno); 96a87e0831SAlan Somers srandom(22'9'1982); // Seed with my birthday 970482ec3eSAlan Somers 98f8ebf1cdSAlan Somers if (get<0>(GetParam())) 99f8ebf1cdSAlan Somers m_init_flags |= FUSE_ASYNC_READ; 1000482ec3eSAlan Somers m_maxwrite = get<1>(GetParam()); 101c51f519bSAlan Somers switch (get<2>(GetParam())) { 102c51f519bSAlan Somers case Uncached: 103c51f519bSAlan Somers m_direct_io = true; 104c51f519bSAlan Somers break; 105c51f519bSAlan Somers case WritebackAsync: 106c51f519bSAlan Somers m_async = true; 107c51f519bSAlan Somers /* FALLTHROUGH */ 108c51f519bSAlan Somers case Writeback: 109f8ebf1cdSAlan Somers m_init_flags |= FUSE_WRITEBACK_CACHE; 110c51f519bSAlan Somers /* FALLTHROUGH */ 111c51f519bSAlan Somers case Writethrough: 112c51f519bSAlan Somers break; 113c51f519bSAlan Somers default: 114c51f519bSAlan Somers FAIL() << "Unknown cache mode"; 115c51f519bSAlan Somers } 1161c909c30SAlan Somers m_kernel_minor_version = get<3>(GetParam()); 11791972cfcSAlan Somers m_noatime = true; // To prevent SETATTR for atime on close 1180482ec3eSAlan Somers 119a87e0831SAlan Somers FuseTest::SetUp(); 120a87e0831SAlan Somers if (IsSkipped()) 121a87e0831SAlan Somers return; 122a87e0831SAlan Somers 123c51f519bSAlan Somers if (verbosity > 0) { 124c51f519bSAlan Somers printf("Test Parameters: init_flags=%#x maxwrite=%#x " 1251c909c30SAlan Somers "%sasync cache=%s kernel_minor_version=%d\n", 126c51f519bSAlan Somers m_init_flags, m_maxwrite, m_async? "" : "no", 1271c909c30SAlan Somers cache_mode_to_s(get<2>(GetParam())), 1281c909c30SAlan Somers m_kernel_minor_version); 129c51f519bSAlan Somers } 130c51f519bSAlan Somers 131a87e0831SAlan Somers expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1); 132c51f519bSAlan Somers expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO : 0, 1); 133a87e0831SAlan Somers EXPECT_CALL(*m_mock, process( 134a87e0831SAlan Somers ResultOf([=](auto in) { 135a87e0831SAlan Somers return (in.header.opcode == FUSE_WRITE && 136a87e0831SAlan Somers in.header.nodeid == ino); 137a87e0831SAlan Somers }, Eq(true)), 138a87e0831SAlan Somers _) 139a87e0831SAlan Somers ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 140a87e0831SAlan Somers const char *buf = (const char*)in.body.bytes + 141a87e0831SAlan Somers sizeof(struct fuse_write_in); 142a87e0831SAlan Somers ssize_t isize = in.body.write.size; 143a87e0831SAlan Somers off_t iofs = in.body.write.offset; 144a87e0831SAlan Somers 1450c9df4afSAlan Somers assert((size_t)isize <= sizeof(in.body.bytes) - 1460c9df4afSAlan Somers sizeof(struct fuse_write_in)); 147a87e0831SAlan Somers ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs)) 148a87e0831SAlan Somers << strerror(errno); 149a87e0831SAlan Somers SET_OUT_HEADER_LEN(out, write); 150a87e0831SAlan Somers out.body.write.size = isize; 151a87e0831SAlan Somers }))); 152a87e0831SAlan Somers EXPECT_CALL(*m_mock, process( 153a87e0831SAlan Somers ResultOf([=](auto in) { 154a87e0831SAlan Somers return (in.header.opcode == FUSE_READ && 155a87e0831SAlan Somers in.header.nodeid == ino); 156a87e0831SAlan Somers }, Eq(true)), 157a87e0831SAlan Somers _) 158a87e0831SAlan Somers ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 159a87e0831SAlan Somers ssize_t isize = in.body.write.size; 160a87e0831SAlan Somers off_t iofs = in.body.write.offset; 161a87e0831SAlan Somers void *buf = out.body.bytes; 16217575badSAlan Somers ssize_t osize; 163a87e0831SAlan Somers 1640c9df4afSAlan Somers assert((size_t)isize <= sizeof(out.body.bytes)); 16517575badSAlan Somers osize = pread(m_backing_fd, buf, isize, iofs); 16617575badSAlan Somers ASSERT_LE(0, osize) << strerror(errno); 16717575badSAlan Somers out.header.len = sizeof(struct fuse_out_header) + osize; 168a87e0831SAlan Somers }))); 169a87e0831SAlan Somers EXPECT_CALL(*m_mock, process( 170a87e0831SAlan Somers ResultOf([=](auto in) { 171a87e0831SAlan Somers return (in.header.opcode == FUSE_SETATTR && 172a87e0831SAlan Somers in.header.nodeid == ino && 173788af953SAlan Somers (in.body.setattr.valid & FATTR_SIZE)); 174788af953SAlan Somers 175a87e0831SAlan Somers }, Eq(true)), 176a87e0831SAlan Somers _) 177a87e0831SAlan Somers ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 178a87e0831SAlan Somers ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size)) 179a87e0831SAlan Somers << strerror(errno); 180a87e0831SAlan Somers SET_OUT_HEADER_LEN(out, attr); 181a87e0831SAlan Somers out.body.attr.attr.ino = ino; 182a87e0831SAlan Somers out.body.attr.attr.mode = S_IFREG | 0755; 183a87e0831SAlan Somers out.body.attr.attr.size = in.body.setattr.size; 184a87e0831SAlan Somers out.body.attr.attr_valid = UINT64_MAX; 185a87e0831SAlan Somers }))); 186a87e0831SAlan Somers /* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */ 187a87e0831SAlan Somers EXPECT_CALL(*m_mock, process( 188a87e0831SAlan Somers ResultOf([=](auto in) { 189a87e0831SAlan Somers return (in.header.opcode == FUSE_FLUSH && 190a87e0831SAlan Somers in.header.nodeid == ino); 191a87e0831SAlan Somers }, Eq(true)), 192a87e0831SAlan Somers _) 193a87e0831SAlan Somers ).WillRepeatedly(Invoke(ReturnErrno(0))); 194a87e0831SAlan Somers EXPECT_CALL(*m_mock, process( 195a87e0831SAlan Somers ResultOf([=](auto in) { 196a87e0831SAlan Somers return (in.header.opcode == FUSE_RELEASE && 197a87e0831SAlan Somers in.header.nodeid == ino); 198a87e0831SAlan Somers }, Eq(true)), 199a87e0831SAlan Somers _) 200a87e0831SAlan Somers ).WillRepeatedly(Invoke(ReturnErrno(0))); 2011c909c30SAlan Somers EXPECT_CALL(*m_mock, process( 2021c909c30SAlan Somers ResultOf([=](auto in) { 2031c909c30SAlan Somers return (in.header.opcode == FUSE_COPY_FILE_RANGE && 2041c909c30SAlan Somers in.header.nodeid == ino && 2051c909c30SAlan Somers in.body.copy_file_range.nodeid_out == ino && 2061c909c30SAlan Somers in.body.copy_file_range.flags == 0); 2071c909c30SAlan Somers }, Eq(true)), 2081c909c30SAlan Somers _) 2091c909c30SAlan Somers ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 2101c909c30SAlan Somers off_t off_in = in.body.copy_file_range.off_in; 2111c909c30SAlan Somers off_t off_out = in.body.copy_file_range.off_out; 2121c909c30SAlan Somers ASSERT_EQ((ssize_t)in.body.copy_file_range.len, 2131c909c30SAlan Somers copy_file_range(m_backing_fd, &off_in, m_backing_fd, 2141c909c30SAlan Somers &off_out, in.body.copy_file_range.len, 0)); 2151c909c30SAlan Somers SET_OUT_HEADER_LEN(out, write); 2161c909c30SAlan Somers out.body.write.size = in.body.copy_file_range.len; 2171c909c30SAlan Somers }))); 2181c909c30SAlan Somers /* Claim that we don't support FUSE_LSEEK */ 2191c909c30SAlan Somers EXPECT_CALL(*m_mock, process( 2201c909c30SAlan Somers ResultOf([=](auto in) { 2211c909c30SAlan Somers return (in.header.opcode == FUSE_LSEEK); 2221c909c30SAlan Somers }, Eq(true)), 2231c909c30SAlan Somers _) 2241c909c30SAlan Somers ).WillRepeatedly(Invoke(ReturnErrno(ENOSYS))); 225a87e0831SAlan Somers 226a87e0831SAlan Somers m_test_fd = open(FULLPATH, O_RDWR ); 227a87e0831SAlan Somers EXPECT_LE(0, m_test_fd) << strerror(errno); 228a87e0831SAlan Somers } 229a87e0831SAlan Somers 230a87e0831SAlan Somers void TearDown() 231a87e0831SAlan Somers { 23248417ae0SAlan Somers if (m_test_fd >= 0) 23348417ae0SAlan Somers close(m_test_fd); 234a87e0831SAlan Somers if (m_backing_fd >= 0) 235a87e0831SAlan Somers close(m_backing_fd); 236a87e0831SAlan Somers if (m_control_fd >= 0) 237a87e0831SAlan Somers close(m_control_fd); 238a87e0831SAlan Somers FuseTest::TearDown(); 2397fc0921dSAlan Somers leak(m_test_fd); 240a87e0831SAlan Somers } 241a87e0831SAlan Somers 24248417ae0SAlan Somers void do_closeopen() 24348417ae0SAlan Somers { 24448417ae0SAlan Somers ASSERT_EQ(0, close(m_test_fd)) << strerror(errno); 24548417ae0SAlan Somers m_test_fd = open("backing_file", O_RDWR); 24648417ae0SAlan Somers ASSERT_LE(0, m_test_fd) << strerror(errno); 24748417ae0SAlan Somers 24848417ae0SAlan Somers ASSERT_EQ(0, close(m_control_fd)) << strerror(errno); 24948417ae0SAlan Somers m_control_fd = open("control", O_RDWR); 25048417ae0SAlan Somers ASSERT_LE(0, m_control_fd) << strerror(errno); 25148417ae0SAlan Somers } 25248417ae0SAlan Somers 2531c909c30SAlan Somers void do_copy_file_range(off_t off_in, off_t off_out, size_t size) 2541c909c30SAlan Somers { 2551c909c30SAlan Somers ssize_t r; 2561c909c30SAlan Somers off_t test_off_in = off_in; 2571c909c30SAlan Somers off_t test_off_out = off_out; 2581c909c30SAlan Somers off_t test_size = size; 2591c909c30SAlan Somers off_t control_off_in = off_in; 2601c909c30SAlan Somers off_t control_off_out = off_out; 2611c909c30SAlan Somers off_t control_size = size; 2621c909c30SAlan Somers 2631c909c30SAlan Somers while (test_size > 0) { 2641c909c30SAlan Somers r = copy_file_range(m_test_fd, &test_off_in, m_test_fd, 2651c909c30SAlan Somers &test_off_out, test_size, 0); 2661c909c30SAlan Somers ASSERT_GT(r, 0) << strerror(errno); 2671c909c30SAlan Somers test_size -= r; 2681c909c30SAlan Somers } 2691c909c30SAlan Somers while (control_size > 0) { 2701c909c30SAlan Somers r = copy_file_range(m_control_fd, &control_off_in, m_control_fd, 2711c909c30SAlan Somers &control_off_out, control_size, 0); 2721c909c30SAlan Somers ASSERT_GT(r, 0) << strerror(errno); 2731c909c30SAlan Somers control_size -= r; 2741c909c30SAlan Somers } 2751c909c30SAlan Somers m_filesize = std::max(m_filesize, off_out + (off_t)size); 2761c909c30SAlan Somers } 2771c909c30SAlan Somers 278a87e0831SAlan Somers void do_ftruncate(off_t offs) 279a87e0831SAlan Somers { 280a87e0831SAlan Somers ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno); 281a87e0831SAlan Somers ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno); 28217575badSAlan Somers m_filesize = offs; 28317575badSAlan Somers } 28417575badSAlan Somers 285daf26f93SAlan Somers void do_mapread(off_t offs, ssize_t size) 28617575badSAlan Somers { 2878bae22bbSAlan Somers char *control_buf; 2888bae22bbSAlan Somers void *p; 28917575badSAlan Somers off_t pg_offset, page_mask; 29017575badSAlan Somers size_t map_size; 29117575badSAlan Somers 29217575badSAlan Somers page_mask = getpagesize() - 1; 29317575badSAlan Somers pg_offset = offs & page_mask; 29417575badSAlan Somers map_size = pg_offset + size; 29517575badSAlan Somers 29617575badSAlan Somers p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd, 29717575badSAlan Somers offs - pg_offset); 29817575badSAlan Somers ASSERT_NE(p, MAP_FAILED) << strerror(errno); 29917575badSAlan Somers 3008bae22bbSAlan Somers control_buf = new char[size]; 30117575badSAlan Somers 30217575badSAlan Somers ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs)) 30317575badSAlan Somers << strerror(errno); 30417575badSAlan Somers 30517575badSAlan Somers compare((void*)((char*)p + pg_offset), control_buf, offs, size); 30617575badSAlan Somers 30717575badSAlan Somers ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno); 3088bae22bbSAlan Somers delete[] control_buf; 309a87e0831SAlan Somers } 310a87e0831SAlan Somers 311daf26f93SAlan Somers void do_read(off_t offs, ssize_t size) 312a87e0831SAlan Somers { 3138bae22bbSAlan Somers char *test_buf, *control_buf; 31417575badSAlan Somers ssize_t r; 315a87e0831SAlan Somers 3168bae22bbSAlan Somers test_buf = new char[size]; 3178bae22bbSAlan Somers control_buf = new char[size]; 318a87e0831SAlan Somers 31917575badSAlan Somers errno = 0; 32017575badSAlan Somers r = pread(m_test_fd, test_buf, size, offs); 32117575badSAlan Somers ASSERT_NE(-1, r) << strerror(errno); 32217575badSAlan Somers ASSERT_EQ(size, r) << "unexpected short read"; 32317575badSAlan Somers r = pread(m_control_fd, control_buf, size, offs); 32417575badSAlan Somers ASSERT_NE(-1, r) << strerror(errno); 32517575badSAlan Somers ASSERT_EQ(size, r) << "unexpected short read"; 326a87e0831SAlan Somers 327dff3a6b4SAlan Somers compare(test_buf, control_buf, offs, size); 328a87e0831SAlan Somers 3298bae22bbSAlan Somers delete[] control_buf; 3308bae22bbSAlan Somers delete[] test_buf; 331a87e0831SAlan Somers } 332a87e0831SAlan Somers 333daf26f93SAlan Somers void do_mapwrite(off_t offs, ssize_t size) 33417575badSAlan Somers { 33517575badSAlan Somers char *buf; 33617575badSAlan Somers void *p; 33717575badSAlan Somers off_t pg_offset, page_mask; 33817575badSAlan Somers size_t map_size; 33917575badSAlan Somers long i; 34017575badSAlan Somers 34117575badSAlan Somers page_mask = getpagesize() - 1; 34217575badSAlan Somers pg_offset = offs & page_mask; 34317575badSAlan Somers map_size = pg_offset + size; 34417575badSAlan Somers 3458bae22bbSAlan Somers buf = new char[size]; 34617575badSAlan Somers for (i=0; i < size; i++) 34717575badSAlan Somers buf[i] = random(); 34817575badSAlan Somers 34917575badSAlan Somers if (offs + size > m_filesize) { 35017575badSAlan Somers /* 35117575badSAlan Somers * Must manually extend. vm_mmap_vnode will not implicitly 35217575badSAlan Somers * extend a vnode 35317575badSAlan Somers */ 35417575badSAlan Somers do_ftruncate(offs + size); 35517575badSAlan Somers } 35617575badSAlan Somers 35717575badSAlan Somers p = mmap(NULL, map_size, PROT_READ | PROT_WRITE, 35817575badSAlan Somers MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset); 35917575badSAlan Somers ASSERT_NE(p, MAP_FAILED) << strerror(errno); 36017575badSAlan Somers 36117575badSAlan Somers bcopy(buf, (char*)p + pg_offset, size); 36217575badSAlan Somers ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs)) 36317575badSAlan Somers << strerror(errno); 36417575badSAlan Somers 3658bae22bbSAlan Somers delete[] buf; 36617575badSAlan Somers ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno); 36717575badSAlan Somers } 36817575badSAlan Somers 369daf26f93SAlan Somers void do_write(off_t offs, ssize_t size) 370a87e0831SAlan Somers { 371a87e0831SAlan Somers char *buf; 372a87e0831SAlan Somers long i; 373a87e0831SAlan Somers 3748bae22bbSAlan Somers buf = new char[size]; 375a87e0831SAlan Somers for (i=0; i < size; i++) 376a87e0831SAlan Somers buf[i] = random(); 377a87e0831SAlan Somers 378a87e0831SAlan Somers ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs )) 379a87e0831SAlan Somers << strerror(errno); 380a87e0831SAlan Somers ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs)) 381a87e0831SAlan Somers << strerror(errno); 38217575badSAlan Somers m_filesize = std::max(m_filesize, offs + size); 38317575badSAlan Somers 3848bae22bbSAlan Somers delete[] buf; 385a87e0831SAlan Somers } 386a87e0831SAlan Somers 387a87e0831SAlan Somers }; 388a87e0831SAlan Somers 38948417ae0SAlan Somers class IoCacheable: public Io { 39048417ae0SAlan Somers public: 39148417ae0SAlan Somers virtual void SetUp() { 39248417ae0SAlan Somers Io::SetUp(); 39348417ae0SAlan Somers } 39448417ae0SAlan Somers }; 39548417ae0SAlan Somers 3961c909c30SAlan Somers class IoCopyFileRange: public Io { 3971c909c30SAlan Somers public: 3981c909c30SAlan Somers virtual void SetUp() { 3991c909c30SAlan Somers Io::SetUp(); 4001c909c30SAlan Somers } 4011c909c30SAlan Somers }; 4021c909c30SAlan Somers 403a87e0831SAlan Somers /* 404a87e0831SAlan Somers * Extend a file with dirty data in the last page of the last block. 405a87e0831SAlan Somers * 406a87e0831SAlan Somers * fsx -WR -P /tmp -S8 -N3 fsx.bin 407a87e0831SAlan Somers */ 4080482ec3eSAlan Somers TEST_P(Io, extend_from_dirty_page) 409a87e0831SAlan Somers { 410a87e0831SAlan Somers off_t wofs = 0x21a0; 411a87e0831SAlan Somers ssize_t wsize = 0xf0a8; 412a87e0831SAlan Somers off_t rofs = 0xb284; 413a87e0831SAlan Somers ssize_t rsize = 0x9b22; 414a87e0831SAlan Somers off_t truncsize = 0x28702; 415a87e0831SAlan Somers 416daf26f93SAlan Somers do_write(wofs, wsize); 417a87e0831SAlan Somers do_ftruncate(truncsize); 418daf26f93SAlan Somers do_read(rofs, rsize); 419a87e0831SAlan Somers } 420a87e0831SAlan Somers 421a87e0831SAlan Somers /* 42217575badSAlan Somers * mapwrite into a newly extended part of a file. 42317575badSAlan Somers * 42417575badSAlan Somers * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin 42517575badSAlan Somers */ 42648417ae0SAlan Somers TEST_P(IoCacheable, extend_by_mapwrite) 42717575badSAlan Somers { 428daf26f93SAlan Somers do_mapwrite(0x29a3a, 0x849e); /* [0x29a3a, 0x31ed7] */ 429daf26f93SAlan Somers do_mapwrite(0x3c7d8, 0x3994); /* [0x3c7d8, 0x4016b] */ 430daf26f93SAlan Somers do_read(0x30c16, 0xf556); /* [0x30c16, 0x4016b] */ 43117575badSAlan Somers } 43217575badSAlan Somers 43317575badSAlan Somers /* 434a87e0831SAlan Somers * When writing the last page of a file, it must be written synchronously. 435a87e0831SAlan Somers * Otherwise the cached page can become invalid by a subsequent extend 436a87e0831SAlan Somers * operation. 437a87e0831SAlan Somers * 438a87e0831SAlan Somers * fsx -WR -P /tmp -S642 -N3 fsx.bin 439a87e0831SAlan Somers */ 4400482ec3eSAlan Somers TEST_P(Io, last_page) 441a87e0831SAlan Somers { 442daf26f93SAlan Somers do_write(0x1134f, 0xcc77); /* [0x1134f, 0x1dfc5] */ 443daf26f93SAlan Somers do_write(0x2096a, 0xdfa7); /* [0x2096a, 0x2e910] */ 444daf26f93SAlan Somers do_read(0x1a3aa, 0xb5b7); /* [0x1a3aa, 0x25960] */ 44517575badSAlan Somers } 446a87e0831SAlan Somers 44717575badSAlan Somers /* 44817575badSAlan Somers * Read a hole using mmap 44917575badSAlan Somers * 45017575badSAlan Somers * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp -S14 fsx.bin 45117575badSAlan Somers */ 45248417ae0SAlan Somers TEST_P(IoCacheable, mapread_hole) 45317575badSAlan Somers { 454daf26f93SAlan Somers do_write(0xf205, 0x123b7); /* [0xf205, 0x215bb] */ 455daf26f93SAlan Somers do_mapread(0x2f4c, 0xeeea); /* [0x2f4c, 0x11e35] */ 456a87e0831SAlan Somers } 457a87e0831SAlan Somers 458a87e0831SAlan Somers /* 459a87e0831SAlan Somers * Read a hole from a block that contains some cached data. 460a87e0831SAlan Somers * 461a87e0831SAlan Somers * fsx -WR -P /tmp -S55 fsx.bin 462a87e0831SAlan Somers */ 4630482ec3eSAlan Somers TEST_P(Io, read_hole_from_cached_block) 464a87e0831SAlan Somers { 465a87e0831SAlan Somers off_t wofs = 0x160c5; 466a87e0831SAlan Somers ssize_t wsize = 0xa996; 467a87e0831SAlan Somers off_t rofs = 0x472e; 468a87e0831SAlan Somers ssize_t rsize = 0xd8d5; 469a87e0831SAlan Somers 470daf26f93SAlan Somers do_write(wofs, wsize); 471daf26f93SAlan Somers do_read(rofs, rsize); 472a87e0831SAlan Somers } 473a87e0831SAlan Somers 474a87e0831SAlan Somers /* 47593c0c1d4SAlan Somers * Truncating a file into a dirty buffer should not causing anything untoward 47693c0c1d4SAlan Somers * to happen when that buffer is eventually flushed. 477a87e0831SAlan Somers * 478a87e0831SAlan Somers * fsx -WR -P /tmp -S839 -d -N6 fsx.bin 479a87e0831SAlan Somers */ 4800482ec3eSAlan Somers TEST_P(Io, truncate_into_dirty_buffer) 481a87e0831SAlan Somers { 482a87e0831SAlan Somers off_t wofs0 = 0x3bad7; 483a87e0831SAlan Somers ssize_t wsize0 = 0x4529; 484a87e0831SAlan Somers off_t wofs1 = 0xc30d; 485a87e0831SAlan Somers ssize_t wsize1 = 0x5f77; 486a87e0831SAlan Somers off_t truncsize0 = 0x10916; 487a87e0831SAlan Somers off_t rofs = 0xdf17; 488a87e0831SAlan Somers ssize_t rsize = 0x29ff; 489a87e0831SAlan Somers off_t truncsize1 = 0x152b4; 490a87e0831SAlan Somers 491daf26f93SAlan Somers do_write(wofs0, wsize0); 492daf26f93SAlan Somers do_write(wofs1, wsize1); 493a87e0831SAlan Somers do_ftruncate(truncsize0); 494daf26f93SAlan Somers do_read(rofs, rsize); 495a87e0831SAlan Somers do_ftruncate(truncsize1); 496a87e0831SAlan Somers close(m_test_fd); 497a87e0831SAlan Somers } 49893c0c1d4SAlan Somers 49993c0c1d4SAlan Somers /* 50093c0c1d4SAlan Somers * Truncating a file into a dirty buffer should not causing anything untoward 50193c0c1d4SAlan Somers * to happen when that buffer is eventually flushed, even when the buffer's 50293c0c1d4SAlan Somers * dirty_off is > 0. 50393c0c1d4SAlan Somers * 50493c0c1d4SAlan Somers * Based on this command with a few steps removed: 50593c0c1d4SAlan Somers * fsx -WR -P /tmp -S677 -d -N8 fsx.bin 50693c0c1d4SAlan Somers */ 5070482ec3eSAlan Somers TEST_P(Io, truncate_into_dirty_buffer2) 50893c0c1d4SAlan Somers { 50993c0c1d4SAlan Somers off_t truncsize0 = 0x344f3; 51093c0c1d4SAlan Somers off_t wofs = 0x2790c; 51193c0c1d4SAlan Somers ssize_t wsize = 0xd86a; 51293c0c1d4SAlan Somers off_t truncsize1 = 0x2de38; 51393c0c1d4SAlan Somers off_t rofs2 = 0x1fd7a; 51493c0c1d4SAlan Somers ssize_t rsize2 = 0xc594; 51593c0c1d4SAlan Somers off_t truncsize2 = 0x31e71; 51693c0c1d4SAlan Somers 51793c0c1d4SAlan Somers /* Sets the file size to something larger than the next write */ 51893c0c1d4SAlan Somers do_ftruncate(truncsize0); 51993c0c1d4SAlan Somers /* 52093c0c1d4SAlan Somers * Creates a dirty buffer. The part in lbn 2 doesn't flush 52193c0c1d4SAlan Somers * synchronously. 52293c0c1d4SAlan Somers */ 523daf26f93SAlan Somers do_write(wofs, wsize); 52493c0c1d4SAlan Somers /* Truncates part of the dirty buffer created in step 2 */ 52593c0c1d4SAlan Somers do_ftruncate(truncsize1); 52693c0c1d4SAlan Somers /* XXX ?I don't know why this is necessary? */ 527daf26f93SAlan Somers do_read(rofs2, rsize2); 52893c0c1d4SAlan Somers /* Truncates the dirty buffer */ 52993c0c1d4SAlan Somers do_ftruncate(truncsize2); 53093c0c1d4SAlan Somers close(m_test_fd); 53193c0c1d4SAlan Somers } 532dff3a6b4SAlan Somers 533dff3a6b4SAlan Somers /* 534dff3a6b4SAlan Somers * Regression test for a bug introduced in r348931 535dff3a6b4SAlan Somers * 536dff3a6b4SAlan Somers * Sequence of operations: 537dff3a6b4SAlan Somers * 1) The first write reads lbn so it can modify it 538dff3a6b4SAlan Somers * 2) The first write flushes lbn 3 immediately because it's the end of file 539dff3a6b4SAlan Somers * 3) The first write then flushes lbn 4 because it's the end of the file 540dff3a6b4SAlan Somers * 4) The second write modifies the cached versions of lbn 3 and 4 541dff3a6b4SAlan Somers * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's 542dff3a6b4SAlan Somers * extending the buffer. Then it flushes lbn 4 because B_DELWRI was set but 543dff3a6b4SAlan Somers * B_CACHE was clear. 544dff3a6b4SAlan Somers * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the 545dff3a6b4SAlan Somers * buffer into a weird write-only state. All read operations would return 546dff3a6b4SAlan Somers * 0. Writes were apparently still processed, because the buffer's contents 547dff3a6b4SAlan Somers * were correct when examined in a core dump. 548dff3a6b4SAlan Somers * 7) The third write reads lbn 4 because cache is clear 549dff3a6b4SAlan Somers * 9) uiomove dutifully copies new data into the buffer 550dff3a6b4SAlan Somers * 10) The buffer's dirty is flushed to lbn 4 551dff3a6b4SAlan Somers * 11) The read returns all zeros because of step 6. 552dff3a6b4SAlan Somers * 553dff3a6b4SAlan Somers * Based on: 554dff3a6b4SAlan Somers * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q fsx.bin 555dff3a6b4SAlan Somers */ 5560482ec3eSAlan Somers TEST_P(Io, resize_a_valid_buffer_while_extending) 557dff3a6b4SAlan Somers { 558daf26f93SAlan Somers do_write(0x36ee6, 0x14530); /* [0x36ee6, 0x4b415] */ 559daf26f93SAlan Somers do_write(0x33256, 0x1507c); /* [0x33256, 0x482d1] */ 560daf26f93SAlan Somers do_write(0x4c03d, 0x175c); /* [0x4c03d, 0x4d798] */ 561daf26f93SAlan Somers do_read(0x3599c, 0xe277); /* [0x3599c, 0x43c12] */ 562dff3a6b4SAlan Somers close(m_test_fd); 563dff3a6b4SAlan Somers } 5640482ec3eSAlan Somers 5656b1c5349SAlan Somers /* 5666b1c5349SAlan Somers * mmap of a suitable region could trigger a panic. I'm not sure what 5676b1c5349SAlan Somers * combination of size and offset counts as "suitable". Regression test for 5686b1c5349SAlan Somers * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=276191 5696b1c5349SAlan Somers */ 5706b1c5349SAlan Somers TEST_P(IoCacheable, vnode_pager_generic_putpage_clean_block_at_eof) 5716b1c5349SAlan Somers { 572daf26f93SAlan Somers do_mapwrite(0x3b4e0, 0x1bbc3); 5736b1c5349SAlan Somers } 5746b1c5349SAlan Somers 5751c909c30SAlan Somers /* 5761c909c30SAlan Somers * A copy_file_range that follows an mmap write to the input area needs to 5771c909c30SAlan Somers * flush the mmap buffer first. 5781c909c30SAlan Somers */ 5791c909c30SAlan Somers TEST_P(IoCopyFileRange, copy_file_range_from_mapped_write) 5801c909c30SAlan Somers { 581daf26f93SAlan Somers do_mapwrite(0, 0x1000); 5821c909c30SAlan Somers do_copy_file_range(0, 0x1000, 0x1000); 5831c909c30SAlan Somers do_read(0x1000, 0x1000); 5841c909c30SAlan Somers } 5851c909c30SAlan Somers 5861c909c30SAlan Somers 587811e0a31SEnji Cooper INSTANTIATE_TEST_SUITE_P(Io, Io, 588f8ebf1cdSAlan Somers Combine(Bool(), /* async read */ 5890482ec3eSAlan Somers Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */ 5901c909c30SAlan Somers Values(Uncached, Writethrough, Writeback, WritebackAsync), 5911c909c30SAlan Somers Values(28) /* kernel_minor_vers */ 592c51f519bSAlan Somers ) 593c51f519bSAlan Somers ); 59448417ae0SAlan Somers 595811e0a31SEnji Cooper INSTANTIATE_TEST_SUITE_P(Io, IoCacheable, 596f8ebf1cdSAlan Somers Combine(Bool(), /* async read */ 59748417ae0SAlan Somers Values(0x1000, 0x10000, 0x20000), /* m_maxwrite */ 5981c909c30SAlan Somers Values(Writethrough, Writeback, WritebackAsync), 5991c909c30SAlan Somers Values(28) /* kernel_minor_vers */ 6001c909c30SAlan Somers ) 6011c909c30SAlan Somers ); 6021c909c30SAlan Somers 6031c909c30SAlan Somers INSTANTIATE_TEST_SUITE_P(Io, IoCopyFileRange, 6041c909c30SAlan Somers Combine(Values(true), /* async read */ 6051c909c30SAlan Somers Values(0x10000), /* m_maxwrite */ 6061c909c30SAlan Somers Values(Writethrough, Writeback, WritebackAsync), 6071c909c30SAlan Somers Values(27, 28) /* kernel_minor_vers */ 608c51f519bSAlan Somers ) 609c51f519bSAlan Somers ); 610