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 <fcntl.h>
339821f1d3SAlan Somers }
349821f1d3SAlan Somers
359821f1d3SAlan Somers #include "mockfs.hh"
369821f1d3SAlan Somers #include "utils.hh"
379821f1d3SAlan Somers
389821f1d3SAlan Somers using namespace testing;
399821f1d3SAlan Somers
409821f1d3SAlan Somers class Mkdir: public FuseTest {};
4116bd2d47SAlan Somers class Mkdir_7_8: public FuseTest {
4216bd2d47SAlan Somers public:
SetUp()4316bd2d47SAlan Somers virtual void SetUp() {
4416bd2d47SAlan Somers m_kernel_minor_version = 8;
4516bd2d47SAlan Somers FuseTest::SetUp();
4616bd2d47SAlan Somers }
4716bd2d47SAlan Somers };
489821f1d3SAlan Somers
499821f1d3SAlan Somers /*
509821f1d3SAlan Somers * EMLINK is possible on filesystems that limit the number of hard links to a
519821f1d3SAlan Somers * single file, like early versions of BtrFS
529821f1d3SAlan Somers */
TEST_F(Mkdir,emlink)539821f1d3SAlan Somers TEST_F(Mkdir, emlink)
549821f1d3SAlan Somers {
559821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_dir";
569821f1d3SAlan Somers const char RELPATH[] = "some_dir";
579821f1d3SAlan Somers mode_t mode = 0755;
589821f1d3SAlan Somers
59a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
60a34cdd26SAlan Somers .WillOnce(Invoke(ReturnErrno(ENOENT)));
619821f1d3SAlan Somers
629821f1d3SAlan Somers EXPECT_CALL(*m_mock, process(
639821f1d3SAlan Somers ResultOf([=](auto in) {
6429edc611SAlan Somers const char *name = (const char*)in.body.bytes +
659821f1d3SAlan Somers sizeof(fuse_mkdir_in);
6629edc611SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
6729edc611SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
689821f1d3SAlan Somers (0 == strcmp(RELPATH, name)));
699821f1d3SAlan Somers }, Eq(true)),
709821f1d3SAlan Somers _)
719821f1d3SAlan Somers ).WillOnce(Invoke(ReturnErrno(EMLINK)));
729821f1d3SAlan Somers
739821f1d3SAlan Somers ASSERT_NE(1, mkdir(FULLPATH, mode));
749821f1d3SAlan Somers ASSERT_EQ(EMLINK, errno);
759821f1d3SAlan Somers }
769821f1d3SAlan Somers
779821f1d3SAlan Somers /*
789821f1d3SAlan Somers * Creating a new directory after FUSE_LOOKUP returned a negative cache entry
799821f1d3SAlan Somers */
TEST_F(Mkdir,entry_cache_negative)806248288eSAlan Somers TEST_F(Mkdir, entry_cache_negative)
819821f1d3SAlan Somers {
829821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
839821f1d3SAlan Somers const char RELPATH[] = "some_file.txt";
849821f1d3SAlan Somers mode_t mode = 0755;
859821f1d3SAlan Somers uint64_t ino = 42;
869821f1d3SAlan Somers /*
879821f1d3SAlan Somers * Set entry_valid = 0 because this test isn't concerned with whether
889821f1d3SAlan Somers * or not we actually cache negative entries, only with whether we
899821f1d3SAlan Somers * interpret negative cache responses correctly.
909821f1d3SAlan Somers */
919821f1d3SAlan Somers struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
929821f1d3SAlan Somers
939821f1d3SAlan Somers /* mkdir will first do a LOOKUP, adding a negative cache entry */
94a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
95a34cdd26SAlan Somers .WillOnce(ReturnNegativeCache(&entry_valid));
969821f1d3SAlan Somers
979821f1d3SAlan Somers EXPECT_CALL(*m_mock, process(
989821f1d3SAlan Somers ResultOf([=](auto in) {
9929edc611SAlan Somers const char *name = (const char*)in.body.bytes +
1009821f1d3SAlan Somers sizeof(fuse_open_in);
10129edc611SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
10229edc611SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
1039821f1d3SAlan Somers (0 == strcmp(RELPATH, name)));
1049821f1d3SAlan Somers }, Eq(true)),
1059821f1d3SAlan Somers _)
10629edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1079821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, entry);
10829edc611SAlan Somers out.body.create.entry.attr.mode = S_IFDIR | mode;
10929edc611SAlan Somers out.body.create.entry.nodeid = ino;
11029edc611SAlan Somers out.body.create.entry.entry_valid = UINT64_MAX;
11129edc611SAlan Somers out.body.create.entry.attr_valid = UINT64_MAX;
1129821f1d3SAlan Somers })));
1139821f1d3SAlan Somers
1149821f1d3SAlan Somers ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1159821f1d3SAlan Somers }
1169821f1d3SAlan Somers
1179821f1d3SAlan Somers /*
1189821f1d3SAlan Somers * Creating a new directory should purge any negative namecache entries
1199821f1d3SAlan Somers */
TEST_F(Mkdir,entry_cache_negative_purge)1206248288eSAlan Somers TEST_F(Mkdir, entry_cache_negative_purge)
1219821f1d3SAlan Somers {
1229821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_file.txt";
1239821f1d3SAlan Somers const char RELPATH[] = "some_file.txt";
1249821f1d3SAlan Somers mode_t mode = 0755;
1259821f1d3SAlan Somers uint64_t ino = 42;
1269821f1d3SAlan Somers struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
1279821f1d3SAlan Somers
1289821f1d3SAlan Somers /* mkdir will first do a LOOKUP, adding a negative cache entry */
129a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
130a34cdd26SAlan Somers .Times(1)
1319821f1d3SAlan Somers .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)))
1329821f1d3SAlan Somers .RetiresOnSaturation();
1339821f1d3SAlan Somers
1349821f1d3SAlan Somers /* Then the MKDIR should purge the negative cache entry */
1359821f1d3SAlan Somers EXPECT_CALL(*m_mock, process(
1369821f1d3SAlan Somers ResultOf([=](auto in) {
13729edc611SAlan Somers const char *name = (const char*)in.body.bytes +
1389821f1d3SAlan Somers sizeof(fuse_open_in);
13929edc611SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
14029edc611SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
1419821f1d3SAlan Somers (0 == strcmp(RELPATH, name)));
1429821f1d3SAlan Somers }, Eq(true)),
1439821f1d3SAlan Somers _)
14429edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1459821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, entry);
14629edc611SAlan Somers out.body.entry.attr.mode = S_IFDIR | mode;
14729edc611SAlan Somers out.body.entry.nodeid = ino;
14829edc611SAlan Somers out.body.entry.attr_valid = UINT64_MAX;
1499821f1d3SAlan Somers })));
1509821f1d3SAlan Somers
1519821f1d3SAlan Somers ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1529821f1d3SAlan Somers
1539821f1d3SAlan Somers /* Finally, a subsequent lookup should query the daemon */
1549821f1d3SAlan Somers expect_lookup(RELPATH, ino, S_IFDIR | mode, 0, 1);
1559821f1d3SAlan Somers
1569821f1d3SAlan Somers ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
1579821f1d3SAlan Somers }
1589821f1d3SAlan Somers
TEST_F(Mkdir,ok)1599821f1d3SAlan Somers TEST_F(Mkdir, ok)
1609821f1d3SAlan Somers {
1619821f1d3SAlan Somers const char FULLPATH[] = "mountpoint/some_dir";
1629821f1d3SAlan Somers const char RELPATH[] = "some_dir";
1639821f1d3SAlan Somers mode_t mode = 0755;
1649821f1d3SAlan Somers uint64_t ino = 42;
165a4856c96SAlan Somers mode_t mask;
166a4856c96SAlan Somers
167a4856c96SAlan Somers mask = umask(0);
168a4856c96SAlan Somers (void)umask(mask);
1699821f1d3SAlan Somers
170a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
171a34cdd26SAlan Somers .WillOnce(Invoke(ReturnErrno(ENOENT)));
1729821f1d3SAlan Somers
1739821f1d3SAlan Somers EXPECT_CALL(*m_mock, process(
1749821f1d3SAlan Somers ResultOf([=](auto in) {
17529edc611SAlan Somers const char *name = (const char*)in.body.bytes +
1769821f1d3SAlan Somers sizeof(fuse_mkdir_in);
17729edc611SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
17829edc611SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
179a4856c96SAlan Somers in.body.mkdir.umask == mask &&
1809821f1d3SAlan Somers (0 == strcmp(RELPATH, name)));
1819821f1d3SAlan Somers }, Eq(true)),
1829821f1d3SAlan Somers _)
18329edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
1849821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, entry);
18529edc611SAlan Somers out.body.create.entry.attr.mode = S_IFDIR | mode;
18629edc611SAlan Somers out.body.create.entry.nodeid = ino;
18729edc611SAlan Somers out.body.create.entry.entry_valid = UINT64_MAX;
18829edc611SAlan Somers out.body.create.entry.attr_valid = UINT64_MAX;
1899821f1d3SAlan Somers })));
1909821f1d3SAlan Somers
1919821f1d3SAlan Somers ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
1929821f1d3SAlan Somers }
19316bd2d47SAlan Somers
1940bef4927SAlan Somers /*
1950bef4927SAlan Somers * Nothing bad should happen if the server returns the parent's inode number
1960bef4927SAlan Somers * for the newly created directory. Regression test for bug 263662.
1970bef4927SAlan Somers * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263662
1980bef4927SAlan Somers */
TEST_F(Mkdir,parent_inode)1990bef4927SAlan Somers TEST_F(Mkdir, parent_inode)
2000bef4927SAlan Somers {
2010bef4927SAlan Somers const char FULLPATH[] = "mountpoint/parent/some_dir";
2020bef4927SAlan Somers const char PPATH[] = "parent";
2030bef4927SAlan Somers const char RELPATH[] = "some_dir";
2040bef4927SAlan Somers mode_t mode = 0755;
2050bef4927SAlan Somers uint64_t ino = 42;
2060bef4927SAlan Somers mode_t mask;
2070bef4927SAlan Somers
2080bef4927SAlan Somers mask = umask(0);
2090bef4927SAlan Somers (void)umask(mask);
2100bef4927SAlan Somers
2110bef4927SAlan Somers expect_lookup(PPATH, ino, S_IFDIR | 0755, 0, 1);
2120bef4927SAlan Somers EXPECT_LOOKUP(ino, RELPATH)
2130bef4927SAlan Somers .WillOnce(Invoke(ReturnErrno(ENOENT)));
2140bef4927SAlan Somers
2150bef4927SAlan Somers EXPECT_CALL(*m_mock, process(
2160bef4927SAlan Somers ResultOf([=](auto in) {
2170bef4927SAlan Somers const char *name = (const char*)in.body.bytes +
2180bef4927SAlan Somers sizeof(fuse_mkdir_in);
2190bef4927SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
2200bef4927SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
2210bef4927SAlan Somers in.body.mkdir.umask == mask &&
2220bef4927SAlan Somers (0 == strcmp(RELPATH, name)));
2230bef4927SAlan Somers }, Eq(true)),
2240bef4927SAlan Somers _)
2250bef4927SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
2260bef4927SAlan Somers SET_OUT_HEADER_LEN(out, entry);
2270bef4927SAlan Somers out.body.create.entry.attr.mode = S_IFDIR | mode;
2280bef4927SAlan Somers out.body.create.entry.nodeid = ino;
2290bef4927SAlan Somers out.body.create.entry.entry_valid = UINT64_MAX;
2300bef4927SAlan Somers out.body.create.entry.attr_valid = UINT64_MAX;
2310bef4927SAlan Somers })));
2320bef4927SAlan Somers // FUSE_FORGET happens asynchronously, so it may or may not arrive
2330bef4927SAlan Somers // before the test completes.
2340bef4927SAlan Somers EXPECT_CALL(*m_mock, process(
2350bef4927SAlan Somers ResultOf([=](auto in) {
2360bef4927SAlan Somers return (in.header.opcode == FUSE_FORGET);
2370bef4927SAlan Somers }, Eq(true)),
2380bef4927SAlan Somers _)
2390bef4927SAlan Somers ).Times(AtMost(1))
2400bef4927SAlan Somers .WillOnce(Invoke([=](auto in __unused, auto &out __unused) { }));
2410bef4927SAlan Somers
2420bef4927SAlan Somers ASSERT_EQ(-1, mkdir(FULLPATH, mode));
2430bef4927SAlan Somers ASSERT_EQ(EIO, errno);
2440bef4927SAlan Somers }
2450bef4927SAlan Somers
TEST_F(Mkdir_7_8,ok)24616bd2d47SAlan Somers TEST_F(Mkdir_7_8, ok)
24716bd2d47SAlan Somers {
24816bd2d47SAlan Somers const char FULLPATH[] = "mountpoint/some_dir";
24916bd2d47SAlan Somers const char RELPATH[] = "some_dir";
25016bd2d47SAlan Somers mode_t mode = 0755;
25116bd2d47SAlan Somers uint64_t ino = 42;
25216bd2d47SAlan Somers
253a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
254a34cdd26SAlan Somers .WillOnce(Invoke(ReturnErrno(ENOENT)));
25516bd2d47SAlan Somers
25616bd2d47SAlan Somers EXPECT_CALL(*m_mock, process(
25716bd2d47SAlan Somers ResultOf([=](auto in) {
25829edc611SAlan Somers const char *name = (const char*)in.body.bytes +
25916bd2d47SAlan Somers sizeof(fuse_mkdir_in);
26029edc611SAlan Somers return (in.header.opcode == FUSE_MKDIR &&
26129edc611SAlan Somers in.body.mkdir.mode == (S_IFDIR | mode) &&
26216bd2d47SAlan Somers (0 == strcmp(RELPATH, name)));
26316bd2d47SAlan Somers }, Eq(true)),
26416bd2d47SAlan Somers _)
26529edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
26616bd2d47SAlan Somers SET_OUT_HEADER_LEN(out, entry_7_8);
26729edc611SAlan Somers out.body.create.entry.attr.mode = S_IFDIR | mode;
26829edc611SAlan Somers out.body.create.entry.nodeid = ino;
26929edc611SAlan Somers out.body.create.entry.entry_valid = UINT64_MAX;
27029edc611SAlan Somers out.body.create.entry.attr_valid = UINT64_MAX;
27116bd2d47SAlan Somers })));
27216bd2d47SAlan Somers
27316bd2d47SAlan Somers ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
27416bd2d47SAlan Somers }
275