1 // SPDX-License-Identifier: GPL-2.0
3 * memfd test file-system
4 * This file uses FUSE to create a dummy file-system with only one file /memfd.
5 * This file is read-only and takes 1s per read.
7 * This file-system is used by the memfd test-cases to force the kernel to pin
8 * pages during reads(). Due to the 1s delay of this file-system, this is a
9 * nice way to test race-conditions against get_user_pages() in the kernel.
11 * We use direct_io==1 to force the kernel to use direct-IO for this
15 #define FUSE_USE_VERSION 26
24 static const char memfd_content[] = "memfd-example-content";
25 static const char memfd_path[] = "/memfd";
27 static int memfd_getattr(const char *path, struct stat *st)
29 memset(st, 0, sizeof(*st));
31 if (!strcmp(path, "/")) {
32 st->st_mode = S_IFDIR | 0755;
34 } else if (!strcmp(path, memfd_path)) {
35 st->st_mode = S_IFREG | 0444;
37 st->st_size = strlen(memfd_content);
45 static int memfd_readdir(const char *path,
47 fuse_fill_dir_t filler,
49 struct fuse_file_info *fi)
51 if (strcmp(path, "/"))
54 filler(buf, ".", NULL, 0);
55 filler(buf, "..", NULL, 0);
56 filler(buf, memfd_path + 1, NULL, 0);
61 static int memfd_open(const char *path, struct fuse_file_info *fi)
63 if (strcmp(path, memfd_path))
66 if ((fi->flags & 3) != O_RDONLY)
75 static int memfd_read(const char *path,
79 struct fuse_file_info *fi)
83 if (strcmp(path, memfd_path) != 0)
88 len = strlen(memfd_content);
90 if (offset + size > len)
93 memcpy(buf, memfd_content + offset, size);
101 static struct fuse_operations memfd_ops = {
102 .getattr = memfd_getattr,
103 .readdir = memfd_readdir,
108 int main(int argc, char *argv[])
110 return fuse_main(argc, argv, &memfd_ops, NULL);