]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/fsyncdir.cc
Fix the branch build
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / fsyncdir.cc
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 extern "C" {
32 #include <aio.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 }
36
37 #include "mockfs.hh"
38 #include "utils.hh"
39
40 using namespace testing;
41
42 /*
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
46  */
47 #ifndef FUSE_FSYNC_FDATASYNC
48 #define FUSE_FSYNC_FDATASYNC 1
49 #endif
50
51 class FsyncDir: public FuseTest {
52 public:
53 void expect_fsyncdir(uint64_t ino, uint32_t flags, int error)
54 {
55         EXPECT_CALL(*m_mock, process(
56                 ResultOf([=](auto in) {
57                         return (in->header.opcode == FUSE_FSYNCDIR &&
58                                 in->header.nodeid == ino &&
59                                 //(pid_t)in->header.pid == getpid() &&
60                                 in->body.fsyncdir.fh == FH &&
61                                 in->body.fsyncdir.fsync_flags == flags);
62                 }, Eq(true)),
63                 _)
64         ).WillOnce(Invoke(ReturnErrno(error)));
65 }
66
67 void expect_lookup(const char *relpath, uint64_t ino)
68 {
69         FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
70 }
71
72 };
73
74 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236379 */
75 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236473 */
76 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236474 */
77 TEST_F(FsyncDir, DISABLED_aio_fsync)
78 {
79         const char FULLPATH[] = "mountpoint/some_file.txt";
80         const char RELPATH[] = "some_file.txt";
81         uint64_t ino = 42;
82         struct aiocb iocb, *piocb;
83         int fd;
84
85         expect_lookup(RELPATH, ino);
86         expect_opendir(ino);
87         expect_fsyncdir(ino, 0, 0);
88
89         fd = open(FULLPATH, O_DIRECTORY);
90         ASSERT_LE(0, fd) << strerror(errno);
91
92         bzero(&iocb, sizeof(iocb));
93         iocb.aio_fildes = fd;
94
95         ASSERT_EQ(0, aio_fsync(O_SYNC, &iocb)) << strerror(errno);
96         ASSERT_EQ(0, aio_waitcomplete(&piocb, NULL)) << strerror(errno);
97
98         /* Deliberately leak fd.  close(2) will be tested in release.cc */
99 }
100
101 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236473 */
102 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236474 */
103 TEST_F(FsyncDir, DISABLED_eio)
104 {
105         const char FULLPATH[] = "mountpoint/some_dir";
106         const char RELPATH[] = "some_dir";
107         uint64_t ino = 42;
108         int fd;
109
110         expect_lookup(RELPATH, ino);
111         expect_opendir(ino);
112         expect_fsyncdir(ino, 0, EIO);
113
114         fd = open(FULLPATH, O_DIRECTORY);
115         ASSERT_LE(0, fd) << strerror(errno);
116         ASSERT_NE(0, fsync(fd));
117         ASSERT_EQ(EIO, errno);
118
119         /* Deliberately leak fd.  close(2) will be tested in release.cc */
120 }
121
122 /*
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
126  */
127 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236474 */
128 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236557 */
129 TEST_F(FsyncDir, DISABLED_enosys)
130 {
131         const char FULLPATH[] = "mountpoint/some_dir";
132         const char RELPATH[] = "some_dir";
133         uint64_t ino = 42;
134         int fd;
135
136         expect_lookup(RELPATH, ino);
137         expect_opendir(ino);
138         expect_fsyncdir(ino, FUSE_FSYNC_FDATASYNC, ENOSYS);
139
140         fd = open(FULLPATH, O_DIRECTORY);
141         ASSERT_LE(0, fd) << strerror(errno);
142         EXPECT_EQ(0, fsync(fd)) << strerror(errno);
143
144         /* Subsequent calls shouldn't query the daemon*/
145         EXPECT_EQ(0, fsync(fd)) << strerror(errno);
146
147         /* Deliberately leak fd.  close(2) will be tested in release.cc */
148 }
149
150 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236474 */
151 TEST_F(FsyncDir, DISABLED_fsyncdata)
152 {
153         const char FULLPATH[] = "mountpoint/some_dir";
154         const char RELPATH[] = "some_dir";
155         uint64_t ino = 42;
156         int fd;
157
158         expect_lookup(RELPATH, ino);
159         expect_opendir(ino);
160         expect_fsyncdir(ino, FUSE_FSYNC_FDATASYNC, 0);
161
162         fd = open(FULLPATH, O_DIRECTORY);
163         ASSERT_LE(0, fd) << strerror(errno);
164         ASSERT_EQ(0, fdatasync(fd)) << strerror(errno);
165
166         /* Deliberately leak fd.  close(2) will be tested in release.cc */
167 }
168
169 /* 
170  * Unlike regular files, the kernel doesn't know whether a directory is or
171  * isn't dirty, so fuse(4) should always send FUSE_FSYNCDIR on fsync(2)
172  */
173 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236473 */
174 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236474 */
175 TEST_F(FsyncDir, DISABLED_fsync)
176 {
177         const char FULLPATH[] = "mountpoint/some_dir";
178         const char RELPATH[] = "some_dir";
179         uint64_t ino = 42;
180         int fd;
181
182         expect_lookup(RELPATH, ino);
183         expect_opendir(ino);
184         expect_fsyncdir(ino, 0, 0);
185
186         fd = open(FULLPATH, O_DIRECTORY);
187         ASSERT_LE(0, fd) << strerror(errno);
188         ASSERT_EQ(0, fsync(fd)) << strerror(errno);
189
190         /* Deliberately leak fd.  close(2) will be tested in release.cc */
191 }