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
32 #include <sys/types.h>
35 #include <sys/sysctl.h>
46 using namespace testing;
48 class Write: public FuseTest {
52 void expect_lookup(const char *relpath, uint64_t ino, uint64_t size)
54 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, size, 1);
57 void expect_release(uint64_t ino, ProcessMockerT r)
59 EXPECT_CALL(*m_mock, process(
60 ResultOf([=](auto in) {
61 return (in->header.opcode == FUSE_RELEASE &&
62 in->header.nodeid == ino);
65 ).WillRepeatedly(Invoke(r));
68 void require_sync_resize_0() {
69 const char *sync_resize_node = "vfs.fusefs.sync_resize";
71 size_t size = sizeof(val);
73 ASSERT_EQ(0, sysctlbyname(sync_resize_node, &val, &size, NULL, 0))
77 "vfs.fusefs.sync_resize must be set to 0 for this test."
78 " That sysctl will probably be removed soon.";
83 class AioWrite: public Write {
84 virtual void SetUp() {
85 const char *node = "vfs.aio.enable_unsafe";
87 size_t size = sizeof(val);
91 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
95 "vfs.aio.enable_unsafe must be set for this test";
99 /* Tests for the write-through cache mode */
100 class WriteThrough: public Write {
102 virtual void SetUp() {
103 const char *cache_mode_node = "vfs.fusefs.data_cache_mode";
105 size_t size = sizeof(val);
111 ASSERT_EQ(0, sysctlbyname(cache_mode_node, &val, &size, NULL, 0))
114 GTEST_SKIP() << "vfs.fusefs.data_cache_mode must be set to 1 "
115 "(writethrough) for this test";
120 /* Tests for the writeback cache mode */
121 class WriteBack: public Write {
123 virtual void SetUp() {
124 const char *node = "vfs.fusefs.data_cache_mode";
126 size_t size = sizeof(val);
132 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
135 GTEST_SKIP() << "vfs.fusefs.data_cache_mode must be set to 2 "
136 "(writeback) for this test";
141 /* AIO writes need to set the header's pid field correctly */
142 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
143 TEST_F(AioWrite, DISABLED_aio_write)
145 const char FULLPATH[] = "mountpoint/some_file.txt";
146 const char RELPATH[] = "some_file.txt";
147 const char *CONTENTS = "abcdefgh";
149 uint64_t offset = 4096;
151 ssize_t bufsize = strlen(CONTENTS);
152 struct aiocb iocb, *piocb;
154 expect_lookup(RELPATH, ino, 0);
155 expect_open(ino, 0, 1);
156 expect_getattr(ino, 0);
157 expect_write(ino, offset, bufsize, bufsize, 0, CONTENTS);
159 fd = open(FULLPATH, O_WRONLY);
160 EXPECT_LE(0, fd) << strerror(errno);
162 iocb.aio_nbytes = bufsize;
163 iocb.aio_fildes = fd;
164 iocb.aio_buf = (void *)CONTENTS;
165 iocb.aio_offset = offset;
166 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
167 ASSERT_EQ(0, aio_write(&iocb)) << strerror(errno);
168 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
169 /* Deliberately leak fd. close(2) will be tested in release.cc */
173 * When a file is opened with O_APPEND, we should forward that flag to
174 * FUSE_OPEN (tested by Open.o_append) but still attempt to calculate the
175 * offset internally. That way we'll work both with filesystems that
176 * understand O_APPEND (and ignore the offset) and filesystems that don't (and
177 * simply use the offset).
179 * Note that verifying the O_APPEND flag in FUSE_OPEN is done in the
180 * Open.o_append test.
182 TEST_F(Write, append)
184 const ssize_t BUFSIZE = 9;
185 const char FULLPATH[] = "mountpoint/some_file.txt";
186 const char RELPATH[] = "some_file.txt";
187 const char CONTENTS[BUFSIZE] = "abcdefgh";
190 * Set offset to a maxbcachebuf boundary so we don't need to RMW when
191 * using writeback caching
193 uint64_t initial_offset = m_maxbcachebuf;
196 require_sync_resize_0();
198 expect_lookup(RELPATH, ino, initial_offset);
199 expect_open(ino, 0, 1);
200 expect_getattr(ino, initial_offset);
201 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, 0, CONTENTS);
203 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
204 fd = open(FULLPATH, O_RDWR | O_APPEND);
205 EXPECT_LE(0, fd) << strerror(errno);
207 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
208 /* Deliberately leak fd. close(2) will be tested in release.cc */
211 TEST_F(Write, append_direct_io)
213 const ssize_t BUFSIZE = 9;
214 const char FULLPATH[] = "mountpoint/some_file.txt";
215 const char RELPATH[] = "some_file.txt";
216 const char CONTENTS[BUFSIZE] = "abcdefgh";
218 uint64_t initial_offset = 4096;
221 expect_lookup(RELPATH, ino, initial_offset);
222 expect_open(ino, FOPEN_DIRECT_IO, 1);
223 expect_getattr(ino, initial_offset);
224 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, 0, CONTENTS);
226 fd = open(FULLPATH, O_WRONLY | O_APPEND);
227 EXPECT_LE(0, fd) << strerror(errno);
229 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
230 /* Deliberately leak fd. close(2) will be tested in release.cc */
233 /* A direct write should evict any overlapping cached data */
234 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235774 */
235 TEST_F(Write, DISABLED_direct_io_evicts_cache)
237 const char FULLPATH[] = "mountpoint/some_file.txt";
238 const char RELPATH[] = "some_file.txt";
239 const char CONTENTS0[] = "abcdefgh";
240 const char CONTENTS1[] = "ijklmnop";
243 ssize_t bufsize = strlen(CONTENTS0) + 1;
244 char readbuf[bufsize];
246 expect_lookup(RELPATH, ino, bufsize);
247 expect_open(ino, 0, 1);
248 expect_getattr(ino, bufsize);
249 expect_read(ino, 0, bufsize, bufsize, CONTENTS0);
250 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS1);
252 fd = open(FULLPATH, O_RDWR);
253 EXPECT_LE(0, fd) << strerror(errno);
256 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
258 // Write directly, evicting cache
259 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
260 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
261 ASSERT_EQ(bufsize, write(fd, CONTENTS1, bufsize)) << strerror(errno);
263 // Read again. Cache should be bypassed
264 expect_read(ino, 0, bufsize, bufsize, CONTENTS1);
265 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
266 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
267 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
268 ASSERT_STREQ(readbuf, CONTENTS1);
270 /* Deliberately leak fd. close(2) will be tested in release.cc */
274 * When the direct_io option is used, filesystems are allowed to write less
275 * data than requested
277 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236381 */
278 TEST_F(Write, DISABLED_direct_io_short_write)
280 const char FULLPATH[] = "mountpoint/some_file.txt";
281 const char RELPATH[] = "some_file.txt";
282 const char *CONTENTS = "abcdefghijklmnop";
285 ssize_t bufsize = strlen(CONTENTS);
286 ssize_t halfbufsize = bufsize / 2;
287 const char *halfcontents = CONTENTS + halfbufsize;
289 expect_lookup(RELPATH, ino, 0);
290 expect_open(ino, FOPEN_DIRECT_IO, 1);
291 expect_getattr(ino, 0);
292 expect_write(ino, 0, bufsize, halfbufsize, 0, CONTENTS);
293 expect_write(ino, halfbufsize, halfbufsize, halfbufsize, 0,
296 fd = open(FULLPATH, O_WRONLY);
297 EXPECT_LE(0, fd) << strerror(errno);
299 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
300 /* Deliberately leak fd. close(2) will be tested in release.cc */
304 * An insidious edge case: the filesystem returns a short write, and the
305 * difference between what we requested and what it actually wrote crosses an
306 * iov element boundary
308 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236381 */
309 TEST_F(Write, DISABLED_direct_io_short_write_iov)
311 const char FULLPATH[] = "mountpoint/some_file.txt";
312 const char RELPATH[] = "some_file.txt";
313 const char *CONTENTS0 = "abcdefgh";
314 const char *CONTENTS1 = "ijklmnop";
315 const char *EXPECTED0 = "abcdefghijklmnop";
316 const char *EXPECTED1 = "hijklmnop";
319 ssize_t size0 = strlen(CONTENTS0) - 1;
320 ssize_t size1 = strlen(CONTENTS1) + 1;
321 ssize_t totalsize = size0 + size1;
324 expect_lookup(RELPATH, ino, 0);
325 expect_open(ino, FOPEN_DIRECT_IO, 1);
326 expect_getattr(ino, 0);
327 expect_write(ino, 0, totalsize, size0, 0, EXPECTED0);
328 expect_write(ino, size0, size1, size1, 0, EXPECTED1);
330 fd = open(FULLPATH, O_WRONLY);
331 EXPECT_LE(0, fd) << strerror(errno);
333 iov[0].iov_base = (void*)CONTENTS0;
334 iov[0].iov_len = strlen(CONTENTS0);
335 iov[1].iov_base = (void*)CONTENTS1;
336 iov[1].iov_len = strlen(CONTENTS1);
337 ASSERT_EQ(totalsize, writev(fd, iov, 2)) << strerror(errno);
338 /* Deliberately leak fd. close(2) will be tested in release.cc */
342 * If the kernel cannot be sure which uid, gid, or pid was responsible for a
343 * write, then it must set the FUSE_WRITE_CACHE bit
345 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236378 */
346 // TODO: check vfs.fusefs.mmap_enable
347 TEST_F(Write, DISABLED_mmap)
349 const char FULLPATH[] = "mountpoint/some_file.txt";
350 const char RELPATH[] = "some_file.txt";
351 const char *CONTENTS = "abcdefgh";
354 ssize_t bufsize = strlen(CONTENTS);
356 uint64_t offset = 10;
358 void *zeros, *expected;
362 zeros = calloc(1, len);
363 ASSERT_NE(NULL, zeros);
364 expected = calloc(1, len);
365 ASSERT_NE(NULL, expected);
366 memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
368 expect_lookup(RELPATH, ino, len);
369 expect_open(ino, 0, 1);
370 expect_getattr(ino, len);
371 expect_read(ino, 0, len, len, zeros);
373 * Writes from the pager may or may not be associated with the correct
374 * pid, so they must set FUSE_WRITE_CACHE
376 expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, expected);
377 expect_release(ino, ReturnErrno(0));
379 fd = open(FULLPATH, O_RDWR);
380 EXPECT_LE(0, fd) << strerror(errno);
382 p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
383 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
385 memmove((uint8_t*)p + offset, CONTENTS, bufsize);
387 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
388 close(fd); // Write mmap'd data on close
394 TEST_F(Write, pwrite)
396 const char FULLPATH[] = "mountpoint/some_file.txt";
397 const char RELPATH[] = "some_file.txt";
398 const char *CONTENTS = "abcdefgh";
400 uint64_t offset = 4096;
402 ssize_t bufsize = strlen(CONTENTS);
404 expect_lookup(RELPATH, ino, 0);
405 expect_open(ino, 0, 1);
406 expect_getattr(ino, 0);
407 expect_write(ino, offset, bufsize, bufsize, 0, CONTENTS);
409 fd = open(FULLPATH, O_WRONLY);
410 EXPECT_LE(0, fd) << strerror(errno);
412 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
414 /* Deliberately leak fd. close(2) will be tested in release.cc */
419 const char FULLPATH[] = "mountpoint/some_file.txt";
420 const char RELPATH[] = "some_file.txt";
421 const char *CONTENTS = "abcdefgh";
424 ssize_t bufsize = strlen(CONTENTS);
426 expect_lookup(RELPATH, ino, 0);
427 expect_open(ino, 0, 1);
428 expect_getattr(ino, 0);
429 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
431 fd = open(FULLPATH, O_WRONLY);
432 EXPECT_LE(0, fd) << strerror(errno);
434 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
435 /* Deliberately leak fd. close(2) will be tested in release.cc */
438 /* fuse(4) should not issue writes of greater size than the daemon requests */
439 TEST_F(Write, write_large)
441 const char FULLPATH[] = "mountpoint/some_file.txt";
442 const char RELPATH[] = "some_file.txt";
446 ssize_t halfbufsize, bufsize;
448 halfbufsize = m_mock->m_max_write;
449 bufsize = halfbufsize * 2;
450 contents = (int*)malloc(bufsize);
451 ASSERT_NE(NULL, contents);
452 for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
456 expect_lookup(RELPATH, ino, 0);
457 expect_open(ino, 0, 1);
458 expect_getattr(ino, 0);
459 expect_write(ino, 0, halfbufsize, halfbufsize, 0, contents);
460 expect_write(ino, halfbufsize, halfbufsize, halfbufsize, 0,
461 &contents[halfbufsize / sizeof(int)]);
463 fd = open(FULLPATH, O_WRONLY);
464 EXPECT_LE(0, fd) << strerror(errno);
466 ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
467 /* Deliberately leak fd. close(2) will be tested in release.cc */
472 TEST_F(Write, write_nothing)
474 const char FULLPATH[] = "mountpoint/some_file.txt";
475 const char RELPATH[] = "some_file.txt";
476 const char *CONTENTS = "";
481 expect_lookup(RELPATH, ino, 0);
482 expect_open(ino, 0, 1);
483 expect_getattr(ino, 0);
485 fd = open(FULLPATH, O_WRONLY);
486 EXPECT_LE(0, fd) << strerror(errno);
488 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
489 /* Deliberately leak fd. close(2) will be tested in release.cc */
492 /* In writeback mode, dirty data should be written on close */
493 TEST_F(WriteBack, close)
495 const char FULLPATH[] = "mountpoint/some_file.txt";
496 const char RELPATH[] = "some_file.txt";
497 const char *CONTENTS = "abcdefgh";
500 ssize_t bufsize = strlen(CONTENTS);
502 expect_lookup(RELPATH, ino, 0);
503 expect_open(ino, 0, 1);
504 expect_getattr(ino, 0);
505 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
506 EXPECT_CALL(*m_mock, process(
507 ResultOf([=](auto in) {
508 return (in->header.opcode == FUSE_SETATTR);
511 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
512 SET_OUT_HEADER_LEN(out, attr);
513 out->body.attr.attr.ino = ino; // Must match nodeid
515 expect_release(ino, ReturnErrno(0));
517 fd = open(FULLPATH, O_RDWR);
518 ASSERT_LE(0, fd) << strerror(errno);
520 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
525 * Without direct_io, writes should be committed to cache
527 TEST_F(WriteBack, writeback)
529 const char FULLPATH[] = "mountpoint/some_file.txt";
530 const char RELPATH[] = "some_file.txt";
531 const char *CONTENTS = "abcdefgh";
534 ssize_t bufsize = strlen(CONTENTS);
535 char readbuf[bufsize];
537 expect_lookup(RELPATH, ino, 0);
538 expect_open(ino, 0, 1);
539 expect_getattr(ino, 0);
540 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
542 fd = open(FULLPATH, O_RDWR);
543 EXPECT_LE(0, fd) << strerror(errno);
545 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
547 * A subsequent read should be serviced by cache, without querying the
550 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
551 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
552 /* Deliberately leak fd. close(2) will be tested in release.cc */
556 * With O_DIRECT, writes should be not committed to cache. Admittedly this is
557 * an odd test, because it would be unusual to use O_DIRECT for writes but not
560 TEST_F(WriteBack, o_direct)
562 const char FULLPATH[] = "mountpoint/some_file.txt";
563 const char RELPATH[] = "some_file.txt";
564 const char *CONTENTS = "abcdefgh";
567 ssize_t bufsize = strlen(CONTENTS);
568 char readbuf[bufsize];
570 expect_lookup(RELPATH, ino, 0);
571 expect_open(ino, 0, 1);
572 expect_getattr(ino, 0);
573 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
574 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
576 fd = open(FULLPATH, O_RDWR | O_DIRECT);
577 EXPECT_LE(0, fd) << strerror(errno);
579 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
580 /* A subsequent read must query the daemon because cache is empty */
581 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
582 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
583 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
584 /* Deliberately leak fd. close(2) will be tested in release.cc */
588 * Without direct_io, writes should be committed to cache
591 * Disabled because we don't yet implement write-through caching. No bugzilla
592 * entry, because that's a feature request, not a bug.
594 TEST_F(WriteThrough, DISABLED_writethrough)
596 const char FULLPATH[] = "mountpoint/some_file.txt";
597 const char RELPATH[] = "some_file.txt";
598 const char *CONTENTS = "abcdefgh";
601 ssize_t bufsize = strlen(CONTENTS);
602 char readbuf[bufsize];
604 expect_lookup(RELPATH, ino, 0);
605 expect_open(ino, 0, 1);
606 expect_getattr(ino, 0);
607 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
609 fd = open(FULLPATH, O_RDWR);
610 EXPECT_LE(0, fd) << strerror(errno);
612 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
614 * A subsequent read should be serviced by cache, without querying the
617 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
618 /* Deliberately leak fd. close(2) will be tested in release.cc */
621 /* With writethrough caching, writes update the cached file size */
622 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
623 TEST_F(WriteThrough, DISABLED_update_file_size)
625 const char FULLPATH[] = "mountpoint/some_file.txt";
626 const char RELPATH[] = "some_file.txt";
627 const char *CONTENTS = "abcdefgh";
631 ssize_t bufsize = strlen(CONTENTS);
633 expect_lookup(RELPATH, ino, 0);
634 expect_open(ino, 0, 1);
635 EXPECT_CALL(*m_mock, process(
636 ResultOf([=](auto in) {
637 return (in->header.opcode == FUSE_GETATTR &&
638 in->header.nodeid == ino);
642 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
643 SET_OUT_HEADER_LEN(out, attr);
644 out->body.attr.attr.ino = ino; // Must match nodeid
645 out->body.attr.attr.mode = S_IFREG | 0644;
646 out->body.attr.attr.size = 0;
647 out->body.attr.attr_valid = UINT64_MAX;
649 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
651 fd = open(FULLPATH, O_RDWR);
652 EXPECT_LE(0, fd) << strerror(errno);
654 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
655 /* Get cached attributes */
656 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
657 ASSERT_EQ(bufsize, sb.st_size);
658 /* Deliberately leak fd. close(2) will be tested in release.cc */