xref: /freebsd/tests/sys/fs/fusefs/opendir.cc (revision 1323ec57)
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  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <dirent.h>
35 
36 #include <fcntl.h>
37 #include <semaphore.h>
38 }
39 
40 #include "mockfs.hh"
41 #include "utils.hh"
42 
43 using namespace testing;
44 
45 class Opendir: public FuseTest {
46 public:
47 void expect_lookup(const char *relpath, uint64_t ino)
48 {
49 	FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
50 }
51 
52 void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r)
53 {
54 	/* opendir(3) calls fstatfs */
55 	EXPECT_CALL(*m_mock, process(
56 		ResultOf([](auto in) {
57 			return (in.header.opcode == FUSE_STATFS);
58 		}, Eq(true)),
59 		_)
60 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
61 		SET_OUT_HEADER_LEN(out, statfs);
62 	})));
63 
64 	EXPECT_CALL(*m_mock, process(
65 		ResultOf([=](auto in) {
66 			return (in.header.opcode == FUSE_OPENDIR &&
67 				in.header.nodeid == ino &&
68 				in.body.opendir.flags == flags);
69 		}, Eq(true)),
70 		_)
71 	).WillOnce(Invoke(r));
72 }
73 
74 };
75 
76 class OpendirNoOpendirSupport: public Opendir {
77 	virtual void SetUp() {
78 		m_init_flags = FUSE_NO_OPENDIR_SUPPORT;
79 		FuseTest::SetUp();
80 	}
81 };
82 
83 
84 /*
85  * The fuse daemon fails the request with enoent.  This usually indicates a
86  * race condition: some other FUSE client removed the file in between when the
87  * kernel checked for it with lookup and tried to open it
88  */
89 TEST_F(Opendir, enoent)
90 {
91 	const char FULLPATH[] = "mountpoint/some_dir";
92 	const char RELPATH[] = "some_dir";
93 	uint64_t ino = 42;
94 	sem_t sem;
95 
96 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
97 
98 	expect_lookup(RELPATH, ino);
99 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
100 	// Since FUSE_OPENDIR returns ENOENT, the kernel will reclaim the vnode
101 	// and send a FUSE_FORGET
102 	expect_forget(ino, 1, &sem);
103 
104 	ASSERT_EQ(-1, open(FULLPATH, O_DIRECTORY));
105 	EXPECT_EQ(ENOENT, errno);
106 
107 	sem_wait(&sem);
108 	sem_destroy(&sem);
109 }
110 
111 /*
112  * The daemon is responsible for checking file permissions (unless the
113  * default_permissions mount option was used)
114  */
115 TEST_F(Opendir, eperm)
116 {
117 	const char FULLPATH[] = "mountpoint/some_dir";
118 	const char RELPATH[] = "some_dir";
119 	uint64_t ino = 42;
120 
121 	expect_lookup(RELPATH, ino);
122 	expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
123 
124 	EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
125 	EXPECT_EQ(EPERM, errno);
126 }
127 
128 TEST_F(Opendir, open)
129 {
130 	const char FULLPATH[] = "mountpoint/some_dir";
131 	const char RELPATH[] = "some_dir";
132 	uint64_t ino = 42;
133 	int fd;
134 
135 	expect_lookup(RELPATH, ino);
136 	expect_opendir(ino, O_RDONLY,
137 	ReturnImmediate([=](auto in __unused, auto& out) {
138 		SET_OUT_HEADER_LEN(out, open);
139 	}));
140 
141 	fd = open(FULLPATH, O_DIRECTORY);
142 	ASSERT_LE(0, fd) << strerror(errno);
143 
144 	leak(fd);
145 }
146 
147 /* Directories can be opened O_EXEC for stuff like fchdir(2) */
148 TEST_F(Opendir, open_exec)
149 {
150 	const char FULLPATH[] = "mountpoint/some_dir";
151 	const char RELPATH[] = "some_dir";
152 	uint64_t ino = 42;
153 	int fd;
154 
155 	expect_lookup(RELPATH, ino);
156 	expect_opendir(ino, O_EXEC,
157 	ReturnImmediate([=](auto in __unused, auto& out) {
158 		SET_OUT_HEADER_LEN(out, open);
159 	}));
160 
161 	fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
162 	ASSERT_LE(0, fd) << strerror(errno);
163 
164 	leak(fd);
165 }
166 
167 TEST_F(Opendir, opendir)
168 {
169 	const char FULLPATH[] = "mountpoint/some_dir";
170 	const char RELPATH[] = "some_dir";
171 	uint64_t ino = 42;
172 
173 	expect_lookup(RELPATH, ino);
174 	expect_opendir(ino, O_RDONLY,
175 	ReturnImmediate([=](auto in __unused, auto& out) {
176 		SET_OUT_HEADER_LEN(out, open);
177 	}));
178 
179 	errno = 0;
180 	EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);
181 }
182 
183 /*
184  * Without FUSE_NO_OPENDIR_SUPPORT, returning ENOSYS is an error
185  */
186 TEST_F(Opendir, enosys)
187 {
188 	const char FULLPATH[] = "mountpoint/some_file.txt";
189 	const char RELPATH[] = "some_file.txt";
190 	uint64_t ino = 42;
191 
192 	expect_lookup(RELPATH, ino);
193 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
194 
195 	EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
196 	EXPECT_EQ(ENOSYS, errno);
197 }
198 
199 /*
200  * If a fuse server sets FUSE_NO_OPENDIR_SUPPORT and returns ENOSYS to a
201  * FUSE_OPENDIR, then it and subsequent FUSE_OPENDIR and FUSE_RELEASEDIR
202  * operations will also succeed automatically without being sent to the server.
203  */
204 TEST_F(OpendirNoOpendirSupport, enosys)
205 {
206 	const char FULLPATH[] = "mountpoint/some_file.txt";
207 	const char RELPATH[] = "some_file.txt";
208 	uint64_t ino = 42;
209 	int fd;
210 
211 	FuseTest::expect_lookup(RELPATH, ino, S_IFDIR | 0755, 0, 2);
212 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
213 
214 	fd = open(FULLPATH, O_DIRECTORY);
215 	ASSERT_LE(0, fd) << strerror(errno);
216 	close(fd);
217 
218 	fd = open(FULLPATH, O_DIRECTORY);
219 	ASSERT_LE(0, fd) << strerror(errno);
220 
221 	leak(fd);
222 }
223