xref: /freebsd/tests/sys/fs/fusefs/write.cc (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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/param.h>
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/uio.h>
40 
41 #include <aio.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <unistd.h>
45 }
46 
47 #include "mockfs.hh"
48 #include "utils.hh"
49 
50 using namespace testing;
51 
52 class Write: public FuseTest {
53 
54 public:
55 static sig_atomic_t s_sigxfsz;
56 
57 void SetUp() {
58 	s_sigxfsz = 0;
59 	FuseTest::SetUp();
60 }
61 
62 void TearDown() {
63 	struct sigaction sa;
64 
65 	bzero(&sa, sizeof(sa));
66 	sa.sa_handler = SIG_DFL;
67 	sigaction(SIGXFSZ, &sa, NULL);
68 
69 	FuseTest::TearDown();
70 }
71 
72 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
73 {
74 	FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
75 }
76 
77 void expect_release(uint64_t ino, ProcessMockerT r)
78 {
79 	EXPECT_CALL(*m_mock, process(
80 		ResultOf([=](auto in) {
81 			return (in.header.opcode == FUSE_RELEASE &&
82 				in.header.nodeid == ino);
83 		}, Eq(true)),
84 		_)
85 	).WillRepeatedly(Invoke(r));
86 }
87 
88 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
89 	uint64_t osize, const void *contents)
90 {
91 	FuseTest::expect_write(ino, offset, isize, osize, 0, 0, contents);
92 }
93 
94 /* Expect a write that may or may not come, depending on the cache mode */
95 void maybe_expect_write(uint64_t ino, uint64_t offset, uint64_t size,
96 	const void *contents)
97 {
98 	EXPECT_CALL(*m_mock, process(
99 		ResultOf([=](auto in) {
100 			const char *buf = (const char*)in.body.bytes +
101 				sizeof(struct fuse_write_in);
102 
103 			return (in.header.opcode == FUSE_WRITE &&
104 				in.header.nodeid == ino &&
105 				in.body.write.offset == offset  &&
106 				in.body.write.size == size &&
107 				0 == bcmp(buf, contents, size));
108 		}, Eq(true)),
109 		_)
110 	).Times(AtMost(1))
111 	.WillRepeatedly(Invoke(
112 		ReturnImmediate([=](auto in __unused, auto& out) {
113 			SET_OUT_HEADER_LEN(out, write);
114 			out.body.write.size = size;
115 		})
116 	));
117 }
118 
119 };
120 
121 sig_atomic_t Write::s_sigxfsz = 0;
122 
123 class Write_7_8: public FuseTest {
124 
125 public:
126 virtual void SetUp() {
127 	m_kernel_minor_version = 8;
128 	FuseTest::SetUp();
129 }
130 
131 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
132 {
133 	FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
134 }
135 
136 };
137 
138 class AioWrite: public Write {
139 virtual void SetUp() {
140 	if (!is_unsafe_aio_enabled())
141 		GTEST_SKIP() <<
142 			"vfs.aio.enable_unsafe must be set for this test";
143 	FuseTest::SetUp();
144 }
145 };
146 
147 /* Tests for the writeback cache mode */
148 class WriteBack: public Write {
149 public:
150 virtual void SetUp() {
151 	m_init_flags |= FUSE_WRITEBACK_CACHE;
152 	FuseTest::SetUp();
153 	if (IsSkipped())
154 		return;
155 }
156 
157 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
158 	uint64_t osize, const void *contents)
159 {
160 	FuseTest::expect_write(ino, offset, isize, osize, FUSE_WRITE_CACHE, 0,
161 		contents);
162 }
163 };
164 
165 class WriteBackAsync: public WriteBack {
166 public:
167 virtual void SetUp() {
168 	m_async = true;
169 	m_maxwrite = 65536;
170 	WriteBack::SetUp();
171 }
172 };
173 
174 class TimeGran: public WriteBackAsync, public WithParamInterface<unsigned> {
175 public:
176 virtual void SetUp() {
177 	m_time_gran = 1 << GetParam();
178 	WriteBackAsync::SetUp();
179 }
180 };
181 
182 /* Tests for clustered writes with WriteBack cacheing */
183 class WriteCluster: public WriteBack {
184 public:
185 virtual void SetUp() {
186 	m_async = true;
187 	m_maxwrite = 1 << 25;	// Anything larger than MAXPHYS will suffice
188 	WriteBack::SetUp();
189 	if (m_maxphys < 2 * DFLTPHYS)
190 		GTEST_SKIP() << "MAXPHYS must be at least twice DFLTPHYS"
191 			<< " for this test";
192 	if (m_maxphys < 2 * m_maxbcachebuf)
193 		GTEST_SKIP() << "MAXPHYS must be at least twice maxbcachebuf"
194 			<< " for this test";
195 }
196 };
197 
198 /* Tests relating to the server's max_write property */
199 class WriteMaxWrite: public Write {
200 public:
201 virtual void SetUp() {
202 	/*
203 	 * For this test, m_maxwrite must be less than either m_maxbcachebuf or
204 	 * maxphys.
205 	 */
206 	m_maxwrite = 32768;
207 	Write::SetUp();
208 }
209 };
210 
211 class WriteEofDuringVnopStrategy: public Write, public WithParamInterface<int>
212 {};
213 
214 void sigxfsz_handler(int __unused sig) {
215 	Write::s_sigxfsz = 1;
216 }
217 
218 /* AIO writes need to set the header's pid field correctly */
219 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
220 TEST_F(AioWrite, DISABLED_aio_write)
221 {
222 	const char FULLPATH[] = "mountpoint/some_file.txt";
223 	const char RELPATH[] = "some_file.txt";
224 	const char *CONTENTS = "abcdefgh";
225 	uint64_t ino = 42;
226 	uint64_t offset = 4096;
227 	int fd;
228 	ssize_t bufsize = strlen(CONTENTS);
229 	struct aiocb iocb, *piocb;
230 
231 	expect_lookup(RELPATH, ino, 0);
232 	expect_open(ino, 0, 1);
233 	expect_write(ino, offset, bufsize, bufsize, CONTENTS);
234 
235 	fd = open(FULLPATH, O_WRONLY);
236 	ASSERT_LE(0, fd) << strerror(errno);
237 
238 	iocb.aio_nbytes = bufsize;
239 	iocb.aio_fildes = fd;
240 	iocb.aio_buf = __DECONST(void *, CONTENTS);
241 	iocb.aio_offset = offset;
242 	iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
243 	ASSERT_EQ(0, aio_write(&iocb)) << strerror(errno);
244 	ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
245 	leak(fd);
246 }
247 
248 /*
249  * When a file is opened with O_APPEND, we should forward that flag to
250  * FUSE_OPEN (tested by Open.o_append) but still attempt to calculate the
251  * offset internally.  That way we'll work both with filesystems that
252  * understand O_APPEND (and ignore the offset) and filesystems that don't (and
253  * simply use the offset).
254  *
255  * Note that verifying the O_APPEND flag in FUSE_OPEN is done in the
256  * Open.o_append test.
257  */
258 TEST_F(Write, append)
259 {
260 	const ssize_t BUFSIZE = 9;
261 	const char FULLPATH[] = "mountpoint/some_file.txt";
262 	const char RELPATH[] = "some_file.txt";
263 	const char CONTENTS[BUFSIZE] = "abcdefgh";
264 	uint64_t ino = 42;
265 	/*
266 	 * Set offset to a maxbcachebuf boundary so we don't need to RMW when
267 	 * using writeback caching
268 	 */
269 	uint64_t initial_offset = m_maxbcachebuf;
270 	int fd;
271 
272 	expect_lookup(RELPATH, ino, initial_offset);
273 	expect_open(ino, 0, 1);
274 	expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
275 
276 	/* Must open O_RDWR or fuse(4) implicitly sets direct_io */
277 	fd = open(FULLPATH, O_RDWR | O_APPEND);
278 	ASSERT_LE(0, fd) << strerror(errno);
279 
280 	ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
281 	leak(fd);
282 }
283 
284 /* If a file is cached, then appending to the end should not cause a read */
285 TEST_F(Write, append_to_cached)
286 {
287 	const ssize_t BUFSIZE = 9;
288 	const char FULLPATH[] = "mountpoint/some_file.txt";
289 	const char RELPATH[] = "some_file.txt";
290 	char *oldcontents, *oldbuf;
291 	const char CONTENTS[BUFSIZE] = "abcdefgh";
292 	uint64_t ino = 42;
293 	/*
294 	 * Set offset in between maxbcachebuf boundary to test buffer handling
295 	 */
296 	uint64_t oldsize = m_maxbcachebuf / 2;
297 	int fd;
298 
299 	oldcontents = (char*)calloc(1, oldsize);
300 	ASSERT_NE(nullptr, oldcontents) << strerror(errno);
301 	oldbuf = (char*)malloc(oldsize);
302 	ASSERT_NE(nullptr, oldbuf) << strerror(errno);
303 
304 	expect_lookup(RELPATH, ino, oldsize);
305 	expect_open(ino, 0, 1);
306 	expect_read(ino, 0, oldsize, oldsize, oldcontents);
307 	maybe_expect_write(ino, oldsize, BUFSIZE, CONTENTS);
308 
309 	/* Must open O_RDWR or fuse(4) implicitly sets direct_io */
310 	fd = open(FULLPATH, O_RDWR | O_APPEND);
311 	ASSERT_LE(0, fd) << strerror(errno);
312 
313 	/* Read the old data into the cache */
314 	ASSERT_EQ((ssize_t)oldsize, read(fd, oldbuf, oldsize))
315 		<< strerror(errno);
316 
317 	/* Write the new data.  There should be no more read operations */
318 	ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
319 	leak(fd);
320 	free(oldbuf);
321 	free(oldcontents);
322 }
323 
324 TEST_F(Write, append_direct_io)
325 {
326 	const ssize_t BUFSIZE = 9;
327 	const char FULLPATH[] = "mountpoint/some_file.txt";
328 	const char RELPATH[] = "some_file.txt";
329 	const char CONTENTS[BUFSIZE] = "abcdefgh";
330 	uint64_t ino = 42;
331 	uint64_t initial_offset = 4096;
332 	int fd;
333 
334 	expect_lookup(RELPATH, ino, initial_offset);
335 	expect_open(ino, FOPEN_DIRECT_IO, 1);
336 	expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
337 
338 	fd = open(FULLPATH, O_WRONLY | O_APPEND);
339 	ASSERT_LE(0, fd) << strerror(errno);
340 
341 	ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
342 	leak(fd);
343 }
344 
345 /* A direct write should evict any overlapping cached data */
346 TEST_F(Write, direct_io_evicts_cache)
347 {
348 	const char FULLPATH[] = "mountpoint/some_file.txt";
349 	const char RELPATH[] = "some_file.txt";
350 	const char CONTENTS0[] = "abcdefgh";
351 	const char CONTENTS1[] = "ijklmnop";
352 	uint64_t ino = 42;
353 	int fd;
354 	ssize_t bufsize = strlen(CONTENTS0) + 1;
355 	char readbuf[bufsize];
356 
357 	expect_lookup(RELPATH, ino, bufsize);
358 	expect_open(ino, 0, 1);
359 	expect_read(ino, 0, bufsize, bufsize, CONTENTS0);
360 	expect_write(ino, 0, bufsize, bufsize, CONTENTS1);
361 
362 	fd = open(FULLPATH, O_RDWR);
363 	ASSERT_LE(0, fd) << strerror(errno);
364 
365 	// Prime cache
366 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
367 
368 	// Write directly, evicting cache
369 	ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
370 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
371 	ASSERT_EQ(bufsize, write(fd, CONTENTS1, bufsize)) << strerror(errno);
372 
373 	// Read again.  Cache should be bypassed
374 	expect_read(ino, 0, bufsize, bufsize, CONTENTS1);
375 	ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
376 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
377 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
378 	ASSERT_STREQ(readbuf, CONTENTS1);
379 
380 	leak(fd);
381 }
382 
383 /*
384  * If the server doesn't return FOPEN_DIRECT_IO during FUSE_OPEN, then it's not
385  * allowed to return a short write for that file handle.  However, if it does
386  * then we should still do our darndest to handle it by resending the unwritten
387  * portion.
388  */
389 TEST_F(Write, indirect_io_short_write)
390 {
391 	const char FULLPATH[] = "mountpoint/some_file.txt";
392 	const char RELPATH[] = "some_file.txt";
393 	const char *CONTENTS = "abcdefghijklmnop";
394 	uint64_t ino = 42;
395 	int fd;
396 	ssize_t bufsize = strlen(CONTENTS);
397 	ssize_t bufsize0 = 11;
398 	ssize_t bufsize1 = strlen(CONTENTS) - bufsize0;
399 	const char *contents1 = CONTENTS + bufsize0;
400 
401 	expect_lookup(RELPATH, ino, 0);
402 	expect_open(ino, 0, 1);
403 	expect_write(ino, 0, bufsize, bufsize0, CONTENTS);
404 	expect_write(ino, bufsize0, bufsize1, bufsize1, contents1);
405 
406 	fd = open(FULLPATH, O_WRONLY);
407 	ASSERT_LE(0, fd) << strerror(errno);
408 
409 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
410 	leak(fd);
411 }
412 
413 /* It is an error if the daemon claims to have written more data than we sent */
414 TEST_F(Write, indirect_io_long_write)
415 {
416 	const char FULLPATH[] = "mountpoint/some_file.txt";
417 	const char RELPATH[] = "some_file.txt";
418 	const char *CONTENTS = "abcdefghijklmnop";
419 	uint64_t ino = 42;
420 	int fd;
421 	ssize_t bufsize = strlen(CONTENTS);
422 	ssize_t bufsize_out = 100;
423 	off_t some_other_size = 25;
424 	struct stat sb;
425 
426 	expect_lookup(RELPATH, ino, 0);
427 	expect_open(ino, 0, 1);
428 	expect_write(ino, 0, bufsize, bufsize_out, CONTENTS);
429 	expect_getattr(ino, some_other_size);
430 
431 	fd = open(FULLPATH, O_WRONLY);
432 	ASSERT_LE(0, fd) << strerror(errno);
433 
434 	ASSERT_EQ(-1, write(fd, CONTENTS, bufsize)) << strerror(errno);
435 	ASSERT_EQ(EINVAL, errno);
436 
437 	/*
438 	 * Following such an error, we should requery the server for the file's
439 	 * size.
440 	 */
441 	fstat(fd, &sb);
442 	ASSERT_EQ(sb.st_size, some_other_size);
443 
444 	leak(fd);
445 }
446 
447 /*
448  * Don't crash if the server returns a write that can't be represented as a
449  * signed 32 bit number.  Regression test for
450  * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263263
451  */
452 TEST_F(Write, indirect_io_very_long_write)
453 {
454 	const char FULLPATH[] = "mountpoint/some_file.txt";
455 	const char RELPATH[] = "some_file.txt";
456 	const char *CONTENTS = "abcdefghijklmnop";
457 	uint64_t ino = 42;
458 	int fd;
459 	ssize_t bufsize = strlen(CONTENTS);
460 	ssize_t bufsize_out = 3 << 30;
461 
462 	expect_lookup(RELPATH, ino, 0);
463 	expect_open(ino, 0, 1);
464 	expect_write(ino, 0, bufsize, bufsize_out, CONTENTS);
465 
466 	fd = open(FULLPATH, O_WRONLY);
467 	ASSERT_LE(0, fd) << strerror(errno);
468 
469 	ASSERT_EQ(-1, write(fd, CONTENTS, bufsize)) << strerror(errno);
470 	ASSERT_EQ(EINVAL, errno);
471 	leak(fd);
472 }
473 
474 /*
475  * When the direct_io option is used, filesystems are allowed to write less
476  * data than requested.  We should return the short write to userland.
477  */
478 TEST_F(Write, direct_io_short_write)
479 {
480 	const char FULLPATH[] = "mountpoint/some_file.txt";
481 	const char RELPATH[] = "some_file.txt";
482 	const char *CONTENTS = "abcdefghijklmnop";
483 	uint64_t ino = 42;
484 	int fd;
485 	ssize_t bufsize = strlen(CONTENTS);
486 	ssize_t halfbufsize = bufsize / 2;
487 
488 	expect_lookup(RELPATH, ino, 0);
489 	expect_open(ino, FOPEN_DIRECT_IO, 1);
490 	expect_write(ino, 0, bufsize, halfbufsize, CONTENTS);
491 
492 	fd = open(FULLPATH, O_WRONLY);
493 	ASSERT_LE(0, fd) << strerror(errno);
494 
495 	ASSERT_EQ(halfbufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
496 	leak(fd);
497 }
498 
499 /*
500  * An insidious edge case: the filesystem returns a short write, and the
501  * difference between what we requested and what it actually wrote crosses an
502  * iov element boundary
503  */
504 TEST_F(Write, direct_io_short_write_iov)
505 {
506 	const char FULLPATH[] = "mountpoint/some_file.txt";
507 	const char RELPATH[] = "some_file.txt";
508 	const char *CONTENTS0 = "abcdefgh";
509 	const char *CONTENTS1 = "ijklmnop";
510 	const char *EXPECTED0 = "abcdefghijklmnop";
511 	uint64_t ino = 42;
512 	int fd;
513 	ssize_t size0 = strlen(CONTENTS0) - 1;
514 	ssize_t size1 = strlen(CONTENTS1) + 1;
515 	ssize_t totalsize = size0 + size1;
516 	struct iovec iov[2];
517 
518 	expect_lookup(RELPATH, ino, 0);
519 	expect_open(ino, FOPEN_DIRECT_IO, 1);
520 	expect_write(ino, 0, totalsize, size0, EXPECTED0);
521 
522 	fd = open(FULLPATH, O_WRONLY);
523 	ASSERT_LE(0, fd) << strerror(errno);
524 
525 	iov[0].iov_base = __DECONST(void*, CONTENTS0);
526 	iov[0].iov_len = strlen(CONTENTS0);
527 	iov[1].iov_base = __DECONST(void*, CONTENTS1);
528 	iov[1].iov_len = strlen(CONTENTS1);
529 	ASSERT_EQ(size0, writev(fd, iov, 2)) << strerror(errno);
530 	leak(fd);
531 }
532 
533 /* fusefs should respect RLIMIT_FSIZE */
534 TEST_F(Write, rlimit_fsize)
535 {
536 	const char FULLPATH[] = "mountpoint/some_file.txt";
537 	const char RELPATH[] = "some_file.txt";
538 	const char *CONTENTS = "abcdefgh";
539 	struct rlimit rl;
540 	ssize_t bufsize = strlen(CONTENTS);
541 	off_t offset = 1'000'000'000;
542 	uint64_t ino = 42;
543 	int fd;
544 
545 	expect_lookup(RELPATH, ino, 0);
546 	expect_open(ino, 0, 1);
547 
548 	rl.rlim_cur = offset;
549 	rl.rlim_max = 10 * offset;
550 	ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rl)) << strerror(errno);
551 	ASSERT_NE(SIG_ERR, signal(SIGXFSZ, sigxfsz_handler)) << strerror(errno);
552 
553 	fd = open(FULLPATH, O_WRONLY);
554 
555 	ASSERT_LE(0, fd) << strerror(errno);
556 
557 	ASSERT_EQ(-1, pwrite(fd, CONTENTS, bufsize, offset));
558 	EXPECT_EQ(EFBIG, errno);
559 	EXPECT_EQ(1, s_sigxfsz);
560 	leak(fd);
561 }
562 
563 /*
564  * A short read indicates EOF.  Test that nothing bad happens if we get EOF
565  * during the R of a RMW operation.
566  */
567 TEST_F(Write, eof_during_rmw)
568 {
569 	const char FULLPATH[] = "mountpoint/some_file.txt";
570 	const char RELPATH[] = "some_file.txt";
571 	const char *CONTENTS = "abcdefgh";
572 	const char *INITIAL   = "XXXXXXXXXX";
573 	uint64_t ino = 42;
574 	uint64_t offset = 1;
575 	ssize_t bufsize = strlen(CONTENTS) + 1;
576 	off_t orig_fsize = 10;
577 	off_t truncated_fsize = 5;
578 	int fd;
579 
580 	FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, orig_fsize, 1);
581 	expect_open(ino, 0, 1);
582 	expect_read(ino, 0, orig_fsize, truncated_fsize, INITIAL, O_RDWR);
583 	maybe_expect_write(ino, offset, bufsize, CONTENTS);
584 
585 	fd = open(FULLPATH, O_RDWR);
586 	ASSERT_LE(0, fd) << strerror(errno);
587 
588 	ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
589 		<< strerror(errno);
590 	leak(fd);
591 }
592 
593 /*
594  * VOP_STRATEGY should not query the server for the file's size, even if its
595  * cached attributes have expired.
596  * Regression test for https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=256937
597  */
598 TEST_P(WriteEofDuringVnopStrategy, eof_during_vop_strategy)
599 {
600 	const char FULLPATH[] = "mountpoint/some_file.txt";
601 	const char RELPATH[] = "some_file.txt";
602 	Sequence seq;
603 	const off_t filesize = 2 * m_maxbcachebuf;
604 	void *contents;
605 	uint64_t ino = 42;
606 	uint64_t attr_valid = 0;
607 	uint64_t attr_valid_nsec = 0;
608 	mode_t mode = S_IFREG | 0644;
609 	int fd;
610 	int ngetattrs;
611 
612 	ngetattrs = GetParam();
613 	contents = calloc(1, filesize);
614 
615 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
616 	.WillRepeatedly(Invoke(
617 		ReturnImmediate([=](auto in __unused, auto& out) {
618 		SET_OUT_HEADER_LEN(out, entry);
619 		out.body.entry.attr.mode = mode;
620 		out.body.entry.nodeid = ino;
621 		out.body.entry.attr.nlink = 1;
622 		out.body.entry.attr.size = filesize;
623 		out.body.entry.attr_valid = attr_valid;
624 		out.body.entry.attr_valid_nsec = attr_valid_nsec;
625 	})));
626 	expect_open(ino, 0, 1);
627 	EXPECT_CALL(*m_mock, process(
628 		ResultOf([=](auto in) {
629 			return (in.header.opcode == FUSE_GETATTR &&
630 				in.header.nodeid == ino);
631 		}, Eq(true)),
632 		_)
633 	).Times(Between(ngetattrs - 1, ngetattrs))
634 	.InSequence(seq)
635 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
636 		SET_OUT_HEADER_LEN(out, attr);
637 		out.body.attr.attr.ino = ino;
638 		out.body.attr.attr.mode = mode;
639 		out.body.attr.attr_valid = attr_valid;
640 		out.body.attr.attr_valid_nsec = attr_valid_nsec;
641 		out.body.attr.attr.size = filesize;
642 	})));
643 	EXPECT_CALL(*m_mock, process(
644 		ResultOf([=](auto in) {
645 			return (in.header.opcode == FUSE_GETATTR &&
646 				in.header.nodeid == ino);
647 		}, Eq(true)),
648 		_)
649 	).InSequence(seq)
650 	.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
651 		SET_OUT_HEADER_LEN(out, attr);
652 		out.body.attr.attr.ino = ino;
653 		out.body.attr.attr.mode = mode;
654 		out.body.attr.attr_valid = attr_valid;
655 		out.body.attr.attr_valid_nsec = attr_valid_nsec;
656 		out.body.attr.attr.size = filesize / 2;
657 	})));
658 	expect_write(ino, 0, filesize / 2, filesize / 2, contents);
659 
660 	fd = open(FULLPATH, O_RDWR);
661 	ASSERT_LE(0, fd) << strerror(errno);
662 	ASSERT_EQ(filesize / 2, write(fd, contents, filesize / 2))
663 		<< strerror(errno);
664 
665 }
666 
667 INSTANTIATE_TEST_CASE_P(W, WriteEofDuringVnopStrategy,
668 	Values(1, 2, 3)
669 );
670 
671 /*
672  * If the kernel cannot be sure which uid, gid, or pid was responsible for a
673  * write, then it must set the FUSE_WRITE_CACHE bit
674  */
675 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236378 */
676 TEST_F(Write, mmap)
677 {
678 	const char FULLPATH[] = "mountpoint/some_file.txt";
679 	const char RELPATH[] = "some_file.txt";
680 	const char *CONTENTS = "abcdefgh";
681 	uint64_t ino = 42;
682 	int fd;
683 	ssize_t bufsize = strlen(CONTENTS);
684 	void *p;
685 	uint64_t offset = 10;
686 	size_t len;
687 	void *zeros, *expected;
688 
689 	len = getpagesize();
690 
691 	zeros = calloc(1, len);
692 	ASSERT_NE(nullptr, zeros);
693 	expected = calloc(1, len);
694 	ASSERT_NE(nullptr, expected);
695 	memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
696 
697 	expect_lookup(RELPATH, ino, len);
698 	expect_open(ino, 0, 1);
699 	expect_read(ino, 0, len, len, zeros);
700 	/*
701 	 * Writes from the pager may or may not be associated with the correct
702 	 * pid, so they must set FUSE_WRITE_CACHE.
703 	 */
704 	FuseTest::expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, 0, expected);
705 	expect_flush(ino, 1, ReturnErrno(0));
706 	expect_release(ino, ReturnErrno(0));
707 
708 	fd = open(FULLPATH, O_RDWR);
709 	ASSERT_LE(0, fd) << strerror(errno);
710 
711 	p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
712 	ASSERT_NE(MAP_FAILED, p) << strerror(errno);
713 
714 	memmove((uint8_t*)p + offset, CONTENTS, bufsize);
715 
716 	ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
717 	close(fd);	// Write mmap'd data on close
718 
719 	free(expected);
720 	free(zeros);
721 
722 	leak(fd);
723 }
724 
725 TEST_F(Write, pwrite)
726 {
727 	const char FULLPATH[] = "mountpoint/some_file.txt";
728 	const char RELPATH[] = "some_file.txt";
729 	const char *CONTENTS = "abcdefgh";
730 	uint64_t ino = 42;
731 	uint64_t offset = m_maxbcachebuf;
732 	int fd;
733 	ssize_t bufsize = strlen(CONTENTS);
734 
735 	expect_lookup(RELPATH, ino, 0);
736 	expect_open(ino, 0, 1);
737 	expect_write(ino, offset, bufsize, bufsize, CONTENTS);
738 
739 	fd = open(FULLPATH, O_WRONLY);
740 	ASSERT_LE(0, fd) << strerror(errno);
741 
742 	ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
743 		<< strerror(errno);
744 	leak(fd);
745 }
746 
747 /* Writing a file should update its cached mtime and ctime */
748 TEST_F(Write, timestamps)
749 {
750 	const char FULLPATH[] = "mountpoint/some_file.txt";
751 	const char RELPATH[] = "some_file.txt";
752 	const char *CONTENTS = "abcdefgh";
753 	ssize_t bufsize = strlen(CONTENTS);
754 	uint64_t ino = 42;
755 	struct stat sb0, sb1;
756 	int fd;
757 
758 	expect_lookup(RELPATH, ino, 0);
759 	expect_open(ino, 0, 1);
760 	maybe_expect_write(ino, 0, bufsize, CONTENTS);
761 
762 	fd = open(FULLPATH, O_RDWR);
763 	ASSERT_LE(0, fd) << strerror(errno);
764 	ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno);
765 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
766 
767 	nap();
768 
769 	ASSERT_EQ(0, fstat(fd, &sb1)) << strerror(errno);
770 
771 	EXPECT_EQ(sb0.st_atime, sb1.st_atime);
772 	EXPECT_NE(sb0.st_mtime, sb1.st_mtime);
773 	EXPECT_NE(sb0.st_ctime, sb1.st_ctime);
774 
775 	leak(fd);
776 }
777 
778 TEST_F(Write, write)
779 {
780 	const char FULLPATH[] = "mountpoint/some_file.txt";
781 	const char RELPATH[] = "some_file.txt";
782 	const char *CONTENTS = "abcdefgh";
783 	uint64_t ino = 42;
784 	int fd;
785 	ssize_t bufsize = strlen(CONTENTS);
786 
787 	expect_lookup(RELPATH, ino, 0);
788 	expect_open(ino, 0, 1);
789 	expect_write(ino, 0, bufsize, bufsize, CONTENTS);
790 
791 	fd = open(FULLPATH, O_WRONLY);
792 	ASSERT_LE(0, fd) << strerror(errno);
793 
794 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
795 	leak(fd);
796 }
797 
798 /* fuse(4) should not issue writes of greater size than the daemon requests */
799 TEST_F(WriteMaxWrite, write)
800 {
801 	const char FULLPATH[] = "mountpoint/some_file.txt";
802 	const char RELPATH[] = "some_file.txt";
803 	int *contents;
804 	uint64_t ino = 42;
805 	int fd;
806 	ssize_t halfbufsize, bufsize;
807 
808 	halfbufsize = m_mock->m_maxwrite;
809 	if (halfbufsize >= m_maxbcachebuf || halfbufsize >= m_maxphys)
810 		GTEST_SKIP() << "Must lower m_maxwrite for this test";
811 	bufsize = halfbufsize * 2;
812 	contents = (int*)malloc(bufsize);
813 	ASSERT_NE(nullptr, contents);
814 	for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
815 		contents[i] = i;
816 	}
817 
818 	expect_lookup(RELPATH, ino, 0);
819 	expect_open(ino, 0, 1);
820 	maybe_expect_write(ino, 0, halfbufsize, contents);
821 	maybe_expect_write(ino, halfbufsize, halfbufsize,
822 		&contents[halfbufsize / sizeof(int)]);
823 
824 	fd = open(FULLPATH, O_WRONLY);
825 	ASSERT_LE(0, fd) << strerror(errno);
826 
827 	ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
828 	leak(fd);
829 
830 	free(contents);
831 }
832 
833 TEST_F(Write, write_nothing)
834 {
835 	const char FULLPATH[] = "mountpoint/some_file.txt";
836 	const char RELPATH[] = "some_file.txt";
837 	const char *CONTENTS = "";
838 	uint64_t ino = 42;
839 	int fd;
840 	ssize_t bufsize = 0;
841 
842 	expect_lookup(RELPATH, ino, 0);
843 	expect_open(ino, 0, 1);
844 
845 	fd = open(FULLPATH, O_WRONLY);
846 	ASSERT_LE(0, fd) << strerror(errno);
847 
848 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
849 	leak(fd);
850 }
851 
852 TEST_F(Write_7_8, write)
853 {
854 	const char FULLPATH[] = "mountpoint/some_file.txt";
855 	const char RELPATH[] = "some_file.txt";
856 	const char *CONTENTS = "abcdefgh";
857 	uint64_t ino = 42;
858 	int fd;
859 	ssize_t bufsize = strlen(CONTENTS);
860 
861 	expect_lookup(RELPATH, ino, 0);
862 	expect_open(ino, 0, 1);
863 	expect_write_7_8(ino, 0, bufsize, bufsize, CONTENTS);
864 
865 	fd = open(FULLPATH, O_WRONLY);
866 	ASSERT_LE(0, fd) << strerror(errno);
867 
868 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
869 	leak(fd);
870 }
871 
872 /* In writeback mode, dirty data should be written on close */
873 TEST_F(WriteBackAsync, close)
874 {
875 	const char FULLPATH[] = "mountpoint/some_file.txt";
876 	const char RELPATH[] = "some_file.txt";
877 	const char *CONTENTS = "abcdefgh";
878 	uint64_t ino = 42;
879 	int fd;
880 	ssize_t bufsize = strlen(CONTENTS);
881 
882 	expect_lookup(RELPATH, ino, 0);
883 	expect_open(ino, 0, 1);
884 	expect_write(ino, 0, bufsize, bufsize, CONTENTS);
885 	EXPECT_CALL(*m_mock, process(
886 		ResultOf([=](auto in) {
887 			return (in.header.opcode == FUSE_SETATTR);
888 		}, Eq(true)),
889 		_)
890 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
891 		SET_OUT_HEADER_LEN(out, attr);
892 		out.body.attr.attr.ino = ino;	// Must match nodeid
893 	})));
894 	expect_flush(ino, 1, ReturnErrno(0));
895 	expect_release(ino, ReturnErrno(0));
896 
897 	fd = open(FULLPATH, O_RDWR);
898 	ASSERT_LE(0, fd) << strerror(errno);
899 
900 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
901 	close(fd);
902 }
903 
904 /* In writeback mode, adjacent writes will be clustered together */
905 TEST_F(WriteCluster, clustering)
906 {
907 	const char FULLPATH[] = "mountpoint/some_file.txt";
908 	const char RELPATH[] = "some_file.txt";
909 	uint64_t ino = 42;
910 	int i, fd;
911 	void *wbuf, *wbuf2x;
912 	ssize_t bufsize = m_maxbcachebuf;
913 	off_t filesize = 5 * bufsize;
914 
915 	wbuf = malloc(bufsize);
916 	ASSERT_NE(nullptr, wbuf) << strerror(errno);
917 	memset(wbuf, 'X', bufsize);
918 	wbuf2x = malloc(2 * bufsize);
919 	ASSERT_NE(nullptr, wbuf2x) << strerror(errno);
920 	memset(wbuf2x, 'X', 2 * bufsize);
921 
922 	expect_lookup(RELPATH, ino, filesize);
923 	expect_open(ino, 0, 1);
924 	/*
925 	 * Writes of bufsize-bytes each should be clustered into greater sizes.
926 	 * The amount of clustering is adaptive, so the first write actually
927 	 * issued will be 2x bufsize and subsequent writes may be larger
928 	 */
929 	expect_write(ino, 0, 2 * bufsize, 2 * bufsize, wbuf2x);
930 	expect_write(ino, 2 * bufsize, 2 * bufsize, 2 * bufsize, wbuf2x);
931 	expect_flush(ino, 1, ReturnErrno(0));
932 	expect_release(ino, ReturnErrno(0));
933 
934 	fd = open(FULLPATH, O_RDWR);
935 	ASSERT_LE(0, fd) << strerror(errno);
936 
937 	for (i = 0; i < 4; i++) {
938 		ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
939 			<< strerror(errno);
940 	}
941 	close(fd);
942 	free(wbuf2x);
943 	free(wbuf);
944 }
945 
946 /*
947  * When clustering writes, an I/O error to any of the cluster's children should
948  * not panic the system on unmount
949  */
950 /*
951  * Regression test for bug 238585
952  * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=238565
953  */
954 TEST_F(WriteCluster, cluster_write_err)
955 {
956 	const char FULLPATH[] = "mountpoint/some_file.txt";
957 	const char RELPATH[] = "some_file.txt";
958 	uint64_t ino = 42;
959 	int i, fd;
960 	void *wbuf;
961 	ssize_t bufsize = m_maxbcachebuf;
962 	off_t filesize = 4 * bufsize;
963 
964 	wbuf = malloc(bufsize);
965 	ASSERT_NE(nullptr, wbuf) << strerror(errno);
966 	memset(wbuf, 'X', bufsize);
967 
968 	expect_lookup(RELPATH, ino, filesize);
969 	expect_open(ino, 0, 1);
970 	EXPECT_CALL(*m_mock, process(
971 		ResultOf([=](auto in) {
972 			return (in.header.opcode == FUSE_WRITE);
973 		}, Eq(true)),
974 		_)
975 	).WillRepeatedly(Invoke(ReturnErrno(EIO)));
976 	expect_flush(ino, 1, ReturnErrno(0));
977 	expect_release(ino, ReturnErrno(0));
978 
979 	fd = open(FULLPATH, O_RDWR);
980 	ASSERT_LE(0, fd) << strerror(errno);
981 
982 	for (i = 0; i < 3; i++) {
983 		ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
984 			<< strerror(errno);
985 	}
986 	close(fd);
987 	free(wbuf);
988 }
989 
990 /*
991  * In writeback mode, writes to an O_WRONLY file could trigger reads from the
992  * server.  The FUSE protocol explicitly allows that.
993  */
994 TEST_F(WriteBack, rmw)
995 {
996 	const char FULLPATH[] = "mountpoint/some_file.txt";
997 	const char RELPATH[] = "some_file.txt";
998 	const char *CONTENTS = "abcdefgh";
999 	const char *INITIAL   = "XXXXXXXXXX";
1000 	uint64_t ino = 42;
1001 	uint64_t offset = 1;
1002 	off_t fsize = 10;
1003 	int fd;
1004 	ssize_t bufsize = strlen(CONTENTS);
1005 
1006 	FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
1007 	expect_open(ino, 0, 1);
1008 	expect_read(ino, 0, fsize, fsize, INITIAL, O_WRONLY);
1009 	maybe_expect_write(ino, offset, bufsize, CONTENTS);
1010 
1011 	fd = open(FULLPATH, O_WRONLY);
1012 	ASSERT_LE(0, fd) << strerror(errno);
1013 
1014 	ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
1015 		<< strerror(errno);
1016 	leak(fd);
1017 }
1018 
1019 /*
1020  * Without direct_io, writes should be committed to cache
1021  */
1022 TEST_F(WriteBack, cache)
1023 {
1024 	const char FULLPATH[] = "mountpoint/some_file.txt";
1025 	const char RELPATH[] = "some_file.txt";
1026 	const char *CONTENTS = "abcdefgh";
1027 	uint64_t ino = 42;
1028 	int fd;
1029 	ssize_t bufsize = strlen(CONTENTS);
1030 	uint8_t readbuf[bufsize];
1031 
1032 	expect_lookup(RELPATH, ino, 0);
1033 	expect_open(ino, 0, 1);
1034 	expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1035 
1036 	fd = open(FULLPATH, O_RDWR);
1037 	ASSERT_LE(0, fd) << strerror(errno);
1038 
1039 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1040 	/*
1041 	 * A subsequent read should be serviced by cache, without querying the
1042 	 * filesystem daemon
1043 	 */
1044 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1045 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1046 	leak(fd);
1047 }
1048 
1049 /*
1050  * With O_DIRECT, writes should be not committed to cache.  Admittedly this is
1051  * an odd test, because it would be unusual to use O_DIRECT for writes but not
1052  * reads.
1053  */
1054 TEST_F(WriteBack, o_direct)
1055 {
1056 	const char FULLPATH[] = "mountpoint/some_file.txt";
1057 	const char RELPATH[] = "some_file.txt";
1058 	const char *CONTENTS = "abcdefgh";
1059 	uint64_t ino = 42;
1060 	int fd;
1061 	ssize_t bufsize = strlen(CONTENTS);
1062 	uint8_t readbuf[bufsize];
1063 
1064 	expect_lookup(RELPATH, ino, 0);
1065 	expect_open(ino, 0, 1);
1066 	FuseTest::expect_write(ino, 0, bufsize, bufsize, 0, FUSE_WRITE_CACHE,
1067 		CONTENTS);
1068 	expect_read(ino, 0, bufsize, bufsize, CONTENTS);
1069 
1070 	fd = open(FULLPATH, O_RDWR | O_DIRECT);
1071 	ASSERT_LE(0, fd) << strerror(errno);
1072 
1073 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1074 	/* A subsequent read must query the daemon because cache is empty */
1075 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1076 	ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1077 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1078 	leak(fd);
1079 }
1080 
1081 TEST_F(WriteBack, direct_io)
1082 {
1083 	const char FULLPATH[] = "mountpoint/some_file.txt";
1084 	const char RELPATH[] = "some_file.txt";
1085 	const char *CONTENTS = "abcdefgh";
1086 	uint64_t ino = 42;
1087 	int fd;
1088 	ssize_t bufsize = strlen(CONTENTS);
1089 	uint8_t readbuf[bufsize];
1090 
1091 	expect_lookup(RELPATH, ino, 0);
1092 	expect_open(ino, FOPEN_DIRECT_IO, 1);
1093 	FuseTest::expect_write(ino, 0, bufsize, bufsize, 0, FUSE_WRITE_CACHE,
1094 		CONTENTS);
1095 	expect_read(ino, 0, bufsize, bufsize, CONTENTS);
1096 
1097 	fd = open(FULLPATH, O_RDWR);
1098 	ASSERT_LE(0, fd) << strerror(errno);
1099 
1100 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1101 	/* A subsequent read must query the daemon because cache is empty */
1102 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1103 	ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1104 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1105 	leak(fd);
1106 }
1107 
1108 /*
1109  * mmap should still be possible even if the server used direct_io.  Mmap will
1110  * still use the cache, though.
1111  *
1112  * Regression test for bug 247276
1113  * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=247276
1114  */
1115 TEST_F(WriteBack, mmap_direct_io)
1116 {
1117 	const char FULLPATH[] = "mountpoint/some_file.txt";
1118 	const char RELPATH[] = "some_file.txt";
1119 	const char *CONTENTS = "abcdefgh";
1120 	uint64_t ino = 42;
1121 	int fd;
1122 	size_t len;
1123 	ssize_t bufsize = strlen(CONTENTS);
1124 	void *p, *zeros;
1125 
1126 	len = getpagesize();
1127 	zeros = calloc(1, len);
1128 	ASSERT_NE(nullptr, zeros);
1129 
1130 	expect_lookup(RELPATH, ino, len);
1131 	expect_open(ino, FOPEN_DIRECT_IO, 1);
1132 	expect_read(ino, 0, len, len, zeros);
1133 	expect_flush(ino, 1, ReturnErrno(0));
1134 	FuseTest::expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, 0, zeros);
1135 	expect_release(ino, ReturnErrno(0));
1136 
1137 	fd = open(FULLPATH, O_RDWR);
1138 	ASSERT_LE(0, fd) << strerror(errno);
1139 
1140 	p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1141 	ASSERT_NE(MAP_FAILED, p) << strerror(errno);
1142 
1143 	memmove((uint8_t*)p, CONTENTS, bufsize);
1144 
1145 	ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
1146 	close(fd);	// Write mmap'd data on close
1147 
1148 	free(zeros);
1149 }
1150 
1151 /*
1152  * When mounted with -o async, the writeback cache mode should delay writes
1153  */
1154 TEST_F(WriteBackAsync, delay)
1155 {
1156 	const char FULLPATH[] = "mountpoint/some_file.txt";
1157 	const char RELPATH[] = "some_file.txt";
1158 	const char *CONTENTS = "abcdefgh";
1159 	uint64_t ino = 42;
1160 	int fd;
1161 	ssize_t bufsize = strlen(CONTENTS);
1162 
1163 	expect_lookup(RELPATH, ino, 0);
1164 	expect_open(ino, 0, 1);
1165 	/* Write should be cached, but FUSE_WRITE shouldn't be sent */
1166 	EXPECT_CALL(*m_mock, process(
1167 		ResultOf([=](auto in) {
1168 			return (in.header.opcode == FUSE_WRITE);
1169 		}, Eq(true)),
1170 		_)
1171 	).Times(0);
1172 
1173 	fd = open(FULLPATH, O_RDWR);
1174 	ASSERT_LE(0, fd) << strerror(errno);
1175 
1176 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1177 
1178 	/* Don't close the file because that would flush the cache */
1179 	leak(fd);
1180 }
1181 
1182 /*
1183  * A direct write should not evict dirty cached data from outside of its own
1184  * byte range.
1185  */
1186 TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
1187 {
1188 	const char FULLPATH[] = "mountpoint/some_file.txt";
1189 	const char RELPATH[] = "some_file.txt";
1190 	const char CONTENTS0[] = "abcdefgh";
1191 	const char CONTENTS1[] = "ijklmnop";
1192 	uint64_t ino = 42;
1193 	int fd;
1194 	ssize_t bufsize = strlen(CONTENTS0) + 1;
1195 	ssize_t fsize = 2 * m_maxbcachebuf;
1196 	char readbuf[bufsize];
1197 	void *zeros;
1198 
1199 	zeros = calloc(1, m_maxbcachebuf);
1200 	ASSERT_NE(nullptr, zeros);
1201 
1202 	expect_lookup(RELPATH, ino, fsize);
1203 	expect_open(ino, 0, 1);
1204 	expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf, zeros);
1205 	FuseTest::expect_write(ino, m_maxbcachebuf, bufsize, bufsize, 0, 0,
1206 		CONTENTS1);
1207 
1208 	fd = open(FULLPATH, O_RDWR);
1209 	ASSERT_LE(0, fd) << strerror(errno);
1210 
1211 	// Cache first block with dirty data.  This will entail first reading
1212 	// the existing data.
1213 	ASSERT_EQ(bufsize, pwrite(fd, CONTENTS0, bufsize, 0))
1214 		<< strerror(errno);
1215 
1216 	// Write directly to second block
1217 	ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
1218 	ASSERT_EQ(bufsize, pwrite(fd, CONTENTS1, bufsize, m_maxbcachebuf))
1219 		<< strerror(errno);
1220 
1221 	// Read from the first block again.  Should be serviced by cache.
1222 	ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1223 	ASSERT_EQ(bufsize, pread(fd, readbuf, bufsize, 0)) << strerror(errno);
1224 	ASSERT_STREQ(readbuf, CONTENTS0);
1225 
1226 	leak(fd);
1227 	free(zeros);
1228 }
1229 
1230 /*
1231  * If a direct io write partially overlaps one or two blocks of dirty cached
1232  * data, No dirty data should be lost.  Admittedly this is a weird test,
1233  * because it would be unusual to use O_DIRECT and the writeback cache.
1234  */
1235 TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
1236 {
1237 	const char FULLPATH[] = "mountpoint/some_file.txt";
1238 	const char RELPATH[] = "some_file.txt";
1239 	uint64_t ino = 42;
1240 	int fd;
1241 	off_t bs = m_maxbcachebuf;
1242 	ssize_t fsize = 3 * bs;
1243 	void *readbuf, *zeros, *ones, *zeroones, *onezeros;
1244 
1245 	readbuf = malloc(bs);
1246 	ASSERT_NE(nullptr, readbuf) << strerror(errno);
1247 	zeros = calloc(1, 3 * bs);
1248 	ASSERT_NE(nullptr, zeros);
1249 	ones = calloc(1, 2 * bs);
1250 	ASSERT_NE(nullptr, ones);
1251 	memset(ones, 1, 2 * bs);
1252 	zeroones = calloc(1, bs);
1253 	ASSERT_NE(nullptr, zeroones);
1254 	memset((uint8_t*)zeroones + bs / 2, 1, bs / 2);
1255 	onezeros = calloc(1, bs);
1256 	ASSERT_NE(nullptr, onezeros);
1257 	memset(onezeros, 1, bs / 2);
1258 
1259 	expect_lookup(RELPATH, ino, fsize);
1260 	expect_open(ino, 0, 1);
1261 
1262 	fd = open(FULLPATH, O_RDWR);
1263 	ASSERT_LE(0, fd) << strerror(errno);
1264 
1265 	/* Cache first and third blocks with dirty data.  */
1266 	ASSERT_EQ(3 * bs, pwrite(fd, zeros, 3 * bs, 0)) << strerror(errno);
1267 
1268 	/*
1269 	 * Write directly to all three blocks.  The partially written blocks
1270 	 * will be flushed because they're dirty.
1271 	 */
1272 	FuseTest::expect_write(ino, 0, bs, bs, 0, 0, zeros);
1273 	FuseTest::expect_write(ino, 2 * bs, bs, bs, 0, 0, zeros);
1274 	/* The direct write is split in two because of the m_maxwrite value */
1275 	FuseTest::expect_write(ino,     bs / 2, bs, bs, 0, 0, ones);
1276 	FuseTest::expect_write(ino, 3 * bs / 2, bs, bs, 0, 0, ones);
1277 	ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
1278 	ASSERT_EQ(2 * bs, pwrite(fd, ones, 2 * bs, bs / 2)) << strerror(errno);
1279 
1280 	/*
1281 	 * Read from both the valid and invalid portions of the first and third
1282 	 * blocks again.  This will entail FUSE_READ operations because these
1283 	 * blocks were invalidated by the direct write.
1284 	 */
1285 	expect_read(ino, 0, bs, bs, zeroones);
1286 	expect_read(ino, 2 * bs, bs, bs, onezeros);
1287 	ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1288 	ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 0)) << strerror(errno);
1289 	EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1290 	ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 5 * bs / 2))
1291 		<< strerror(errno);
1292 	EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1293 	ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, bs / 2))
1294 		<< strerror(errno);
1295 	EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1296 	ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 2 * bs))
1297 		<< strerror(errno);
1298 	EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1299 
1300 	leak(fd);
1301 	free(zeroones);
1302 	free(onezeros);
1303 	free(ones);
1304 	free(zeros);
1305 	free(readbuf);
1306 }
1307 
1308 /*
1309  * In WriteBack mode, writes may be cached beyond what the server thinks is the
1310  * EOF.  In this case, a short read at EOF should _not_ cause fusefs to update
1311  * the file's size.
1312  */
1313 TEST_F(WriteBackAsync, eof)
1314 {
1315 	const char FULLPATH[] = "mountpoint/some_file.txt";
1316 	const char RELPATH[] = "some_file.txt";
1317 	const char *CONTENTS0 = "abcdefgh";
1318 	const char *CONTENTS1 = "ijklmnop";
1319 	uint64_t ino = 42;
1320 	int fd;
1321 	off_t offset = m_maxbcachebuf;
1322 	ssize_t wbufsize = strlen(CONTENTS1);
1323 	off_t old_filesize = (off_t)strlen(CONTENTS0);
1324 	ssize_t rbufsize = 2 * old_filesize;
1325 	char readbuf[rbufsize];
1326 	size_t holesize = rbufsize - old_filesize;
1327 	char hole[holesize];
1328 	struct stat sb;
1329 	ssize_t r;
1330 
1331 	expect_lookup(RELPATH, ino, 0);
1332 	expect_open(ino, 0, 1);
1333 	expect_read(ino, 0, m_maxbcachebuf, old_filesize, CONTENTS0);
1334 
1335 	fd = open(FULLPATH, O_RDWR);
1336 	ASSERT_LE(0, fd) << strerror(errno);
1337 
1338 	/* Write and cache data beyond EOF */
1339 	ASSERT_EQ(wbufsize, pwrite(fd, CONTENTS1, wbufsize, offset))
1340 		<< strerror(errno);
1341 
1342 	/* Read from the old EOF */
1343 	r = pread(fd, readbuf, rbufsize, 0);
1344 	ASSERT_LE(0, r) << strerror(errno);
1345 	EXPECT_EQ(rbufsize, r) << "read should've synthesized a hole";
1346 	EXPECT_EQ(0, memcmp(CONTENTS0, readbuf, old_filesize));
1347 	bzero(hole, holesize);
1348 	EXPECT_EQ(0, memcmp(hole, readbuf + old_filesize, holesize));
1349 
1350 	/* The file's size should still be what was established by pwrite */
1351 	ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1352 	EXPECT_EQ(offset + wbufsize, sb.st_size);
1353 	leak(fd);
1354 }
1355 
1356 /*
1357  * When a file has dirty writes that haven't been flushed, the server's notion
1358  * of its mtime and ctime will be wrong.  The kernel should ignore those if it
1359  * gets them from a FUSE_GETATTR before flushing.
1360  */
1361 TEST_F(WriteBackAsync, timestamps)
1362 {
1363 	const char FULLPATH[] = "mountpoint/some_file.txt";
1364 	const char RELPATH[] = "some_file.txt";
1365 	const char *CONTENTS = "abcdefgh";
1366 	ssize_t bufsize = strlen(CONTENTS);
1367 	uint64_t ino = 42;
1368 	uint64_t attr_valid = 0;
1369 	uint64_t attr_valid_nsec = 0;
1370 	uint64_t server_time = 12345;
1371 	mode_t mode = S_IFREG | 0644;
1372 	int fd;
1373 
1374 	struct stat sb;
1375 
1376 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
1377 	.WillRepeatedly(Invoke(
1378 		ReturnImmediate([=](auto in __unused, auto& out) {
1379 		SET_OUT_HEADER_LEN(out, entry);
1380 		out.body.entry.attr.mode = mode;
1381 		out.body.entry.nodeid = ino;
1382 		out.body.entry.attr.nlink = 1;
1383 		out.body.entry.attr_valid = attr_valid;
1384 		out.body.entry.attr_valid_nsec = attr_valid_nsec;
1385 	})));
1386 	expect_open(ino, 0, 1);
1387 	EXPECT_CALL(*m_mock, process(
1388 		ResultOf([=](auto in) {
1389 			return (in.header.opcode == FUSE_GETATTR &&
1390 				in.header.nodeid == ino);
1391 		}, Eq(true)),
1392 		_)
1393 	).WillRepeatedly(Invoke(
1394 	ReturnImmediate([=](auto i __unused, auto& out) {
1395 		SET_OUT_HEADER_LEN(out, attr);
1396 		out.body.attr.attr.ino = ino;
1397 		out.body.attr.attr.mode = mode;
1398 		out.body.attr.attr_valid = attr_valid;
1399 		out.body.attr.attr_valid_nsec = attr_valid_nsec;
1400 		out.body.attr.attr.atime = server_time;
1401 		out.body.attr.attr.mtime = server_time;
1402 		out.body.attr.attr.ctime = server_time;
1403 	})));
1404 
1405 	fd = open(FULLPATH, O_RDWR);
1406 	ASSERT_LE(0, fd) << strerror(errno);
1407 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1408 
1409 	ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1410 	EXPECT_EQ((time_t)server_time, sb.st_atime);
1411 	EXPECT_NE((time_t)server_time, sb.st_mtime);
1412 	EXPECT_NE((time_t)server_time, sb.st_ctime);
1413 
1414 	leak(fd);
1415 }
1416 
1417 /* Any dirty timestamp fields should be flushed during a SETATTR */
1418 TEST_F(WriteBackAsync, timestamps_during_setattr)
1419 {
1420 	const char FULLPATH[] = "mountpoint/some_file.txt";
1421 	const char RELPATH[] = "some_file.txt";
1422 	const char *CONTENTS = "abcdefgh";
1423 	ssize_t bufsize = strlen(CONTENTS);
1424 	uint64_t ino = 42;
1425 	const mode_t newmode = 0755;
1426 	int fd;
1427 
1428 	expect_lookup(RELPATH, ino, 0);
1429 	expect_open(ino, 0, 1);
1430 	EXPECT_CALL(*m_mock, process(
1431 		ResultOf([=](auto in) {
1432 			uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1433 			return (in.header.opcode == FUSE_SETATTR &&
1434 				in.header.nodeid == ino &&
1435 				in.body.setattr.valid == valid);
1436 		}, Eq(true)),
1437 		_)
1438 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1439 		SET_OUT_HEADER_LEN(out, attr);
1440 		out.body.attr.attr.ino = ino;
1441 		out.body.attr.attr.mode = S_IFREG | newmode;
1442 	})));
1443 
1444 	fd = open(FULLPATH, O_RDWR);
1445 	ASSERT_LE(0, fd) << strerror(errno);
1446 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1447 	ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1448 
1449 	leak(fd);
1450 }
1451 
1452 /* fuse_init_out.time_gran controls the granularity of timestamps */
1453 TEST_P(TimeGran, timestamps_during_setattr)
1454 {
1455 	const char FULLPATH[] = "mountpoint/some_file.txt";
1456 	const char RELPATH[] = "some_file.txt";
1457 	const char *CONTENTS = "abcdefgh";
1458 	ssize_t bufsize = strlen(CONTENTS);
1459 	uint64_t ino = 42;
1460 	const mode_t newmode = 0755;
1461 	int fd;
1462 
1463 	expect_lookup(RELPATH, ino, 0);
1464 	expect_open(ino, 0, 1);
1465 	EXPECT_CALL(*m_mock, process(
1466 		ResultOf([=](auto in) {
1467 			uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1468 			return (in.header.opcode == FUSE_SETATTR &&
1469 				in.header.nodeid == ino &&
1470 				in.body.setattr.valid == valid &&
1471 				in.body.setattr.mtimensec % m_time_gran == 0 &&
1472 				in.body.setattr.ctimensec % m_time_gran == 0);
1473 		}, Eq(true)),
1474 		_)
1475 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1476 		SET_OUT_HEADER_LEN(out, attr);
1477 		out.body.attr.attr.ino = ino;
1478 		out.body.attr.attr.mode = S_IFREG | newmode;
1479 	})));
1480 
1481 	fd = open(FULLPATH, O_RDWR);
1482 	ASSERT_LE(0, fd) << strerror(errno);
1483 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1484 	ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1485 
1486 	leak(fd);
1487 }
1488 
1489 INSTANTIATE_TEST_CASE_P(RA, TimeGran, Range(0u, 10u));
1490 
1491 /*
1492  * Without direct_io, writes should be committed to cache
1493  */
1494 TEST_F(Write, writethrough)
1495 {
1496 	const char FULLPATH[] = "mountpoint/some_file.txt";
1497 	const char RELPATH[] = "some_file.txt";
1498 	const char *CONTENTS = "abcdefgh";
1499 	uint64_t ino = 42;
1500 	int fd;
1501 	ssize_t bufsize = strlen(CONTENTS);
1502 	uint8_t readbuf[bufsize];
1503 
1504 	expect_lookup(RELPATH, ino, 0);
1505 	expect_open(ino, 0, 1);
1506 	expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1507 
1508 	fd = open(FULLPATH, O_RDWR);
1509 	ASSERT_LE(0, fd) << strerror(errno);
1510 
1511 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1512 	/*
1513 	 * A subsequent read should be serviced by cache, without querying the
1514 	 * filesystem daemon
1515 	 */
1516 	ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1517 	ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1518 	leak(fd);
1519 }
1520 
1521 /* Writes that extend a file should update the cached file size */
1522 TEST_F(Write, update_file_size)
1523 {
1524 	const char FULLPATH[] = "mountpoint/some_file.txt";
1525 	const char RELPATH[] = "some_file.txt";
1526 	const char *CONTENTS = "abcdefgh";
1527 	struct stat sb;
1528 	uint64_t ino = 42;
1529 	int fd;
1530 	ssize_t bufsize = strlen(CONTENTS);
1531 
1532 	expect_lookup(RELPATH, ino, 0);
1533 	expect_open(ino, 0, 1);
1534 	expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1535 
1536 	fd = open(FULLPATH, O_RDWR);
1537 	ASSERT_LE(0, fd) << strerror(errno);
1538 
1539 	ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1540 	/* Get cached attributes */
1541 	ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1542 	ASSERT_EQ(bufsize, sb.st_size);
1543 	leak(fd);
1544 }
1545