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)
54 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 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.fuse.sync_resize";
71 size_t size = sizeof(val);
73 ASSERT_EQ(0, sysctlbyname(sync_resize_node, &val, &size, NULL, 0))
76 FAIL() << "vfs.fuse.sync_resize must be set to 0 for this test."
77 " That sysctl will probably be removed soon.";
82 class AioWrite: public Write {
83 virtual void SetUp() {
84 const char *node = "vfs.aio.enable_unsafe";
86 size_t size = sizeof(val);
88 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
90 // TODO: With GoogleTest 1.8.2, use SKIP instead
92 FAIL() << "vfs.aio.enable_unsafe must be set for this test";
97 /* Tests for the write-through cache mode */
98 class WriteThrough: public Write {
100 virtual void SetUp() {
101 const char *cache_mode_node = "vfs.fuse.data_cache_mode";
103 size_t size = sizeof(val);
105 ASSERT_EQ(0, sysctlbyname(cache_mode_node, &val, &size, NULL, 0))
107 // TODO: With GoogleTest 1.8.2, use SKIP instead
109 FAIL() << "vfs.fuse.data_cache_mode must be set to 1 "
110 "(writethrough) for this test";
117 /* Tests for the writeback cache mode */
118 class WriteBack: public Write {
120 virtual void SetUp() {
121 const char *node = "vfs.fuse.data_cache_mode";
123 size_t size = sizeof(val);
125 ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
127 // TODO: With GoogleTest 1.8.2, use SKIP instead
129 FAIL() << "vfs.fuse.data_cache_mode must be set to 2 "
130 "(writeback) for this test";
136 /* AIO writes need to set the header's pid field correctly */
137 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
138 TEST_F(AioWrite, DISABLED_aio_write)
140 const char FULLPATH[] = "mountpoint/some_file.txt";
141 const char RELPATH[] = "some_file.txt";
142 const char *CONTENTS = "abcdefgh";
144 uint64_t offset = 4096;
146 ssize_t bufsize = strlen(CONTENTS);
147 struct aiocb iocb, *piocb;
149 expect_lookup(RELPATH, ino);
150 expect_open(ino, 0, 1);
151 expect_getattr(ino, 0);
152 expect_write(ino, offset, bufsize, bufsize, 0, CONTENTS);
154 fd = open(FULLPATH, O_WRONLY);
155 EXPECT_LE(0, fd) << strerror(errno);
157 iocb.aio_nbytes = bufsize;
158 iocb.aio_fildes = fd;
159 iocb.aio_buf = (void *)CONTENTS;
160 iocb.aio_offset = offset;
161 iocb.aio_sigevent.sigev_notify = SIGEV_NONE;
162 ASSERT_EQ(0, aio_write(&iocb)) << strerror(errno);
163 ASSERT_EQ(bufsize, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
164 /* Deliberately leak fd. close(2) will be tested in release.cc */
168 * When a file is opened with O_APPEND, we should forward that flag to
169 * FUSE_OPEN (tested by Open.o_append) but still attempt to calculate the
170 * offset internally. That way we'll work both with filesystems that
171 * understand O_APPEND (and ignore the offset) and filesystems that don't (and
172 * simply use the offset).
174 * Note that verifying the O_APPEND flag in FUSE_OPEN is done in the
175 * Open.o_append test.
177 TEST_F(Write, append)
179 const ssize_t BUFSIZE = 9;
180 const char FULLPATH[] = "mountpoint/some_file.txt";
181 const char RELPATH[] = "some_file.txt";
182 const char CONTENTS[BUFSIZE] = "abcdefgh";
185 * Set offset to a maxbcachebuf boundary so we don't need to RMW when
186 * using writeback caching
188 uint64_t initial_offset = m_maxbcachebuf;
191 require_sync_resize_0();
193 expect_lookup(RELPATH, ino);
194 expect_open(ino, 0, 1);
195 expect_getattr(ino, initial_offset);
196 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, 0, CONTENTS);
198 /* Must open O_RDWR or fuse(4) implicitly sets direct_io */
199 fd = open(FULLPATH, O_RDWR | O_APPEND);
200 EXPECT_LE(0, fd) << strerror(errno);
202 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
203 /* Deliberately leak fd. close(2) will be tested in release.cc */
206 TEST_F(Write, append_direct_io)
208 const ssize_t BUFSIZE = 9;
209 const char FULLPATH[] = "mountpoint/some_file.txt";
210 const char RELPATH[] = "some_file.txt";
211 const char CONTENTS[BUFSIZE] = "abcdefgh";
213 uint64_t initial_offset = 4096;
216 expect_lookup(RELPATH, ino);
217 expect_open(ino, FOPEN_DIRECT_IO, 1);
218 expect_getattr(ino, initial_offset);
219 expect_write(ino, initial_offset, BUFSIZE, BUFSIZE, 0, CONTENTS);
221 fd = open(FULLPATH, O_WRONLY | O_APPEND);
222 EXPECT_LE(0, fd) << strerror(errno);
224 ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
225 /* Deliberately leak fd. close(2) will be tested in release.cc */
228 /* A direct write should evict any overlapping cached data */
229 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235774 */
230 TEST_F(Write, DISABLED_direct_io_evicts_cache)
232 const char FULLPATH[] = "mountpoint/some_file.txt";
233 const char RELPATH[] = "some_file.txt";
234 const char CONTENTS0[] = "abcdefgh";
235 const char CONTENTS1[] = "ijklmnop";
238 ssize_t bufsize = strlen(CONTENTS0) + 1;
239 char readbuf[bufsize];
241 expect_lookup(RELPATH, ino);
242 expect_open(ino, 0, 1);
243 expect_getattr(ino, bufsize);
244 expect_read(ino, 0, bufsize, bufsize, CONTENTS0);
245 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS1);
247 fd = open(FULLPATH, O_RDWR);
248 EXPECT_LE(0, fd) << strerror(errno);
251 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
253 // Write directly, evicting cache
254 ASSERT_EQ(0, fcntl(fd, F_SETFL, O_DIRECT)) << strerror(errno);
255 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
256 ASSERT_EQ(bufsize, write(fd, CONTENTS1, bufsize)) << strerror(errno);
258 // Read again. Cache should be bypassed
259 expect_read(ino, 0, bufsize, bufsize, CONTENTS1);
260 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
261 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
262 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
263 ASSERT_STREQ(readbuf, CONTENTS1);
265 /* Deliberately leak fd. close(2) will be tested in release.cc */
269 * When the direct_io option is used, filesystems are allowed to write less
270 * data than requested
272 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236381 */
273 TEST_F(Write, DISABLED_direct_io_short_write)
275 const char FULLPATH[] = "mountpoint/some_file.txt";
276 const char RELPATH[] = "some_file.txt";
277 const char *CONTENTS = "abcdefghijklmnop";
280 ssize_t bufsize = strlen(CONTENTS);
281 ssize_t halfbufsize = bufsize / 2;
282 const char *halfcontents = CONTENTS + halfbufsize;
284 expect_lookup(RELPATH, ino);
285 expect_open(ino, FOPEN_DIRECT_IO, 1);
286 expect_getattr(ino, 0);
287 expect_write(ino, 0, bufsize, halfbufsize, 0, CONTENTS);
288 expect_write(ino, halfbufsize, halfbufsize, halfbufsize, 0,
291 fd = open(FULLPATH, O_WRONLY);
292 EXPECT_LE(0, fd) << strerror(errno);
294 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
295 /* Deliberately leak fd. close(2) will be tested in release.cc */
299 * An insidious edge case: the filesystem returns a short write, and the
300 * difference between what we requested and what it actually wrote crosses an
301 * iov element boundary
303 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236381 */
304 TEST_F(Write, DISABLED_direct_io_short_write_iov)
306 const char FULLPATH[] = "mountpoint/some_file.txt";
307 const char RELPATH[] = "some_file.txt";
308 const char *CONTENTS0 = "abcdefgh";
309 const char *CONTENTS1 = "ijklmnop";
310 const char *EXPECTED0 = "abcdefghijklmnop";
311 const char *EXPECTED1 = "hijklmnop";
314 ssize_t size0 = strlen(CONTENTS0) - 1;
315 ssize_t size1 = strlen(CONTENTS1) + 1;
316 ssize_t totalsize = size0 + size1;
319 expect_lookup(RELPATH, ino);
320 expect_open(ino, FOPEN_DIRECT_IO, 1);
321 expect_getattr(ino, 0);
322 expect_write(ino, 0, totalsize, size0, 0, EXPECTED0);
323 expect_write(ino, size0, size1, size1, 0, EXPECTED1);
325 fd = open(FULLPATH, O_WRONLY);
326 EXPECT_LE(0, fd) << strerror(errno);
328 iov[0].iov_base = (void*)CONTENTS0;
329 iov[0].iov_len = strlen(CONTENTS0);
330 iov[1].iov_base = (void*)CONTENTS1;
331 iov[1].iov_len = strlen(CONTENTS1);
332 ASSERT_EQ(totalsize, writev(fd, iov, 2)) << strerror(errno);
333 /* Deliberately leak fd. close(2) will be tested in release.cc */
337 * If the kernel cannot be sure which uid, gid, or pid was responsible for a
338 * write, then it must set the FUSE_WRITE_CACHE bit
340 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236378 */
341 // TODO: check vfs.fuse.mmap_enable
342 TEST_F(Write, DISABLED_mmap)
344 const char FULLPATH[] = "mountpoint/some_file.txt";
345 const char RELPATH[] = "some_file.txt";
346 const char *CONTENTS = "abcdefgh";
349 ssize_t bufsize = strlen(CONTENTS);
351 uint64_t offset = 10;
353 void *zeros, *expected;
357 zeros = calloc(1, len);
358 ASSERT_NE(NULL, zeros);
359 expected = calloc(1, len);
360 ASSERT_NE(NULL, expected);
361 memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
363 expect_lookup(RELPATH, ino);
364 expect_open(ino, 0, 1);
365 expect_getattr(ino, len);
366 expect_read(ino, 0, len, len, zeros);
368 * Writes from the pager may or may not be associated with the correct
369 * pid, so they must set FUSE_WRITE_CACHE
371 expect_write(ino, 0, len, len, FUSE_WRITE_CACHE, expected);
372 expect_release(ino, ReturnErrno(0));
374 fd = open(FULLPATH, O_RDWR);
375 EXPECT_LE(0, fd) << strerror(errno);
377 p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
378 ASSERT_NE(MAP_FAILED, p) << strerror(errno);
380 memmove((uint8_t*)p + offset, CONTENTS, bufsize);
382 ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
383 close(fd); // Write mmap'd data on close
389 TEST_F(Write, pwrite)
391 const char FULLPATH[] = "mountpoint/some_file.txt";
392 const char RELPATH[] = "some_file.txt";
393 const char *CONTENTS = "abcdefgh";
395 uint64_t offset = 4096;
397 ssize_t bufsize = strlen(CONTENTS);
399 expect_lookup(RELPATH, ino);
400 expect_open(ino, 0, 1);
401 expect_getattr(ino, 0);
402 expect_write(ino, offset, bufsize, bufsize, 0, CONTENTS);
404 fd = open(FULLPATH, O_WRONLY);
405 EXPECT_LE(0, fd) << strerror(errno);
407 ASSERT_EQ(bufsize, pwrite(fd, CONTENTS, bufsize, offset))
409 /* Deliberately leak fd. close(2) will be tested in release.cc */
414 const char FULLPATH[] = "mountpoint/some_file.txt";
415 const char RELPATH[] = "some_file.txt";
416 const char *CONTENTS = "abcdefgh";
419 ssize_t bufsize = strlen(CONTENTS);
421 expect_lookup(RELPATH, ino);
422 expect_open(ino, 0, 1);
423 expect_getattr(ino, 0);
424 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
426 fd = open(FULLPATH, O_WRONLY);
427 EXPECT_LE(0, fd) << strerror(errno);
429 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
430 /* Deliberately leak fd. close(2) will be tested in release.cc */
433 /* fuse(4) should not issue writes of greater size than the daemon requests */
434 TEST_F(Write, write_large)
436 const char FULLPATH[] = "mountpoint/some_file.txt";
437 const char RELPATH[] = "some_file.txt";
441 ssize_t halfbufsize, bufsize;
443 halfbufsize = m_mock->m_max_write;
444 bufsize = halfbufsize * 2;
445 contents = (int*)malloc(bufsize);
446 ASSERT_NE(NULL, contents);
447 for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
451 expect_lookup(RELPATH, ino);
452 expect_open(ino, 0, 1);
453 expect_getattr(ino, 0);
454 expect_write(ino, 0, halfbufsize, halfbufsize, 0, contents);
455 expect_write(ino, halfbufsize, halfbufsize, halfbufsize, 0,
456 &contents[halfbufsize / sizeof(int)]);
458 fd = open(FULLPATH, O_WRONLY);
459 EXPECT_LE(0, fd) << strerror(errno);
461 ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
462 /* Deliberately leak fd. close(2) will be tested in release.cc */
467 TEST_F(Write, write_nothing)
469 const char FULLPATH[] = "mountpoint/some_file.txt";
470 const char RELPATH[] = "some_file.txt";
471 const char *CONTENTS = "";
476 expect_lookup(RELPATH, ino);
477 expect_open(ino, 0, 1);
478 expect_getattr(ino, 0);
480 fd = open(FULLPATH, O_WRONLY);
481 EXPECT_LE(0, fd) << strerror(errno);
483 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
484 /* Deliberately leak fd. close(2) will be tested in release.cc */
487 /* In writeback mode, dirty data should be written on close */
488 TEST_F(WriteBack, close)
490 const char FULLPATH[] = "mountpoint/some_file.txt";
491 const char RELPATH[] = "some_file.txt";
492 const char *CONTENTS = "abcdefgh";
495 ssize_t bufsize = strlen(CONTENTS);
497 expect_lookup(RELPATH, ino);
498 expect_open(ino, 0, 1);
499 expect_getattr(ino, 0);
500 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
501 EXPECT_CALL(*m_mock, process(
502 ResultOf([=](auto in) {
503 return (in->header.opcode == FUSE_SETATTR);
506 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto out) {
507 SET_OUT_HEADER_LEN(out, attr);
508 out->body.attr.attr.ino = ino; // Must match nodeid
510 expect_release(ino, ReturnErrno(0));
512 fd = open(FULLPATH, O_RDWR);
513 ASSERT_LE(0, fd) << strerror(errno);
515 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
520 * Without direct_io, writes should be committed to cache
522 TEST_F(WriteBack, writeback)
524 const char FULLPATH[] = "mountpoint/some_file.txt";
525 const char RELPATH[] = "some_file.txt";
526 const char *CONTENTS = "abcdefgh";
529 ssize_t bufsize = strlen(CONTENTS);
530 char readbuf[bufsize];
532 expect_lookup(RELPATH, ino);
533 expect_open(ino, 0, 1);
534 expect_getattr(ino, 0);
535 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
537 fd = open(FULLPATH, O_RDWR);
538 EXPECT_LE(0, fd) << strerror(errno);
540 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
542 * A subsequent read should be serviced by cache, without querying the
545 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
546 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
547 /* Deliberately leak fd. close(2) will be tested in release.cc */
551 * With O_DIRECT, writes should be not committed to cache. Admittedly this is
552 * an odd test, because it would be unusual to use O_DIRECT for writes but not
555 TEST_F(WriteBack, o_direct)
557 const char FULLPATH[] = "mountpoint/some_file.txt";
558 const char RELPATH[] = "some_file.txt";
559 const char *CONTENTS = "abcdefgh";
562 ssize_t bufsize = strlen(CONTENTS);
563 char readbuf[bufsize];
565 expect_lookup(RELPATH, ino);
566 expect_open(ino, 0, 1);
567 expect_getattr(ino, 0);
568 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
569 expect_read(ino, 0, bufsize, bufsize, CONTENTS);
571 fd = open(FULLPATH, O_RDWR | O_DIRECT);
572 EXPECT_LE(0, fd) << strerror(errno);
574 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
575 /* A subsequent read must query the daemon because cache is empty */
576 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
577 ASSERT_EQ(0, fcntl(fd, F_SETFL, 0)) << strerror(errno);
578 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
579 /* Deliberately leak fd. close(2) will be tested in release.cc */
583 * Without direct_io, writes should be committed to cache
586 * Disabled because we don't yet implement write-through caching. No bugzilla
587 * entry, because that's a feature request, not a bug.
589 TEST_F(WriteThrough, DISABLED_writethrough)
591 const char FULLPATH[] = "mountpoint/some_file.txt";
592 const char RELPATH[] = "some_file.txt";
593 const char *CONTENTS = "abcdefgh";
596 ssize_t bufsize = strlen(CONTENTS);
597 char readbuf[bufsize];
599 expect_lookup(RELPATH, ino);
600 expect_open(ino, 0, 1);
601 expect_getattr(ino, 0);
602 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
604 fd = open(FULLPATH, O_RDWR);
605 EXPECT_LE(0, fd) << strerror(errno);
607 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
609 * A subsequent read should be serviced by cache, without querying the
612 ASSERT_EQ(bufsize, read(fd, readbuf, bufsize)) << strerror(errno);
613 /* Deliberately leak fd. close(2) will be tested in release.cc */
616 /* With writethrough caching, writes update the cached file size */
617 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
618 TEST_F(WriteThrough, DISABLED_update_file_size)
620 const char FULLPATH[] = "mountpoint/some_file.txt";
621 const char RELPATH[] = "some_file.txt";
622 const char *CONTENTS = "abcdefgh";
626 ssize_t bufsize = strlen(CONTENTS);
628 expect_lookup(RELPATH, ino);
629 expect_open(ino, 0, 1);
630 EXPECT_CALL(*m_mock, process(
631 ResultOf([=](auto in) {
632 return (in->header.opcode == FUSE_GETATTR &&
633 in->header.nodeid == ino);
637 .WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
638 SET_OUT_HEADER_LEN(out, attr);
639 out->body.attr.attr.ino = ino; // Must match nodeid
640 out->body.attr.attr.mode = S_IFREG | 0644;
641 out->body.attr.attr.size = 0;
642 out->body.attr.attr_valid = UINT64_MAX;
644 expect_write(ino, 0, bufsize, bufsize, 0, CONTENTS);
646 fd = open(FULLPATH, O_RDWR);
647 EXPECT_LE(0, fd) << strerror(errno);
649 ASSERT_EQ(bufsize, write(fd, CONTENTS, bufsize)) << strerror(errno);
650 /* Get cached attributes */
651 ASSERT_EQ(0, fstat(fd, &sb)) << strerror(errno);
652 ASSERT_EQ(bufsize, sb.st_size);
653 /* Deliberately leak fd. close(2) will be tested in release.cc */