]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/mkdir.cc
fuse(4): add tests for FUSE_INTERRUPT
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / mkdir.cc
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 extern "C" {
32 #include <fcntl.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Mkdir: public FuseTest {};
41
42 /* 
43  * EMLINK is possible on filesystems that limit the number of hard links to a
44  * single file, like early versions of BtrFS
45  */
46 TEST_F(Mkdir, emlink)
47 {
48         const char FULLPATH[] = "mountpoint/some_dir";
49         const char RELPATH[] = "some_dir";
50         mode_t mode = 0755;
51
52         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
53
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)));
61                 }, Eq(true)),
62                 _)
63         ).WillOnce(Invoke(ReturnErrno(EMLINK)));
64
65         ASSERT_NE(1, mkdir(FULLPATH, mode));
66         ASSERT_EQ(EMLINK, errno);
67 }
68
69 /*
70  * Creating a new directory after FUSE_LOOKUP returned a negative cache entry
71  */
72 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
73 TEST_F(Mkdir, DISABLED_entry_cache_negative)
74 {
75         const char FULLPATH[] = "mountpoint/some_file.txt";
76         const char RELPATH[] = "some_file.txt";
77         mode_t mode = 0755;
78         uint64_t ino = 42;
79         /* 
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.
83          */
84         struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 0};
85
86         /* mkdir will first do a LOOKUP, adding a negative cache entry */
87         EXPECT_LOOKUP(1, RELPATH).WillOnce(ReturnNegativeCache(&entry_valid));
88
89         EXPECT_CALL(*m_mock, process(
90                 ResultOf([=](auto in) {
91                         const char *name = (const char*)in->body.bytes +
92                                 sizeof(fuse_open_in);
93                         return (in->header.opcode == FUSE_MKDIR &&
94                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
95                                 (0 == strcmp(RELPATH, name)));
96                 }, Eq(true)),
97                 _)
98         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
99                 out->header.unique = in->header.unique;
100                 SET_OUT_HEADER_LEN(out, entry);
101                 out->body.create.entry.attr.mode = S_IFDIR | mode;
102                 out->body.create.entry.nodeid = ino;
103                 out->body.create.entry.entry_valid = UINT64_MAX;
104                 out->body.create.entry.attr_valid = UINT64_MAX;
105         })));
106
107         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
108 }
109
110 /*
111  * Creating a new directory should purge any negative namecache entries
112  */
113 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
114 TEST_F(Mkdir, DISABLED_entry_cache_negative_purge)
115 {
116         const char FULLPATH[] = "mountpoint/some_file.txt";
117         const char RELPATH[] = "some_file.txt";
118         mode_t mode = 0755;
119         uint64_t ino = 42;
120         struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
121
122         /* mkdir will first do a LOOKUP, adding a negative cache entry */
123         EXPECT_LOOKUP(1, RELPATH).Times(1)
124         .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)))
125         .RetiresOnSaturation();
126
127         /* Then the MKDIR should purge the negative cache entry */
128         EXPECT_CALL(*m_mock, process(
129                 ResultOf([=](auto in) {
130                         const char *name = (const char*)in->body.bytes +
131                                 sizeof(fuse_open_in);
132                         return (in->header.opcode == FUSE_MKDIR &&
133                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
134                                 (0 == strcmp(RELPATH, name)));
135                 }, Eq(true)),
136                 _)
137         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
138                 out->header.unique = in->header.unique;
139                 SET_OUT_HEADER_LEN(out, entry);
140                 out->body.entry.attr.mode = S_IFDIR | mode;
141                 out->body.entry.nodeid = ino;
142                 out->body.entry.attr_valid = UINT64_MAX;
143         })));
144
145         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
146
147         /* Finally, a subsequent lookup should query the daemon */
148         expect_lookup(RELPATH, ino, S_IFDIR | mode, 1);
149
150         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
151 }
152
153 TEST_F(Mkdir, ok)
154 {
155         const char FULLPATH[] = "mountpoint/some_dir";
156         const char RELPATH[] = "some_dir";
157         mode_t mode = 0755;
158         uint64_t ino = 42;
159
160         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
161
162         EXPECT_CALL(*m_mock, process(
163                 ResultOf([=](auto in) {
164                         const char *name = (const char*)in->body.bytes +
165                                 sizeof(fuse_mkdir_in);
166                         return (in->header.opcode == FUSE_MKDIR &&
167                                 in->body.mkdir.mode == (S_IFDIR | mode) &&
168                                 (0 == strcmp(RELPATH, name)));
169                 }, Eq(true)),
170                 _)
171         ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto out) {
172                 out->header.unique = in->header.unique;
173                 SET_OUT_HEADER_LEN(out, entry);
174                 out->body.create.entry.attr.mode = S_IFDIR | mode;
175                 out->body.create.entry.nodeid = ino;
176                 out->body.create.entry.entry_valid = UINT64_MAX;
177                 out->body.create.entry.attr_valid = UINT64_MAX;
178         })));
179
180         ASSERT_EQ(0, mkdir(FULLPATH, mode)) << strerror(errno);
181 }