xref: /freebsd/tests/sys/fs/fusefs/opendir.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
19821f1d3SAlan Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
39821f1d3SAlan Somers  *
49821f1d3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
59821f1d3SAlan Somers  *
69821f1d3SAlan Somers  * This software was developed by BFF Storage Systems, LLC under sponsorship
79821f1d3SAlan Somers  * from the FreeBSD Foundation.
89821f1d3SAlan Somers  *
99821f1d3SAlan Somers  * Redistribution and use in source and binary forms, with or without
109821f1d3SAlan Somers  * modification, are permitted provided that the following conditions
119821f1d3SAlan Somers  * are met:
129821f1d3SAlan Somers  * 1. Redistributions of source code must retain the above copyright
139821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer.
149821f1d3SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
159821f1d3SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
169821f1d3SAlan Somers  *    documentation and/or other materials provided with the distribution.
179821f1d3SAlan Somers  *
189821f1d3SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199821f1d3SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209821f1d3SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219821f1d3SAlan Somers  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
229821f1d3SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239821f1d3SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249821f1d3SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259821f1d3SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269821f1d3SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279821f1d3SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289821f1d3SAlan Somers  * SUCH DAMAGE.
299821f1d3SAlan Somers  */
309821f1d3SAlan Somers 
319821f1d3SAlan Somers extern "C" {
329821f1d3SAlan Somers #include <dirent.h>
3327537990SAlan Somers 
349821f1d3SAlan Somers #include <fcntl.h>
3527537990SAlan Somers #include <semaphore.h>
369821f1d3SAlan Somers }
379821f1d3SAlan Somers 
389821f1d3SAlan Somers #include "mockfs.hh"
399821f1d3SAlan Somers #include "utils.hh"
409821f1d3SAlan Somers 
419821f1d3SAlan Somers using namespace testing;
429821f1d3SAlan Somers 
439821f1d3SAlan Somers class Opendir: public FuseTest {
449821f1d3SAlan Somers public:
expect_lookup(const char * relpath,uint64_t ino)459821f1d3SAlan Somers void expect_lookup(const char *relpath, uint64_t ino)
469821f1d3SAlan Somers {
479821f1d3SAlan Somers 	FuseTest::expect_lookup(relpath, ino, S_IFDIR | 0755, 0, 1);
489821f1d3SAlan Somers }
49363a7416SAlan Somers 
expect_opendir(uint64_t ino,uint32_t flags,ProcessMockerT r)50363a7416SAlan Somers void expect_opendir(uint64_t ino, uint32_t flags, ProcessMockerT r)
51363a7416SAlan Somers {
52363a7416SAlan Somers 	/* opendir(3) calls fstatfs */
53363a7416SAlan Somers 	EXPECT_CALL(*m_mock, process(
54363a7416SAlan Somers 		ResultOf([](auto in) {
5529edc611SAlan Somers 			return (in.header.opcode == FUSE_STATFS);
56363a7416SAlan Somers 		}, Eq(true)),
57363a7416SAlan Somers 		_)
5829edc611SAlan Somers 	).WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
59363a7416SAlan Somers 		SET_OUT_HEADER_LEN(out, statfs);
60363a7416SAlan Somers 	})));
61363a7416SAlan Somers 
62363a7416SAlan Somers 	EXPECT_CALL(*m_mock, process(
63363a7416SAlan Somers 		ResultOf([=](auto in) {
6429edc611SAlan Somers 			return (in.header.opcode == FUSE_OPENDIR &&
6529edc611SAlan Somers 				in.header.nodeid == ino &&
6629edc611SAlan Somers 				in.body.opendir.flags == flags);
67363a7416SAlan Somers 		}, Eq(true)),
68363a7416SAlan Somers 		_)
69363a7416SAlan Somers 	).WillOnce(Invoke(r));
70363a7416SAlan Somers }
71363a7416SAlan Somers 
729821f1d3SAlan Somers };
739821f1d3SAlan Somers 
747124d2bcSAlan Somers class OpendirNoOpendirSupport: public Opendir {
SetUp()757124d2bcSAlan Somers 	virtual void SetUp() {
767124d2bcSAlan Somers 		m_init_flags = FUSE_NO_OPENDIR_SUPPORT;
777124d2bcSAlan Somers 		FuseTest::SetUp();
787124d2bcSAlan Somers 	}
797124d2bcSAlan Somers };
807124d2bcSAlan Somers 
819821f1d3SAlan Somers 
829821f1d3SAlan Somers /*
839821f1d3SAlan Somers  * The fuse daemon fails the request with enoent.  This usually indicates a
849821f1d3SAlan Somers  * race condition: some other FUSE client removed the file in between when the
859821f1d3SAlan Somers  * kernel checked for it with lookup and tried to open it
869821f1d3SAlan Somers  */
TEST_F(Opendir,enoent)879821f1d3SAlan Somers TEST_F(Opendir, enoent)
889821f1d3SAlan Somers {
899821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
909821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
919821f1d3SAlan Somers 	uint64_t ino = 42;
9227537990SAlan Somers 	sem_t sem;
9327537990SAlan Somers 
9427537990SAlan Somers 	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);
959821f1d3SAlan Somers 
969821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
97363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOENT));
9827537990SAlan Somers 	// Since FUSE_OPENDIR returns ENOENT, the kernel will reclaim the vnode
9927537990SAlan Somers 	// and send a FUSE_FORGET
10027537990SAlan Somers 	expect_forget(ino, 1, &sem);
1019821f1d3SAlan Somers 
1028e765737SAlan Somers 	ASSERT_EQ(-1, open(FULLPATH, O_DIRECTORY));
1039821f1d3SAlan Somers 	EXPECT_EQ(ENOENT, errno);
10427537990SAlan Somers 
10527537990SAlan Somers 	sem_wait(&sem);
10627537990SAlan Somers 	sem_destroy(&sem);
1079821f1d3SAlan Somers }
1089821f1d3SAlan Somers 
1099821f1d3SAlan Somers /*
1109821f1d3SAlan Somers  * The daemon is responsible for checking file permissions (unless the
1119821f1d3SAlan Somers  * default_permissions mount option was used)
1129821f1d3SAlan Somers  */
TEST_F(Opendir,eperm)1139821f1d3SAlan Somers TEST_F(Opendir, eperm)
1149821f1d3SAlan Somers {
1159821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1169821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1179821f1d3SAlan Somers 	uint64_t ino = 42;
1189821f1d3SAlan Somers 
1199821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
120363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(EPERM));
1219821f1d3SAlan Somers 
1224ca1c0b7SAlan Somers 	EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
1239821f1d3SAlan Somers 	EXPECT_EQ(EPERM, errno);
1249821f1d3SAlan Somers }
1259821f1d3SAlan Somers 
TEST_F(Opendir,open)1269821f1d3SAlan Somers TEST_F(Opendir, open)
1279821f1d3SAlan Somers {
1289821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1299821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1309821f1d3SAlan Somers 	uint64_t ino = 42;
1318e765737SAlan Somers 	int fd;
1329821f1d3SAlan Somers 
1339821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
134363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY,
13529edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
1369821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
137363a7416SAlan Somers 	}));
1389821f1d3SAlan Somers 
1398e765737SAlan Somers 	fd = open(FULLPATH, O_DIRECTORY);
140d2621689SAlan Somers 	ASSERT_LE(0, fd) << strerror(errno);
1418e765737SAlan Somers 
1428e765737SAlan Somers 	leak(fd);
1439821f1d3SAlan Somers }
1449821f1d3SAlan Somers 
145363a7416SAlan Somers /* Directories can be opened O_EXEC for stuff like fchdir(2) */
TEST_F(Opendir,open_exec)146363a7416SAlan Somers TEST_F(Opendir, open_exec)
147363a7416SAlan Somers {
148363a7416SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
149363a7416SAlan Somers 	const char RELPATH[] = "some_dir";
150363a7416SAlan Somers 	uint64_t ino = 42;
151363a7416SAlan Somers 	int fd;
152363a7416SAlan Somers 
153363a7416SAlan Somers 	expect_lookup(RELPATH, ino);
154363a7416SAlan Somers 	expect_opendir(ino, O_EXEC,
15529edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
156363a7416SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
157363a7416SAlan Somers 	}));
158363a7416SAlan Somers 
159363a7416SAlan Somers 	fd = open(FULLPATH, O_EXEC | O_DIRECTORY);
160363a7416SAlan Somers 	ASSERT_LE(0, fd) << strerror(errno);
1618e765737SAlan Somers 
1628e765737SAlan Somers 	leak(fd);
163363a7416SAlan Somers }
164363a7416SAlan Somers 
TEST_F(Opendir,opendir)1659821f1d3SAlan Somers TEST_F(Opendir, opendir)
1669821f1d3SAlan Somers {
1679821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1689821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1699821f1d3SAlan Somers 	uint64_t ino = 42;
1709821f1d3SAlan Somers 
1719821f1d3SAlan Somers 	expect_lookup(RELPATH, ino);
172363a7416SAlan Somers 	expect_opendir(ino, O_RDONLY,
17329edc611SAlan Somers 	ReturnImmediate([=](auto in __unused, auto& out) {
1749821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, open);
175363a7416SAlan Somers 	}));
1769821f1d3SAlan Somers 
1779821f1d3SAlan Somers 	errno = 0;
1785a0b9a27SAlan Somers 	EXPECT_NE(nullptr, opendir(FULLPATH)) << strerror(errno);
1799821f1d3SAlan Somers }
1807124d2bcSAlan Somers 
1817124d2bcSAlan Somers /*
1827124d2bcSAlan Somers  * Without FUSE_NO_OPENDIR_SUPPORT, returning ENOSYS is an error
1837124d2bcSAlan Somers  */
TEST_F(Opendir,enosys)1847124d2bcSAlan Somers TEST_F(Opendir, enosys)
1857124d2bcSAlan Somers {
1867124d2bcSAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
1877124d2bcSAlan Somers 	const char RELPATH[] = "some_file.txt";
1887124d2bcSAlan Somers 	uint64_t ino = 42;
1897124d2bcSAlan Somers 
1907124d2bcSAlan Somers 	expect_lookup(RELPATH, ino);
1917124d2bcSAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
1927124d2bcSAlan Somers 
1937124d2bcSAlan Somers 	EXPECT_EQ(-1, open(FULLPATH, O_DIRECTORY));
1947124d2bcSAlan Somers 	EXPECT_EQ(ENOSYS, errno);
1957124d2bcSAlan Somers }
1967124d2bcSAlan Somers 
1977124d2bcSAlan Somers /*
1987124d2bcSAlan Somers  * If a fuse server sets FUSE_NO_OPENDIR_SUPPORT and returns ENOSYS to a
1997124d2bcSAlan Somers  * FUSE_OPENDIR, then it and subsequent FUSE_OPENDIR and FUSE_RELEASEDIR
2007124d2bcSAlan Somers  * operations will also succeed automatically without being sent to the server.
2017124d2bcSAlan Somers  */
TEST_F(OpendirNoOpendirSupport,enosys)2027124d2bcSAlan Somers TEST_F(OpendirNoOpendirSupport, enosys)
2037124d2bcSAlan Somers {
2047124d2bcSAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
2057124d2bcSAlan Somers 	const char RELPATH[] = "some_file.txt";
2067124d2bcSAlan Somers 	uint64_t ino = 42;
2077124d2bcSAlan Somers 	int fd;
2087124d2bcSAlan Somers 
2097124d2bcSAlan Somers 	FuseTest::expect_lookup(RELPATH, ino, S_IFDIR | 0755, 0, 2);
2107124d2bcSAlan Somers 	expect_opendir(ino, O_RDONLY, ReturnErrno(ENOSYS));
2117124d2bcSAlan Somers 
2127124d2bcSAlan Somers 	fd = open(FULLPATH, O_DIRECTORY);
2137124d2bcSAlan Somers 	ASSERT_LE(0, fd) << strerror(errno);
2147124d2bcSAlan Somers 	close(fd);
2157124d2bcSAlan Somers 
2167124d2bcSAlan Somers 	fd = open(FULLPATH, O_DIRECTORY);
2177124d2bcSAlan Somers 	ASSERT_LE(0, fd) << strerror(errno);
2187124d2bcSAlan Somers 
2197124d2bcSAlan Somers 	leak(fd);
2207124d2bcSAlan Somers }
221