xref: /freebsd/tests/sys/fs/fusefs/mkdir.cc (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
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.
291fa8ebfbSAlan Somers  *
301fa8ebfbSAlan Somers  * $FreeBSD$
319821f1d3SAlan Somers  */
329821f1d3SAlan Somers 
339821f1d3SAlan Somers extern "C" {
349821f1d3SAlan Somers #include <fcntl.h>
359821f1d3SAlan Somers }
369821f1d3SAlan Somers 
379821f1d3SAlan Somers #include "mockfs.hh"
389821f1d3SAlan Somers #include "utils.hh"
399821f1d3SAlan Somers 
409821f1d3SAlan Somers using namespace testing;
419821f1d3SAlan Somers 
429821f1d3SAlan Somers class Mkdir: public FuseTest {};
4316bd2d47SAlan Somers class Mkdir_7_8: public FuseTest {
4416bd2d47SAlan Somers public:
4516bd2d47SAlan Somers virtual void SetUp() {
4616bd2d47SAlan Somers 	m_kernel_minor_version = 8;
4716bd2d47SAlan Somers 	FuseTest::SetUp();
4816bd2d47SAlan Somers }
4916bd2d47SAlan Somers };
509821f1d3SAlan Somers 
519821f1d3SAlan Somers /*
529821f1d3SAlan Somers  * EMLINK is possible on filesystems that limit the number of hard links to a
539821f1d3SAlan Somers  * single file, like early versions of BtrFS
549821f1d3SAlan Somers  */
559821f1d3SAlan Somers TEST_F(Mkdir, emlink)
569821f1d3SAlan Somers {
579821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
589821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
599821f1d3SAlan Somers 	mode_t mode = 0755;
609821f1d3SAlan Somers 
61a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
62a34cdd26SAlan Somers 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
639821f1d3SAlan Somers 
649821f1d3SAlan Somers 	EXPECT_CALL(*m_mock, process(
659821f1d3SAlan Somers 		ResultOf([=](auto in) {
6629edc611SAlan Somers 			const char *name = (const char*)in.body.bytes +
679821f1d3SAlan Somers 				sizeof(fuse_mkdir_in);
6829edc611SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
6929edc611SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
709821f1d3SAlan Somers 				(0 == strcmp(RELPATH, name)));
719821f1d3SAlan Somers 		}, Eq(true)),
729821f1d3SAlan Somers 		_)
739821f1d3SAlan Somers 	).WillOnce(Invoke(ReturnErrno(EMLINK)));
749821f1d3SAlan Somers 
759821f1d3SAlan Somers 	ASSERT_NE(1, mkdir(FULLPATH, mode));
769821f1d3SAlan Somers 	ASSERT_EQ(EMLINK, errno);
779821f1d3SAlan Somers }
789821f1d3SAlan Somers 
799821f1d3SAlan Somers /*
809821f1d3SAlan Somers  * Creating a new directory after FUSE_LOOKUP returned a negative cache entry
819821f1d3SAlan Somers  */
826248288eSAlan Somers TEST_F(Mkdir, entry_cache_negative)
839821f1d3SAlan Somers {
849821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
859821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
869821f1d3SAlan Somers 	mode_t mode = 0755;
879821f1d3SAlan Somers 	uint64_t ino = 42;
889821f1d3SAlan Somers 	/*
899821f1d3SAlan Somers 	 * Set entry_valid = 0 because this test isn't concerned with whether
909821f1d3SAlan Somers 	 * or not we actually cache negative entries, only with whether we
919821f1d3SAlan Somers 	 * interpret negative cache responses correctly.
929821f1d3SAlan Somers 	 */
939821f1d3SAlan Somers 	struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
949821f1d3SAlan Somers 
959821f1d3SAlan Somers 	/* mkdir will first do a LOOKUP, adding a negative cache entry */
96a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
97a34cdd26SAlan Somers 	.WillOnce(ReturnNegativeCache(&entry_valid));
989821f1d3SAlan Somers 
999821f1d3SAlan Somers 	EXPECT_CALL(*m_mock, process(
1009821f1d3SAlan Somers 		ResultOf([=](auto in) {
10129edc611SAlan Somers 			const char *name = (const char*)in.body.bytes +
1029821f1d3SAlan Somers 				sizeof(fuse_open_in);
10329edc611SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
10429edc611SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
1059821f1d3SAlan Somers 				(0 == strcmp(RELPATH, name)));
1069821f1d3SAlan Somers 		}, Eq(true)),
1079821f1d3SAlan Somers 		_)
10829edc611SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1099821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
11029edc611SAlan Somers 		out.body.create.entry.attr.mode = S_IFDIR | mode;
11129edc611SAlan Somers 		out.body.create.entry.nodeid = ino;
11229edc611SAlan Somers 		out.body.create.entry.entry_valid = UINT64_MAX;
11329edc611SAlan Somers 		out.body.create.entry.attr_valid = UINT64_MAX;
1149821f1d3SAlan Somers 	})));
1159821f1d3SAlan Somers 
1169821f1d3SAlan Somers 	ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1179821f1d3SAlan Somers }
1189821f1d3SAlan Somers 
1199821f1d3SAlan Somers /*
1209821f1d3SAlan Somers  * Creating a new directory should purge any negative namecache entries
1219821f1d3SAlan Somers  */
1226248288eSAlan Somers TEST_F(Mkdir, entry_cache_negative_purge)
1239821f1d3SAlan Somers {
1249821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_file.txt";
1259821f1d3SAlan Somers 	const char RELPATH[] = "some_file.txt";
1269821f1d3SAlan Somers 	mode_t mode = 0755;
1279821f1d3SAlan Somers 	uint64_t ino = 42;
1289821f1d3SAlan Somers 	struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
1299821f1d3SAlan Somers 
1309821f1d3SAlan Somers 	/* mkdir will first do a LOOKUP, adding a negative cache entry */
131a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
132a34cdd26SAlan Somers 	.Times(1)
1339821f1d3SAlan Somers 	.WillOnce(Invoke(ReturnNegativeCache(&entry_valid)))
1349821f1d3SAlan Somers 	.RetiresOnSaturation();
1359821f1d3SAlan Somers 
1369821f1d3SAlan Somers 	/* Then the MKDIR should purge the negative cache entry */
1379821f1d3SAlan Somers 	EXPECT_CALL(*m_mock, process(
1389821f1d3SAlan Somers 		ResultOf([=](auto in) {
13929edc611SAlan Somers 			const char *name = (const char*)in.body.bytes +
1409821f1d3SAlan Somers 				sizeof(fuse_open_in);
14129edc611SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
14229edc611SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
1439821f1d3SAlan Somers 				(0 == strcmp(RELPATH, name)));
1449821f1d3SAlan Somers 		}, Eq(true)),
1459821f1d3SAlan Somers 		_)
14629edc611SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1479821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
14829edc611SAlan Somers 		out.body.entry.attr.mode = S_IFDIR | mode;
14929edc611SAlan Somers 		out.body.entry.nodeid = ino;
15029edc611SAlan Somers 		out.body.entry.attr_valid = UINT64_MAX;
1519821f1d3SAlan Somers 	})));
1529821f1d3SAlan Somers 
1539821f1d3SAlan Somers 	ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1549821f1d3SAlan Somers 
1559821f1d3SAlan Somers 	/* Finally, a subsequent lookup should query the daemon */
1569821f1d3SAlan Somers 	expect_lookup(RELPATH, ino, S_IFDIR | mode, 0, 1);
1579821f1d3SAlan Somers 
1589821f1d3SAlan Somers 	ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
1599821f1d3SAlan Somers }
1609821f1d3SAlan Somers 
1619821f1d3SAlan Somers TEST_F(Mkdir, ok)
1629821f1d3SAlan Somers {
1639821f1d3SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
1649821f1d3SAlan Somers 	const char RELPATH[] = "some_dir";
1659821f1d3SAlan Somers 	mode_t mode = 0755;
1669821f1d3SAlan Somers 	uint64_t ino = 42;
167a4856c96SAlan Somers 	mode_t mask;
168a4856c96SAlan Somers 
169a4856c96SAlan Somers 	mask = umask(0);
170a4856c96SAlan Somers 	(void)umask(mask);
1719821f1d3SAlan Somers 
172a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
173a34cdd26SAlan Somers 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
1749821f1d3SAlan Somers 
1759821f1d3SAlan Somers 	EXPECT_CALL(*m_mock, process(
1769821f1d3SAlan Somers 		ResultOf([=](auto in) {
17729edc611SAlan Somers 			const char *name = (const char*)in.body.bytes +
1789821f1d3SAlan Somers 				sizeof(fuse_mkdir_in);
17929edc611SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
18029edc611SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
181a4856c96SAlan Somers 				in.body.mkdir.umask == mask &&
1829821f1d3SAlan Somers 				(0 == strcmp(RELPATH, name)));
1839821f1d3SAlan Somers 		}, Eq(true)),
1849821f1d3SAlan Somers 		_)
18529edc611SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1869821f1d3SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
18729edc611SAlan Somers 		out.body.create.entry.attr.mode = S_IFDIR | mode;
18829edc611SAlan Somers 		out.body.create.entry.nodeid = ino;
18929edc611SAlan Somers 		out.body.create.entry.entry_valid = UINT64_MAX;
19029edc611SAlan Somers 		out.body.create.entry.attr_valid = UINT64_MAX;
1919821f1d3SAlan Somers 	})));
1929821f1d3SAlan Somers 
1939821f1d3SAlan Somers 	ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1949821f1d3SAlan Somers }
19516bd2d47SAlan Somers 
1960bef4927SAlan Somers /*
1970bef4927SAlan Somers  * Nothing bad should happen if the server returns the parent's inode number
1980bef4927SAlan Somers  * for the newly created directory.  Regression test for bug 263662.
1990bef4927SAlan Somers  * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263662
2000bef4927SAlan Somers  */
2010bef4927SAlan Somers TEST_F(Mkdir, parent_inode)
2020bef4927SAlan Somers {
2030bef4927SAlan Somers 	const char FULLPATH[] = "mountpoint/parent/some_dir";
2040bef4927SAlan Somers 	const char PPATH[] = "parent";
2050bef4927SAlan Somers 	const char RELPATH[] = "some_dir";
2060bef4927SAlan Somers 	mode_t mode = 0755;
2070bef4927SAlan Somers 	uint64_t ino = 42;
2080bef4927SAlan Somers 	mode_t mask;
2090bef4927SAlan Somers 
2100bef4927SAlan Somers 	mask = umask(0);
2110bef4927SAlan Somers 	(void)umask(mask);
2120bef4927SAlan Somers 
2130bef4927SAlan Somers 	expect_lookup(PPATH, ino, S_IFDIR | 0755, 0, 1);
2140bef4927SAlan Somers 	EXPECT_LOOKUP(ino, RELPATH)
2150bef4927SAlan Somers 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
2160bef4927SAlan Somers 
2170bef4927SAlan Somers 	EXPECT_CALL(*m_mock, process(
2180bef4927SAlan Somers 		ResultOf([=](auto in) {
2190bef4927SAlan Somers 			const char *name = (const char*)in.body.bytes +
2200bef4927SAlan Somers 				sizeof(fuse_mkdir_in);
2210bef4927SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
2220bef4927SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
2230bef4927SAlan Somers 				in.body.mkdir.umask == mask &&
2240bef4927SAlan Somers 				(0 == strcmp(RELPATH, name)));
2250bef4927SAlan Somers 		}, Eq(true)),
2260bef4927SAlan Somers 		_)
2270bef4927SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
2280bef4927SAlan Somers 		SET_OUT_HEADER_LEN(out, entry);
2290bef4927SAlan Somers 		out.body.create.entry.attr.mode = S_IFDIR | mode;
2300bef4927SAlan Somers 		out.body.create.entry.nodeid = ino;
2310bef4927SAlan Somers 		out.body.create.entry.entry_valid = UINT64_MAX;
2320bef4927SAlan Somers 		out.body.create.entry.attr_valid = UINT64_MAX;
2330bef4927SAlan Somers 	})));
2340bef4927SAlan Somers 	// FUSE_FORGET happens asynchronously, so it may or may not arrive
2350bef4927SAlan Somers 	// before the test completes.
2360bef4927SAlan Somers 	EXPECT_CALL(*m_mock, process(
2370bef4927SAlan Somers 		ResultOf([=](auto in) {
2380bef4927SAlan Somers 			return (in.header.opcode == FUSE_FORGET);
2390bef4927SAlan Somers 		}, Eq(true)),
2400bef4927SAlan Somers 		_)
2410bef4927SAlan Somers 	).Times(AtMost(1))
2420bef4927SAlan Somers 	.WillOnce(Invoke([=](auto in __unused, auto &out __unused) { }));
2430bef4927SAlan Somers 
2440bef4927SAlan Somers 	ASSERT_EQ(-1, mkdir(FULLPATH, mode));
2450bef4927SAlan Somers 	ASSERT_EQ(EIO, errno);
2460bef4927SAlan Somers 	usleep(100000);
2470bef4927SAlan Somers }
2480bef4927SAlan Somers 
24916bd2d47SAlan Somers TEST_F(Mkdir_7_8, ok)
25016bd2d47SAlan Somers {
25116bd2d47SAlan Somers 	const char FULLPATH[] = "mountpoint/some_dir";
25216bd2d47SAlan Somers 	const char RELPATH[] = "some_dir";
25316bd2d47SAlan Somers 	mode_t mode = 0755;
25416bd2d47SAlan Somers 	uint64_t ino = 42;
25516bd2d47SAlan Somers 
256a34cdd26SAlan Somers 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
257a34cdd26SAlan Somers 	.WillOnce(Invoke(ReturnErrno(ENOENT)));
25816bd2d47SAlan Somers 
25916bd2d47SAlan Somers 	EXPECT_CALL(*m_mock, process(
26016bd2d47SAlan Somers 		ResultOf([=](auto in) {
26129edc611SAlan Somers 			const char *name = (const char*)in.body.bytes +
26216bd2d47SAlan Somers 				sizeof(fuse_mkdir_in);
26329edc611SAlan Somers 			return (in.header.opcode == FUSE_MKDIR &&
26429edc611SAlan Somers 				in.body.mkdir.mode == (S_IFDIR | mode) &&
26516bd2d47SAlan Somers 				(0 == strcmp(RELPATH, name)));
26616bd2d47SAlan Somers 		}, Eq(true)),
26716bd2d47SAlan Somers 		_)
26829edc611SAlan Somers 	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
26916bd2d47SAlan Somers 		SET_OUT_HEADER_LEN(out, entry_7_8);
27029edc611SAlan Somers 		out.body.create.entry.attr.mode = S_IFDIR | mode;
27129edc611SAlan Somers 		out.body.create.entry.nodeid = ino;
27229edc611SAlan Somers 		out.body.create.entry.entry_valid = UINT64_MAX;
27329edc611SAlan Somers 		out.body.create.entry.attr_valid = UINT64_MAX;
27416bd2d47SAlan Somers 	})));
27516bd2d47SAlan Somers 
27616bd2d47SAlan Somers 	ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
27716bd2d47SAlan Somers }
278