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
40 using namespace testing;
43 * TODO: remove FUSE_FSYNC_FDATASYNC definition when upgrading to protocol 7.28.
44 * This bit was actually part of kernel protocol version 5.2, but never
45 * documented until after 7.28
47 #ifndef FUSE_FSYNC_FDATASYNC
48 #define FUSE_FSYNC_FDATASYNC 1
51 class FsyncDir: public FuseTest {
53 void expect_fsyncdir(uint64_t ino, uint32_t flags, int error)
55 EXPECT_CALL(*m_mock, process(
56 ResultOf([=](auto in) {
57 return (in.header.opcode == FUSE_FSYNCDIR &&
58 in.header.nodeid == ino &&
60 * TODO: reenable pid check after fixing
63 //(pid_t)in.header.pid == getpid() &&
64 in.body.fsyncdir.fh == FH &&
65 in.body.fsyncdir.fsync_flags == flags);
68 ).WillOnce(Invoke(ReturnErrno(error)));
71 void expect_lookup(const char *relpath, uint64_t ino)
73 FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
78 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
79 TEST_F(FsyncDir, aio_fsync)
81 const char FULLPATH[] = "mountpoint/some_file.txt";
82 const char RELPATH[] = "some_file.txt";
84 struct aiocb iocb, *piocb;
87 expect_lookup(RELPATH, ino);
89 expect_fsyncdir(ino, 0, 0);
91 fd = open(FULLPATH, O_DIRECTORY);
92 ASSERT_LE(0, fd) << strerror(errno);
94 bzero(&iocb, sizeof(iocb));
97 ASSERT_EQ(0, aio_fsync(O_SYNC, &iocb)) << strerror(errno);
98 ASSERT_EQ(0, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
103 TEST_F(FsyncDir, eio)
105 const char FULLPATH[] = "mountpoint/some_dir";
106 const char RELPATH[] = "some_dir";
110 expect_lookup(RELPATH, ino);
112 expect_fsyncdir(ino, 0, EIO);
114 fd = open(FULLPATH, O_DIRECTORY);
115 ASSERT_LE(0, fd) << strerror(errno);
116 ASSERT_NE(0, fsync(fd));
117 ASSERT_EQ(EIO, errno);
123 * If the filesystem returns ENOSYS, it will be treated as success and
124 * subsequent calls to VOP_FSYNC will succeed automatically without being sent
125 * to the filesystem daemon
127 TEST_F(FsyncDir, enosys)
129 const char FULLPATH[] = "mountpoint/some_dir";
130 const char RELPATH[] = "some_dir";
134 expect_lookup(RELPATH, ino);
136 expect_fsyncdir(ino, 0, ENOSYS);
138 fd = open(FULLPATH, O_DIRECTORY);
139 ASSERT_LE(0, fd) << strerror(errno);
140 EXPECT_EQ(0, fsync(fd)) << strerror(errno);
142 /* Subsequent calls shouldn't query the daemon*/
143 EXPECT_EQ(0, fsync(fd)) << strerror(errno);
148 TEST_F(FsyncDir, fsyncdata)
150 const char FULLPATH[] = "mountpoint/some_dir";
151 const char RELPATH[] = "some_dir";
155 expect_lookup(RELPATH, ino);
157 expect_fsyncdir(ino, FUSE_FSYNC_FDATASYNC, 0);
159 fd = open(FULLPATH, O_DIRECTORY);
160 ASSERT_LE(0, fd) << strerror(errno);
161 ASSERT_EQ(0, fdatasync(fd)) << strerror(errno);
167 * Unlike regular files, the kernel doesn't know whether a directory is or
168 * isn't dirty, so fuse(4) should always send FUSE_FSYNCDIR on fsync(2)
170 TEST_F(FsyncDir, fsync)
172 const char FULLPATH[] = "mountpoint/some_dir";
173 const char RELPATH[] = "some_dir";
177 expect_lookup(RELPATH, ino);
179 expect_fsyncdir(ino, 0, 0);
181 fd = open(FULLPATH, O_DIRECTORY);
182 ASSERT_LE(0, fd) << strerror(errno);
183 ASSERT_EQ(0, fsync(fd)) << strerror(errno);