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
31 /* This file tests functionality needed by NFS servers */
33 #include <sys/param.h>
34 #include <sys/mount.h>
45 using namespace testing;
48 class Nfs: public FuseTest {
50 virtual void SetUp() {
52 GTEST_SKIP() << "This test requires a privileged user";
57 class Exportable: public Nfs {
59 virtual void SetUp() {
60 m_init_flags = FUSE_EXPORT_SUPPORT;
65 class Fhstat: public Exportable {};
66 class FhstatNotExportable: public Nfs {};
67 class Getfh: public Exportable {};
68 class Readdir: public Exportable {};
70 /* If the server returns a different generation number, then file is stale */
71 TEST_F(Fhstat, estale)
73 const char FULLPATH[] = "mountpoint/some_dir/.";
74 const char RELDIRPATH[] = "some_dir";
77 const uint64_t ino = 42;
78 const mode_t mode = S_IFDIR | 0755;
81 EXPECT_LOOKUP(1, RELDIRPATH)
83 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
84 SET_OUT_HEADER_LEN(out, entry);
85 out.body.entry.attr.mode = mode;
86 out.body.entry.nodeid = ino;
87 out.body.entry.generation = 1;
88 out.body.entry.attr_valid = UINT64_MAX;
89 out.body.entry.entry_valid = 0;
92 EXPECT_LOOKUP(ino, ".")
94 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
95 SET_OUT_HEADER_LEN(out, entry);
96 out.body.entry.attr.mode = mode;
97 out.body.entry.nodeid = ino;
98 out.body.entry.generation = 2;
99 out.body.entry.attr_valid = UINT64_MAX;
100 out.body.entry.entry_valid = 0;
103 ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
104 ASSERT_EQ(-1, fhstat(&fhp, &sb));
105 EXPECT_EQ(ESTALE, errno);
108 /* If we must lookup an entry from the server, send a LOOKUP request for "." */
109 TEST_F(Fhstat, lookup_dot)
111 const char FULLPATH[] = "mountpoint/some_dir/.";
112 const char RELDIRPATH[] = "some_dir";
115 const uint64_t ino = 42;
116 const mode_t mode = S_IFDIR | 0755;
117 const uid_t uid = 12345;
119 EXPECT_LOOKUP(1, RELDIRPATH)
120 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
121 SET_OUT_HEADER_LEN(out, entry);
122 out.body.entry.attr.mode = mode;
123 out.body.entry.nodeid = ino;
124 out.body.entry.generation = 1;
125 out.body.entry.attr.uid = uid;
126 out.body.entry.attr_valid = UINT64_MAX;
127 out.body.entry.entry_valid = 0;
130 EXPECT_LOOKUP(ino, ".")
131 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
132 SET_OUT_HEADER_LEN(out, entry);
133 out.body.entry.attr.mode = mode;
134 out.body.entry.nodeid = ino;
135 out.body.entry.generation = 1;
136 out.body.entry.attr.uid = uid;
137 out.body.entry.attr_valid = UINT64_MAX;
138 out.body.entry.entry_valid = 0;
141 ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
142 ASSERT_EQ(0, fhstat(&fhp, &sb)) << strerror(errno);
143 EXPECT_EQ(uid, sb.st_uid);
144 EXPECT_EQ(mode, sb.st_mode);
147 /* Use a file handle whose entry is still cached */
149 * Disabled because fuse_vfsop_vget doesn't yet check the entry cache. No PR
150 * because that's a feature request, not a bug
152 TEST_F(Fhstat, DISABLED_cached)
154 const char FULLPATH[] = "mountpoint/some_dir/.";
155 const char RELDIRPATH[] = "some_dir";
158 const uint64_t ino = 42;
159 const mode_t mode = S_IFDIR | 0755;
160 const uid_t uid = 12345;
162 EXPECT_LOOKUP(1, RELDIRPATH)
163 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
164 SET_OUT_HEADER_LEN(out, entry);
165 out.body.entry.attr.mode = mode;
166 out.body.entry.nodeid = ino;
167 out.body.entry.generation = 1;
168 out.body.entry.attr.uid = uid;
169 out.body.entry.attr_valid = UINT64_MAX;
170 out.body.entry.entry_valid = UINT64_MAX;
173 ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
174 ASSERT_EQ(0, fhstat(&fhp, &sb)) << strerror(errno);
175 EXPECT_EQ(uid, sb.st_uid);
176 EXPECT_EQ(mode, sb.st_mode);
180 * If the server doesn't set FUSE_EXPORT_SUPPORT, then we can't do NFS-style
183 TEST_F(FhstatNotExportable, lookup_dot)
185 const char FULLPATH[] = "mountpoint/some_dir/.";
186 const char RELDIRPATH[] = "some_dir";
188 const uint64_t ino = 42;
189 const mode_t mode = S_IFDIR | 0755;
191 EXPECT_LOOKUP(1, RELDIRPATH)
192 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
193 SET_OUT_HEADER_LEN(out, entry);
194 out.body.entry.attr.mode = mode;
195 out.body.entry.nodeid = ino;
196 out.body.entry.generation = 1;
197 out.body.entry.attr_valid = UINT64_MAX;
198 out.body.entry.entry_valid = 0;
201 ASSERT_EQ(-1, getfh(FULLPATH, &fhp));
202 ASSERT_EQ(EOPNOTSUPP, errno);
205 /* FreeBSD's fid struct doesn't have enough space for 64-bit generations */
206 TEST_F(Getfh, eoverflow)
208 const char FULLPATH[] = "mountpoint/some_dir/.";
209 const char RELDIRPATH[] = "some_dir";
213 EXPECT_LOOKUP(1, RELDIRPATH)
214 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
215 SET_OUT_HEADER_LEN(out, entry);
216 out.body.entry.attr.mode = S_IFDIR | 0755;
217 out.body.entry.nodeid = ino;
218 out.body.entry.generation = (uint64_t)UINT32_MAX + 1;
219 out.body.entry.attr_valid = UINT64_MAX;
220 out.body.entry.entry_valid = UINT64_MAX;
223 ASSERT_NE(0, getfh(FULLPATH, &fhp));
224 EXPECT_EQ(EOVERFLOW, errno);
227 /* Get an NFS file handle */
230 const char FULLPATH[] = "mountpoint/some_dir/.";
231 const char RELDIRPATH[] = "some_dir";
235 EXPECT_LOOKUP(1, RELDIRPATH)
236 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
237 SET_OUT_HEADER_LEN(out, entry);
238 out.body.entry.attr.mode = S_IFDIR | 0755;
239 out.body.entry.nodeid = ino;
240 out.body.entry.attr_valid = UINT64_MAX;
241 out.body.entry.entry_valid = UINT64_MAX;
244 ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
248 * Call readdir via a file handle.
250 * This is how a userspace nfs server like nfs-ganesha or unfs3 would call
251 * readdir. The in-kernel NFS server never does any equivalent of open. I
252 * haven't discovered a way to mimic nfsd's behavior short of actually running
255 TEST_F(Readdir, getdirentries)
257 const char FULLPATH[] = "mountpoint/some_dir";
258 const char RELPATH[] = "some_dir";
260 mode_t mode = S_IFDIR | 0755;
266 EXPECT_LOOKUP(1, RELPATH)
267 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
268 SET_OUT_HEADER_LEN(out, entry);
269 out.body.entry.attr.mode = mode;
270 out.body.entry.nodeid = ino;
271 out.body.entry.generation = 1;
272 out.body.entry.attr_valid = UINT64_MAX;
273 out.body.entry.entry_valid = 0;
276 EXPECT_LOOKUP(ino, ".")
277 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
278 SET_OUT_HEADER_LEN(out, entry);
279 out.body.entry.attr.mode = mode;
280 out.body.entry.nodeid = ino;
281 out.body.entry.generation = 1;
282 out.body.entry.attr_valid = UINT64_MAX;
283 out.body.entry.entry_valid = 0;
288 EXPECT_CALL(*m_mock, process(
289 ResultOf([=](auto in) {
290 return (in.header.opcode == FUSE_READDIR &&
291 in.header.nodeid == ino &&
292 in.body.readdir.size == sizeof(buf));
295 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
296 out.header.error = 0;
297 out.header.len = sizeof(out.header);
300 ASSERT_EQ(0, getfh(FULLPATH, &fhp)) << strerror(errno);
301 fd = fhopen(&fhp, O_DIRECTORY);
302 ASSERT_LE(0, fd) << strerror(errno);
303 r = getdirentries(fd, buf, sizeof(buf), 0);
304 ASSERT_EQ(0, r) << strerror(errno);
306 /* Deliberately leak fd. RELEASEDIR will be tested separately */