]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/utils.cc
fusefs: use cluster_read for more readahead
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / utils.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 <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/wait.h>
37
38 #include <dirent.h>
39 #include <fcntl.h>
40 #include <grp.h>
41 #include <pwd.h>
42 #include <semaphore.h>
43 #include <unistd.h>
44 }
45
46 #include <gtest/gtest.h>
47
48 #include "mockfs.hh"
49 #include "utils.hh"
50
51 using namespace testing;
52
53 /*
54  * The default max_write is set to this formula in libfuse, though
55  * individual filesystems can lower it.  The "- 4096" was added in
56  * commit 154ffe2, with the commit message "fix".
57  */
58 const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096;
59
60 /* 
61  * Set the default max_write to a distinct value from MAXPHYS to catch bugs
62  * that confuse the two.
63  */
64 const uint32_t default_max_write = MIN(libfuse_max_write, MAXPHYS / 2);
65
66
67 /* Check that fusefs(4) is accessible and the current user can mount(2) */
68 void check_environment()
69 {
70         const char *devnode = "/dev/fuse";
71         const char *usermount_node = "vfs.usermount";
72         int usermount_val = 0;
73         size_t usermount_size = sizeof(usermount_val);
74         if (eaccess(devnode, R_OK | W_OK)) {
75                 if (errno == ENOENT) {
76                         GTEST_SKIP() << devnode << " does not exist";
77                 } else if (errno == EACCES) {
78                         GTEST_SKIP() << devnode <<
79                             " is not accessible by the current user";
80                 } else {
81                         GTEST_SKIP() << strerror(errno);
82                 }
83         }
84         sysctlbyname(usermount_node, &usermount_val, &usermount_size,
85                      NULL, 0);
86         if (geteuid() != 0 && !usermount_val)
87                 GTEST_SKIP() << "current user is not allowed to mount";
88 }
89
90 class FuseEnv: public Environment {
91         virtual void SetUp() {
92         }
93 };
94
95 void FuseTest::SetUp() {
96         const char *node = "vfs.maxbcachebuf";
97         int val = 0;
98         size_t size = sizeof(val);
99
100         /*
101          * XXX check_environment should be called from FuseEnv::SetUp, but
102          * can't due to https://github.com/google/googletest/issues/2189
103          */
104         check_environment();
105         if (IsSkipped())
106                 return;
107
108         ASSERT_EQ(0, sysctlbyname(node, &val, &size, NULL, 0))
109                 << strerror(errno);
110         m_maxbcachebuf = val;
111
112         try {
113                 m_mock = new MockFS(m_maxreadahead, m_allow_other,
114                         m_default_permissions, m_push_symlinks_in, m_ro,
115                         m_pm, m_init_flags, m_kernel_minor_version,
116                         m_maxwrite, m_async, m_noclusterr);
117                 /* 
118                  * FUSE_ACCESS is called almost universally.  Expecting it in
119                  * each test case would be super-annoying.  Instead, set a
120                  * default expectation for FUSE_ACCESS and return ENOSYS.
121                  *
122                  * Individual test cases can override this expectation since
123                  * googlemock evaluates expectations in LIFO order.
124                  */
125                 EXPECT_CALL(*m_mock, process(
126                         ResultOf([=](auto in) {
127                                 return (in.header.opcode == FUSE_ACCESS);
128                         }, Eq(true)),
129                         _)
130                 ).Times(AnyNumber())
131                 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
132         } catch (std::system_error err) {
133                 FAIL() << err.what();
134         }
135 }
136
137 void
138 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
139 {
140         EXPECT_CALL(*m_mock, process(
141                 ResultOf([=](auto in) {
142                         return (in.header.opcode == FUSE_ACCESS &&
143                                 in.header.nodeid == ino &&
144                                 in.body.access.mask == access_mode);
145                 }, Eq(true)),
146                 _)
147         ).WillOnce(Invoke(ReturnErrno(error)));
148 }
149
150 void
151 FuseTest::expect_destroy(int error)
152 {
153         EXPECT_CALL(*m_mock, process(
154                 ResultOf([=](auto in) {
155                         return (in.header.opcode == FUSE_DESTROY);
156                 }, Eq(true)),
157                 _)
158         ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) {
159                 m_mock->m_quit = true;
160                 out.header.len = sizeof(out.header);
161                 out.header.unique = in.header.unique;
162                 out.header.error = -error;
163         })));
164 }
165
166 void
167 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
168 {
169         EXPECT_CALL(*m_mock, process(
170                 ResultOf([=](auto in) {
171                         return (in.header.opcode == FUSE_FLUSH &&
172                                 in.header.nodeid == ino);
173                 }, Eq(true)),
174                 _)
175         ).Times(times)
176         .WillRepeatedly(Invoke(r));
177 }
178
179 void
180 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
181 {
182         EXPECT_CALL(*m_mock, process(
183                 ResultOf([=](auto in) {
184                         return (in.header.opcode == FUSE_FORGET &&
185                                 in.header.nodeid == ino &&
186                                 in.body.forget.nlookup == nlookup);
187                 }, Eq(true)),
188                 _)
189         ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
190                 if (sem != NULL)
191                         sem_post(sem);
192                 /* FUSE_FORGET has no response! */
193         }));
194 }
195
196 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
197 {
198         EXPECT_CALL(*m_mock, process(
199                 ResultOf([=](auto in) {
200                         return (in.header.opcode == FUSE_GETATTR &&
201                                 in.header.nodeid == ino);
202                 }, Eq(true)),
203                 _)
204         ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
205                 SET_OUT_HEADER_LEN(out, attr);
206                 out.body.attr.attr.ino = ino;   // Must match nodeid
207                 out.body.attr.attr.mode = S_IFREG | 0644;
208                 out.body.attr.attr.size = size;
209                 out.body.attr.attr_valid = UINT64_MAX;
210         })));
211 }
212
213 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
214         uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
215 {
216         EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
217         .Times(times)
218         .WillRepeatedly(Invoke(
219                 ReturnImmediate([=](auto in __unused, auto& out) {
220                 SET_OUT_HEADER_LEN(out, entry);
221                 out.body.entry.attr.mode = mode;
222                 out.body.entry.nodeid = ino;
223                 out.body.entry.attr.nlink = 1;
224                 out.body.entry.attr_valid = attr_valid;
225                 out.body.entry.attr.size = size;
226                 out.body.entry.attr.uid = uid;
227                 out.body.entry.attr.gid = gid;
228         })));
229 }
230
231 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
232         uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
233 {
234         EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
235         .Times(times)
236         .WillRepeatedly(Invoke(
237                 ReturnImmediate([=](auto in __unused, auto& out) {
238                 SET_OUT_HEADER_LEN(out, entry_7_8);
239                 out.body.entry.attr.mode = mode;
240                 out.body.entry.nodeid = ino;
241                 out.body.entry.attr.nlink = 1;
242                 out.body.entry.attr_valid = attr_valid;
243                 out.body.entry.attr.size = size;
244                 out.body.entry.attr.uid = uid;
245                 out.body.entry.attr.gid = gid;
246         })));
247 }
248
249 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
250 {
251         EXPECT_CALL(*m_mock, process(
252                 ResultOf([=](auto in) {
253                         return (in.header.opcode == FUSE_OPEN &&
254                                 in.header.nodeid == ino);
255                 }, Eq(true)),
256                 _)
257         ).Times(times)
258         .WillRepeatedly(Invoke(
259                 ReturnImmediate([=](auto in __unused, auto& out) {
260                 out.header.len = sizeof(out.header);
261                 SET_OUT_HEADER_LEN(out, open);
262                 out.body.open.fh = FH;
263                 out.body.open.open_flags = flags;
264         })));
265 }
266
267 void FuseTest::expect_opendir(uint64_t ino)
268 {
269         /* opendir(3) calls fstatfs */
270         EXPECT_CALL(*m_mock, process(
271                 ResultOf([](auto in) {
272                         return (in.header.opcode == FUSE_STATFS);
273                 }, Eq(true)),
274                 _)
275         ).WillRepeatedly(Invoke(
276         ReturnImmediate([=](auto i __unused, auto& out) {
277                 SET_OUT_HEADER_LEN(out, statfs);
278         })));
279
280         EXPECT_CALL(*m_mock, process(
281                 ResultOf([=](auto in) {
282                         return (in.header.opcode == FUSE_OPENDIR &&
283                                 in.header.nodeid == ino);
284                 }, Eq(true)),
285                 _)
286         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
287                 out.header.len = sizeof(out.header);
288                 SET_OUT_HEADER_LEN(out, open);
289                 out.body.open.fh = FH;
290         })));
291 }
292
293 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
294         uint64_t osize, const void *contents, int flags)
295 {
296         EXPECT_CALL(*m_mock, process(
297                 ResultOf([=](auto in) {
298                         return (in.header.opcode == FUSE_READ &&
299                                 in.header.nodeid == ino &&
300                                 in.body.read.fh == FH &&
301                                 in.body.read.offset == offset &&
302                                 in.body.read.size == isize &&
303                                 flags == -1 ?
304                                         (in.body.read.flags == O_RDONLY ||
305                                          in.body.read.flags == O_RDWR)
306                                 : in.body.read.flags == (uint32_t)flags);
307                 }, Eq(true)),
308                 _)
309         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
310                 out.header.len = sizeof(struct fuse_out_header) + osize;
311                 memmove(out.body.bytes, contents, osize);
312         }))).RetiresOnSaturation();
313 }
314
315 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
316         std::vector<struct dirent> &ents)
317 {
318         EXPECT_CALL(*m_mock, process(
319                 ResultOf([=](auto in) {
320                         return (in.header.opcode == FUSE_READDIR &&
321                                 in.header.nodeid == ino &&
322                                 in.body.readdir.fh == FH &&
323                                 in.body.readdir.offset == off);
324                 }, Eq(true)),
325                 _)
326         ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
327                 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
328                 int i = 0;
329
330                 out.header.error = 0;
331                 out.header.len = 0;
332
333                 for (const auto& it: ents) {
334                         size_t entlen, entsize;
335
336                         fde->ino = it.d_fileno;
337                         fde->off = it.d_off;
338                         fde->type = it.d_type;
339                         fde->namelen = it.d_namlen;
340                         strncpy(fde->name, it.d_name, it.d_namlen);
341                         entlen = FUSE_NAME_OFFSET + fde->namelen;
342                         entsize = FUSE_DIRENT_SIZE(fde);
343                         /* 
344                          * The FUSE protocol does not require zeroing out the
345                          * unused portion of the name.  But it's a good
346                          * practice to prevent information disclosure to the
347                          * FUSE client, even though the client is usually the
348                          * kernel
349                          */
350                         memset(fde->name + fde->namelen, 0, entsize - entlen);
351                         if (out.header.len + entsize > in.body.read.size) {
352                                 printf("Overflow in readdir expectation: i=%d\n"
353                                         , i);
354                                 break;
355                         }
356                         out.header.len += entsize;
357                         fde = (struct fuse_dirent*)
358                                 ((intmax_t*)fde + entsize / sizeof(intmax_t));
359                         i++;
360                 }
361                 out.header.len += sizeof(out.header);
362         })));
363
364 }
365 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
366 {
367         EXPECT_CALL(*m_mock, process(
368                 ResultOf([=](auto in) {
369                         return (in.header.opcode == FUSE_RELEASE &&
370                                 in.header.nodeid == ino &&
371                                 in.body.release.fh == fh);
372                 }, Eq(true)),
373                 _)
374         ).WillOnce(Invoke(ReturnErrno(0)));
375 }
376
377 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
378 {
379         EXPECT_CALL(*m_mock, process(
380                 ResultOf([=](auto in) {
381                         return (in.header.opcode == FUSE_RELEASEDIR &&
382                                 in.header.nodeid == ino &&
383                                 in.body.release.fh == FH);
384                 }, Eq(true)),
385                 _)
386         ).WillOnce(Invoke(r));
387 }
388
389 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
390 {
391         EXPECT_CALL(*m_mock, process(
392                 ResultOf([=](auto in) {
393                         return (in.header.opcode == FUSE_UNLINK &&
394                                 0 == strcmp(path, in.body.unlink) &&
395                                 in.header.nodeid == parent);
396                 }, Eq(true)),
397                 _)
398         ).WillOnce(Invoke(ReturnErrno(error)));
399 }
400
401 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
402         uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
403         const void *contents)
404 {
405         EXPECT_CALL(*m_mock, process(
406                 ResultOf([=](auto in) {
407                         const char *buf = (const char*)in.body.bytes +
408                                 sizeof(struct fuse_write_in);
409                         bool pid_ok;
410                         uint32_t wf = in.body.write.write_flags;
411
412                         if (wf & FUSE_WRITE_CACHE)
413                                 pid_ok = true;
414                         else
415                                 pid_ok = (pid_t)in.header.pid == getpid();
416
417                         return (in.header.opcode == FUSE_WRITE &&
418                                 in.header.nodeid == ino &&
419                                 in.body.write.fh == FH &&
420                                 in.body.write.offset == offset  &&
421                                 in.body.write.size == isize &&
422                                 pid_ok &&
423                                 (wf & flags_set) == flags_set &&
424                                 (wf & flags_unset) == 0 &&
425                                 (in.body.write.flags == O_WRONLY ||
426                                  in.body.write.flags == O_RDWR) &&
427                                 0 == bcmp(buf, contents, isize));
428                 }, Eq(true)),
429                 _)
430         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
431                 SET_OUT_HEADER_LEN(out, write);
432                 out.body.write.size = osize;
433         })));
434 }
435
436 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
437         uint64_t osize, const void *contents)
438 {
439         EXPECT_CALL(*m_mock, process(
440                 ResultOf([=](auto in) {
441                         const char *buf = (const char*)in.body.bytes +
442                                 FUSE_COMPAT_WRITE_IN_SIZE;
443                         bool pid_ok = (pid_t)in.header.pid == getpid();
444                         return (in.header.opcode == FUSE_WRITE &&
445                                 in.header.nodeid == ino &&
446                                 in.body.write.fh == FH &&
447                                 in.body.write.offset == offset  &&
448                                 in.body.write.size == isize &&
449                                 pid_ok &&
450                                 0 == bcmp(buf, contents, isize));
451                 }, Eq(true)),
452                 _)
453         ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
454                 SET_OUT_HEADER_LEN(out, write);
455                 out.body.write.size = osize;
456         })));
457 }
458
459 void
460 get_unprivileged_id(uid_t *uid, gid_t *gid)
461 {
462         struct passwd *pw;
463         struct group *gr;
464
465         /* 
466          * First try "tests", Kyua's default unprivileged user.  XXX after
467          * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
468          */
469         pw = getpwnam("tests");
470         if (pw == NULL) {
471                 /* Fall back to "nobody" */
472                 pw = getpwnam("nobody");
473         }
474         if (pw == NULL)
475                 GTEST_SKIP() << "Test requires an unprivileged user";
476         /* Use group "nobody", which is Kyua's default unprivileged group */
477         gr = getgrnam("nobody");
478         if (gr == NULL)
479                 GTEST_SKIP() << "Test requires an unprivileged group";
480         *uid = pw->pw_uid;
481         *gid = gr->gr_gid;
482 }
483
484 void
485 FuseTest::fork(bool drop_privs, int *child_status,
486         std::function<void()> parent_func,
487         std::function<int()> child_func)
488 {
489         sem_t *sem;
490         int mprot = PROT_READ | PROT_WRITE;
491         int mflags = MAP_ANON | MAP_SHARED;
492         pid_t child;
493         uid_t uid;
494         gid_t gid;
495         
496         if (drop_privs) {
497                 get_unprivileged_id(&uid, &gid);
498                 if (IsSkipped())
499                         return;
500         }
501
502         sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
503         ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
504         ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
505
506         if ((child = ::fork()) == 0) {
507                 /* In child */
508                 int err = 0;
509
510                 if (sem_wait(sem)) {
511                         perror("sem_wait");
512                         err = 1;
513                         goto out;
514                 }
515
516                 if (drop_privs && 0 != setegid(gid)) {
517                         perror("setegid");
518                         err = 1;
519                         goto out;
520                 }
521                 if (drop_privs && 0 != setreuid(-1, uid)) {
522                         perror("setreuid");
523                         err = 1;
524                         goto out;
525                 }
526                 err = child_func();
527
528 out:
529                 sem_destroy(sem);
530                 _exit(err);
531         } else if (child > 0) {
532                 /* 
533                  * In parent.  Cleanup must happen here, because it's still
534                  * privileged.
535                  */
536                 m_mock->m_child_pid = child;
537                 ASSERT_NO_FATAL_FAILURE(parent_func());
538
539                 /* Signal the child process to go */
540                 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
541
542                 ASSERT_LE(0, wait(child_status)) << strerror(errno);
543         } else {
544                 FAIL() << strerror(errno);
545         }
546         munmap(sem, sizeof(*sem));
547         return;
548 }
549
550 static void usage(char* progname) {
551         fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
552         exit(2);
553 }
554
555 int main(int argc, char **argv) {
556         int ch;
557         FuseEnv *fuse_env = new FuseEnv;
558
559         InitGoogleTest(&argc, argv);
560         AddGlobalTestEnvironment(fuse_env);
561
562         while ((ch = getopt(argc, argv, "v")) != -1) {
563                 switch (ch) {
564                         case 'v':
565                                 verbosity++;
566                                 break;
567                         default:
568                                 usage(argv[0]);
569                                 break;
570                 }
571         }
572
573         return (RUN_ALL_TESTS());
574 }