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