xref: /freebsd/tests/sys/fs/fusefs/io.cc (revision 93c0c1d4ce2f0ae333124789a66b3356698ead82)
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.
29a87e0831SAlan Somers  */
30a87e0831SAlan Somers 
31a87e0831SAlan Somers extern "C" {
32a87e0831SAlan Somers #include <fcntl.h>
33a87e0831SAlan Somers #include <stdlib.h>
34a87e0831SAlan Somers #include <unistd.h>
35a87e0831SAlan Somers }
36a87e0831SAlan Somers 
37a87e0831SAlan Somers #include "mockfs.hh"
38a87e0831SAlan Somers #include "utils.hh"
39a87e0831SAlan Somers 
40a87e0831SAlan Somers /*
41a87e0831SAlan Somers  * For testing I/O like fsx does, but deterministically and without a real
42a87e0831SAlan Somers  * underlying file system
43a87e0831SAlan Somers  *
44a87e0831SAlan Somers  * TODO: after fusefs gains the options to select cache mode for each mount
45a87e0831SAlan Somers  * point, run each of these tests for all cache modes.
46a87e0831SAlan Somers  */
47a87e0831SAlan Somers 
48a87e0831SAlan Somers using namespace testing;
49a87e0831SAlan Somers 
50a87e0831SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
51a87e0831SAlan Somers const char RELPATH[] = "some_file.txt";
52a87e0831SAlan Somers const uint64_t ino = 42;
53a87e0831SAlan Somers 
54a87e0831SAlan Somers class Io: public FuseTest {
55a87e0831SAlan Somers public:
56a87e0831SAlan Somers int m_backing_fd, m_control_fd, m_test_fd;
57a87e0831SAlan Somers 
58a87e0831SAlan Somers Io(): m_backing_fd(-1), m_control_fd(-1) {};
59a87e0831SAlan Somers 
60a87e0831SAlan Somers void SetUp()
61a87e0831SAlan Somers {
62*93c0c1d4SAlan Somers 	m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
63a87e0831SAlan Somers 	if (m_backing_fd < 0)
64a87e0831SAlan Somers 		FAIL() << strerror(errno);
65*93c0c1d4SAlan Somers 	m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
66a87e0831SAlan Somers 	if (m_control_fd < 0)
67a87e0831SAlan Somers 		FAIL() << strerror(errno);
68a87e0831SAlan Somers 	srandom(22'9'1982);	// Seed with my birthday
69a87e0831SAlan Somers 	FuseTest::SetUp();
70a87e0831SAlan Somers 	if (IsSkipped())
71a87e0831SAlan Somers 		return;
72a87e0831SAlan Somers 
73a87e0831SAlan Somers 	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
74a87e0831SAlan Somers 	expect_open(ino, 0, 1);
75a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
76a87e0831SAlan Somers 		ResultOf([=](auto in) {
77a87e0831SAlan Somers 			return (in.header.opcode == FUSE_WRITE &&
78a87e0831SAlan Somers 				in.header.nodeid == ino);
79a87e0831SAlan Somers 		}, Eq(true)),
80a87e0831SAlan Somers 		_)
81a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
82a87e0831SAlan Somers 		const char *buf = (const char*)in.body.bytes +
83a87e0831SAlan Somers 			sizeof(struct fuse_write_in);
84a87e0831SAlan Somers 		ssize_t isize = in.body.write.size;
85a87e0831SAlan Somers 		off_t iofs = in.body.write.offset;
86a87e0831SAlan Somers 
87a87e0831SAlan Somers 		ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
88a87e0831SAlan Somers 			<< strerror(errno);
89a87e0831SAlan Somers 		SET_OUT_HEADER_LEN(out, write);
90a87e0831SAlan Somers 		out.body.write.size = isize;
91a87e0831SAlan Somers 	})));
92a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
93a87e0831SAlan Somers 		ResultOf([=](auto in) {
94a87e0831SAlan Somers 			return (in.header.opcode == FUSE_READ &&
95a87e0831SAlan Somers 				in.header.nodeid == ino);
96a87e0831SAlan Somers 		}, Eq(true)),
97a87e0831SAlan Somers 		_)
98a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
99a87e0831SAlan Somers 		ssize_t isize = in.body.write.size;
100a87e0831SAlan Somers 		off_t iofs = in.body.write.offset;
101a87e0831SAlan Somers 		void *buf = out.body.bytes;
102a87e0831SAlan Somers 
103a87e0831SAlan Somers 		ASSERT_LE(0, pread(m_backing_fd, buf, isize, iofs))
104a87e0831SAlan Somers 			<< strerror(errno);
105a87e0831SAlan Somers 		out.header.len = sizeof(struct fuse_out_header) + isize;
106a87e0831SAlan Somers 	})));
107a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
108a87e0831SAlan Somers 		ResultOf([=](auto in) {
109a87e0831SAlan Somers 			uint32_t valid = FATTR_SIZE | FATTR_FH;
110a87e0831SAlan Somers 			return (in.header.opcode == FUSE_SETATTR &&
111a87e0831SAlan Somers 				in.header.nodeid == ino &&
112a87e0831SAlan Somers 				in.body.setattr.valid == valid);
113a87e0831SAlan Somers 		}, Eq(true)),
114a87e0831SAlan Somers 		_)
115a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
116a87e0831SAlan Somers 		ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
117a87e0831SAlan Somers 			<< strerror(errno);
118a87e0831SAlan Somers 		SET_OUT_HEADER_LEN(out, attr);
119a87e0831SAlan Somers 		out.body.attr.attr.ino = ino;
120a87e0831SAlan Somers 		out.body.attr.attr.mode = S_IFREG | 0755;
121a87e0831SAlan Somers 		out.body.attr.attr.size = in.body.setattr.size;
122a87e0831SAlan Somers 		out.body.attr.attr_valid = UINT64_MAX;
123a87e0831SAlan Somers 	})));
124a87e0831SAlan Somers 	/* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
125a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
126a87e0831SAlan Somers 		ResultOf([=](auto in) {
127a87e0831SAlan Somers 			return (in.header.opcode == FUSE_FLUSH &&
128a87e0831SAlan Somers 				in.header.nodeid == ino);
129a87e0831SAlan Somers 		}, Eq(true)),
130a87e0831SAlan Somers 		_)
131a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnErrno(0)));
132a87e0831SAlan Somers 	EXPECT_CALL(*m_mock, process(
133a87e0831SAlan Somers 		ResultOf([=](auto in) {
134a87e0831SAlan Somers 			return (in.header.opcode == FUSE_RELEASE &&
135a87e0831SAlan Somers 				in.header.nodeid == ino);
136a87e0831SAlan Somers 		}, Eq(true)),
137a87e0831SAlan Somers 		_)
138a87e0831SAlan Somers 	).WillRepeatedly(Invoke(ReturnErrno(0)));
139a87e0831SAlan Somers 
140a87e0831SAlan Somers 	m_test_fd = open(FULLPATH, O_RDWR );
141a87e0831SAlan Somers 	EXPECT_LE(0, m_test_fd) << strerror(errno);
142a87e0831SAlan Somers }
143a87e0831SAlan Somers 
144a87e0831SAlan Somers void TearDown()
145a87e0831SAlan Somers {
146a87e0831SAlan Somers 	if (m_backing_fd >= 0)
147a87e0831SAlan Somers 		close(m_backing_fd);
148a87e0831SAlan Somers 	if (m_control_fd >= 0)
149a87e0831SAlan Somers 		close(m_control_fd);
150a87e0831SAlan Somers 	FuseTest::TearDown();
151a87e0831SAlan Somers 	/* Deliberately leak test_fd */
152a87e0831SAlan Somers }
153a87e0831SAlan Somers 
154a87e0831SAlan Somers void do_ftruncate(off_t offs)
155a87e0831SAlan Somers {
156a87e0831SAlan Somers 	ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
157a87e0831SAlan Somers 	ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
158a87e0831SAlan Somers }
159a87e0831SAlan Somers 
160a87e0831SAlan Somers void do_read(ssize_t size, off_t offs)
161a87e0831SAlan Somers {
162a87e0831SAlan Somers 	void *test_buf, *control_buf;
163a87e0831SAlan Somers 
164a87e0831SAlan Somers 	test_buf = malloc(size);
165a87e0831SAlan Somers 	ASSERT_NE(NULL, test_buf) << strerror(errno);
166a87e0831SAlan Somers 	control_buf = malloc(size);
167a87e0831SAlan Somers 	ASSERT_NE(NULL, control_buf) << strerror(errno);
168a87e0831SAlan Somers 
169a87e0831SAlan Somers 	ASSERT_EQ(size, pread(m_test_fd, test_buf, size, offs))
170a87e0831SAlan Somers 		<< strerror(errno);
171a87e0831SAlan Somers 	ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
172a87e0831SAlan Somers 		<< strerror(errno);
173a87e0831SAlan Somers 
174a87e0831SAlan Somers 	ASSERT_EQ(0, memcmp(test_buf, control_buf, size));
175a87e0831SAlan Somers 
176a87e0831SAlan Somers 	free(control_buf);
177a87e0831SAlan Somers 	free(test_buf);
178a87e0831SAlan Somers }
179a87e0831SAlan Somers 
180a87e0831SAlan Somers void do_write(ssize_t size, off_t offs)
181a87e0831SAlan Somers {
182a87e0831SAlan Somers 	char *buf;
183a87e0831SAlan Somers 	long i;
184a87e0831SAlan Somers 
185a87e0831SAlan Somers 	buf = (char*)malloc(size);
186a87e0831SAlan Somers 	ASSERT_NE(NULL, buf) << strerror(errno);
187a87e0831SAlan Somers 	for (i=0; i < size; i++)
188a87e0831SAlan Somers 		buf[i] = random();
189a87e0831SAlan Somers 
190a87e0831SAlan Somers 	ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
191a87e0831SAlan Somers 		<< strerror(errno);
192a87e0831SAlan Somers 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
193a87e0831SAlan Somers 		<< strerror(errno);
194a87e0831SAlan Somers }
195a87e0831SAlan Somers 
196a87e0831SAlan Somers };
197a87e0831SAlan Somers 
198a87e0831SAlan Somers /*
199a87e0831SAlan Somers  * Extend a file with dirty data in the last page of the last block.
200a87e0831SAlan Somers  *
201a87e0831SAlan Somers  * fsx -WR -P /tmp -S8 -N3 fsx.bin
202a87e0831SAlan Somers  */
203a87e0831SAlan Somers TEST_F(Io, extend_from_dirty_page)
204a87e0831SAlan Somers {
205a87e0831SAlan Somers 	off_t wofs = 0x21a0;
206a87e0831SAlan Somers 	ssize_t wsize = 0xf0a8;
207a87e0831SAlan Somers 	off_t rofs = 0xb284;
208a87e0831SAlan Somers 	ssize_t rsize = 0x9b22;
209a87e0831SAlan Somers 	off_t truncsize = 0x28702;
210a87e0831SAlan Somers 
211a87e0831SAlan Somers 	do_write(wsize, wofs);
212a87e0831SAlan Somers 	do_ftruncate(truncsize);
213a87e0831SAlan Somers 	do_read(rsize, rofs);
214a87e0831SAlan Somers }
215a87e0831SAlan Somers 
216a87e0831SAlan Somers /*
217a87e0831SAlan Somers  * When writing the last page of a file, it must be written synchronously.
218a87e0831SAlan Somers  * Otherwise the cached page can become invalid by a subsequent extend
219a87e0831SAlan Somers  * operation.
220a87e0831SAlan Somers  *
221a87e0831SAlan Somers  * fsx -WR -P /tmp -S642 -N3 fsx.bin
222a87e0831SAlan Somers  */
223a87e0831SAlan Somers TEST_F(Io, last_page)
224a87e0831SAlan Somers {
225a87e0831SAlan Somers 	off_t wofs0 = 0x1134f;
226a87e0831SAlan Somers 	ssize_t wsize0 = 0xcc77;
227a87e0831SAlan Somers 	off_t wofs1 = 0x2096a;
228a87e0831SAlan Somers 	ssize_t wsize1 = 0xdfa7;
229a87e0831SAlan Somers 	off_t rofs = 0x1a3aa;
230a87e0831SAlan Somers 	ssize_t rsize = 0xb5b7;
231a87e0831SAlan Somers 
232a87e0831SAlan Somers 	do_write(wsize0, wofs0);
233a87e0831SAlan Somers 	do_write(wsize1, wofs1);
234a87e0831SAlan Somers 	do_read(rsize, rofs);
235a87e0831SAlan Somers }
236a87e0831SAlan Somers 
237a87e0831SAlan Somers /*
238a87e0831SAlan Somers  * Read a hole from a block that contains some cached data.
239a87e0831SAlan Somers  *
240a87e0831SAlan Somers  * fsx -WR -P /tmp -S55  fsx.bin
241a87e0831SAlan Somers  */
242a87e0831SAlan Somers TEST_F(Io, read_hole_from_cached_block)
243a87e0831SAlan Somers {
244a87e0831SAlan Somers 	off_t wofs = 0x160c5;
245a87e0831SAlan Somers 	ssize_t wsize = 0xa996;
246a87e0831SAlan Somers 	off_t rofs = 0x472e;
247a87e0831SAlan Somers 	ssize_t rsize = 0xd8d5;
248a87e0831SAlan Somers 
249a87e0831SAlan Somers 	do_write(wsize, wofs);
250a87e0831SAlan Somers 	do_read(rsize, rofs);
251a87e0831SAlan Somers }
252a87e0831SAlan Somers 
253a87e0831SAlan Somers /*
254*93c0c1d4SAlan Somers  * Truncating a file into a dirty buffer should not causing anything untoward
255*93c0c1d4SAlan Somers  * to happen when that buffer is eventually flushed.
256a87e0831SAlan Somers  *
257a87e0831SAlan Somers  * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
258a87e0831SAlan Somers  */
259*93c0c1d4SAlan Somers TEST_F(Io, truncate_into_dirty_buffer)
260a87e0831SAlan Somers {
261a87e0831SAlan Somers 	off_t wofs0 = 0x3bad7;
262a87e0831SAlan Somers 	ssize_t wsize0 = 0x4529;
263a87e0831SAlan Somers 	off_t wofs1 = 0xc30d;
264a87e0831SAlan Somers 	ssize_t wsize1 = 0x5f77;
265a87e0831SAlan Somers 	off_t truncsize0 = 0x10916;
266a87e0831SAlan Somers 	off_t rofs = 0xdf17;
267a87e0831SAlan Somers 	ssize_t rsize = 0x29ff;
268a87e0831SAlan Somers 	off_t truncsize1 = 0x152b4;
269a87e0831SAlan Somers 
270a87e0831SAlan Somers 	do_write(wsize0, wofs0);
271a87e0831SAlan Somers 	do_write(wsize1, wofs1);
272a87e0831SAlan Somers 	do_ftruncate(truncsize0);
273a87e0831SAlan Somers 	do_read(rsize, rofs);
274a87e0831SAlan Somers 	do_ftruncate(truncsize1);
275a87e0831SAlan Somers 	close(m_test_fd);
276a87e0831SAlan Somers }
277*93c0c1d4SAlan Somers 
278*93c0c1d4SAlan Somers /*
279*93c0c1d4SAlan Somers  * Truncating a file into a dirty buffer should not causing anything untoward
280*93c0c1d4SAlan Somers  * to happen when that buffer is eventually flushed, even when the buffer's
281*93c0c1d4SAlan Somers  * dirty_off is > 0.
282*93c0c1d4SAlan Somers  *
283*93c0c1d4SAlan Somers  * Based on this command with a few steps removed:
284*93c0c1d4SAlan Somers  * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
285*93c0c1d4SAlan Somers  */
286*93c0c1d4SAlan Somers TEST_F(Io, truncate_into_dirty_buffer2)
287*93c0c1d4SAlan Somers {
288*93c0c1d4SAlan Somers 	off_t truncsize0 = 0x344f3;
289*93c0c1d4SAlan Somers 	off_t wofs = 0x2790c;
290*93c0c1d4SAlan Somers 	ssize_t wsize = 0xd86a;
291*93c0c1d4SAlan Somers 	off_t truncsize1 = 0x2de38;
292*93c0c1d4SAlan Somers 	off_t rofs2 = 0x1fd7a;
293*93c0c1d4SAlan Somers 	ssize_t rsize2 = 0xc594;
294*93c0c1d4SAlan Somers 	off_t truncsize2 = 0x31e71;
295*93c0c1d4SAlan Somers 
296*93c0c1d4SAlan Somers 	/* Sets the file size to something larger than the next write */
297*93c0c1d4SAlan Somers 	do_ftruncate(truncsize0);
298*93c0c1d4SAlan Somers 	/*
299*93c0c1d4SAlan Somers 	 * Creates a dirty buffer.  The part in lbn 2 doesn't flush
300*93c0c1d4SAlan Somers 	 * synchronously.
301*93c0c1d4SAlan Somers 	 */
302*93c0c1d4SAlan Somers 	do_write(wsize, wofs);
303*93c0c1d4SAlan Somers 	/* Truncates part of the dirty buffer created in step 2 */
304*93c0c1d4SAlan Somers 	do_ftruncate(truncsize1);
305*93c0c1d4SAlan Somers 	/* XXX ?I don't know why this is necessary? */
306*93c0c1d4SAlan Somers 	do_read(rsize2, rofs2);
307*93c0c1d4SAlan Somers 	/* Truncates the dirty buffer */
308*93c0c1d4SAlan Somers 	do_ftruncate(truncsize2);
309*93c0c1d4SAlan Somers 	close(m_test_fd);
310*93c0c1d4SAlan Somers }
311