]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/mockfs.hh
fusefs: use cluster_read for more readahead
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / mockfs.hh
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/types.h>
33
34 #include <pthread.h>
35
36 #include "fuse_kernel.h"
37 }
38
39 #include <gmock/gmock.h>
40
41 #define TIME_T_MAX (std::numeric_limits<time_t>::max())
42
43 /* 
44  * A pseudo-fuse errno used indicate that a fuse operation should have no
45  * response, at least not immediately
46  */
47 #define FUSE_NORESPONSE 9999
48
49 #define SET_OUT_HEADER_LEN(out, variant) { \
50         (out).header.len = (sizeof((out).header) + \
51                             sizeof((out).body.variant)); \
52 }
53
54 /*
55  * Create an expectation on FUSE_LOOKUP and return it so the caller can set
56  * actions.
57  *
58  * This must be a macro instead of a method because EXPECT_CALL returns a type
59  * with a deleted constructor.
60  */
61 #define EXPECT_LOOKUP(parent, path)                                     \
62         EXPECT_CALL(*m_mock, process(                                   \
63                 ResultOf([=](auto in) {                                 \
64                         return (in.header.opcode == FUSE_LOOKUP &&      \
65                                 in.header.nodeid == (parent) && \
66                                 strcmp(in.body.lookup, (path)) == 0);   \
67                 }, Eq(true)),                                           \
68                 _)                                                      \
69         )
70
71 extern int verbosity;
72
73 /* This struct isn't defined by fuse_kernel.h or libfuse, but it should be */
74 struct fuse_create_out {
75         struct fuse_entry_out   entry;
76         struct fuse_open_out    open;
77 };
78
79 /* Protocol 7.8 version of struct fuse_attr */
80 struct fuse_attr_7_8
81 {
82         __u64   ino;
83         __u64   size;
84         __u64   blocks;
85         __u64   atime;
86         __u64   mtime;
87         __u64   ctime;
88         __u32   atimensec;
89         __u32   mtimensec;
90         __u32   ctimensec;
91         __u32   mode;
92         __u32   nlink;
93         __u32   uid;
94         __u32   gid;
95         __u32   rdev;
96 };
97
98 /* Protocol 7.8 version of struct fuse_attr_out */
99 struct fuse_attr_out_7_8
100 {
101         __u64   attr_valid;
102         __u32   attr_valid_nsec;
103         __u32   dummy;
104         struct fuse_attr_7_8 attr;
105 };
106
107 /* Protocol 7.8 version of struct fuse_entry_out */
108 struct fuse_entry_out_7_8 {
109         __u64   nodeid;         /* Inode ID */
110         __u64   generation;     /* Inode generation: nodeid:gen must
111                                    be unique for the fs's lifetime */
112         __u64   entry_valid;    /* Cache timeout for the name */
113         __u64   attr_valid;     /* Cache timeout for the attributes */
114         __u32   entry_valid_nsec;
115         __u32   attr_valid_nsec;
116         struct fuse_attr_7_8 attr;
117 };
118
119 /* Output struct for FUSE_CREATE for protocol 7.8 servers */
120 struct fuse_create_out_7_8 {
121         struct fuse_entry_out_7_8       entry;
122         struct fuse_open_out    open;
123 };
124
125 union fuse_payloads_in {
126         fuse_access_in  access;
127         /* value is from fuse_kern_chan.c in fusefs-libs */
128         uint8_t         bytes[0x21000 - sizeof(struct fuse_in_header)];
129         fuse_create_in  create;
130         fuse_flush_in   flush;
131         fuse_fsync_in   fsync;
132         fuse_fsync_in   fsyncdir;
133         fuse_forget_in  forget;
134         fuse_interrupt_in interrupt;
135         fuse_lk_in      getlk;
136         fuse_getxattr_in getxattr;
137         fuse_init_in    init;
138         fuse_link_in    link;
139         fuse_listxattr_in listxattr;
140         char            lookup[0];
141         fuse_mkdir_in   mkdir;
142         fuse_mknod_in   mknod;
143         fuse_open_in    open;
144         fuse_open_in    opendir;
145         fuse_read_in    read;
146         fuse_read_in    readdir;
147         fuse_release_in release;
148         fuse_release_in releasedir;
149         fuse_rename_in  rename;
150         char            rmdir[0];
151         fuse_setattr_in setattr;
152         fuse_setxattr_in setxattr;
153         fuse_lk_in      setlk;
154         fuse_lk_in      setlkw;
155         char            unlink[0];
156         fuse_write_in   write;
157 };
158
159 struct mockfs_buf_in {
160         fuse_in_header          header;
161         union fuse_payloads_in  body;
162 };
163
164 union fuse_payloads_out {
165         fuse_attr_out           attr;
166         fuse_attr_out_7_8       attr_7_8;
167         fuse_create_out         create;
168         fuse_create_out_7_8     create_7_8;
169         /*
170          * The protocol places no limits on the size of bytes.  Choose
171          * a size big enough for anything we'll test.
172          */
173         uint8_t                 bytes[0x20000];
174         fuse_entry_out          entry;
175         fuse_entry_out_7_8      entry_7_8;
176         fuse_lk_out             getlk;
177         fuse_getxattr_out       getxattr;
178         fuse_init_out           init;
179         /* The inval_entry structure should be followed by the entry's name */
180         fuse_notify_inval_entry_out     inval_entry;
181         fuse_notify_inval_inode_out     inval_inode;
182         fuse_listxattr_out      listxattr;
183         fuse_open_out           open;
184         fuse_statfs_out         statfs;
185         /*
186          * The protocol places no limits on the length of the string.  This is
187          * merely convenient for testing.
188          */
189         char                    str[80];
190         fuse_write_out          write;
191 };
192
193 struct mockfs_buf_out {
194         fuse_out_header         header;
195         union fuse_payloads_out body;
196
197         /* Default constructor: zero everything */
198         mockfs_buf_out() {
199                 memset(this, 0, sizeof(*this));
200         }
201 };
202
203 /* A function that can be invoked in place of MockFS::process */
204 typedef std::function<void (const mockfs_buf_in& in,
205                             std::vector<std::unique_ptr<mockfs_buf_out>> &out)>
206 ProcessMockerT;
207
208 /*
209  * Helper function used for setting an error expectation for any fuse operation.
210  * The operation will return the supplied error
211  */
212 ProcessMockerT ReturnErrno(int error);
213
214 /* Helper function used for returning negative cache entries for LOOKUP */
215 ProcessMockerT ReturnNegativeCache(const struct timespec *entry_valid);
216
217 /* Helper function used for returning a single immediate response */
218 ProcessMockerT ReturnImmediate(
219         std::function<void(const mockfs_buf_in& in,
220                            struct mockfs_buf_out &out)> f);
221
222 /* How the daemon should check /dev/fuse for readiness */
223 enum poll_method {
224         BLOCKING,
225         SELECT,
226         POLL,
227         KQ
228 };
229
230 /*
231  * Fake FUSE filesystem
232  *
233  * "Mounts" a filesystem to a temporary directory and services requests
234  * according to the programmed expectations.
235  *
236  * Operates directly on the fusefs(4) kernel API, not the libfuse(3) user api.
237  */
238 class MockFS {
239         /*
240          * thread id of the fuse daemon thread
241          *
242          * It must run in a separate thread so it doesn't deadlock with the
243          * client test code.
244          */
245         pthread_t m_daemon_id;
246
247         /* file descriptor of /dev/fuse control device */
248         int m_fuse_fd;
249         
250         /* The minor version of the kernel API that this mock daemon targets */
251         uint32_t m_kernel_minor_version;
252
253         int m_kq;
254
255         /* The max_readahead file system option */
256         uint32_t m_maxreadahead;
257
258         /* pid of the test process */
259         pid_t m_pid;
260
261         /* Method the daemon should use for I/O to and from /dev/fuse */
262         enum poll_method m_pm;
263
264         void debug_request(const mockfs_buf_in&);
265         void debug_response(const mockfs_buf_out&);
266
267         /* Initialize a session after mounting */
268         void init(uint32_t flags);
269
270         /* Is pid from a process that might be involved in the test? */
271         bool pid_ok(pid_t pid);
272
273         /* Default request handler */
274         void process_default(const mockfs_buf_in&,
275                 std::vector<std::unique_ptr<mockfs_buf_out>>&);
276
277         /* Entry point for the daemon thread */
278         static void* service(void*);
279
280         /* Read, but do not process, a single request from the kernel */
281         void read_request(mockfs_buf_in& in);
282
283         /* Write a single response back to the kernel */
284         void write_response(const mockfs_buf_out &out);
285
286         public:
287         /* pid of child process, for two-process test cases */
288         pid_t m_child_pid;
289
290         /* Maximum size of a FUSE_WRITE write */
291         uint32_t m_maxwrite;
292
293         /* 
294          * Number of events that were available from /dev/fuse after the last
295          * kevent call.  Only valid when m_pm = KQ.
296          */
297         int m_nready;
298
299         /* Tell the daemon to shut down ASAP */
300         bool m_quit;
301
302         /* Create a new mockfs and mount it to a tempdir */
303         MockFS(int max_readahead, bool allow_other,
304                 bool default_permissions, bool push_symlinks_in, bool ro,
305                 enum poll_method pm, uint32_t flags,
306                 uint32_t kernel_minor_version, uint32_t max_write, bool async,
307                 bool no_clusterr);
308
309         virtual ~MockFS();
310
311         /* Kill the filesystem daemon without unmounting the filesystem */
312         void kill_daemon();
313
314         /* Process FUSE requests endlessly */
315         void loop();
316
317         /*
318          * Send an asynchronous notification to invalidate a directory entry.
319          * Similar to libfuse's fuse_lowlevel_notify_inval_entry
320          *
321          * This method will block until the client has responded, so it should
322          * generally be run in a separate thread from request processing.
323          *
324          * @param       parent  Parent directory's inode number
325          * @param       name    name of dirent to invalidate
326          * @param       namelen size of name, including the NUL
327          */
328         int notify_inval_entry(ino_t parent, const char *name, size_t namelen);
329
330         /*
331          * Send an asynchronous notification to invalidate an inode's cached
332          * data and/or attributes.  Similar to libfuse's
333          * fuse_lowlevel_notify_inval_inode.
334          *
335          * This method will block until the client has responded, so it should
336          * generally be run in a separate thread from request processing.
337          *
338          * @param       ino     File's inode number
339          * @param       off     offset at which to begin invalidation.  A
340          *                      negative offset means to invalidate attributes
341          *                      only.
342          * @param       len     Size of region of data to invalidate.  0 means
343          *                      to invalidate all cached data.
344          */
345         int notify_inval_inode(ino_t ino, off_t off, ssize_t len);
346
347         /* 
348          * Request handler
349          *
350          * This method is expected to provide the responses to each FUSE
351          * operation.  For an immediate response, push one buffer into out.
352          * For a delayed response, push nothing.  For an immediate response
353          * plus a delayed response to an earlier operation, push two bufs.
354          * Test cases must define each response using Googlemock expectations
355          */
356         MOCK_METHOD2(process, void(const mockfs_buf_in&,
357                                 std::vector<std::unique_ptr<mockfs_buf_out>>&));
358
359         /* Gracefully unmount */
360         void unmount();
361 };