2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
36 #include "fuse_kernel.h"
39 #include <gmock/gmock.h>
41 #define TIME_T_MAX (std::numeric_limits<time_t>::max())
44 * A pseudo-fuse errno used indicate that a fuse operation should have no
45 * response, at least not immediately
47 #define FUSE_NORESPONSE 9999
49 #define SET_OUT_HEADER_LEN(out, variant) { \
50 (out).header.len = (sizeof((out).header) + \
51 sizeof((out).body.variant)); \
55 * Create an expectation on FUSE_LOOKUP and return it so the caller can set
58 * This must be a macro instead of a method because EXPECT_CALL returns a type
59 * with a deleted constructor.
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); \
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;
79 /* Protocol 7.8 version of struct fuse_attr */
98 /* Protocol 7.8 version of struct fuse_attr_out */
99 struct fuse_attr_out_7_8
102 __u32 attr_valid_nsec;
104 struct fuse_attr_7_8 attr;
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;
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;
125 union fuse_payloads_in {
126 fuse_access_in access;
128 /* value is from fuse_kern_chan.c in fusefs-libs */
129 uint8_t bytes[0x21000 - sizeof(struct fuse_in_header)];
130 fuse_create_in create;
133 fuse_fsync_in fsyncdir;
134 fuse_forget_in forget;
135 fuse_interrupt_in interrupt;
137 fuse_getxattr_in getxattr;
140 fuse_listxattr_in listxattr;
145 fuse_open_in opendir;
147 fuse_read_in readdir;
148 fuse_release_in release;
149 fuse_release_in releasedir;
150 fuse_rename_in rename;
152 fuse_setattr_in setattr;
153 fuse_setxattr_in setxattr;
160 struct mockfs_buf_in {
161 fuse_in_header header;
162 union fuse_payloads_in body;
165 union fuse_payloads_out {
167 fuse_attr_out_7_8 attr_7_8;
169 fuse_create_out create;
170 fuse_create_out_7_8 create_7_8;
172 * The protocol places no limits on the size of bytes. Choose
173 * a size big enough for anything we'll test.
175 uint8_t bytes[0x20000];
176 fuse_entry_out entry;
177 fuse_entry_out_7_8 entry_7_8;
179 fuse_getxattr_out getxattr;
181 /* The inval_entry structure should be followed by the entry's name */
182 fuse_notify_inval_entry_out inval_entry;
183 fuse_notify_inval_inode_out inval_inode;
184 fuse_listxattr_out listxattr;
186 fuse_statfs_out statfs;
188 * The protocol places no limits on the length of the string. This is
189 * merely convenient for testing.
192 fuse_write_out write;
195 struct mockfs_buf_out {
196 fuse_out_header header;
197 union fuse_payloads_out body;
199 /* Default constructor: zero everything */
201 memset(this, 0, sizeof(*this));
205 /* A function that can be invoked in place of MockFS::process */
206 typedef std::function<void (const mockfs_buf_in& in,
207 std::vector<std::unique_ptr<mockfs_buf_out>> &out)>
211 * Helper function used for setting an error expectation for any fuse operation.
212 * The operation will return the supplied error
214 ProcessMockerT ReturnErrno(int error);
216 /* Helper function used for returning negative cache entries for LOOKUP */
217 ProcessMockerT ReturnNegativeCache(const struct timespec *entry_valid);
219 /* Helper function used for returning a single immediate response */
220 ProcessMockerT ReturnImmediate(
221 std::function<void(const mockfs_buf_in& in,
222 struct mockfs_buf_out &out)> f);
224 /* How the daemon should check /dev/fuse for readiness */
233 * Fake FUSE filesystem
235 * "Mounts" a filesystem to a temporary directory and services requests
236 * according to the programmed expectations.
238 * Operates directly on the fusefs(4) kernel API, not the libfuse(3) user api.
242 * thread id of the fuse daemon thread
244 * It must run in a separate thread so it doesn't deadlock with the
247 pthread_t m_daemon_id;
249 /* file descriptor of /dev/fuse control device */
252 /* The minor version of the kernel API that this mock daemon targets */
253 uint32_t m_kernel_minor_version;
257 /* The max_readahead file system option */
258 uint32_t m_maxreadahead;
260 /* pid of the test process */
263 /* Method the daemon should use for I/O to and from /dev/fuse */
264 enum poll_method m_pm;
266 void debug_request(const mockfs_buf_in&);
267 void debug_response(const mockfs_buf_out&);
269 /* Initialize a session after mounting */
270 void init(uint32_t flags);
272 /* Is pid from a process that might be involved in the test? */
273 bool pid_ok(pid_t pid);
275 /* Default request handler */
276 void process_default(const mockfs_buf_in&,
277 std::vector<std::unique_ptr<mockfs_buf_out>>&);
279 /* Entry point for the daemon thread */
280 static void* service(void*);
282 /* Read, but do not process, a single request from the kernel */
283 void read_request(mockfs_buf_in& in);
285 /* Write a single response back to the kernel */
286 void write_response(const mockfs_buf_out &out);
289 /* pid of child process, for two-process test cases */
292 /* Maximum size of a FUSE_WRITE write */
296 * Number of events that were available from /dev/fuse after the last
297 * kevent call. Only valid when m_pm = KQ.
301 /* Tell the daemon to shut down ASAP */
304 /* Create a new mockfs and mount it to a tempdir */
305 MockFS(int max_readahead, bool allow_other,
306 bool default_permissions, bool push_symlinks_in, bool ro,
307 enum poll_method pm, uint32_t flags,
308 uint32_t kernel_minor_version, uint32_t max_write, bool async,
313 /* Kill the filesystem daemon without unmounting the filesystem */
316 /* Process FUSE requests endlessly */
320 * Send an asynchronous notification to invalidate a directory entry.
321 * Similar to libfuse's fuse_lowlevel_notify_inval_entry
323 * This method will block until the client has responded, so it should
324 * generally be run in a separate thread from request processing.
326 * @param parent Parent directory's inode number
327 * @param name name of dirent to invalidate
328 * @param namelen size of name, including the NUL
330 int notify_inval_entry(ino_t parent, const char *name, size_t namelen);
333 * Send an asynchronous notification to invalidate an inode's cached
334 * data and/or attributes. Similar to libfuse's
335 * fuse_lowlevel_notify_inval_inode.
337 * This method will block until the client has responded, so it should
338 * generally be run in a separate thread from request processing.
340 * @param ino File's inode number
341 * @param off offset at which to begin invalidation. A
342 * negative offset means to invalidate attributes
344 * @param len Size of region of data to invalidate. 0 means
345 * to invalidate all cached data.
347 int notify_inval_inode(ino_t ino, off_t off, ssize_t len);
352 * This method is expected to provide the responses to each FUSE
353 * operation. For an immediate response, push one buffer into out.
354 * For a delayed response, push nothing. For an immediate response
355 * plus a delayed response to an earlier operation, push two bufs.
356 * Test cases must define each response using Googlemock expectations
358 MOCK_METHOD2(process, void(const mockfs_buf_in&,
359 std::vector<std::unique_ptr<mockfs_buf_out>>&));
361 /* Gracefully unmount */