xref: /freebsd/tests/sys/fs/fusefs/io.cc (revision 9c9634d1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 #include <sys/sysctl.h>
37 
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 }
42 
43 #include "mockfs.hh"
44 #include "utils.hh"
45 
46 /*
47  * For testing I/O like fsx does, but deterministically and without a real
48  * underlying file system
49  *
50  * TODO: after fusefs gains the options to select cache mode for each mount
51  * point, run each of these tests for all cache modes.
52  */
53 
54 using namespace testing;
55 
56 enum cache_mode {
57 	Uncached,
58 	Writethrough,
59 	Writeback,
60 	WritebackAsync
61 };
62 
63 const char *cache_mode_to_s(enum cache_mode cm) {
64 	switch (cm) {
65 	case Uncached:
66 		return "Uncached";
67 	case Writethrough:
68 		return "Writethrough";
69 	case Writeback:
70 		return "Writeback";
71 	case WritebackAsync:
72 		return "WritebackAsync";
73 	default:
74 		return "Unknown";
75 	}
76 }
77 
78 const char FULLPATH[] = "mountpoint/some_file.txt";
79 const char RELPATH[] = "some_file.txt";
80 const uint64_t ino = 42;
81 
82 static void compare(const void *tbuf, const void *controlbuf, off_t baseofs,
83 	ssize_t size)
84 {
85 	int i;
86 
87 	for (i = 0; i < size; i++) {
88 		if (((const char*)tbuf)[i] != ((const char*)controlbuf)[i]) {
89 			off_t ofs = baseofs + i;
90 			FAIL() << "miscompare at offset "
91 			       << std::hex
92 			       << std::showbase
93 			       << ofs
94 			       << ".  expected = "
95 			       << std::setw(2)
96 			       << (unsigned)((const uint8_t*)controlbuf)[i]
97 			       << " got = "
98 			       << (unsigned)((const uint8_t*)tbuf)[i];
99 		}
100 	}
101 }
102 
103 typedef tuple<bool, uint32_t, cache_mode> IoParam;
104 
105 class Io: public FuseTest, public WithParamInterface<IoParam> {
106 public:
107 int m_backing_fd, m_control_fd, m_test_fd;
108 off_t m_filesize;
109 bool m_direct_io;
110 
111 Io(): m_backing_fd(-1), m_control_fd(-1), m_test_fd(-1), m_filesize(0),
112 	m_direct_io(false) {};
113 
114 void SetUp()
115 {
116 	m_backing_fd = open("backing_file", O_RDWR | O_CREAT | O_TRUNC, 0644);
117 	if (m_backing_fd < 0)
118 		FAIL() << strerror(errno);
119 	m_control_fd = open("control", O_RDWR | O_CREAT | O_TRUNC, 0644);
120 	if (m_control_fd < 0)
121 		FAIL() << strerror(errno);
122 	srandom(22'9'1982);	// Seed with my birthday
123 
124 	if (get<0>(GetParam()))
125 		m_init_flags |= FUSE_ASYNC_READ;
126 	m_maxwrite = get<1>(GetParam());
127 	switch (get<2>(GetParam())) {
128 		case Uncached:
129 			m_direct_io = true;
130 			break;
131 		case WritebackAsync:
132 			m_async = true;
133 			/* FALLTHROUGH */
134 		case Writeback:
135 			m_init_flags |= FUSE_WRITEBACK_CACHE;
136 			/* FALLTHROUGH */
137 		case Writethrough:
138 			break;
139 		default:
140 			FAIL() << "Unknown cache mode";
141 	}
142 
143 	FuseTest::SetUp();
144 	if (IsSkipped())
145 		return;
146 
147 	if (verbosity > 0) {
148 		printf("Test Parameters: init_flags=%#x maxwrite=%#x "
149 		    "%sasync cache=%s\n",
150 		    m_init_flags, m_maxwrite, m_async? "" : "no",
151 		    cache_mode_to_s(get<2>(GetParam())));
152 	}
153 
154 	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
155 	expect_open(ino, m_direct_io ? FOPEN_DIRECT_IO : 0, 1);
156 	EXPECT_CALL(*m_mock, process(
157 		ResultOf([=](auto in) {
158 			return (in.header.opcode == FUSE_WRITE &&
159 				in.header.nodeid == ino);
160 		}, Eq(true)),
161 		_)
162 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
163 		const char *buf = (const char*)in.body.bytes +
164 			sizeof(struct fuse_write_in);
165 		ssize_t isize = in.body.write.size;
166 		off_t iofs = in.body.write.offset;
167 
168 		ASSERT_EQ(isize, pwrite(m_backing_fd, buf, isize, iofs))
169 			<< strerror(errno);
170 		SET_OUT_HEADER_LEN(out, write);
171 		out.body.write.size = isize;
172 	})));
173 	EXPECT_CALL(*m_mock, process(
174 		ResultOf([=](auto in) {
175 			return (in.header.opcode == FUSE_READ &&
176 				in.header.nodeid == ino);
177 		}, Eq(true)),
178 		_)
179 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
180 		ssize_t isize = in.body.write.size;
181 		off_t iofs = in.body.write.offset;
182 		void *buf = out.body.bytes;
183 		ssize_t osize;
184 
185 		osize = pread(m_backing_fd, buf, isize, iofs);
186 		ASSERT_LE(0, osize) << strerror(errno);
187 		out.header.len = sizeof(struct fuse_out_header) + osize;
188 	})));
189 	EXPECT_CALL(*m_mock, process(
190 		ResultOf([=](auto in) {
191 			return (in.header.opcode == FUSE_SETATTR &&
192 				in.header.nodeid == ino &&
193 				(in.body.setattr.valid & FATTR_SIZE));
194 
195 		}, Eq(true)),
196 		_)
197 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
198 		ASSERT_EQ(0, ftruncate(m_backing_fd, in.body.setattr.size))
199 			<< strerror(errno);
200 		SET_OUT_HEADER_LEN(out, attr);
201 		out.body.attr.attr.ino = ino;
202 		out.body.attr.attr.mode = S_IFREG | 0755;
203 		out.body.attr.attr.size = in.body.setattr.size;
204 		out.body.attr.attr_valid = UINT64_MAX;
205 	})));
206 	/* Any test that close()s will send FUSE_FLUSH and FUSE_RELEASE */
207 	EXPECT_CALL(*m_mock, process(
208 		ResultOf([=](auto in) {
209 			return (in.header.opcode == FUSE_FLUSH &&
210 				in.header.nodeid == ino);
211 		}, Eq(true)),
212 		_)
213 	).WillRepeatedly(Invoke(ReturnErrno(0)));
214 	EXPECT_CALL(*m_mock, process(
215 		ResultOf([=](auto in) {
216 			return (in.header.opcode == FUSE_RELEASE &&
217 				in.header.nodeid == ino);
218 		}, Eq(true)),
219 		_)
220 	).WillRepeatedly(Invoke(ReturnErrno(0)));
221 
222 	m_test_fd = open(FULLPATH, O_RDWR );
223 	EXPECT_LE(0, m_test_fd) << strerror(errno);
224 }
225 
226 void TearDown()
227 {
228 	if (m_test_fd >= 0)
229 		close(m_test_fd);
230 	if (m_backing_fd >= 0)
231 		close(m_backing_fd);
232 	if (m_control_fd >= 0)
233 		close(m_control_fd);
234 	FuseTest::TearDown();
235 	leak(m_test_fd);
236 }
237 
238 void do_closeopen()
239 {
240 	ASSERT_EQ(0, close(m_test_fd)) << strerror(errno);
241 	m_test_fd = open("backing_file", O_RDWR);
242 	ASSERT_LE(0, m_test_fd) << strerror(errno);
243 
244 	ASSERT_EQ(0, close(m_control_fd)) << strerror(errno);
245 	m_control_fd = open("control", O_RDWR);
246 	ASSERT_LE(0, m_control_fd) << strerror(errno);
247 }
248 
249 void do_ftruncate(off_t offs)
250 {
251 	ASSERT_EQ(0, ftruncate(m_test_fd, offs)) << strerror(errno);
252 	ASSERT_EQ(0, ftruncate(m_control_fd, offs)) << strerror(errno);
253 	m_filesize = offs;
254 }
255 
256 void do_mapread(ssize_t size, off_t offs)
257 {
258 	void *control_buf, *p;
259 	off_t pg_offset, page_mask;
260 	size_t map_size;
261 
262 	page_mask = getpagesize() - 1;
263 	pg_offset = offs & page_mask;
264 	map_size = pg_offset + size;
265 
266 	p = mmap(NULL, map_size, PROT_READ, MAP_FILE | MAP_SHARED, m_test_fd,
267 	    offs - pg_offset);
268 	ASSERT_NE(p, MAP_FAILED) << strerror(errno);
269 
270 	control_buf = malloc(size);
271 	ASSERT_NE(nullptr, control_buf) << strerror(errno);
272 
273 	ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
274 		<< strerror(errno);
275 
276 	compare((void*)((char*)p + pg_offset), control_buf, offs, size);
277 
278 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
279 	free(control_buf);
280 }
281 
282 void do_read(ssize_t size, off_t offs)
283 {
284 	void *test_buf, *control_buf;
285 	ssize_t r;
286 
287 	test_buf = malloc(size);
288 	ASSERT_NE(nullptr, test_buf) << strerror(errno);
289 	control_buf = malloc(size);
290 	ASSERT_NE(nullptr, control_buf) << strerror(errno);
291 
292 	errno = 0;
293 	r = pread(m_test_fd, test_buf, size, offs);
294 	ASSERT_NE(-1, r) << strerror(errno);
295 	ASSERT_EQ(size, r) << "unexpected short read";
296 	r = pread(m_control_fd, control_buf, size, offs);
297 	ASSERT_NE(-1, r) << strerror(errno);
298 	ASSERT_EQ(size, r) << "unexpected short read";
299 
300 	compare(test_buf, control_buf, offs, size);
301 
302 	free(control_buf);
303 	free(test_buf);
304 }
305 
306 void do_mapwrite(ssize_t size, off_t offs)
307 {
308 	char *buf;
309 	void *p;
310 	off_t pg_offset, page_mask;
311 	size_t map_size;
312 	long i;
313 
314 	page_mask = getpagesize() - 1;
315 	pg_offset = offs & page_mask;
316 	map_size = pg_offset + size;
317 
318 	buf = (char*)malloc(size);
319 	ASSERT_NE(nullptr, buf) << strerror(errno);
320 	for (i=0; i < size; i++)
321 		buf[i] = random();
322 
323 	if (offs + size > m_filesize) {
324 		/*
325 		 * Must manually extend.  vm_mmap_vnode will not implicitly
326 		 * extend a vnode
327 		 */
328 		do_ftruncate(offs + size);
329 	}
330 
331 	p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
332 	    MAP_FILE | MAP_SHARED, m_test_fd, offs - pg_offset);
333 	ASSERT_NE(p, MAP_FAILED) << strerror(errno);
334 
335 	bcopy(buf, (char*)p + pg_offset, size);
336 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
337 		<< strerror(errno);
338 
339 	free(buf);
340 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
341 }
342 
343 void do_write(ssize_t size, off_t offs)
344 {
345 	char *buf;
346 	long i;
347 
348 	buf = (char*)malloc(size);
349 	ASSERT_NE(nullptr, buf) << strerror(errno);
350 	for (i=0; i < size; i++)
351 		buf[i] = random();
352 
353 	ASSERT_EQ(size, pwrite(m_test_fd, buf, size, offs ))
354 		<< strerror(errno);
355 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
356 		<< strerror(errno);
357 	m_filesize = std::max(m_filesize, offs + size);
358 
359 	free(buf);
360 }
361 
362 };
363 
364 class IoCacheable: public Io {
365 public:
366 virtual void SetUp() {
367 	Io::SetUp();
368 }
369 };
370 
371 /*
372  * Extend a file with dirty data in the last page of the last block.
373  *
374  * fsx -WR -P /tmp -S8 -N3 fsx.bin
375  */
376 TEST_P(Io, extend_from_dirty_page)
377 {
378 	off_t wofs = 0x21a0;
379 	ssize_t wsize = 0xf0a8;
380 	off_t rofs = 0xb284;
381 	ssize_t rsize = 0x9b22;
382 	off_t truncsize = 0x28702;
383 
384 	do_write(wsize, wofs);
385 	do_ftruncate(truncsize);
386 	do_read(rsize, rofs);
387 }
388 
389 /*
390  * mapwrite into a newly extended part of a file.
391  *
392  * fsx -c 100 -i 100 -l 524288 -o 131072 -N5 -P /tmp -S19 fsx.bin
393  */
394 TEST_P(IoCacheable, extend_by_mapwrite)
395 {
396 	do_mapwrite(0x849e, 0x29a3a);	/* [0x29a3a, 0x31ed7] */
397 	do_mapwrite(0x3994, 0x3c7d8);	/* [0x3c7d8, 0x4016b] */
398 	do_read(0xf556, 0x30c16);	/* [0x30c16, 0x4016b] */
399 }
400 
401 /*
402  * When writing the last page of a file, it must be written synchronously.
403  * Otherwise the cached page can become invalid by a subsequent extend
404  * operation.
405  *
406  * fsx -WR -P /tmp -S642 -N3 fsx.bin
407  */
408 TEST_P(Io, last_page)
409 {
410 	do_write(0xcc77, 0x1134f);	/* [0x1134f, 0x1dfc5] */
411 	do_write(0xdfa7, 0x2096a);	/* [0x2096a, 0x2e910] */
412 	do_read(0xb5b7, 0x1a3aa);	/* [0x1a3aa, 0x25960] */
413 }
414 
415 /*
416  * Read a hole using mmap
417  *
418  * fsx -c 100 -i 100 -l 524288 -o 131072 -N11 -P /tmp  -S14 fsx.bin
419  */
420 TEST_P(IoCacheable, mapread_hole)
421 {
422 	do_write(0x123b7, 0xf205);	/* [0xf205, 0x215bb] */
423 	do_mapread(0xeeea, 0x2f4c);	/* [0x2f4c, 0x11e35] */
424 }
425 
426 /*
427  * Read a hole from a block that contains some cached data.
428  *
429  * fsx -WR -P /tmp -S55  fsx.bin
430  */
431 TEST_P(Io, read_hole_from_cached_block)
432 {
433 	off_t wofs = 0x160c5;
434 	ssize_t wsize = 0xa996;
435 	off_t rofs = 0x472e;
436 	ssize_t rsize = 0xd8d5;
437 
438 	do_write(wsize, wofs);
439 	do_read(rsize, rofs);
440 }
441 
442 /*
443  * Truncating a file into a dirty buffer should not causing anything untoward
444  * to happen when that buffer is eventually flushed.
445  *
446  * fsx -WR -P /tmp -S839 -d -N6 fsx.bin
447  */
448 TEST_P(Io, truncate_into_dirty_buffer)
449 {
450 	off_t wofs0 = 0x3bad7;
451 	ssize_t wsize0 = 0x4529;
452 	off_t wofs1 = 0xc30d;
453 	ssize_t wsize1 = 0x5f77;
454 	off_t truncsize0 = 0x10916;
455 	off_t rofs = 0xdf17;
456 	ssize_t rsize = 0x29ff;
457 	off_t truncsize1 = 0x152b4;
458 
459 	do_write(wsize0, wofs0);
460 	do_write(wsize1, wofs1);
461 	do_ftruncate(truncsize0);
462 	do_read(rsize, rofs);
463 	do_ftruncate(truncsize1);
464 	close(m_test_fd);
465 }
466 
467 /*
468  * Truncating a file into a dirty buffer should not causing anything untoward
469  * to happen when that buffer is eventually flushed, even when the buffer's
470  * dirty_off is > 0.
471  *
472  * Based on this command with a few steps removed:
473  * fsx -WR -P /tmp -S677 -d -N8 fsx.bin
474  */
475 TEST_P(Io, truncate_into_dirty_buffer2)
476 {
477 	off_t truncsize0 = 0x344f3;
478 	off_t wofs = 0x2790c;
479 	ssize_t wsize = 0xd86a;
480 	off_t truncsize1 = 0x2de38;
481 	off_t rofs2 = 0x1fd7a;
482 	ssize_t rsize2 = 0xc594;
483 	off_t truncsize2 = 0x31e71;
484 
485 	/* Sets the file size to something larger than the next write */
486 	do_ftruncate(truncsize0);
487 	/*
488 	 * Creates a dirty buffer.  The part in lbn 2 doesn't flush
489 	 * synchronously.
490 	 */
491 	do_write(wsize, wofs);
492 	/* Truncates part of the dirty buffer created in step 2 */
493 	do_ftruncate(truncsize1);
494 	/* XXX ?I don't know why this is necessary? */
495 	do_read(rsize2, rofs2);
496 	/* Truncates the dirty buffer */
497 	do_ftruncate(truncsize2);
498 	close(m_test_fd);
499 }
500 
501 /*
502  * Regression test for a bug introduced in r348931
503  *
504  * Sequence of operations:
505  * 1) The first write reads lbn so it can modify it
506  * 2) The first write flushes lbn 3 immediately because it's the end of file
507  * 3) The first write then flushes lbn 4 because it's the end of the file
508  * 4) The second write modifies the cached versions of lbn 3 and 4
509  * 5) The third write's getblkx invalidates lbn 4's B_CACHE because it's
510  *    extending the buffer.  Then it flushes lbn 4 because B_DELWRI was set but
511  *    B_CACHE was clear.
512  * 6) fuse_write_biobackend erroneously called vfs_bio_clrbuf, putting the
513  *    buffer into a weird write-only state.  All read operations would return
514  *    0.  Writes were apparently still processed, because the buffer's contents
515  *    were correct when examined in a core dump.
516  * 7) The third write reads lbn 4 because cache is clear
517  * 9) uiomove dutifully copies new data into the buffer
518  * 10) The buffer's dirty is flushed to lbn 4
519  * 11) The read returns all zeros because of step 6.
520  *
521  * Based on:
522  * fsx -WR -l 524388 -o 131072 -P /tmp -S6456 -q  fsx.bin
523  */
524 TEST_P(Io, resize_a_valid_buffer_while_extending)
525 {
526 	do_write(0x14530, 0x36ee6);	/* [0x36ee6, 0x4b415] */
527 	do_write(0x1507c, 0x33256);	/* [0x33256, 0x482d1] */
528 	do_write(0x175c, 0x4c03d);	/* [0x4c03d, 0x4d798] */
529 	do_read(0xe277, 0x3599c);	/* [0x3599c, 0x43c12] */
530 	close(m_test_fd);
531 }
532 
533 INSTANTIATE_TEST_CASE_P(Io, Io,
534 	Combine(Bool(),					/* async read */
535 		Values(0x1000, 0x10000, 0x20000),	/* m_maxwrite */
536 		Values(Uncached, Writethrough, Writeback, WritebackAsync)
537 	)
538 );
539 
540 INSTANTIATE_TEST_CASE_P(Io, IoCacheable,
541 	Combine(Bool(),					/* async read */
542 		Values(0x1000, 0x10000, 0x20000),	/* m_maxwrite */
543 		Values(Writethrough, Writeback, WritebackAsync)
544 	)
545 );
546