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