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 = 1 << 25; // Anything larger than MAXPHYS will suffice
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
570 TEST_F(Write, pwrite)
572 const char FULLPATH[] = "mountpoint/some_file.txt";
573 const char RELPATH[] = "some_file.txt";
574 const char *CONTENTS = "abcdefgh";
576 uint64_t offset = m_maxbcachebuf;
578 ssize_t bufsize = strlen(CONTENTS);
580 expect_lookup(RELPATH, ino, 0);
581 expect_open(ino, 0, 1);
582 expect_write(ino, offset, bufsize, bufsize, CONTENTS);
584 fd = open(FULLPATH, O_WRONLY);
585 EXPECT_LE(0, fd) << strerror(errno);
587 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
592 /* Writing a file should update its cached mtime and ctime */
593 TEST_F(Write, timestamps)
595 const char FULLPATH[] = "mountpoint/some_file.txt";
596 const char RELPATH[] = "some_file.txt";
597 const char *CONTENTS = "abcdefgh";
598 ssize_t bufsize = strlen(CONTENTS);
600 struct stat sb0, sb1;
603 expect_lookup(RELPATH, ino, 0);
604 expect_open(ino, 0, 1);
605 maybe_expect_write(ino, 0, bufsize, CONTENTS);
607 fd = open(FULLPATH, O_RDWR);
608 EXPECT_LE(0, fd) << strerror(errno);
609 ASSERT_EQ(0, fstat(fd, &sb0)) << strerror(errno);
610 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
614 ASSERT_EQ(0, fstat(fd, &sb1)) << strerror(errno);
616 EXPECT_EQ(sb0.st_atime, sb1.st_atime);
617 EXPECT_NE(sb0.st_mtime, sb1.st_mtime);
618 EXPECT_NE(sb0.st_ctime, sb1.st_ctime);
625 const char FULLPATH[] = "mountpoint/some_file.txt";
626 const char RELPATH[] = "some_file.txt";
627 const char *CONTENTS = "abcdefgh";
630 ssize_t bufsize = strlen(CONTENTS);
632 expect_lookup(RELPATH, ino, 0);
633 expect_open(ino, 0, 1);
634 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
636 fd = open(FULLPATH, O_WRONLY);
637 EXPECT_LE(0, fd) << strerror(errno);
639 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
643 /* fuse(4) should not issue writes of greater size than the daemon requests */
644 TEST_F(Write, write_large)
646 const char FULLPATH[] = "mountpoint/some_file.txt";
647 const char RELPATH[] = "some_file.txt";
651 ssize_t halfbufsize, bufsize;
653 halfbufsize = m_mock->m_maxwrite;
654 bufsize = halfbufsize * 2;
655 contents = (int*)malloc(bufsize);
656 ASSERT_NE(nullptr, contents);
657 for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
661 expect_lookup(RELPATH, ino, 0);
662 expect_open(ino, 0, 1);
663 maybe_expect_write(ino, 0, halfbufsize, contents);
664 maybe_expect_write(ino, halfbufsize, halfbufsize,
665 &contents[halfbufsize / sizeof(int)]);
667 fd = open(FULLPATH, O_WRONLY);
668 EXPECT_LE(0, fd) << strerror(errno);
670 ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
676 TEST_F(Write, write_nothing)
678 const char FULLPATH[] = "mountpoint/some_file.txt";
679 const char RELPATH[] = "some_file.txt";
680 const char *CONTENTS = "";
685 expect_lookup(RELPATH, ino, 0);
686 expect_open(ino, 0, 1);
688 fd = open(FULLPATH, O_WRONLY);
689 EXPECT_LE(0, fd) << strerror(errno);
691 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
695 TEST_F(Write_7_8, write)
697 const char FULLPATH[] = "mountpoint/some_file.txt";
698 const char RELPATH[] = "some_file.txt";
699 const char *CONTENTS = "abcdefgh";
702 ssize_t bufsize = strlen(CONTENTS);
704 expect_lookup(RELPATH, ino, 0);
705 expect_open(ino, 0, 1);
706 expect_write_7_8(ino, 0, bufsize, bufsize, CONTENTS);
708 fd = open(FULLPATH, O_WRONLY);
709 EXPECT_LE(0, fd) << strerror(errno);
711 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
715 /* In writeback mode, dirty data should be written on close */
716 TEST_F(WriteBackAsync, close)
718 const char FULLPATH[] = "mountpoint/some_file.txt";
719 const char RELPATH[] = "some_file.txt";
720 const char *CONTENTS = "abcdefgh";
723 ssize_t bufsize = strlen(CONTENTS);
725 expect_lookup(RELPATH, ino, 0);
726 expect_open(ino, 0, 1);
727 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
728 EXPECT_CALL(*m_mock, process(
729 ResultOf([=](auto in) {
730 return (in.header.opcode == FUSE_SETATTR);
733 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
734 SET_OUT_HEADER_LEN(out, attr);
735 out.body.attr.attr.ino = ino; // Must match nodeid
737 expect_flush(ino, 1, ReturnErrno(0));
738 expect_release(ino, ReturnErrno(0));
740 fd = open(FULLPATH, O_RDWR);
741 ASSERT_LE(0, fd) << strerror(errno);
743 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
747 /* In writeback mode, adjacent writes will be clustered together */
748 TEST_F(WriteCluster, clustering)
750 const char FULLPATH[] = "mountpoint/some_file.txt";
751 const char RELPATH[] = "some_file.txt";
755 ssize_t bufsize = m_maxbcachebuf;
756 off_t filesize = 5 * bufsize;
758 wbuf = malloc(bufsize);
759 ASSERT_NE(nullptr, wbuf) << strerror(errno);
760 memset(wbuf, 'X', bufsize);
761 wbuf2x = malloc(2 * bufsize);
762 ASSERT_NE(nullptr, wbuf2x) << strerror(errno);
763 memset(wbuf2x, 'X', 2 * bufsize);
765 expect_lookup(RELPATH, ino, filesize);
766 expect_open(ino, 0, 1);
768 * Writes of bufsize-bytes each should be clustered into greater sizes.
769 * The amount of clustering is adaptive, so the first write actually
770 * issued will be 2x bufsize and subsequent writes may be larger
772 expect_write(ino, 0, 2 * bufsize, 2 * bufsize, wbuf2x);
773 expect_write(ino, 2 * bufsize, 2 * bufsize, 2 * bufsize, wbuf2x);
774 expect_flush(ino, 1, ReturnErrno(0));
775 expect_release(ino, ReturnErrno(0));
777 fd = open(FULLPATH, O_RDWR);
778 ASSERT_LE(0, fd) << strerror(errno);
780 for (i = 0; i < 4; i++) {
781 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
788 * When clustering writes, an I/O error to any of the cluster's children should
789 * not panic the system on unmount
792 * Disabled because it panics.
793 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=238565
795 TEST_F(WriteCluster, DISABLED_cluster_write_err)
797 const char FULLPATH[] = "mountpoint/some_file.txt";
798 const char RELPATH[] = "some_file.txt";
802 ssize_t bufsize = m_maxbcachebuf;
803 off_t filesize = 4 * bufsize;
805 wbuf = malloc(bufsize);
806 ASSERT_NE(nullptr, wbuf) << strerror(errno);
807 memset(wbuf, 'X', bufsize);
809 expect_lookup(RELPATH, ino, filesize);
810 expect_open(ino, 0, 1);
811 EXPECT_CALL(*m_mock, process(
812 ResultOf([=](auto in) {
813 return (in.header.opcode == FUSE_WRITE);
816 ).WillRepeatedly(Invoke(ReturnErrno(EIO)));
817 expect_flush(ino, 1, ReturnErrno(0));
818 expect_release(ino, ReturnErrno(0));
820 fd = open(FULLPATH, O_RDWR);
821 ASSERT_LE(0, fd) << strerror(errno);
823 for (i = 0; i < 3; i++) {
824 ASSERT_EQ(bufsize, write(fd, wbuf, bufsize))
831 * In writeback mode, writes to an O_WRONLY file could trigger reads from the
832 * server. The FUSE protocol explicitly allows that.
834 TEST_F(WriteBack, rmw)
836 const char FULLPATH[] = "mountpoint/some_file.txt";
837 const char RELPATH[] = "some_file.txt";
838 const char *CONTENTS = "abcdefgh";
839 const char *INITIAL = "XXXXXXXXXX";
844 ssize_t bufsize = strlen(CONTENTS);
846 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
847 expect_open(ino, 0, 1);
848 expect_read(ino, 0, fsize, fsize, INITIAL, O_WRONLY);
849 maybe_expect_write(ino, offset, bufsize, CONTENTS);
851 fd = open(FULLPATH, O_WRONLY);
852 EXPECT_LE(0, fd) << strerror(errno);
854 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
860 * Without direct_io, writes should be committed to cache
862 TEST_F(WriteBack, cache)
864 const char FULLPATH[] = "mountpoint/some_file.txt";
865 const char RELPATH[] = "some_file.txt";
866 const char *CONTENTS = "abcdefgh";
869 ssize_t bufsize = strlen(CONTENTS);
870 uint8_t readbuf[bufsize];
872 expect_lookup(RELPATH, ino, 0);
873 expect_open(ino, 0, 1);
874 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
876 fd = open(FULLPATH, O_RDWR);
877 EXPECT_LE(0, fd) << strerror(errno);
879 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
881 * A subsequent read should be serviced by cache, without querying the
884 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
885 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
890 * With O_DIRECT, writes should be not committed to cache. Admittedly this is
891 * an odd test, because it would be unusual to use O_DIRECT for writes but not
894 TEST_F(WriteBack, o_direct)
896 const char FULLPATH[] = "mountpoint/some_file.txt";
897 const char RELPATH[] = "some_file.txt";
898 const char *CONTENTS = "abcdefgh";
901 ssize_t bufsize = strlen(CONTENTS);
902 uint8_t readbuf[bufsize];
904 expect_lookup(RELPATH, ino, 0);
905 expect_open(ino, 0, 1);
906 FuseTest::expect_write(ino, 0, bufsize, bufsize, 0, FUSE_WRITE_CACHE,
908 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
910 fd = open(FULLPATH, O_RDWR | O_DIRECT);
911 EXPECT_LE(0, fd) << strerror(errno);
913 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
914 /* A subsequent read must query the daemon because cache is empty */
915 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
916 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
917 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
922 * When mounted with -o async, the writeback cache mode should delay writes
924 TEST_F(WriteBackAsync, delay)
926 const char FULLPATH[] = "mountpoint/some_file.txt";
927 const char RELPATH[] = "some_file.txt";
928 const char *CONTENTS = "abcdefgh";
931 ssize_t bufsize = strlen(CONTENTS);
933 expect_lookup(RELPATH, ino, 0);
934 expect_open(ino, 0, 1);
935 /* Write should be cached, but FUSE_WRITE shouldn't be sent */
936 EXPECT_CALL(*m_mock, process(
937 ResultOf([=](auto in) {
938 return (in.header.opcode == FUSE_WRITE);
943 fd = open(FULLPATH, O_RDWR);
944 EXPECT_LE(0, fd) << strerror(errno);
946 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
948 /* Don't close the file because that would flush the cache */
953 * A direct write should not evict dirty cached data from outside of its own
956 TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
958 const char FULLPATH[] = "mountpoint/some_file.txt";
959 const char RELPATH[] = "some_file.txt";
960 const char CONTENTS0[] = "abcdefgh";
961 const char CONTENTS1[] = "ijklmnop";
964 ssize_t bufsize = strlen(CONTENTS0) + 1;
965 ssize_t fsize = 2 * m_maxbcachebuf;
966 char readbuf[bufsize];
969 zeros = calloc(1, m_maxbcachebuf);
970 ASSERT_NE(nullptr, zeros);
972 expect_lookup(RELPATH, ino, fsize);
973 expect_open(ino, 0, 1);
974 expect_read(ino, 0, m_maxbcachebuf, m_maxbcachebuf, zeros);
975 FuseTest::expect_write(ino, m_maxbcachebuf, bufsize, bufsize, 0, 0,
978 fd = open(FULLPATH, O_RDWR);
979 EXPECT_LE(0, fd) << strerror(errno);
981 // Cache first block with dirty data. This will entail first reading
982 // the existing data.
983 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS0, bufsize, 0))
986 // Write directly to second block
987 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
988 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS1, bufsize, m_maxbcachebuf))
991 // Read from the first block again. Should be serviced by cache.
992 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
993 ASSERT_EQ(bufsize, pread(fd, readbuf, bufsize, 0)) << strerror(errno);
994 ASSERT_STREQ(readbuf, CONTENTS0);
1001 * If a direct io write partially overlaps one or two blocks of dirty cached
1002 * data, No dirty data should be lost. Admittedly this is a weird test,
1003 * because it would be unusual to use O_DIRECT and the writeback cache.
1005 TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
1007 const char FULLPATH[] = "mountpoint/some_file.txt";
1008 const char RELPATH[] = "some_file.txt";
1011 off_t bs = m_maxbcachebuf;
1012 ssize_t fsize = 3 * bs;
1013 void *readbuf, *zeros, *ones, *zeroones, *onezeros;
1015 readbuf = malloc(bs);
1016 ASSERT_NE(nullptr, readbuf) << strerror(errno);
1017 zeros = calloc(1, 3 * bs);
1018 ASSERT_NE(nullptr, zeros);
1019 ones = calloc(1, 2 * bs);
1020 ASSERT_NE(nullptr, ones);
1021 memset(ones, 1, 2 * bs);
1022 zeroones = calloc(1, bs);
1023 ASSERT_NE(nullptr, zeroones);
1024 memset((uint8_t*)zeroones + bs / 2, 1, bs / 2);
1025 onezeros = calloc(1, bs);
1026 ASSERT_NE(nullptr, onezeros);
1027 memset(onezeros, 1, bs / 2);
1029 expect_lookup(RELPATH, ino, fsize);
1030 expect_open(ino, 0, 1);
1032 fd = open(FULLPATH, O_RDWR);
1033 EXPECT_LE(0, fd) << strerror(errno);
1035 /* Cache first and third blocks with dirty data. */
1036 ASSERT_EQ(3 * bs, pwrite(fd, zeros, 3 * bs, 0)) << strerror(errno);
1039 * Write directly to all three blocks. The partially written blocks
1040 * will be flushed because they're dirty.
1042 FuseTest::expect_write(ino, 0, bs, bs, 0, 0, zeros);
1043 FuseTest::expect_write(ino, 2 * bs, bs, bs, 0, 0, zeros);
1044 /* The direct write is split in two because of the m_maxwrite value */
1045 FuseTest::expect_write(ino, bs / 2, bs, bs, 0, 0, ones);
1046 FuseTest::expect_write(ino, 3 * bs / 2, bs, bs, 0, 0, ones);
1047 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
1048 ASSERT_EQ(2 * bs, pwrite(fd, ones, 2 * bs, bs / 2)) << strerror(errno);
1051 * Read from both the valid and invalid portions of the first and third
1052 * blocks again. This will entail FUSE_READ operations because these
1053 * blocks were invalidated by the direct write.
1055 expect_read(ino, 0, bs, bs, zeroones);
1056 expect_read(ino, 2 * bs, bs, bs, onezeros);
1057 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
1058 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 0)) << strerror(errno);
1059 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1060 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 5 * bs / 2))
1062 EXPECT_EQ(0, memcmp(zeros, readbuf, bs / 2));
1063 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, bs / 2))
1065 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1066 ASSERT_EQ(bs / 2, pread(fd, readbuf, bs / 2, 2 * bs))
1068 EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
1079 * In WriteBack mode, writes may be cached beyond what the server thinks is the
1080 * EOF. In this case, a short read at EOF should _not_ cause fusefs to update
1083 TEST_F(WriteBackAsync, eof)
1085 const char FULLPATH[] = "mountpoint/some_file.txt";
1086 const char RELPATH[] = "some_file.txt";
1087 const char *CONTENTS0 = "abcdefgh";
1088 const char *CONTENTS1 = "ijklmnop";
1091 off_t offset = m_maxbcachebuf;
1092 ssize_t wbufsize = strlen(CONTENTS1);
1093 off_t old_filesize = (off_t)strlen(CONTENTS0);
1094 ssize_t rbufsize = 2 * old_filesize;
1095 char readbuf[rbufsize];
1096 size_t holesize = rbufsize - old_filesize;
1097 char hole[holesize];
1101 expect_lookup(RELPATH, ino, 0);
1102 expect_open(ino, 0, 1);
1103 expect_read(ino, 0, m_maxbcachebuf, old_filesize, CONTENTS0);
1105 fd = open(FULLPATH, O_RDWR);
1106 EXPECT_LE(0, fd) << strerror(errno);
1108 /* Write and cache data beyond EOF */
1109 ASSERT_EQ(wbufsize, pwrite(fd, CONTENTS1, wbufsize, offset))
1112 /* Read from the old EOF */
1113 r = pread(fd, readbuf, rbufsize, 0);
1114 ASSERT_LE(0, r) << strerror(errno);
1115 EXPECT_EQ(rbufsize, r) << "read should've synthesized a hole";
1116 EXPECT_EQ(0, memcmp(CONTENTS0, readbuf, old_filesize));
1117 bzero(hole, holesize);
1118 EXPECT_EQ(0, memcmp(hole, readbuf + old_filesize, holesize));
1120 /* The file's size should still be what was established by pwrite */
1121 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1122 EXPECT_EQ(offset + wbufsize, sb.st_size);
1127 * When a file has dirty writes that haven't been flushed, the server's notion
1128 * of its mtime and ctime will be wrong. The kernel should ignore those if it
1129 * gets them from a FUSE_GETATTR before flushing.
1131 TEST_F(WriteBackAsync, timestamps)
1133 const char FULLPATH[] = "mountpoint/some_file.txt";
1134 const char RELPATH[] = "some_file.txt";
1135 const char *CONTENTS = "abcdefgh";
1136 ssize_t bufsize = strlen(CONTENTS);
1138 uint64_t attr_valid = 0;
1139 uint64_t attr_valid_nsec = 0;
1140 uint64_t server_time = 12345;
1141 mode_t mode = S_IFREG | 0644;
1146 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
1147 .WillRepeatedly(Invoke(
1148 ReturnImmediate([=](auto in __unused, auto& out) {
1149 SET_OUT_HEADER_LEN(out, entry);
1150 out.body.entry.attr.mode = mode;
1151 out.body.entry.nodeid = ino;
1152 out.body.entry.attr.nlink = 1;
1153 out.body.entry.attr_valid = attr_valid;
1154 out.body.entry.attr_valid_nsec = attr_valid_nsec;
1156 expect_open(ino, 0, 1);
1157 EXPECT_CALL(*m_mock, process(
1158 ResultOf([=](auto in) {
1159 return (in.header.opcode == FUSE_GETATTR &&
1160 in.header.nodeid == ino);
1163 ).WillRepeatedly(Invoke(
1164 ReturnImmediate([=](auto i __unused, auto& out) {
1165 SET_OUT_HEADER_LEN(out, attr);
1166 out.body.attr.attr.ino = ino;
1167 out.body.attr.attr.mode = mode;
1168 out.body.attr.attr_valid = attr_valid;
1169 out.body.attr.attr_valid_nsec = attr_valid_nsec;
1170 out.body.attr.attr.atime = server_time;
1171 out.body.attr.attr.mtime = server_time;
1172 out.body.attr.attr.ctime = server_time;
1175 fd = open(FULLPATH, O_RDWR);
1176 EXPECT_LE(0, fd) << strerror(errno);
1177 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1179 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1180 EXPECT_EQ((time_t)server_time, sb.st_atime);
1181 EXPECT_NE((time_t)server_time, sb.st_mtime);
1182 EXPECT_NE((time_t)server_time, sb.st_ctime);
1187 /* Any dirty timestamp fields should be flushed during a SETATTR */
1188 TEST_F(WriteBackAsync, timestamps_during_setattr)
1190 const char FULLPATH[] = "mountpoint/some_file.txt";
1191 const char RELPATH[] = "some_file.txt";
1192 const char *CONTENTS = "abcdefgh";
1193 ssize_t bufsize = strlen(CONTENTS);
1195 const mode_t newmode = 0755;
1198 expect_lookup(RELPATH, ino, 0);
1199 expect_open(ino, 0, 1);
1200 EXPECT_CALL(*m_mock, process(
1201 ResultOf([=](auto in) {
1202 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1203 return (in.header.opcode == FUSE_SETATTR &&
1204 in.header.nodeid == ino &&
1205 in.body.setattr.valid == valid);
1208 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1209 SET_OUT_HEADER_LEN(out, attr);
1210 out.body.attr.attr.ino = ino;
1211 out.body.attr.attr.mode = S_IFREG | newmode;
1214 fd = open(FULLPATH, O_RDWR);
1215 EXPECT_LE(0, fd) << strerror(errno);
1216 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1217 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1222 /* fuse_init_out.time_gran controls the granularity of timestamps */
1223 TEST_P(TimeGran, timestamps_during_setattr)
1225 const char FULLPATH[] = "mountpoint/some_file.txt";
1226 const char RELPATH[] = "some_file.txt";
1227 const char *CONTENTS = "abcdefgh";
1228 ssize_t bufsize = strlen(CONTENTS);
1230 const mode_t newmode = 0755;
1233 expect_lookup(RELPATH, ino, 0);
1234 expect_open(ino, 0, 1);
1235 EXPECT_CALL(*m_mock, process(
1236 ResultOf([=](auto in) {
1237 uint32_t valid = FATTR_MODE | FATTR_MTIME | FATTR_CTIME;
1238 return (in.header.opcode == FUSE_SETATTR &&
1239 in.header.nodeid == ino &&
1240 in.body.setattr.valid == valid &&
1241 in.body.setattr.mtimensec % m_time_gran == 0 &&
1242 in.body.setattr.ctimensec % m_time_gran == 0);
1245 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1246 SET_OUT_HEADER_LEN(out, attr);
1247 out.body.attr.attr.ino = ino;
1248 out.body.attr.attr.mode = S_IFREG | newmode;
1251 fd = open(FULLPATH, O_RDWR);
1252 EXPECT_LE(0, fd) << strerror(errno);
1253 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1254 ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
1259 INSTANTIATE_TEST_CASE_P(RA, TimeGran, Range(0u, 10u));
1262 * Without direct_io, writes should be committed to cache
1264 TEST_F(Write, writethrough)
1266 const char FULLPATH[] = "mountpoint/some_file.txt";
1267 const char RELPATH[] = "some_file.txt";
1268 const char *CONTENTS = "abcdefgh";
1271 ssize_t bufsize = strlen(CONTENTS);
1272 uint8_t readbuf[bufsize];
1274 expect_lookup(RELPATH, ino, 0);
1275 expect_open(ino, 0, 1);
1276 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1278 fd = open(FULLPATH, O_RDWR);
1279 EXPECT_LE(0, fd) << strerror(errno);
1281 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1283 * A subsequent read should be serviced by cache, without querying the
1286 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
1287 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
1291 /* Writes that extend a file should update the cached file size */
1292 TEST_F(Write, update_file_size)
1294 const char FULLPATH[] = "mountpoint/some_file.txt";
1295 const char RELPATH[] = "some_file.txt";
1296 const char *CONTENTS = "abcdefgh";
1300 ssize_t bufsize = strlen(CONTENTS);
1302 expect_lookup(RELPATH, ino, 0);
1303 expect_open(ino, 0, 1);
1304 expect_write(ino, 0, bufsize, bufsize, CONTENTS);
1306 fd = open(FULLPATH, O_RDWR);
1307 EXPECT_LE(0, fd) << strerror(errno);
1309 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
1310 /* Get cached attributes */
1311 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
1312 ASSERT_EQ(bufsize, sb.st_size);