]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fuse/lookup.cc
fuse(4): add test cases for FUSE_LINK and FUSE_RENAME
[FreeBSD/FreeBSD.git] / tests / sys / fs / fuse / lookup.cc
1 /*-
2  * Copyright (c) 2019 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by BFF Storage Systems, LLC under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 extern "C" {
31 #include <unistd.h>
32 }
33
34 #include "mockfs.hh"
35 #include "utils.hh"
36
37 using namespace testing;
38
39 class Lookup: public FuseTest {};
40
41 /*
42  * If lookup returns a non-zero cache timeout, then subsequent VOP_GETATTRs
43  * should use the cached attributes, rather than query the daemon
44  */
45 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
46 TEST_F(Lookup, DISABLED_attr_cache)
47 {
48         const char FULLPATH[] = "mountpoint/some_file.txt";
49         const char RELPATH[] = "some_file.txt";
50         const uint64_t ino = 42;
51         struct stat sb;
52
53         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
54                 out->header.unique = in->header.unique;
55                 SET_OUT_HEADER_LEN(out, entry);
56                 out->body.entry.nodeid = ino;
57                 out->body.entry.attr_valid = UINT64_MAX;
58                 out->body.entry.attr.ino = ino; // Must match nodeid
59                 out->body.entry.attr.mode = S_IFREG | 0644;
60                 out->body.entry.attr.size = 1;
61                 out->body.entry.attr.blocks = 2;
62                 out->body.entry.attr.atime = 3;
63                 out->body.entry.attr.mtime = 4;
64                 out->body.entry.attr.ctime = 5;
65                 out->body.entry.attr.atimensec = 6;
66                 out->body.entry.attr.mtimensec = 7;
67                 out->body.entry.attr.ctimensec = 8;
68                 out->body.entry.attr.nlink = 9;
69                 out->body.entry.attr.uid = 10;
70                 out->body.entry.attr.gid = 11;
71                 out->body.entry.attr.rdev = 12;
72         }));
73         /* stat(2) issues a VOP_LOOKUP followed by a VOP_GETATTR */
74         ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
75         EXPECT_EQ(1, sb.st_size);
76         EXPECT_EQ(2, sb.st_blocks);
77         EXPECT_EQ(3, sb.st_atim.tv_sec);
78         EXPECT_EQ(6, sb.st_atim.tv_nsec);
79         EXPECT_EQ(4, sb.st_mtim.tv_sec);
80         EXPECT_EQ(7, sb.st_mtim.tv_nsec);
81         EXPECT_EQ(5, sb.st_ctim.tv_sec);
82         EXPECT_EQ(8, sb.st_ctim.tv_nsec);
83         EXPECT_EQ(9ull, sb.st_nlink);
84         EXPECT_EQ(10ul, sb.st_uid);
85         EXPECT_EQ(11ul, sb.st_gid);
86         EXPECT_EQ(12ul, sb.st_rdev);
87         EXPECT_EQ(ino, sb.st_ino);
88         EXPECT_EQ(S_IFREG | 0644, sb.st_mode);
89
90         // fuse(4) does not _yet_ support inode generations
91         //EXPECT_EQ(generation, sb.st_gen);
92
93         //st_birthtim and st_flags are not supported by protocol 7.8.  They're
94         //only supported as OS-specific extensions to OSX.
95         //EXPECT_EQ(, sb.st_birthtim);
96         //EXPECT_EQ(, sb.st_flags);
97         
98         //FUSE can't set st_blksize until protocol 7.9
99 }
100
101 /*
102  * If lookup returns a finite but non-zero cache timeout, then we should discard
103  * the cached attributes and requery the daemon.
104  */
105 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
106 TEST_F(Lookup, attr_cache_timeout)
107 {
108         const char FULLPATH[] = "mountpoint/some_file.txt";
109         const char RELPATH[] = "some_file.txt";
110         const uint64_t ino = 42;
111         struct stat sb;
112         /* 
113          * The timeout should be longer than the longest plausible time the
114          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
115          */
116         long timeout_ns = 250'000'000;
117
118         EXPECT_LOOKUP(1, RELPATH).WillRepeatedly(Invoke([=](auto in, auto out) {
119                 out->header.unique = in->header.unique;
120                 SET_OUT_HEADER_LEN(out, entry);
121                 out->body.entry.nodeid = ino;
122                 out->body.entry.attr_valid_nsec = timeout_ns;
123                 out->body.entry.attr.ino = ino; // Must match nodeid
124                 out->body.entry.attr.mode = S_IFREG | 0644;
125         }));
126         EXPECT_CALL(*m_mock, process(
127                 ResultOf([](auto in) {
128                         return (in->header.opcode == FUSE_GETATTR &&
129                                 in->header.nodeid == ino);
130                 }, Eq(true)),
131                 _)
132         ).WillOnce(Invoke([](auto in, auto out) {
133                 out->header.unique = in->header.unique;
134                 SET_OUT_HEADER_LEN(out, attr);
135                 out->body.attr.attr.ino = ino;  // Must match nodeid
136                 out->body.attr.attr.mode = S_IFREG | 0644;
137         }));
138
139         /* access(2) will issue a VOP_LOOKUP but not a VOP_GETATTR */
140         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
141         usleep(2 * timeout_ns / 1000);
142         /* The cache has timed out; VOP_GETATTR should query the daemon*/
143         ASSERT_EQ(0, stat(FULLPATH, &sb)) << strerror(errno);
144 }
145
146 TEST_F(Lookup, enoent)
147 {
148         const char FULLPATH[] = "mountpoint/does_not_exist";
149         const char RELPATH[] = "does_not_exist";
150
151         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
152         EXPECT_NE(0, access(FULLPATH, F_OK));
153         EXPECT_EQ(ENOENT, errno);
154 }
155
156 /*
157  * If lookup returns a non-zero entry timeout, then subsequent VOP_LOOKUPs
158  * should use the cached inode rather than requery the daemon
159  */
160 TEST_F(Lookup, entry_cache)
161 {
162         const char FULLPATH[] = "mountpoint/some_file.txt";
163         const char RELPATH[] = "some_file.txt";
164
165         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
166                 out->header.unique = in->header.unique;
167                 SET_OUT_HEADER_LEN(out, entry);
168                 out->body.entry.entry_valid = UINT64_MAX;
169                 out->body.entry.attr.mode = S_IFREG | 0644;
170                 out->body.entry.nodeid = 14;
171         }));
172         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
173         /* The second access(2) should use the cache */
174         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
175 }
176
177 /* 
178  * If the daemon returns an error of 0 and an inode of 0, that's a flag for
179  * "ENOENT and cache it" with the given entry_timeout
180  */
181 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236226 */
182 TEST_F(Lookup, DISABLED_entry_cache_negative)
183 {
184         EXPECT_LOOKUP(1, "does_not_exist").Times(1)
185         .WillOnce(Invoke([](auto in, auto out) {
186                 out->header.unique = in->header.unique;
187                 out->header.error = 0;
188                 out->body.entry.nodeid = 0;
189                 out->body.entry.entry_valid = UINT64_MAX;
190                 SET_OUT_HEADER_LEN(out, entry);
191         }));
192         EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
193         EXPECT_EQ(ENOENT, errno);
194         EXPECT_NE(0, access("mountpoint/does_not_exist", F_OK));
195         EXPECT_EQ(ENOENT, errno);
196 }
197
198 /* Negative entry caches should timeout, too */
199 TEST_F(Lookup, entry_cache_negative_timeout)
200 {
201         const char *RELPATH = "does_not_exist";
202         const char *FULLPATH = "mountpoint/does_not_exist";
203         /* 
204          * The timeout should be longer than the longest plausible time the
205          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
206          */
207         long timeout_ns = 250'000'000;
208
209         EXPECT_LOOKUP(1, RELPATH).Times(2)
210         .WillRepeatedly(Invoke([=](auto in, auto out) {
211                 out->header.unique = in->header.unique;
212                 out->header.error = 0;
213                 out->body.entry.nodeid = 0;
214                 out->body.entry.entry_valid_nsec = timeout_ns;
215                 SET_OUT_HEADER_LEN(out, entry);
216         }));
217         EXPECT_NE(0, access(FULLPATH, F_OK));
218         EXPECT_EQ(ENOENT, errno);
219
220         usleep(2 * timeout_ns / 1000);
221
222         /* The cache has timed out; VOP_LOOKUP should requery the daemon*/
223         EXPECT_NE(0, access(FULLPATH, F_OK));
224         EXPECT_EQ(ENOENT, errno);
225 }
226
227 /*
228  * If lookup returns a finite but non-zero entry cache timeout, then we should
229  * discard the cached inode and requery the daemon
230  */
231 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235773 */
232 TEST_F(Lookup, DISABLED_entry_cache_timeout)
233 {
234         const char FULLPATH[] = "mountpoint/some_file.txt";
235         const char RELPATH[] = "some_file.txt";
236         /* 
237          * The timeout should be longer than the longest plausible time the
238          * daemon would take to complete a write(2) to /dev/fuse, but no longer.
239          */
240         long timeout_ns = 250'000'000;
241
242         EXPECT_LOOKUP(1, RELPATH).Times(2)
243         .WillRepeatedly(Invoke([=](auto in, auto out) {
244                 out->header.unique = in->header.unique;
245                 SET_OUT_HEADER_LEN(out, entry);
246                 out->body.entry.entry_valid_nsec = timeout_ns;
247                 out->body.entry.attr.mode = S_IFREG | 0644;
248                 out->body.entry.nodeid = 14;
249         }));
250         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
251         usleep(2 * timeout_ns / 1000);
252         /* The cache has timed out; VOP_LOOKUP should query the daemon*/
253         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
254 }
255
256 TEST_F(Lookup, ok)
257 {
258         const char FULLPATH[] = "mountpoint/some_file.txt";
259         const char RELPATH[] = "some_file.txt";
260
261         EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
262                 out->header.unique = in->header.unique;
263                 SET_OUT_HEADER_LEN(out, entry);
264                 out->body.entry.attr.mode = S_IFREG | 0644;
265                 out->body.entry.nodeid = 14;
266         }));
267         /*
268          * access(2) is one of the few syscalls that will not (always) follow
269          * up a successful VOP_LOOKUP with another VOP.
270          */
271         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
272 }
273
274 // Lookup in a subdirectory of the fuse mount
275 TEST_F(Lookup, subdir)
276 {
277         const char FULLPATH[] = "mountpoint/some_dir/some_file.txt";
278         const char DIRPATH[] = "some_dir";
279         const char RELPATH[] = "some_file.txt";
280         uint64_t dir_ino = 2;
281         uint64_t file_ino = 3;
282
283         EXPECT_LOOKUP(1, DIRPATH).WillOnce(Invoke([=](auto in, auto out) {
284                 out->header.unique = in->header.unique;
285                 SET_OUT_HEADER_LEN(out, entry);
286                 out->body.entry.attr.mode = S_IFDIR | 0755;
287                 out->body.entry.nodeid = dir_ino;
288         }));
289         EXPECT_LOOKUP(dir_ino, RELPATH).WillOnce(Invoke([=](auto in, auto out) {
290                 out->header.unique = in->header.unique;
291                 SET_OUT_HEADER_LEN(out, entry);
292                 out->body.entry.attr.mode = S_IFREG | 0644;
293                 out->body.entry.nodeid = file_ino;
294         }));
295         /*
296          * access(2) is one of the few syscalls that will not (always) follow
297          * up a successful VOP_LOOKUP with another VOP.
298          */
299         ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
300 }
301
302