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
38 using namespace testing;
40 class Mkdir: public FuseTest {};
43 * EMLINK is possible on filesystems that limit the number of hard links to a
44 * single file, like early versions of BtrFS
48 const char FULLPATH[] = "mountpoint/some_dir";
49 const char RELPATH[] = "some_dir";
52 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
54 EXPECT_CALL(*m_mock, process(
55 ResultOf([=](auto in) {
56 const char *name = (const char*)in->body.bytes +
57 sizeof(fuse_mkdir_in);
58 return (in->header.opcode == FUSE_MKDIR &&
59 in->body.mkdir.mode == (S_IFDIR | mode) &&
60 (0 == strcmp(RELPATH, name)));
63 ).WillOnce(Invoke(ReturnErrno(EMLINK)));
65 ASSERT_NE(1, mkdir(FULLPATH, mode));
66 ASSERT_EQ(EMLINK, errno);
70 * Creating a new directory after FUSE_LOOKUP returned a negative cache entry
72 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
73 TEST_F(Mkdir, DISABLED_entry_cache_negative)
75 const char FULLPATH[] = "mountpoint/some_file.txt";
76 const char RELPATH[] = "some_file.txt";
80 * Set entry_valid = 0 because this test isn't concerned with whether
81 * or not we actually cache negative entries, only with whether we
82 * interpret negative cache responses correctly.
84 struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
86 /* mkdir will first do a LOOKUP, adding a negative cache entry */
87 EXPECT_LOOKUP(1, RELPATH).WillOnce(ReturnNegativeCache(&entry_valid));
89 EXPECT_CALL(*m_mock, process(
90 ResultOf([=](auto in) {
91 const char *name = (const char*)in->body.bytes +
93 return (in->header.opcode == FUSE_MKDIR &&
94 in->body.mkdir.mode == (S_IFDIR | mode) &&
95 (0 == strcmp(RELPATH, name)));
98 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
99 SET_OUT_HEADER_LEN(out, entry);
100 out->body.create.entry.attr.mode = S_IFDIR | mode;
101 out->body.create.entry.nodeid = ino;
102 out->body.create.entry.entry_valid = UINT64_MAX;
103 out->body.create.entry.attr_valid = UINT64_MAX;
106 ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
110 * Creating a new directory should purge any negative namecache entries
112 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
113 TEST_F(Mkdir, DISABLED_entry_cache_negative_purge)
115 const char FULLPATH[] = "mountpoint/some_file.txt";
116 const char RELPATH[] = "some_file.txt";
119 struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
121 /* mkdir will first do a LOOKUP, adding a negative cache entry */
122 EXPECT_LOOKUP(1, RELPATH).Times(1)
123 .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)))
124 .RetiresOnSaturation();
126 /* Then the MKDIR should purge the negative cache entry */
127 EXPECT_CALL(*m_mock, process(
128 ResultOf([=](auto in) {
129 const char *name = (const char*)in->body.bytes +
130 sizeof(fuse_open_in);
131 return (in->header.opcode == FUSE_MKDIR &&
132 in->body.mkdir.mode == (S_IFDIR | mode) &&
133 (0 == strcmp(RELPATH, name)));
136 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
137 SET_OUT_HEADER_LEN(out, entry);
138 out->body.entry.attr.mode = S_IFDIR | mode;
139 out->body.entry.nodeid = ino;
140 out->body.entry.attr_valid = UINT64_MAX;
143 ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
145 /* Finally, a subsequent lookup should query the daemon */
146 expect_lookup(RELPATH, ino, S_IFDIR | mode, 1);
148 ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
153 const char FULLPATH[] = "mountpoint/some_dir";
154 const char RELPATH[] = "some_dir";
158 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
160 EXPECT_CALL(*m_mock, process(
161 ResultOf([=](auto in) {
162 const char *name = (const char*)in->body.bytes +
163 sizeof(fuse_mkdir_in);
164 return (in->header.opcode == FUSE_MKDIR &&
165 in->body.mkdir.mode == (S_IFDIR | mode) &&
166 (0 == strcmp(RELPATH, name)));
169 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
170 SET_OUT_HEADER_LEN(out, entry);
171 out->body.create.entry.attr.mode = S_IFDIR | mode;
172 out->body.create.entry.nodeid = ino;
173 out->body.create.entry.entry_valid = UINT64_MAX;
174 out->body.create.entry.attr_valid = UINT64_MAX;
177 ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);