xref: /freebsd/tests/sys/fs/fusefs/io.cc (revision 91972cfcddf950d7a9c33df5a9171ada1805a144)
1a87e0831SAlan Somers /*-
2a87e0831SAlan Somers  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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.
291fa8ebfbSAlan Somers  *
301fa8ebfbSAlan Somers  * $FreeBSD$
31a87e0831SAlan Somers  */
32a87e0831SAlan Somers 
33a87e0831SAlan Somers extern "C" {
3448417ae0SAlan Somers #include <sys/types.h>
3517575badSAlan Somers #include <sys/mman.h>
3648417ae0SAlan Somers #include <sys/sysctl.h>
3717575badSAlan Somers 
38a87e0831SAlan Somers #include <fcntl.h>
39a87e0831SAlan Somers #include <stdlib.h>
40a87e0831SAlan Somers #include <unistd.h>
41a87e0831SAlan Somers }
42a87e0831SAlan Somers 
43a87e0831SAlan Somers #include "mockfs.hh"
44a87e0831SAlan Somers #include "utils.hh"
45a87e0831SAlan Somers 
46a87e0831SAlan Somers /*
47a87e0831SAlan Somers  * For testing I/O like fsx does, but deterministically and without a real
48a87e0831SAlan Somers  * underlying file system
49a87e0831SAlan Somers  */
50a87e0831SAlan Somers 
51a87e0831SAlan Somers using namespace testing;
52a87e0831SAlan Somers 
53a87e0831SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
54a87e0831SAlan Somers const char RELPATH[] = "some_file.txt";
55a87e0831SAlan Somers const uint64_t ino = 42;
56a87e0831SAlan Somers 
57dff3a6b4SAlan Somers static void compare(const void *tbuf, const void *controlbuf, off_t baseofs,
58dff3a6b4SAlan Somers 	ssize_t size)
59dff3a6b4SAlan Somers {
60dff3a6b4SAlan Somers 	int i;
61dff3a6b4SAlan Somers 
62dff3a6b4SAlan Somers 	for (i = 0; i < size; i++) {
63dff3a6b4SAlan Somers 		if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) {
64dff3a6b4SAlan Somers 			off_t ofs = baseofs + i;
65dff3a6b4SAlan Somers 			FAIL() << "miscompare at offset "
66dff3a6b4SAlan Somers 			       << std::hex
67dff3a6b4SAlan Somers 			       << std::showbase
68dff3a6b4SAlan Somers 			       << ofs
69dff3a6b4SAlan Somers 			       << ".  expected = "
70dff3a6b4SAlan Somers 			       << std::setw(2)
71dff3a6b4SAlan Somers 			       << (unsigned)((const uint8_t*)controlbuf)[i]
72dff3a6b4SAlan Somers 			       << " got = "
73dff3a6b4SAlan Somers 			       << (unsigned)((const uint8_t*)tbuf)[i];
74dff3a6b4SAlan Somers 		}
75dff3a6b4SAlan Somers 	}
76dff3a6b4SAlan Somers }
77dff3a6b4SAlan Somers 
78c51f519bSAlan Somers typedef tuple<bool, uint32_t, cache_mode> IoParam;
79c51f519bSAlan Somers 
80c51f519bSAlan Somers class Io: public FuseTest, public WithParamInterface<IoParam> {
81a87e0831SAlan Somers public:
82a87e0831SAlan Somers int m_backing_fd, m_control_fd, m_test_fd;
8317575badSAlan Somers off_t m_filesize;
84c51f519bSAlan Somers bool m_direct_io;
85a87e0831SAlan Somers 
869c9634d1SAlan Somers Io(): m_backing_fd(-1), m_control_fd(-1), m_test_fd(-1), m_filesize(0),
879c9634d1SAlan Somers 	m_direct_io(false) {};
88a87e0831SAlan Somers 
89a87e0831SAlan Somers void SetUp()
90a87e0831SAlan Somers {
9193c0c1d4SAlan Somers 	m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
92a87e0831SAlan Somers 	if (m_backing_fd < 0)
93a87e0831SAlan Somers 		FAIL() << strerror(errno);
9493c0c1d4SAlan Somers 	m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
95a87e0831SAlan Somers 	if (m_control_fd < 0)
96a87e0831SAlan Somers 		FAIL() << strerror(errno);
97a87e0831SAlan Somers 	srandom(22'9'1982);	// Seed with my birthday
980482ec3eSAlan Somers 
99f8ebf1cdSAlan Somers 	if (get<0>(GetParam()))
100f8ebf1cdSAlan Somers 		m_init_flags |= FUSE_ASYNC_READ;
1010482ec3eSAlan Somers 	m_maxwrite = get<1>(GetParam());
102c51f519bSAlan Somers 	switch (get<2>(GetParam())) {
103c51f519bSAlan Somers 		case Uncached:
104c51f519bSAlan Somers 			m_direct_io = true;
105c51f519bSAlan Somers 			break;
106c51f519bSAlan Somers 		case WritebackAsync:
107c51f519bSAlan Somers 			m_async = true;
108c51f519bSAlan Somers 			/* FALLTHROUGH */
109c51f519bSAlan Somers 		case Writeback:
110f8ebf1cdSAlan Somers 			m_init_flags |= FUSE_WRITEBACK_CACHE;
111c51f519bSAlan Somers 			/* FALLTHROUGH */
112c51f519bSAlan Somers 		case Writethrough:
113c51f519bSAlan Somers 			break;
114c51f519bSAlan Somers 		default:
115c51f519bSAlan Somers 			FAIL() << "Unknown cache mode";
116c51f519bSAlan Somers 	}
117*91972cfcSAlan 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 "
125c51f519bSAlan Somers 		    "%sasync cache=%s\n",
126c51f519bSAlan Somers 		    m_init_flags, m_maxwrite, m_async? "" : "no",
127c51f519bSAlan Somers 		    cache_mode_to_s(get<2>(GetParam())));
128c51f519bSAlan Somers 	}
129c51f519bSAlan Somers 
130a87e0831SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
131c51f519bSAlan Somers 	expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO : 0, 1);
132a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
133a87e0831SAlan Somers 		ResultOf([=](auto in) {
134a87e0831SAlan Somers 			return (in.header.opcode == FUSE_WRITE &&
135a87e0831SAlan Somers 				in.header.nodeid == ino);
136a87e0831SAlan Somers 		}, Eq(true)),
137a87e0831SAlan Somers 		_)
138a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
139a87e0831SAlan Somers 		const char *buf = (const char*)in.body.bytes +
140a87e0831SAlan Somers 			sizeof(struct fuse_write_in);
141a87e0831SAlan Somers 		ssize_t isize = in.body.write.size;
142a87e0831SAlan Somers 		off_t iofs = in.body.write.offset;
143a87e0831SAlan Somers 
144a87e0831SAlan Somers 		ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
145a87e0831SAlan Somers 			<< strerror(errno);
146a87e0831SAlan Somers 		SET_OUT_HEADER_LEN(out, write);
147a87e0831SAlan Somers 		out.body.write.size = isize;
148a87e0831SAlan Somers 	})));
149a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
150a87e0831SAlan Somers 		ResultOf([=](auto in) {
151a87e0831SAlan Somers 			return (in.header.opcode == FUSE_READ &&
152a87e0831SAlan Somers 				in.header.nodeid == ino);
153a87e0831SAlan Somers 		}, Eq(true)),
154a87e0831SAlan Somers 		_)
155a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
156a87e0831SAlan Somers 		ssize_t isize = in.body.write.size;
157a87e0831SAlan Somers 		off_t iofs = in.body.write.offset;
158a87e0831SAlan Somers 		void *buf = out.body.bytes;
15917575badSAlan Somers 		ssize_t osize;
160a87e0831SAlan Somers 
16117575badSAlan Somers 		osize = pread(m_backing_fd, buf, isize, iofs);
16217575badSAlan Somers 		ASSERT_LE(0, osize) << strerror(errno);
16317575badSAlan Somers 		out.header.len = sizeof(struct fuse_out_header) + osize;
164a87e0831SAlan Somers 	})));
165a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
166a87e0831SAlan Somers 		ResultOf([=](auto in) {
167a87e0831SAlan Somers 			return (in.header.opcode == FUSE_SETATTR &&
168a87e0831SAlan Somers 				in.header.nodeid == ino &&
169788af953SAlan Somers 				(in.body.setattr.valid & FATTR_SIZE));
170788af953SAlan Somers 
171a87e0831SAlan Somers 		}, Eq(true)),
172a87e0831SAlan Somers 		_)
173a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
174a87e0831SAlan Somers 		ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
175a87e0831SAlan Somers 			<< strerror(errno);
176a87e0831SAlan Somers 		SET_OUT_HEADER_LEN(out, attr);
177a87e0831SAlan Somers 		out.body.attr.attr.ino = ino;
178a87e0831SAlan Somers 		out.body.attr.attr.mode = S_IFREG | 0755;
179a87e0831SAlan Somers 		out.body.attr.attr.size = in.body.setattr.size;
180a87e0831SAlan Somers 		out.body.attr.attr_valid = UINT64_MAX;
181a87e0831SAlan Somers 	})));
182a87e0831SAlan Somers 	/* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
183a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
184a87e0831SAlan Somers 		ResultOf([=](auto in) {
185a87e0831SAlan Somers 			return (in.header.opcode == FUSE_FLUSH &&
186a87e0831SAlan Somers 				in.header.nodeid == ino);
187a87e0831SAlan Somers 		}, Eq(true)),
188a87e0831SAlan Somers 		_)
189a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnErrno(0)));
190a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
191a87e0831SAlan Somers 		ResultOf([=](auto in) {
192a87e0831SAlan Somers 			return (in.header.opcode == FUSE_RELEASE &&
193a87e0831SAlan Somers 				in.header.nodeid == ino);
194a87e0831SAlan Somers 		}, Eq(true)),
195a87e0831SAlan Somers 		_)
196a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnErrno(0)));
197a87e0831SAlan Somers 
198a87e0831SAlan Somers 	m_test_fd = open(FULLPATH, O_RDWR );
199a87e0831SAlan Somers 	EXPECT_LE(0, m_test_fd) << strerror(errno);
200a87e0831SAlan Somers }
201a87e0831SAlan Somers 
202a87e0831SAlan Somers void TearDown()
203a87e0831SAlan Somers {
20448417ae0SAlan Somers 	if (m_test_fd >= 0)
20548417ae0SAlan Somers 		close(m_test_fd);
206a87e0831SAlan Somers 	if (m_backing_fd >= 0)
207a87e0831SAlan Somers 		close(m_backing_fd);
208a87e0831SAlan Somers 	if (m_control_fd >= 0)
209a87e0831SAlan Somers 		close(m_control_fd);
210a87e0831SAlan Somers 	FuseTest::TearDown();
2117fc0921dSAlan Somers 	leak(m_test_fd);
212a87e0831SAlan Somers }
213a87e0831SAlan Somers 
21448417ae0SAlan Somers void do_closeopen()
21548417ae0SAlan Somers {
21648417ae0SAlan Somers 	ASSERT_EQ(0, close(m_test_fd)) << strerror(errno);
21748417ae0SAlan Somers 	m_test_fd = open("backing_file", O_RDWR);
21848417ae0SAlan Somers 	ASSERT_LE(0, m_test_fd) << strerror(errno);
21948417ae0SAlan Somers 
22048417ae0SAlan Somers 	ASSERT_EQ(0, close(m_control_fd)) << strerror(errno);
22148417ae0SAlan Somers 	m_control_fd = open("control", O_RDWR);
22248417ae0SAlan Somers 	ASSERT_LE(0, m_control_fd) << strerror(errno);
22348417ae0SAlan Somers }
22448417ae0SAlan Somers 
225a87e0831SAlan Somers void do_ftruncate(off_t offs)
226a87e0831SAlan Somers {
227a87e0831SAlan Somers 	ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
228a87e0831SAlan Somers 	ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
22917575badSAlan Somers 	m_filesize = offs;
23017575badSAlan Somers }
23117575badSAlan Somers 
23217575badSAlan Somers void do_mapread(ssize_t size, off_t offs)
23317575badSAlan Somers {
23417575badSAlan Somers 	void *control_buf, *p;
23517575badSAlan Somers 	off_t pg_offset, page_mask;
23617575badSAlan Somers 	size_t map_size;
23717575badSAlan Somers 
23817575badSAlan Somers 	page_mask = getpagesize() - 1;
23917575badSAlan Somers 	pg_offset = offs & page_mask;
24017575badSAlan Somers 	map_size = pg_offset + size;
24117575badSAlan Somers 
24217575badSAlan Somers 	p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd,
24317575badSAlan Somers 	    offs - pg_offset);
24417575badSAlan Somers 	ASSERT_NE(p, MAP_FAILED) << strerror(errno);
24517575badSAlan Somers 
24617575badSAlan Somers 	control_buf = malloc(size);
2475a0b9a27SAlan Somers 	ASSERT_NE(nullptr, control_buf) << strerror(errno);
24817575badSAlan Somers 
24917575badSAlan Somers 	ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
25017575badSAlan Somers 		<< strerror(errno);
25117575badSAlan Somers 
25217575badSAlan Somers 	compare((void*)((char*)p + pg_offset), control_buf, offs, size);
25317575badSAlan Somers 
25417575badSAlan Somers 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
25517575badSAlan Somers 	free(control_buf);
256a87e0831SAlan Somers }
257a87e0831SAlan Somers 
258a87e0831SAlan Somers void do_read(ssize_t size, off_t offs)
259a87e0831SAlan Somers {
260a87e0831SAlan Somers 	void *test_buf, *control_buf;
26117575badSAlan Somers 	ssize_t r;
262a87e0831SAlan Somers 
263a87e0831SAlan Somers 	test_buf = malloc(size);
2645a0b9a27SAlan Somers 	ASSERT_NE(nullptr, test_buf) << strerror(errno);
265a87e0831SAlan Somers 	control_buf = malloc(size);
2665a0b9a27SAlan Somers 	ASSERT_NE(nullptr, control_buf) << strerror(errno);
267a87e0831SAlan Somers 
26817575badSAlan Somers 	errno = 0;
26917575badSAlan Somers 	r = pread(m_test_fd, test_buf, size, offs);
27017575badSAlan Somers 	ASSERT_NE(-1, r) << strerror(errno);
27117575badSAlan Somers 	ASSERT_EQ(size, r) << "unexpected short read";
27217575badSAlan Somers 	r = pread(m_control_fd, control_buf, size, offs);
27317575badSAlan Somers 	ASSERT_NE(-1, r) << strerror(errno);
27417575badSAlan Somers 	ASSERT_EQ(size, r) << "unexpected short read";
275a87e0831SAlan Somers 
276dff3a6b4SAlan Somers 	compare(test_buf, control_buf, offs, size);
277a87e0831SAlan Somers 
278a87e0831SAlan Somers 	free(control_buf);
279a87e0831SAlan Somers 	free(test_buf);
280a87e0831SAlan Somers }
281a87e0831SAlan Somers 
28217575badSAlan Somers void do_mapwrite(ssize_t size, off_t offs)
28317575badSAlan Somers {
28417575badSAlan Somers 	char *buf;
28517575badSAlan Somers 	void *p;
28617575badSAlan Somers 	off_t pg_offset, page_mask;
28717575badSAlan Somers 	size_t map_size;
28817575badSAlan Somers 	long i;
28917575badSAlan Somers 
29017575badSAlan Somers 	page_mask = getpagesize() - 1;
29117575badSAlan Somers 	pg_offset = offs & page_mask;
29217575badSAlan Somers 	map_size = pg_offset + size;
29317575badSAlan Somers 
29417575badSAlan Somers 	buf = (char*)malloc(size);
2955a0b9a27SAlan Somers 	ASSERT_NE(nullptr, buf) << strerror(errno);
29617575badSAlan Somers 	for (i=0; i < size; i++)
29717575badSAlan Somers 		buf[i] = random();
29817575badSAlan Somers 
29917575badSAlan Somers 	if (offs + size > m_filesize) {
30017575badSAlan Somers 		/*
30117575badSAlan Somers 		 * Must manually extend.  vm_mmap_vnode will not implicitly
30217575badSAlan Somers 		 * extend a vnode
30317575badSAlan Somers 		 */
30417575badSAlan Somers 		do_ftruncate(offs + size);
30517575badSAlan Somers 	}
30617575badSAlan Somers 
30717575badSAlan Somers 	p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
30817575badSAlan Somers 	    MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset);
30917575badSAlan Somers 	ASSERT_NE(p, MAP_FAILED) << strerror(errno);
31017575badSAlan Somers 
31117575badSAlan Somers 	bcopy(buf, (char*)p + pg_offset, size);
31217575badSAlan Somers 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
31317575badSAlan Somers 		<< strerror(errno);
31417575badSAlan Somers 
31517575badSAlan Somers 	free(buf);
31617575badSAlan Somers 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
31717575badSAlan Somers }
31817575badSAlan Somers 
319a87e0831SAlan Somers void do_write(ssize_t size, off_t offs)
320a87e0831SAlan Somers {
321a87e0831SAlan Somers 	char *buf;
322a87e0831SAlan Somers 	long i;
323a87e0831SAlan Somers 
324a87e0831SAlan Somers 	buf = (char*)malloc(size);
3255a0b9a27SAlan Somers 	ASSERT_NE(nullptr, buf) << strerror(errno);
326a87e0831SAlan Somers 	for (i=0; i < size; i++)
327a87e0831SAlan Somers 		buf[i] = random();
328a87e0831SAlan Somers 
329a87e0831SAlan Somers 	ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
330a87e0831SAlan Somers 		<< strerror(errno);
331a87e0831SAlan Somers 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
332a87e0831SAlan Somers 		<< strerror(errno);
33317575badSAlan Somers 	m_filesize = std::max(m_filesize, offs + size);
33417575badSAlan Somers 
33517575badSAlan Somers 	free(buf);
336a87e0831SAlan Somers }
337a87e0831SAlan Somers 
338a87e0831SAlan Somers };
339a87e0831SAlan Somers 
34048417ae0SAlan Somers class IoCacheable: public Io {
34148417ae0SAlan Somers public:
34248417ae0SAlan Somers virtual void SetUp() {
34348417ae0SAlan Somers 	Io::SetUp();
34448417ae0SAlan Somers }
34548417ae0SAlan Somers };
34648417ae0SAlan Somers 
347a87e0831SAlan Somers /*
348a87e0831SAlan Somers  * Extend a file with dirty data in the last page of the last block.
349a87e0831SAlan Somers  *
350a87e0831SAlan Somers  * fsx -WR -P /tmp -S8 -N3 fsx.bin
351a87e0831SAlan Somers  */
3520482ec3eSAlan Somers TEST_P(Io, extend_from_dirty_page)
353a87e0831SAlan Somers {
354a87e0831SAlan Somers 	off_t wofs = 0x21a0;
355a87e0831SAlan Somers 	ssize_t wsize = 0xf0a8;
356a87e0831SAlan Somers 	off_t rofs = 0xb284;
357a87e0831SAlan Somers 	ssize_t rsize = 0x9b22;
358a87e0831SAlan Somers 	off_t truncsize = 0x28702;
359a87e0831SAlan Somers 
360a87e0831SAlan Somers 	do_write(wsize, wofs);
361a87e0831SAlan Somers 	do_ftruncate(truncsize);
362a87e0831SAlan Somers 	do_read(rsize, rofs);
363a87e0831SAlan Somers }
364a87e0831SAlan Somers 
365a87e0831SAlan Somers /*
36617575badSAlan Somers  * mapwrite into a newly extended part of a file.
36717575badSAlan Somers  *
36817575badSAlan Somers  * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin
36917575badSAlan Somers  */
37048417ae0SAlan Somers TEST_P(IoCacheable, extend_by_mapwrite)
37117575badSAlan Somers {
37217575badSAlan Somers 	do_mapwrite(0x849e, 0x29a3a);	/* [0x29a3a, 0x31ed7] */
37317575badSAlan Somers 	do_mapwrite(0x3994, 0x3c7d8);	/* [0x3c7d8, 0x4016b] */
37417575badSAlan Somers 	do_read(0xf556, 0x30c16);	/* [0x30c16, 0x4016b] */
37517575badSAlan Somers }
37617575badSAlan Somers 
37717575badSAlan Somers /*
378a87e0831SAlan Somers  * When writing the last page of a file, it must be written synchronously.
379a87e0831SAlan Somers  * Otherwise the cached page can become invalid by a subsequent extend
380a87e0831SAlan Somers  * operation.
381a87e0831SAlan Somers  *
382a87e0831SAlan Somers  * fsx -WR -P /tmp -S642 -N3 fsx.bin
383a87e0831SAlan Somers  */
3840482ec3eSAlan Somers TEST_P(Io, last_page)
385a87e0831SAlan Somers {
38617575badSAlan Somers 	do_write(0xcc77, 0x1134f);	/* [0x1134f, 0x1dfc5] */
38717575badSAlan Somers 	do_write(0xdfa7, 0x2096a);	/* [0x2096a, 0x2e910] */
38817575badSAlan Somers 	do_read(0xb5b7, 0x1a3aa);	/* [0x1a3aa, 0x25960] */
38917575badSAlan Somers }
390a87e0831SAlan Somers 
39117575badSAlan Somers /*
39217575badSAlan Somers  * Read a hole using mmap
39317575badSAlan Somers  *
39417575badSAlan Somers  * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp  -S14 fsx.bin
39517575badSAlan Somers  */
39648417ae0SAlan Somers TEST_P(IoCacheable, mapread_hole)
39717575badSAlan Somers {
39817575badSAlan Somers 	do_write(0x123b7, 0xf205);	/* [0xf205, 0x215bb] */
39917575badSAlan Somers 	do_mapread(0xeeea, 0x2f4c);	/* [0x2f4c, 0x11e35] */
400a87e0831SAlan Somers }
401a87e0831SAlan Somers 
402a87e0831SAlan Somers /*
403a87e0831SAlan Somers  * Read a hole from a block that contains some cached data.
404a87e0831SAlan Somers  *
405a87e0831SAlan Somers  * fsx -WR -P /tmp -S55  fsx.bin
406a87e0831SAlan Somers  */
4070482ec3eSAlan Somers TEST_P(Io, read_hole_from_cached_block)
408a87e0831SAlan Somers {
409a87e0831SAlan Somers 	off_t wofs = 0x160c5;
410a87e0831SAlan Somers 	ssize_t wsize = 0xa996;
411a87e0831SAlan Somers 	off_t rofs = 0x472e;
412a87e0831SAlan Somers 	ssize_t rsize = 0xd8d5;
413a87e0831SAlan Somers 
414a87e0831SAlan Somers 	do_write(wsize, wofs);
415a87e0831SAlan Somers 	do_read(rsize, rofs);
416a87e0831SAlan Somers }
417a87e0831SAlan Somers 
418a87e0831SAlan Somers /*
41993c0c1d4SAlan Somers  * Truncating a file into a dirty buffer should not causing anything untoward
42093c0c1d4SAlan Somers  * to happen when that buffer is eventually flushed.
421a87e0831SAlan Somers  *
422a87e0831SAlan Somers  * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
423a87e0831SAlan Somers  */
4240482ec3eSAlan Somers TEST_P(Io, truncate_into_dirty_buffer)
425a87e0831SAlan Somers {
426a87e0831SAlan Somers 	off_t wofs0 = 0x3bad7;
427a87e0831SAlan Somers 	ssize_t wsize0 = 0x4529;
428a87e0831SAlan Somers 	off_t wofs1 = 0xc30d;
429a87e0831SAlan Somers 	ssize_t wsize1 = 0x5f77;
430a87e0831SAlan Somers 	off_t truncsize0 = 0x10916;
431a87e0831SAlan Somers 	off_t rofs = 0xdf17;
432a87e0831SAlan Somers 	ssize_t rsize = 0x29ff;
433a87e0831SAlan Somers 	off_t truncsize1 = 0x152b4;
434a87e0831SAlan Somers 
435a87e0831SAlan Somers 	do_write(wsize0, wofs0);
436a87e0831SAlan Somers 	do_write(wsize1, wofs1);
437a87e0831SAlan Somers 	do_ftruncate(truncsize0);
438a87e0831SAlan Somers 	do_read(rsize, rofs);
439a87e0831SAlan Somers 	do_ftruncate(truncsize1);
440a87e0831SAlan Somers 	close(m_test_fd);
441a87e0831SAlan Somers }
44293c0c1d4SAlan Somers 
44393c0c1d4SAlan Somers /*
44493c0c1d4SAlan Somers  * Truncating a file into a dirty buffer should not causing anything untoward
44593c0c1d4SAlan Somers  * to happen when that buffer is eventually flushed, even when the buffer's
44693c0c1d4SAlan Somers  * dirty_off is > 0.
44793c0c1d4SAlan Somers  *
44893c0c1d4SAlan Somers  * Based on this command with a few steps removed:
44993c0c1d4SAlan Somers  * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
45093c0c1d4SAlan Somers  */
4510482ec3eSAlan Somers TEST_P(Io, truncate_into_dirty_buffer2)
45293c0c1d4SAlan Somers {
45393c0c1d4SAlan Somers 	off_t truncsize0 = 0x344f3;
45493c0c1d4SAlan Somers 	off_t wofs = 0x2790c;
45593c0c1d4SAlan Somers 	ssize_t wsize = 0xd86a;
45693c0c1d4SAlan Somers 	off_t truncsize1 = 0x2de38;
45793c0c1d4SAlan Somers 	off_t rofs2 = 0x1fd7a;
45893c0c1d4SAlan Somers 	ssize_t rsize2 = 0xc594;
45993c0c1d4SAlan Somers 	off_t truncsize2 = 0x31e71;
46093c0c1d4SAlan Somers 
46193c0c1d4SAlan Somers 	/* Sets the file size to something larger than the next write */
46293c0c1d4SAlan Somers 	do_ftruncate(truncsize0);
46393c0c1d4SAlan Somers 	/*
46493c0c1d4SAlan Somers 	 * Creates a dirty buffer.  The part in lbn 2 doesn't flush
46593c0c1d4SAlan Somers 	 * synchronously.
46693c0c1d4SAlan Somers 	 */
46793c0c1d4SAlan Somers 	do_write(wsize, wofs);
46893c0c1d4SAlan Somers 	/* Truncates part of the dirty buffer created in step 2 */
46993c0c1d4SAlan Somers 	do_ftruncate(truncsize1);
47093c0c1d4SAlan Somers 	/* XXX ?I don't know why this is necessary? */
47193c0c1d4SAlan Somers 	do_read(rsize2, rofs2);
47293c0c1d4SAlan Somers 	/* Truncates the dirty buffer */
47393c0c1d4SAlan Somers 	do_ftruncate(truncsize2);
47493c0c1d4SAlan Somers 	close(m_test_fd);
47593c0c1d4SAlan Somers }
476dff3a6b4SAlan Somers 
477dff3a6b4SAlan Somers /*
478dff3a6b4SAlan Somers  * Regression test for a bug introduced in r348931
479dff3a6b4SAlan Somers  *
480dff3a6b4SAlan Somers  * Sequence of operations:
481dff3a6b4SAlan Somers  * 1) The first write reads lbn so it can modify it
482dff3a6b4SAlan Somers  * 2) The first write flushes lbn 3 immediately because it's the end of file
483dff3a6b4SAlan Somers  * 3) The first write then flushes lbn 4 because it's the end of the file
484dff3a6b4SAlan Somers  * 4) The second write modifies the cached versions of lbn 3 and 4
485dff3a6b4SAlan Somers  * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's
486dff3a6b4SAlan Somers  *    extending the buffer.  Then it flushes lbn 4 because B_DELWRI was set but
487dff3a6b4SAlan Somers  *    B_CACHE was clear.
488dff3a6b4SAlan Somers  * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the
489dff3a6b4SAlan Somers  *    buffer into a weird write-only state.  All read operations would return
490dff3a6b4SAlan Somers  *    0.  Writes were apparently still processed, because the buffer's contents
491dff3a6b4SAlan Somers  *    were correct when examined in a core dump.
492dff3a6b4SAlan Somers  * 7) The third write reads lbn 4 because cache is clear
493dff3a6b4SAlan Somers  * 9) uiomove dutifully copies new data into the buffer
494dff3a6b4SAlan Somers  * 10) The buffer's dirty is flushed to lbn 4
495dff3a6b4SAlan Somers  * 11) The read returns all zeros because of step 6.
496dff3a6b4SAlan Somers  *
497dff3a6b4SAlan Somers  * Based on:
498dff3a6b4SAlan Somers  * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q  fsx.bin
499dff3a6b4SAlan Somers  */
5000482ec3eSAlan Somers TEST_P(Io, resize_a_valid_buffer_while_extending)
501dff3a6b4SAlan Somers {
502dff3a6b4SAlan Somers 	do_write(0x14530, 0x36ee6);	/* [0x36ee6, 0x4b415] */
503dff3a6b4SAlan Somers 	do_write(0x1507c, 0x33256);	/* [0x33256, 0x482d1] */
504dff3a6b4SAlan Somers 	do_write(0x175c, 0x4c03d);	/* [0x4c03d, 0x4d798] */
505dff3a6b4SAlan Somers 	do_read(0xe277, 0x3599c);	/* [0x3599c, 0x43c12] */
506dff3a6b4SAlan Somers 	close(m_test_fd);
507dff3a6b4SAlan Somers }
5080482ec3eSAlan Somers 
5090482ec3eSAlan Somers INSTANTIATE_TEST_CASE_P(Io, Io,
510f8ebf1cdSAlan Somers 	Combine(Bool(),					/* async read */
5110482ec3eSAlan Somers 		Values(0x1000, 0x10000, 0x20000),	/* m_maxwrite */
512c51f519bSAlan Somers 		Values(Uncached, Writethrough, Writeback, WritebackAsync)
513c51f519bSAlan Somers 	)
514c51f519bSAlan Somers );
51548417ae0SAlan Somers 
51648417ae0SAlan Somers INSTANTIATE_TEST_CASE_P(Io, IoCacheable,
517f8ebf1cdSAlan Somers 	Combine(Bool(),					/* async read */
51848417ae0SAlan Somers 		Values(0x1000, 0x10000, 0x20000),	/* m_maxwrite */
519c51f519bSAlan Somers 		Values(Writethrough, Writeback, WritebackAsync)
520c51f519bSAlan Somers 	)
521c51f519bSAlan Somers );
522