]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/lookup.cc
fuse(4): combine common code in the tests
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / lookup.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 <unistd.h>
33 }
34
35 #include "mockfs.hh"
36 #include "utils.hh"
37
38 using namespace testing;
39
40 class Lookup: public FuseTest {};
41
42 /*
43  * If lookup returns a non-zero cache timeout, then subsequent VOP_GETATTRs
44  * should use the cached attributes, rather than query the daemon
45  */
46 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
47 TEST_F(Lookup, DISABLED_attr_cache)
48 {
49         const char FULLPATH[] = "mountpoint/some_file.txt";
50         const char RELPATH[] = "some_file.txt";
51         const uint64_t ino = 42;
52         const uint64_t generation = 13;
53         struct stat sb;
54
55         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
56                 out->header.unique = in->header.unique;
57                 SET_OUT_HEADER_LEN(out, entry);
58                 out->body.entry.nodeid = ino;
59                 out->body.entry.attr_valid = UINT64_MAX;
60                 out->body.entry.attr.ino = ino; // Must match nodeid
61                 out->body.entry.attr.mode = S_IFREG | 0644;
62                 out->body.entry.attr.size = 1;
63                 out->body.entry.attr.blocks = 2;
64                 out->body.entry.attr.atime = 3;
65                 out->body.entry.attr.mtime = 4;
66                 out->body.entry.attr.ctime = 5;
67                 out->body.entry.attr.atimensec = 6;
68                 out->body.entry.attr.mtimensec = 7;
69                 out->body.entry.attr.ctimensec = 8;
70                 out->body.entry.attr.nlink = 9;
71                 out->body.entry.attr.uid = 10;
72                 out->body.entry.attr.gid = 11;
73                 out->body.entry.attr.rdev = 12;
74                 out->body.entry.generation = generation;
75         }));
76         /* stat(2) issues a VOP_LOOKUP followed by a VOP_GETATTR */
77         ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
78         EXPECT_EQ(1, sb.st_size);
79         EXPECT_EQ(2, sb.st_blocks);
80         EXPECT_EQ(3, sb.st_atim.tv_sec);
81         EXPECT_EQ(6, sb.st_atim.tv_nsec);
82         EXPECT_EQ(4, sb.st_mtim.tv_sec);
83         EXPECT_EQ(7, sb.st_mtim.tv_nsec);
84         EXPECT_EQ(5, sb.st_ctim.tv_sec);
85         EXPECT_EQ(8, sb.st_ctim.tv_nsec);
86         EXPECT_EQ(9ull, sb.st_nlink);
87         EXPECT_EQ(10ul, sb.st_uid);
88         EXPECT_EQ(11ul, sb.st_gid);
89         EXPECT_EQ(12ul, sb.st_rdev);
90         EXPECT_EQ(ino, sb.st_ino);
91         EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
92
93         // fuse(4) does not _yet_ support inode generations
94         //EXPECT_EQ(generation, sb.st_gen);
95
96         //st_birthtim and st_flags are not supported by protocol 7.8.  They're
97         //only supported as OS-specific extensions to OSX.
98         //EXPECT_EQ(, sb.st_birthtim);
99         //EXPECT_EQ(, sb.st_flags);
100         
101         //FUSE can't set st_blksize until protocol 7.9
102 }
103
104 /*
105  * If lookup returns a finite but non-zero cache timeout, then we should discard
106  * the cached attributes and requery the daemon.
107  */
108 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
109 TEST_F(Lookup, attr_cache_timeout)
110 {
111         const char FULLPATH[] = "mountpoint/some_file.txt";
112         const char RELPATH[] = "some_file.txt";
113         const uint64_t ino = 42;
114         struct stat sb;
115         /* 
116          * The timeout should be longer than the longest plausible time the
117          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
118          */
119         long timeout_ns = 250'000'000;
120
121         EXPECT_LOOKUP(1, RELPATH).WillRepeatedly(Invoke([=](auto in, auto out) {
122                 out->header.unique = in->header.unique;
123                 SET_OUT_HEADER_LEN(out, entry);
124                 out->body.entry.nodeid = ino;
125                 out->body.entry.attr_valid_nsec = timeout_ns;
126                 out->body.entry.attr.ino = ino; // Must match nodeid
127                 out->body.entry.attr.mode = S_IFREG | 0644;
128         }));
129         expect_getattr(ino, 0);
130
131         /* access(2) will issue a VOP_LOOKUP but not a VOP_GETATTR */
132         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
133         usleep(2 * timeout_ns / 1000);
134         /* The cache has timed out; VOP_GETATTR should query the daemon*/
135         ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
136 }
137
138 TEST_F(Lookup, enoent)
139 {
140         const char FULLPATH[] = "mountpoint/does_not_exist";
141         const char RELPATH[] = "does_not_exist";
142
143         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
144         EXPECT_NE(0, access(FULLPATH, F_OK));
145         EXPECT_EQ(ENOENT, errno);
146 }
147
148 /*
149  * If lookup returns a non-zero entry timeout, then subsequent VOP_LOOKUPs
150  * should use the cached inode rather than requery the daemon
151  */
152 TEST_F(Lookup, entry_cache)
153 {
154         const char FULLPATH[] = "mountpoint/some_file.txt";
155         const char RELPATH[] = "some_file.txt";
156
157         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
158                 out->header.unique = in->header.unique;
159                 SET_OUT_HEADER_LEN(out, entry);
160                 out->body.entry.entry_valid = UINT64_MAX;
161                 out->body.entry.attr.mode = S_IFREG | 0644;
162                 out->body.entry.nodeid = 14;
163         }));
164         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
165         /* The second access(2) should use the cache */
166         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
167 }
168
169 /* 
170  * If the daemon returns an error of 0 and an inode of 0, that's a flag for
171  * "ENOENT and cache it" with the given entry_timeout
172  */
173 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236226 */
174 TEST_F(Lookup, DISABLED_entry_cache_negative)
175 {
176         struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
177
178         EXPECT_LOOKUP(1, "does_not_exist").Times(1)
179         .WillOnce(Invoke(ReturnNegativeCache(&entry_valid)));
180
181         EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
182         EXPECT_EQ(ENOENT, errno);
183         EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
184         EXPECT_EQ(ENOENT, errno);
185 }
186
187 /* Negative entry caches should timeout, too */
188 TEST_F(Lookup, entry_cache_negative_timeout)
189 {
190         const char *RELPATH = "does_not_exist";
191         const char *FULLPATH = "mountpoint/does_not_exist";
192         /* 
193          * The timeout should be longer than the longest plausible time the
194          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
195          */
196         struct timespec entry_valid = {.tv_sec = 0, .tv_nsec = 250'000'000};
197
198         EXPECT_LOOKUP(1, RELPATH).Times(2)
199         .WillRepeatedly(Invoke(ReturnNegativeCache(&entry_valid)));
200
201         EXPECT_NE(0, access(FULLPATH, F_OK));
202         EXPECT_EQ(ENOENT, errno);
203
204         usleep(2 * entry_valid.tv_nsec / 1000);
205
206         /* The cache has timed out; VOP_LOOKUP should requery the daemon*/
207         EXPECT_NE(0, access(FULLPATH, F_OK));
208         EXPECT_EQ(ENOENT, errno);
209 }
210
211 /*
212  * If lookup returns a finite but non-zero entry cache timeout, then we should
213  * discard the cached inode and requery the daemon
214  */
215 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
216 TEST_F(Lookup, DISABLED_entry_cache_timeout)
217 {
218         const char FULLPATH[] = "mountpoint/some_file.txt";
219         const char RELPATH[] = "some_file.txt";
220         /* 
221          * The timeout should be longer than the longest plausible time the
222          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
223          */
224         long timeout_ns = 250'000'000;
225
226         EXPECT_LOOKUP(1, RELPATH).Times(2)
227         .WillRepeatedly(Invoke([=](auto in, auto out) {
228                 out->header.unique = in->header.unique;
229                 SET_OUT_HEADER_LEN(out, entry);
230                 out->body.entry.entry_valid_nsec = timeout_ns;
231                 out->body.entry.attr.mode = S_IFREG | 0644;
232                 out->body.entry.nodeid = 14;
233         }));
234         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
235         usleep(2 * timeout_ns / 1000);
236         /* The cache has timed out; VOP_LOOKUP should query the daemon*/
237         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
238 }
239
240 // TODO: export_support
241 // After upgrading the protocol to 7.10, check that the kernel will only
242 // attempt to lookup "." and ".." if the filesystem sets FUSE_EXPORT_SUPPORT in
243 // the init flags.  If not, then all lookups for those entries will return
244 // ESTALE.
245
246 TEST_F(Lookup, ok)
247 {
248         const char FULLPATH[] = "mountpoint/some_file.txt";
249         const char RELPATH[] = "some_file.txt";
250
251         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
252                 out->header.unique = in->header.unique;
253                 SET_OUT_HEADER_LEN(out, entry);
254                 out->body.entry.attr.mode = S_IFREG | 0644;
255                 out->body.entry.nodeid = 14;
256         }));
257         /*
258          * access(2) is one of the few syscalls that will not (always) follow
259          * up a successful VOP_LOOKUP with another VOP.
260          */
261         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
262 }
263
264 // Lookup in a subdirectory of the fuse mount
265 TEST_F(Lookup, subdir)
266 {
267         const char FULLPATH[] = "mountpoint/some_dir/some_file.txt";
268         const char DIRPATH[] = "some_dir";
269         const char RELPATH[] = "some_file.txt";
270         uint64_t dir_ino = 2;
271         uint64_t file_ino = 3;
272
273         EXPECT_LOOKUP(1, DIRPATH).WillOnce(Invoke([=](auto in, auto out) {
274                 out->header.unique = in->header.unique;
275                 SET_OUT_HEADER_LEN(out, entry);
276                 out->body.entry.attr.mode = S_IFDIR | 0755;
277                 out->body.entry.nodeid = dir_ino;
278         }));
279         EXPECT_LOOKUP(dir_ino, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
280                 out->header.unique = in->header.unique;
281                 SET_OUT_HEADER_LEN(out, entry);
282                 out->body.entry.attr.mode = S_IFREG | 0644;
283                 out->body.entry.nodeid = file_ino;
284         }));
285         /*
286          * access(2) is one of the few syscalls that will not (always) follow
287          * up a successful VOP_LOOKUP with another VOP.
288          */
289         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
290 }
291
292