xref: /linux/tools/testing/selftests/memfd/fuse_mnt.c (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
287b2d440SDavid Herrmann /*
387b2d440SDavid Herrmann  * memfd test file-system
487b2d440SDavid Herrmann  * This file uses FUSE to create a dummy file-system with only one file /memfd.
587b2d440SDavid Herrmann  * This file is read-only and takes 1s per read.
687b2d440SDavid Herrmann  *
787b2d440SDavid Herrmann  * This file-system is used by the memfd test-cases to force the kernel to pin
887b2d440SDavid Herrmann  * pages during reads(). Due to the 1s delay of this file-system, this is a
987b2d440SDavid Herrmann  * nice way to test race-conditions against get_user_pages() in the kernel.
1087b2d440SDavid Herrmann  *
1187b2d440SDavid Herrmann  * We use direct_io==1 to force the kernel to use direct-IO for this
1287b2d440SDavid Herrmann  * file-system.
1387b2d440SDavid Herrmann  */
1487b2d440SDavid Herrmann 
1587b2d440SDavid Herrmann #define FUSE_USE_VERSION 26
1687b2d440SDavid Herrmann 
1787b2d440SDavid Herrmann #include <fuse.h>
1887b2d440SDavid Herrmann #include <stdio.h>
1987b2d440SDavid Herrmann #include <string.h>
2087b2d440SDavid Herrmann #include <errno.h>
2187b2d440SDavid Herrmann #include <fcntl.h>
2287b2d440SDavid Herrmann #include <unistd.h>
2387b2d440SDavid Herrmann 
2487b2d440SDavid Herrmann static const char memfd_content[] = "memfd-example-content";
2587b2d440SDavid Herrmann static const char memfd_path[] = "/memfd";
2687b2d440SDavid Herrmann 
memfd_getattr(const char * path,struct stat * st)2787b2d440SDavid Herrmann static int memfd_getattr(const char *path, struct stat *st)
2887b2d440SDavid Herrmann {
2987b2d440SDavid Herrmann 	memset(st, 0, sizeof(*st));
3087b2d440SDavid Herrmann 
3187b2d440SDavid Herrmann 	if (!strcmp(path, "/")) {
3287b2d440SDavid Herrmann 		st->st_mode = S_IFDIR | 0755;
3387b2d440SDavid Herrmann 		st->st_nlink = 2;
3487b2d440SDavid Herrmann 	} else if (!strcmp(path, memfd_path)) {
3587b2d440SDavid Herrmann 		st->st_mode = S_IFREG | 0444;
3687b2d440SDavid Herrmann 		st->st_nlink = 1;
3787b2d440SDavid Herrmann 		st->st_size = strlen(memfd_content);
3887b2d440SDavid Herrmann 	} else {
3987b2d440SDavid Herrmann 		return -ENOENT;
4087b2d440SDavid Herrmann 	}
4187b2d440SDavid Herrmann 
4287b2d440SDavid Herrmann 	return 0;
4387b2d440SDavid Herrmann }
4487b2d440SDavid Herrmann 
memfd_readdir(const char * path,void * buf,fuse_fill_dir_t filler,off_t offset,struct fuse_file_info * fi)4587b2d440SDavid Herrmann static int memfd_readdir(const char *path,
4687b2d440SDavid Herrmann 			 void *buf,
4787b2d440SDavid Herrmann 			 fuse_fill_dir_t filler,
4887b2d440SDavid Herrmann 			 off_t offset,
4987b2d440SDavid Herrmann 			 struct fuse_file_info *fi)
5087b2d440SDavid Herrmann {
5187b2d440SDavid Herrmann 	if (strcmp(path, "/"))
5287b2d440SDavid Herrmann 		return -ENOENT;
5387b2d440SDavid Herrmann 
5487b2d440SDavid Herrmann 	filler(buf, ".", NULL, 0);
5587b2d440SDavid Herrmann 	filler(buf, "..", NULL, 0);
5687b2d440SDavid Herrmann 	filler(buf, memfd_path + 1, NULL, 0);
5787b2d440SDavid Herrmann 
5887b2d440SDavid Herrmann 	return 0;
5987b2d440SDavid Herrmann }
6087b2d440SDavid Herrmann 
memfd_open(const char * path,struct fuse_file_info * fi)6187b2d440SDavid Herrmann static int memfd_open(const char *path, struct fuse_file_info *fi)
6287b2d440SDavid Herrmann {
6387b2d440SDavid Herrmann 	if (strcmp(path, memfd_path))
6487b2d440SDavid Herrmann 		return -ENOENT;
6587b2d440SDavid Herrmann 
6687b2d440SDavid Herrmann 	if ((fi->flags & 3) != O_RDONLY)
6787b2d440SDavid Herrmann 		return -EACCES;
6887b2d440SDavid Herrmann 
6987b2d440SDavid Herrmann 	/* force direct-IO */
7087b2d440SDavid Herrmann 	fi->direct_io = 1;
7187b2d440SDavid Herrmann 
7287b2d440SDavid Herrmann 	return 0;
7387b2d440SDavid Herrmann }
7487b2d440SDavid Herrmann 
memfd_read(const char * path,char * buf,size_t size,off_t offset,struct fuse_file_info * fi)7587b2d440SDavid Herrmann static int memfd_read(const char *path,
7687b2d440SDavid Herrmann 		      char *buf,
7787b2d440SDavid Herrmann 		      size_t size,
7887b2d440SDavid Herrmann 		      off_t offset,
7987b2d440SDavid Herrmann 		      struct fuse_file_info *fi)
8087b2d440SDavid Herrmann {
8187b2d440SDavid Herrmann 	size_t len;
8287b2d440SDavid Herrmann 
8387b2d440SDavid Herrmann 	if (strcmp(path, memfd_path) != 0)
8487b2d440SDavid Herrmann 		return -ENOENT;
8587b2d440SDavid Herrmann 
8687b2d440SDavid Herrmann 	sleep(1);
8787b2d440SDavid Herrmann 
8887b2d440SDavid Herrmann 	len = strlen(memfd_content);
8987b2d440SDavid Herrmann 	if (offset < len) {
9087b2d440SDavid Herrmann 		if (offset + size > len)
9187b2d440SDavid Herrmann 			size = len - offset;
9287b2d440SDavid Herrmann 
9387b2d440SDavid Herrmann 		memcpy(buf, memfd_content + offset, size);
9487b2d440SDavid Herrmann 	} else {
9587b2d440SDavid Herrmann 		size = 0;
9687b2d440SDavid Herrmann 	}
9787b2d440SDavid Herrmann 
9887b2d440SDavid Herrmann 	return size;
9987b2d440SDavid Herrmann }
10087b2d440SDavid Herrmann 
10187b2d440SDavid Herrmann static struct fuse_operations memfd_ops = {
10287b2d440SDavid Herrmann 	.getattr	= memfd_getattr,
10387b2d440SDavid Herrmann 	.readdir	= memfd_readdir,
10487b2d440SDavid Herrmann 	.open		= memfd_open,
10587b2d440SDavid Herrmann 	.read		= memfd_read,
10687b2d440SDavid Herrmann };
10787b2d440SDavid Herrmann 
main(int argc,char * argv[])10887b2d440SDavid Herrmann int main(int argc, char *argv[])
10987b2d440SDavid Herrmann {
11087b2d440SDavid Herrmann 	return fuse_main(argc, argv, &memfd_ops, NULL);
11187b2d440SDavid Herrmann }
112