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;
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;
132 fuse_fsync_in fsyncdir;
133 fuse_forget_in forget;
134 fuse_interrupt_in interrupt;
136 fuse_getxattr_in getxattr;
139 fuse_listxattr_in listxattr;
144 fuse_open_in opendir;
146 fuse_read_in readdir;
147 fuse_release_in release;
148 fuse_release_in releasedir;
149 fuse_rename_in rename;
151 fuse_setattr_in setattr;
152 fuse_setxattr_in setxattr;
159 struct mockfs_buf_in {
160 fuse_in_header header;
161 union fuse_payloads_in body;
164 union fuse_payloads_out {
166 fuse_attr_out_7_8 attr_7_8;
167 fuse_create_out create;
168 fuse_create_out_7_8 create_7_8;
170 * The protocol places no limits on the size of bytes. Choose
171 * a size big enough for anything we'll test.
173 uint8_t bytes[0x20000];
174 fuse_entry_out entry;
175 fuse_entry_out_7_8 entry_7_8;
177 fuse_getxattr_out getxattr;
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;
184 fuse_statfs_out statfs;
186 * The protocol places no limits on the length of the string. This is
187 * merely convenient for testing.
190 fuse_write_out write;
193 struct mockfs_buf_out {
194 fuse_out_header header;
195 union fuse_payloads_out body;
197 /* Default constructor: zero everything */
199 memset(this, 0, sizeof(*this));
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)>
209 * Helper function used for setting an error expectation for any fuse operation.
210 * The operation will return the supplied error
212 ProcessMockerT ReturnErrno(int error);
214 /* Helper function used for returning negative cache entries for LOOKUP */
215 ProcessMockerT ReturnNegativeCache(const struct timespec *entry_valid);
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);
222 /* How the daemon should check /dev/fuse for readiness */
231 * Fake FUSE filesystem
233 * "Mounts" a filesystem to a temporary directory and services requests
234 * according to the programmed expectations.
236 * Operates directly on the fusefs(4) kernel API, not the libfuse(3) user api.
240 * thread id of the fuse daemon thread
242 * It must run in a separate thread so it doesn't deadlock with the
245 pthread_t m_daemon_id;
247 /* file descriptor of /dev/fuse control device */
250 /* The minor version of the kernel API that this mock daemon targets */
251 uint32_t m_kernel_minor_version;
255 /* The max_readahead file system option */
256 uint32_t m_maxreadahead;
258 /* pid of the test process */
261 /* Method the daemon should use for I/O to and from /dev/fuse */
262 enum poll_method m_pm;
264 void debug_request(const mockfs_buf_in&);
265 void debug_response(const mockfs_buf_out&);
267 /* Initialize a session after mounting */
268 void init(uint32_t flags);
270 /* Is pid from a process that might be involved in the test? */
271 bool pid_ok(pid_t pid);
273 /* Default request handler */
274 void process_default(const mockfs_buf_in&,
275 std::vector<std::unique_ptr<mockfs_buf_out>>&);
277 /* Entry point for the daemon thread */
278 static void* service(void*);
280 /* Read, but do not process, a single request from the kernel */
281 void read_request(mockfs_buf_in& in);
283 /* Write a single response back to the kernel */
284 void write_response(const mockfs_buf_out &out);
287 /* pid of child process, for two-process test cases */
290 /* Maximum size of a FUSE_WRITE write */
294 * Number of events that were available from /dev/fuse after the last
295 * kevent call. Only valid when m_pm = KQ.
299 /* Tell the daemon to shut down ASAP */
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);
310 /* Kill the filesystem daemon without unmounting the filesystem */
313 /* Process FUSE requests endlessly */
317 * Send an asynchronous notification to invalidate a directory entry.
318 * Similar to libfuse's fuse_lowlevel_notify_inval_entry
320 * This method will block until the client has responded, so it should
321 * generally be run in a separate thread from request processing.
323 * @param parent Parent directory's inode number
324 * @param name name of dirent to invalidate
325 * @param namelen size of name, including the NUL
327 int notify_inval_entry(ino_t parent, const char *name, size_t namelen);
330 * Send an asynchronous notification to invalidate an inode's cached
331 * data and/or attributes. Similar to libfuse's
332 * fuse_lowlevel_notify_inval_inode.
334 * This method will block until the client has responded, so it should
335 * generally be run in a separate thread from request processing.
337 * @param ino File's inode number
338 * @param off offset at which to begin invalidation. A
339 * negative offset means to invalidate attributes
341 * @param len Size of region of data to invalidate. 0 means
342 * to invalidate all cached data.
344 int notify_inval_inode(ino_t ino, off_t off, ssize_t len);
349 * This method is expected to provide the responses to each FUSE
350 * operation. For an immediate response, push one buffer into out.
351 * For a delayed response, push nothing. For an immediate response
352 * plus a delayed response to an earlier operation, push two bufs.
353 * Test cases must define each response using Googlemock expectations
355 MOCK_METHOD2(process, void(const mockfs_buf_in&,
356 std::vector<std::unique_ptr<mockfs_buf_out>>&));
358 /* Gracefully unmount */