2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
36 #include <sys/resource.h>
50 using namespace testing;
52 class Write: public FuseTest {
55 static sig_atomic_t s_sigxfsz;
65 bzero(&sa, sizeof(sa));
66 sa.sa_handler = SIG_DFL;
67 sigaction(SIGXFSZ, &sa, NULL);
72 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
74 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
77 void expect_release(uint64_t ino, ProcessMockerT r)
79 EXPECT_CALL(*m_mock, process(
80 ResultOf([=](auto in) {
81 return (in.header.opcode == FUSE_RELEASE &&
82 in.header.nodeid == ino);
85 ).WillRepeatedly(Invoke(r));
88 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
89 uint64_t osize, const void *contents)
91 FuseTest::expect_write(ino, offset, isize, osize, 0, 0, contents);
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,
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);
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));
111 .WillRepeatedly(Invoke(
112 ReturnImmediate([=](auto in __unused, auto& out) {
113 SET_OUT_HEADER_LEN(out, write);
114 out.body.write.size = size;
121 sig_atomic_t Write::s_sigxfsz = 0;
123 class Write_7_8: public FuseTest {
126 virtual void SetUp() {
127 m_kernel_minor_version = 8;
131 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
133 FuseTest::expect_lookup_7_8(relpath, ino, S_IFREG | 0644, size, 1);
138 class AioWrite: public Write {
139 virtual void SetUp() {
140 if (!is_unsafe_aio_enabled())
142 "vfs.aio.enable_unsafe must be set for this test";
147 /* Tests for the writeback cache mode */
148 class WriteBack: public Write {
150 virtual void SetUp() {
151 m_init_flags |= FUSE_WRITEBACK_CACHE;
157 void expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
158 uint64_t osize, const void *contents)
160 FuseTest::expect_write(ino, offset, isize, osize, FUSE_WRITE_CACHE, 0,
165 class WriteBackAsync: public WriteBack {
167 virtual void SetUp() {
173 class TimeGran: public WriteBackAsync, public WithParamInterface<unsigned> {
175 virtual void SetUp() {
176 m_time_gran = 1 << GetParam();
177 WriteBackAsync::SetUp();
181 /* Tests for clustered writes with WriteBack cacheing */
182 class WriteCluster: public WriteBack {
184 virtual void SetUp() {
186 m_maxwrite = m_maxphys;
188 if (m_maxphys < 2 * DFLTPHYS)
189 GTEST_SKIP() << "MAXPHYS must be at least twice DFLTPHYS"
191 if (m_maxphys < 2 * m_maxbcachebuf)
192 GTEST_SKIP() << "MAXPHYS must be at least twice maxbcachebuf"
197 void sigxfsz_handler(int __unused sig) {
198 Write::s_sigxfsz = 1;
201 /* AIO writes need to set the header's pid field correctly */
202 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
203 TEST_F(AioWrite, DISABLED_aio_write)
205 const char FULLPATH[] = "mountpoint/some_file.txt";
206 const char RELPATH[] = "some_file.txt";
207 const char *CONTENTS = "abcdefgh";
209 uint64_t offset = 4096;
211 ssize_t bufsize = strlen(CONTENTS);
212 struct aiocb iocb, *piocb;
214 expect_lookup(RELPATH, ino, 0);
215 expect_open(ino, 0, 1);
216 expect_write(ino, offset, bufsize, bufsize, CONTENTS);
218 fd = open(FULLPATH, O_WRONLY);
219 EXPECT_LE(0, fd) << strerror(errno);
221 iocb.aio_nbytes = bufsize;
222 iocb.aio_fildes = fd;
223 iocb.aio_buf = __DECONST(void *, CONTENTS);
224 iocb.aio_offset = offset;
225 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
226 ASSERT_EQ(0, aio_write(&iocb)) << strerror(errno);
227 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
232 * When a file is opened with O_APPEND, we should forward that flag to
233 * FUSE_OPEN (tested by Open.o_append) but still attempt to calculate the
234 * offset internally. That way we'll work both with filesystems that
235 * understand O_APPEND (and ignore the offset) and filesystems that don't (and
236 * simply use the offset).
238 * Note that verifying the O_APPEND flag in FUSE_OPEN is done in the
239 * Open.o_append test.
241 TEST_F(Write, append)
243 const ssize_t BUFSIZE = 9;
244 const char FULLPATH[] = "mountpoint/some_file.txt";
245 const char RELPATH[] = "some_file.txt";
246 const char CONTENTS[BUFSIZE] = "abcdefgh";
249 * Set offset to a maxbcachebuf boundary so we don't need to RMW when
250 * using writeback caching
252 uint64_t initial_offset = m_maxbcachebuf;
255 expect_lookup(RELPATH, ino, initial_offset);
256 expect_open(ino, 0, 1);
257 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
259 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
260 fd = open(FULLPATH, O_RDWR | O_APPEND);
261 EXPECT_LE(0, fd) << strerror(errno);
263 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
267 /* If a file is cached, then appending to the end should not cause a read */
268 TEST_F(Write, append_to_cached)
270 const ssize_t BUFSIZE = 9;
271 const char FULLPATH[] = "mountpoint/some_file.txt";
272 const char RELPATH[] = "some_file.txt";
273 char *oldcontents, *oldbuf;
274 const char CONTENTS[BUFSIZE] = "abcdefgh";
277 * Set offset in between maxbcachebuf boundary to test buffer handling
279 uint64_t oldsize = m_maxbcachebuf / 2;
282 oldcontents = (char*)calloc(1, oldsize);
283 ASSERT_NE(nullptr, oldcontents) << strerror(errno);
284 oldbuf = (char*)malloc(oldsize);
285 ASSERT_NE(nullptr, oldbuf) << strerror(errno);
287 expect_lookup(RELPATH, ino, oldsize);
288 expect_open(ino, 0, 1);
289 expect_read(ino, 0, oldsize, oldsize, oldcontents);
290 maybe_expect_write(ino, oldsize, BUFSIZE, CONTENTS);
292 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
293 fd = open(FULLPATH, O_RDWR | O_APPEND);
294 EXPECT_LE(0, fd) << strerror(errno);
296 /* Read the old data into the cache */
297 ASSERT_EQ((ssize_t)oldsize, read(fd, oldbuf, oldsize))
300 /* Write the new data. There should be no more read operations */
301 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
305 TEST_F(Write, append_direct_io)
307 const ssize_t BUFSIZE = 9;
308 const char FULLPATH[] = "mountpoint/some_file.txt";
309 const char RELPATH[] = "some_file.txt";
310 const char CONTENTS[BUFSIZE] = "abcdefgh";
312 uint64_t initial_offset = 4096;
315 expect_lookup(RELPATH, ino, initial_offset);
316 expect_open(ino, FOPEN_DIRECT_IO, 1);
317 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, CONTENTS);
319 fd = open(FULLPATH, O_WRONLY | O_APPEND);
320 EXPECT_LE(0, fd) << strerror(errno);
322 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
326 /* A direct write should evict any overlapping cached data */
327 TEST_F(Write, direct_io_evicts_cache)
329 const char FULLPATH[] = "mountpoint/some_file.txt";
330 const char RELPATH[] = "some_file.txt";
331 const char CONTENTS0[] = "abcdefgh";
332 const char CONTENTS1[] = "ijklmnop";
335 ssize_t bufsize = strlen(CONTENTS0) + 1;
336 char readbuf[bufsize];
338 expect_lookup(RELPATH, ino, bufsize);
339 expect_open(ino, 0, 1);
340 expect_read(ino, 0, bufsize, bufsize, CONTENTS0);
341 expect_write(ino, 0, bufsize, bufsize, CONTENTS1);
343 fd = open(FULLPATH, O_RDWR);
344 EXPECT_LE(0, fd) << strerror(errno);
347 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
349 // Write directly, evicting cache
350 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
351 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
352 ASSERT_EQ(bufsize, write(fd, CONTENTS1, bufsize)) << strerror(errno);
354 // Read again. Cache should be bypassed
355 expect_read(ino, 0, bufsize, bufsize, CONTENTS1);
356 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
357 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
358 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
359 ASSERT_STREQ(readbuf, CONTENTS1);
365 * If the server doesn't return FOPEN_DIRECT_IO during FUSE_OPEN, then it's not
366 * allowed to return a short write for that file handle. However, if it does
367 * then we should still do our darndest to handle it by resending the unwritten
370 TEST_F(Write, indirect_io_short_write)
372 const char FULLPATH[] = "mountpoint/some_file.txt";
373 const char RELPATH[] = "some_file.txt";
374 const char *CONTENTS = "abcdefghijklmnop";
377 ssize_t bufsize = strlen(CONTENTS);
378 ssize_t bufsize0 = 11;
379 ssize_t bufsize1 = strlen(CONTENTS) - bufsize0;
380 const char *contents1 = CONTENTS + bufsize0;
382 expect_lookup(RELPATH, ino, 0);
383 expect_open(ino, 0, 1);
384 expect_write(ino, 0, bufsize, bufsize0, CONTENTS);
385 expect_write(ino, bufsize0, bufsize1, bufsize1, contents1);
387 fd = open(FULLPATH, O_WRONLY);
388 EXPECT_LE(0, fd) << strerror(errno);
390 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
395 * When the direct_io option is used, filesystems are allowed to write less
396 * data than requested. We should return the short write to userland.
398 TEST_F(Write, direct_io_short_write)
400 const char FULLPATH[] = "mountpoint/some_file.txt";
401 const char RELPATH[] = "some_file.txt";
402 const char *CONTENTS = "abcdefghijklmnop";
405 ssize_t bufsize = strlen(CONTENTS);
406 ssize_t halfbufsize = bufsize / 2;
408 expect_lookup(RELPATH, ino, 0);
409 expect_open(ino, FOPEN_DIRECT_IO, 1);
410 expect_write(ino, 0, bufsize, halfbufsize, CONTENTS);
412 fd = open(FULLPATH, O_WRONLY);
413 EXPECT_LE(0, fd) << strerror(errno);
415 ASSERT_EQ(halfbufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
420 * An insidious edge case: the filesystem returns a short write, and the
421 * difference between what we requested and what it actually wrote crosses an
422 * iov element boundary
424 TEST_F(Write, direct_io_short_write_iov)
426 const char FULLPATH[] = "mountpoint/some_file.txt";
427 const char RELPATH[] = "some_file.txt";
428 const char *CONTENTS0 = "abcdefgh";
429 const char *CONTENTS1 = "ijklmnop";
430 const char *EXPECTED0 = "abcdefghijklmnop";
433 ssize_t size0 = strlen(CONTENTS0) - 1;
434 ssize_t size1 = strlen(CONTENTS1) + 1;
435 ssize_t totalsize = size0 + size1;
438 expect_lookup(RELPATH, ino, 0);
439 expect_open(ino, FOPEN_DIRECT_IO, 1);
440 expect_write(ino, 0, totalsize, size0, EXPECTED0);
442 fd = open(FULLPATH, O_WRONLY);
443 EXPECT_LE(0, fd) << strerror(errno);
445 iov[0].iov_base = __DECONST(void*, CONTENTS0);
446 iov[0].iov_len = strlen(CONTENTS0);
447 iov[1].iov_base = __DECONST(void*, CONTENTS1);
448 iov[1].iov_len = strlen(CONTENTS1);
449 ASSERT_EQ(size0, writev(fd, iov, 2)) << strerror(errno);
453 /* fusefs should respect RLIMIT_FSIZE */
454 TEST_F(Write, rlimit_fsize)
456 const char FULLPATH[] = "mountpoint/some_file.txt";
457 const char RELPATH[] = "some_file.txt";
458 const char *CONTENTS = "abcdefgh";
460 ssize_t bufsize = strlen(CONTENTS);
461 off_t offset = 1'000'000'000;
465 expect_lookup(RELPATH, ino, 0);
466 expect_open(ino, 0, 1);
468 rl.rlim_cur = offset;
469 rl.rlim_max = 10 * offset;
470 ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rl)) << strerror(errno);
471 ASSERT_NE(SIG_ERR, signal(SIGXFSZ, sigxfsz_handler)) << strerror(errno);
473 fd = open(FULLPATH, O_WRONLY);
475 EXPECT_LE(0, fd) << strerror(errno);
477 ASSERT_EQ(-1, pwrite(fd, CONTENTS, bufsize, offset));
478 EXPECT_EQ(EFBIG, errno);
479 EXPECT_EQ(1, s_sigxfsz);
484 * A short read indicates EOF. Test that nothing bad happens if we get EOF
485 * during the R of a RMW operation.
487 TEST_F(Write, eof_during_rmw)
489 const char FULLPATH[] = "mountpoint/some_file.txt";
490 const char RELPATH[] = "some_file.txt";
491 const char *CONTENTS = "abcdefgh";
492 const char *INITIAL = "XXXXXXXXXX";
495 ssize_t bufsize = strlen(CONTENTS);
496 off_t orig_fsize = 10;
497 off_t truncated_fsize = 5;
498 off_t final_fsize = bufsize;
501 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, orig_fsize, 1);
502 expect_open(ino, 0, 1);
503 expect_read(ino, 0, orig_fsize, truncated_fsize, INITIAL, O_RDWR);
504 expect_getattr(ino, truncated_fsize);
505 expect_read(ino, 0, final_fsize, final_fsize, INITIAL, O_RDWR);
506 maybe_expect_write(ino, offset, bufsize, CONTENTS);
508 fd = open(FULLPATH, O_RDWR);
509 EXPECT_LE(0, fd) << strerror(errno);
511 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
517 * If the kernel cannot be sure which uid, gid, or pid was responsible for a
518 * write, then it must set the FUSE_WRITE_CACHE bit
520 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236378 */
523 const char FULLPATH[] = "mountpoint/some_file.txt";
524 const char RELPATH[] = "some_file.txt";
525 const char *CONTENTS = "abcdefgh";
528 ssize_t bufsize = strlen(CONTENTS);
530 uint64_t offset = 10;
532 void *zeros, *expected;
536 zeros = calloc(1, len);
537 ASSERT_NE(nullptr, zeros);
538 expected = calloc(1, len);
539 ASSERT_NE(nullptr, expected);
540 memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
542 expect_lookup(RELPATH, ino, len);
543 expect_open(ino, 0, 1);
544 expect_read(ino, 0, len, len, zeros);
546 * Writes from the pager may or may not be associated with the correct
547 * pid, so they must set FUSE_WRITE_CACHE.
549 FuseTest::expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, 0, expected);
550 expect_flush(ino, 1, ReturnErrno(0));
551 expect_release(ino, ReturnErrno(0));
553 fd = open(FULLPATH, O_RDWR);
554 EXPECT_LE(0, fd) << strerror(errno);
556 p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
557 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
559 memmove((uint8_t*)p + offset, CONTENTS, bufsize);
561 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
562 close(fd); // Write mmap'd data on close
568 TEST_F(Write, pwrite)
570 const char FULLPATH[] = "mountpoint/some_file.txt";
571 const char RELPATH[] = "some_file.txt";
572 const char *CONTENTS = "abcdefgh";
574 uint64_t offset = m_maxbcachebuf;
576 ssize_t bufsize = strlen(CONTENTS);
578 expect_lookup(RELPATH, ino, 0);
579 expect_open(ino, 0, 1);
580 expect_write(ino, offset, bufsize, bufsize, CONTENTS);
582 fd = open(FULLPATH, O_WRONLY);
583 EXPECT_LE(0, fd) << strerror(errno);
585 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
590 /* Writing a file should update its cached mtime and ctime */
591 TEST_F(Write, timestamps)
593 const char FULLPATH[] = "mountpoint/some_file.txt";
594 const char RELPATH[] = "some_file.txt";
595 const char *CONTENTS = "abcdefgh";
596 ssize_t bufsize = strlen(CONTENTS);
598 struct stat sb0, sb1;
601 expect_lookup(RELPATH, ino, 0);
602 expect_open(ino, 0, 1);
603 maybe_expect_write(ino, 0, bufsize, CONTENTS);
605 fd = open(FULLPATH, O_RDWR);
606 EXPECT_LE(0, fd) << strerror(errno);
607 ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno);
608 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
612 ASSERT_EQ(0, fstat(fd, &sb1)) << strerror(errno);
614 EXPECT_EQ(sb0.st_atime, sb1.st_atime);
615 EXPECT_NE(sb0.st_mtime, sb1.st_mtime);
616 EXPECT_NE(sb0.st_ctime, sb1.st_ctime);
621 const char FULLPATH[] = "mountpoint/some_file.txt";
622 const char RELPATH[] = "some_file.txt";
623 const char *CONTENTS = "abcdefgh";
626 ssize_t bufsize = strlen(CONTENTS);
628 expect_lookup(RELPATH, ino, 0);
629 expect_open(ino, 0, 1);
630 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
632 fd = open(FULLPATH, O_WRONLY);
633 EXPECT_LE(0, fd) << strerror(errno);
635 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
639 /* fuse(4) should not issue writes of greater size than the daemon requests */
640 TEST_F(Write, write_large)
642 const char FULLPATH[] = "mountpoint/some_file.txt";
643 const char RELPATH[] = "some_file.txt";
647 ssize_t halfbufsize, bufsize;
649 halfbufsize = m_mock->m_maxwrite;
650 bufsize = halfbufsize * 2;
651 contents = (int*)malloc(bufsize);
652 ASSERT_NE(nullptr, contents);
653 for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
657 expect_lookup(RELPATH, ino, 0);
658 expect_open(ino, 0, 1);
659 maybe_expect_write(ino, 0, halfbufsize, contents);
660 maybe_expect_write(ino, halfbufsize, halfbufsize,
661 &contents[halfbufsize / sizeof(int)]);
663 fd = open(FULLPATH, O_WRONLY);
664 EXPECT_LE(0, fd) << strerror(errno);
666 ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
672 TEST_F(Write, write_nothing)
674 const char FULLPATH[] = "mountpoint/some_file.txt";
675 const char RELPATH[] = "some_file.txt";
676 const char *CONTENTS = "";
681 expect_lookup(RELPATH, ino, 0);
682 expect_open(ino, 0, 1);
684 fd = open(FULLPATH, O_WRONLY);
685 EXPECT_LE(0, fd) << strerror(errno);
687 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
691 TEST_F(Write_7_8, write)
693 const char FULLPATH[] = "mountpoint/some_file.txt";
694 const char RELPATH[] = "some_file.txt";
695 const char *CONTENTS = "abcdefgh";
698 ssize_t bufsize = strlen(CONTENTS);
700 expect_lookup(RELPATH, ino, 0);
701 expect_open(ino, 0, 1);
702 expect_write_7_8(ino, 0, bufsize, bufsize, CONTENTS);
704 fd = open(FULLPATH, O_WRONLY);
705 EXPECT_LE(0, fd) << strerror(errno);
707 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
711 /* In writeback mode, dirty data should be written on close */
712 TEST_F(WriteBackAsync, close)
714 const char FULLPATH[] = "mountpoint/some_file.txt";
715 const char RELPATH[] = "some_file.txt";
716 const char *CONTENTS = "abcdefgh";
719 ssize_t bufsize = strlen(CONTENTS);
721 expect_lookup(RELPATH, ino, 0);
722 expect_open(ino, 0, 1);
723 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
724 EXPECT_CALL(*m_mock, process(
725 ResultOf([=](auto in) {
726 return (in.header.opcode == FUSE_SETATTR);
729 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
730 SET_OUT_HEADER_LEN(out, attr);
731 out.body.attr.attr.ino = ino; // Must match nodeid
733 expect_flush(ino, 1, ReturnErrno(0));
734 expect_release(ino, ReturnErrno(0));
736 fd = open(FULLPATH, O_RDWR);
737 ASSERT_LE(0, fd) << strerror(errno);
739 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
743 /* In writeback mode, adjacent writes will be clustered together */
744 TEST_F(WriteCluster, clustering)
746 const char FULLPATH[] = "mountpoint/some_file.txt";
747 const char RELPATH[] = "some_file.txt";
751 ssize_t bufsize = m_maxbcachebuf;
752 off_t filesize = 5 * bufsize;
754 wbuf = malloc(bufsize);
755 ASSERT_NE(nullptr, wbuf) << strerror(errno);
756 memset(wbuf, 'X', bufsize);
757 wbuf2x = malloc(2 * bufsize);
758 ASSERT_NE(nullptr, wbuf2x) << strerror(errno);
759 memset(wbuf2x, 'X', 2 * bufsize);
761 expect_lookup(RELPATH, ino, filesize);
762 expect_open(ino, 0, 1);
764 * Writes of bufsize-bytes each should be clustered into greater sizes.
765 * The amount of clustering is adaptive, so the first write actually
766 * issued will be 2x bufsize and subsequent writes may be larger
768 expect_write(ino, 0, 2 * bufsize, 2 * bufsize, wbuf2x);
769 expect_write(ino, 2 * bufsize, 2 * bufsize, 2 * bufsize, wbuf2x);
770 expect_flush(ino, 1, ReturnErrno(0));
771 expect_release(ino, ReturnErrno(0));
773 fd = open(FULLPATH, O_RDWR);
774 ASSERT_LE(0, fd) << strerror(errno);
776 for (i = 0; i < 4; i++) {
777 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
784 * When clustering writes, an I/O error to any of the cluster's children should
785 * not panic the system on unmount
788 * Disabled because it panics.
789 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=238565
791 TEST_F(WriteCluster, DISABLED_cluster_write_err)
793 const char FULLPATH[] = "mountpoint/some_file.txt";
794 const char RELPATH[] = "some_file.txt";
798 ssize_t bufsize = m_maxbcachebuf;
799 off_t filesize = 4 * bufsize;
801 wbuf = malloc(bufsize);
802 ASSERT_NE(nullptr, wbuf) << strerror(errno);
803 memset(wbuf, 'X', bufsize);
805 expect_lookup(RELPATH, ino, filesize);
806 expect_open(ino, 0, 1);
807 EXPECT_CALL(*m_mock, process(
808 ResultOf([=](auto in) {
809 return (in.header.opcode == FUSE_WRITE);
812 ).WillRepeatedly(Invoke(ReturnErrno(EIO)));
813 expect_flush(ino, 1, ReturnErrno(0));
814 expect_release(ino, ReturnErrno(0));
816 fd = open(FULLPATH, O_RDWR);
817 ASSERT_LE(0, fd) << strerror(errno);
819 for (i = 0; i < 3; i++) {
820 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
827 * In writeback mode, writes to an O_WRONLY file could trigger reads from the
828 * server. The FUSE protocol explicitly allows that.
830 TEST_F(WriteBack, rmw)
832 const char FULLPATH[] = "mountpoint/some_file.txt";
833 const char RELPATH[] = "some_file.txt";
834 const char *CONTENTS = "abcdefgh";
835 const char *INITIAL = "XXXXXXXXXX";
840 ssize_t bufsize = strlen(CONTENTS);
842 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
843 expect_open(ino, 0, 1);
844 expect_read(ino, 0, fsize, fsize, INITIAL, O_WRONLY);
845 maybe_expect_write(ino, offset, bufsize, CONTENTS);
847 fd = open(FULLPATH, O_WRONLY);
848 EXPECT_LE(0, fd) << strerror(errno);
850 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
856 * Without direct_io, writes should be committed to cache
858 TEST_F(WriteBack, cache)
860 const char FULLPATH[] = "mountpoint/some_file.txt";
861 const char RELPATH[] = "some_file.txt";
862 const char *CONTENTS = "abcdefgh";
865 ssize_t bufsize = strlen(CONTENTS);
866 char readbuf[bufsize];
868 expect_lookup(RELPATH, ino, 0);
869 expect_open(ino, 0, 1);
870 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
872 fd = open(FULLPATH, O_RDWR);
873 EXPECT_LE(0, fd) << strerror(errno);
875 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
877 * A subsequent read should be serviced by cache, without querying the
880 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
881 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
886 * With O_DIRECT, writes should be not committed to cache. Admittedly this is
887 * an odd test, because it would be unusual to use O_DIRECT for writes but not
890 TEST_F(WriteBack, o_direct)
892 const char FULLPATH[] = "mountpoint/some_file.txt";
893 const char RELPATH[] = "some_file.txt";
894 const char *CONTENTS = "abcdefgh";
897 ssize_t bufsize = strlen(CONTENTS);
898 char readbuf[bufsize];
900 expect_lookup(RELPATH, ino, 0);
901 expect_open(ino, 0, 1);
902 FuseTest::expect_write(ino, 0, bufsize, bufsize, 0, FUSE_WRITE_CACHE,
904 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
906 fd = open(FULLPATH, O_RDWR | O_DIRECT);
907 EXPECT_LE(0, fd) << strerror(errno);
909 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
910 /* A subsequent read must query the daemon because cache is empty */
911 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
912 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
913 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
918 * When mounted with -o async, the writeback cache mode should delay writes
920 TEST_F(WriteBackAsync, delay)
922 const char FULLPATH[] = "mountpoint/some_file.txt";
923 const char RELPATH[] = "some_file.txt";
924 const char *CONTENTS = "abcdefgh";
927 ssize_t bufsize = strlen(CONTENTS);
929 expect_lookup(RELPATH, ino, 0);
930 expect_open(ino, 0, 1);
931 /* Write should be cached, but FUSE_WRITE shouldn't be sent */
932 EXPECT_CALL(*m_mock, process(
933 ResultOf([=](auto in) {
934 return (in.header.opcode == FUSE_WRITE);
939 fd = open(FULLPATH, O_RDWR);
940 EXPECT_LE(0, fd) << strerror(errno);
942 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
944 /* Don't close the file because that would flush the cache */
948 * A direct write should not evict dirty cached data from outside of its own
951 TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
953 const char FULLPATH[] = "mountpoint/some_file.txt";
954 const char RELPATH[] = "some_file.txt";
955 const char CONTENTS0[] = "abcdefgh";
956 const char CONTENTS1[] = "ijklmnop";
959 ssize_t bufsize = strlen(CONTENTS0) + 1;
960 ssize_t fsize = 2 * m_maxbcachebuf;
961 char readbuf[bufsize];
964 zeros = calloc(1, m_maxbcachebuf);
965 ASSERT_NE(nullptr, zeros);
967 expect_lookup(RELPATH, ino, fsize);
968 expect_open(ino, 0, 1);
969 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf, zeros);
970 FuseTest::expect_write(ino, m_maxbcachebuf, bufsize, bufsize, 0, 0,
973 fd = open(FULLPATH, O_RDWR);
974 EXPECT_LE(0, fd) << strerror(errno);
976 // Cache first block with dirty data. This will entail first reading
977 // the existing data.
978 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS0, bufsize, 0))
981 // Write directly to second block
982 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
983 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS1, bufsize, m_maxbcachebuf))
986 // Read from the first block again. Should be serviced by cache.
987 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
988 ASSERT_EQ(bufsize, pread(fd, readbuf, bufsize, 0)) << strerror(errno);
989 ASSERT_STREQ(readbuf, CONTENTS0);
996 * If a direct io write partially overlaps one or two blocks of dirty cached
997 * data, No dirty data should be lost. Admittedly this is a weird test,
998 * because it would be unusual to use O_DIRECT and the writeback cache.
1000 TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
1002 const char FULLPATH[] = "mountpoint/some_file.txt";
1003 const char RELPATH[] = "some_file.txt";
1006 off_t bs = m_maxbcachebuf;
1007 ssize_t fsize = 3 * bs;
1008 void *readbuf, *zeros, *ones, *zeroones, *onezeros;
1010 readbuf = malloc(bs);
1011 ASSERT_NE(nullptr, readbuf) << strerror(errno);
1012 zeros = calloc(1, 3 * bs);
1013 ASSERT_NE(nullptr, zeros);
1014 ones = calloc(1, 2 * bs);
1015 ASSERT_NE(nullptr, ones);
1016 memset(ones, 1, 2 * bs);
1017 zeroones = calloc(1, bs);
1018 ASSERT_NE(nullptr, zeroones);
1019 memset((uint8_t*)zeroones + bs / 2, 1, bs / 2);
1020 onezeros = calloc(1, bs);
1021 ASSERT_NE(nullptr, onezeros);
1022 memset(onezeros, 1, bs / 2);
1024 expect_lookup(RELPATH, ino, fsize);
1025 expect_open(ino, 0, 1);
1027 fd = open(FULLPATH, O_RDWR);
1028 EXPECT_LE(0, fd) << strerror(errno);
1030 /* Cache first and third blocks with dirty data. */
1031 ASSERT_EQ(3 * bs, pwrite(fd, zeros, 3 * bs, 0)) << strerror(errno);
1034 * Write directly to all three blocks. The partially written blocks
1035 * will be flushed because they're dirty.
1037 FuseTest::expect_write(ino, 0, bs, bs, 0, 0, zeros);
1038 FuseTest::expect_write(ino, 2 * bs, bs, bs, 0, 0, zeros);
1039 /* The direct write is split in two because of the m_maxwrite value */
1040 FuseTest::expect_write(ino, bs / 2, bs, bs, 0, 0, ones);
1041 FuseTest::expect_write(ino, 3 * bs / 2, bs, bs, 0, 0, ones);
1042 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
1043 ASSERT_EQ(2 * bs, pwrite(fd, ones, 2 * bs, bs / 2)) << strerror(errno);
1046 * Read from both the valid and invalid portions of the first and third
1047 * blocks again. This will entail FUSE_READ operations because these
1048 * blocks were invalidated by the direct write.
1050 expect_read(ino, 0, bs, bs, zeroones);
1051 expect_read(ino, 2 * bs, bs, bs, onezeros);
1052 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1053 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 0)) << strerror(errno);
1054 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1055 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 5 * bs / 2))
1057 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1058 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, bs / 2))
1060 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1061 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 2 * bs))
1063 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1074 * In WriteBack mode, writes may be cached beyond what the server thinks is the
1075 * EOF. In this case, a short read at EOF should _not_ cause fusefs to update
1078 TEST_F(WriteBackAsync, eof)
1080 const char FULLPATH[] = "mountpoint/some_file.txt";
1081 const char RELPATH[] = "some_file.txt";
1082 const char *CONTENTS0 = "abcdefgh";
1083 const char *CONTENTS1 = "ijklmnop";
1086 off_t offset = m_maxbcachebuf;
1087 ssize_t wbufsize = strlen(CONTENTS1);
1088 off_t old_filesize = (off_t)strlen(CONTENTS0);
1089 ssize_t rbufsize = 2 * old_filesize;
1090 char readbuf[rbufsize];
1091 size_t holesize = rbufsize - old_filesize;
1092 char hole[holesize];
1096 expect_lookup(RELPATH, ino, 0);
1097 expect_open(ino, 0, 1);
1098 expect_read(ino, 0, m_maxbcachebuf, old_filesize, CONTENTS0);
1100 fd = open(FULLPATH, O_RDWR);
1101 EXPECT_LE(0, fd) << strerror(errno);
1103 /* Write and cache data beyond EOF */
1104 ASSERT_EQ(wbufsize, pwrite(fd, CONTENTS1, wbufsize, offset))
1107 /* Read from the old EOF */
1108 r = pread(fd, readbuf, rbufsize, 0);
1109 ASSERT_LE(0, r) << strerror(errno);
1110 EXPECT_EQ(rbufsize, r) << "read should've synthesized a hole";
1111 EXPECT_EQ(0, memcmp(CONTENTS0, readbuf, old_filesize));
1112 bzero(hole, holesize);
1113 EXPECT_EQ(0, memcmp(hole, readbuf + old_filesize, holesize));
1115 /* The file's size should still be what was established by pwrite */
1116 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1117 EXPECT_EQ(offset + wbufsize, sb.st_size);
1122 * When a file has dirty writes that haven't been flushed, the server's notion
1123 * of its mtime and ctime will be wrong. The kernel should ignore those if it
1124 * gets them from a FUSE_GETATTR before flushing.
1126 TEST_F(WriteBackAsync, timestamps)
1128 const char FULLPATH[] = "mountpoint/some_file.txt";
1129 const char RELPATH[] = "some_file.txt";
1130 const char *CONTENTS = "abcdefgh";
1131 ssize_t bufsize = strlen(CONTENTS);
1133 uint64_t attr_valid = 0;
1134 uint64_t attr_valid_nsec = 0;
1135 uint64_t server_time = 12345;
1136 mode_t mode = S_IFREG | 0644;
1141 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
1142 .WillRepeatedly(Invoke(
1143 ReturnImmediate([=](auto in __unused, auto& out) {
1144 SET_OUT_HEADER_LEN(out, entry);
1145 out.body.entry.attr.mode = mode;
1146 out.body.entry.nodeid = ino;
1147 out.body.entry.attr.nlink = 1;
1148 out.body.entry.attr_valid = attr_valid;
1149 out.body.entry.attr_valid_nsec = attr_valid_nsec;
1151 expect_open(ino, 0, 1);
1152 EXPECT_CALL(*m_mock, process(
1153 ResultOf([=](auto in) {
1154 return (in.header.opcode == FUSE_GETATTR &&
1155 in.header.nodeid == ino);
1158 ).WillRepeatedly(Invoke(
1159 ReturnImmediate([=](auto i __unused, auto& out) {
1160 SET_OUT_HEADER_LEN(out, attr);
1161 out.body.attr.attr.ino = ino;
1162 out.body.attr.attr.mode = mode;
1163 out.body.attr.attr_valid = attr_valid;
1164 out.body.attr.attr_valid_nsec = attr_valid_nsec;
1165 out.body.attr.attr.atime = server_time;
1166 out.body.attr.attr.mtime = server_time;
1167 out.body.attr.attr.ctime = server_time;
1170 fd = open(FULLPATH, O_RDWR);
1171 EXPECT_LE(0, fd) << strerror(errno);
1172 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1174 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1175 EXPECT_EQ((time_t)server_time, sb.st_atime);
1176 EXPECT_NE((time_t)server_time, sb.st_mtime);
1177 EXPECT_NE((time_t)server_time, sb.st_ctime);
1180 /* Any dirty timestamp fields should be flushed during a SETATTR */
1181 TEST_F(WriteBackAsync, timestamps_during_setattr)
1183 const char FULLPATH[] = "mountpoint/some_file.txt";
1184 const char RELPATH[] = "some_file.txt";
1185 const char *CONTENTS = "abcdefgh";
1186 ssize_t bufsize = strlen(CONTENTS);
1188 const mode_t newmode = 0755;
1191 expect_lookup(RELPATH, ino, 0);
1192 expect_open(ino, 0, 1);
1193 EXPECT_CALL(*m_mock, process(
1194 ResultOf([=](auto in) {
1195 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1196 return (in.header.opcode == FUSE_SETATTR &&
1197 in.header.nodeid == ino &&
1198 in.body.setattr.valid == valid);
1201 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1202 SET_OUT_HEADER_LEN(out, attr);
1203 out.body.attr.attr.ino = ino;
1204 out.body.attr.attr.mode = S_IFREG | newmode;
1207 fd = open(FULLPATH, O_RDWR);
1208 EXPECT_LE(0, fd) << strerror(errno);
1209 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1210 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1213 /* fuse_init_out.time_gran controls the granularity of timestamps */
1214 TEST_P(TimeGran, timestamps_during_setattr)
1216 const char FULLPATH[] = "mountpoint/some_file.txt";
1217 const char RELPATH[] = "some_file.txt";
1218 const char *CONTENTS = "abcdefgh";
1219 ssize_t bufsize = strlen(CONTENTS);
1221 const mode_t newmode = 0755;
1224 expect_lookup(RELPATH, ino, 0);
1225 expect_open(ino, 0, 1);
1226 EXPECT_CALL(*m_mock, process(
1227 ResultOf([=](auto in) {
1228 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1229 return (in.header.opcode == FUSE_SETATTR &&
1230 in.header.nodeid == ino &&
1231 in.body.setattr.valid == valid &&
1232 in.body.setattr.mtimensec % m_time_gran == 0 &&
1233 in.body.setattr.ctimensec % m_time_gran == 0);
1236 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1237 SET_OUT_HEADER_LEN(out, attr);
1238 out.body.attr.attr.ino = ino;
1239 out.body.attr.attr.mode = S_IFREG | newmode;
1242 fd = open(FULLPATH, O_RDWR);
1243 EXPECT_LE(0, fd) << strerror(errno);
1244 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1245 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1248 INSTANTIATE_TEST_CASE_P(RA, TimeGran, Range(0u, 10u));
1251 * Without direct_io, writes should be committed to cache
1253 TEST_F(Write, writethrough)
1255 const char FULLPATH[] = "mountpoint/some_file.txt";
1256 const char RELPATH[] = "some_file.txt";
1257 const char *CONTENTS = "abcdefgh";
1260 ssize_t bufsize = strlen(CONTENTS);
1261 char readbuf[bufsize];
1263 expect_lookup(RELPATH, ino, 0);
1264 expect_open(ino, 0, 1);
1265 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1267 fd = open(FULLPATH, O_RDWR);
1268 EXPECT_LE(0, fd) << strerror(errno);
1270 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1272 * A subsequent read should be serviced by cache, without querying the
1275 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1276 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1280 /* Writes that extend a file should update the cached file size */
1281 TEST_F(Write, update_file_size)
1283 const char FULLPATH[] = "mountpoint/some_file.txt";
1284 const char RELPATH[] = "some_file.txt";
1285 const char *CONTENTS = "abcdefgh";
1289 ssize_t bufsize = strlen(CONTENTS);
1291 expect_lookup(RELPATH, ino, 0);
1292 expect_open(ino, 0, 1);
1293 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1295 fd = open(FULLPATH, O_RDWR);
1296 EXPECT_LE(0, fd) << strerror(errno);
1298 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1299 /* Get cached attributes */
1300 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1301 ASSERT_EQ(bufsize, sb.st_size);