1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * xenfs.c - a filesystem for passing info between the a domain and
4*4882a593Smuzhiyun * the hypervisor.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
7*4882a593Smuzhiyun * and /proc/xen compatibility mount point.
8*4882a593Smuzhiyun * Turned xenfs into a loadable module.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/fs.h>
17*4882a593Smuzhiyun #include <linux/fs_context.h>
18*4882a593Smuzhiyun #include <linux/magic.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <xen/xen.h>
21*4882a593Smuzhiyun #include <xen/xenbus.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "xenfs.h"
24*4882a593Smuzhiyun #include "../privcmd.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <asm/xen/hypervisor.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun MODULE_DESCRIPTION("Xen filesystem");
29*4882a593Smuzhiyun MODULE_LICENSE("GPL");
30*4882a593Smuzhiyun
capabilities_read(struct file * file,char __user * buf,size_t size,loff_t * off)31*4882a593Smuzhiyun static ssize_t capabilities_read(struct file *file, char __user *buf,
32*4882a593Smuzhiyun size_t size, loff_t *off)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun char *tmp = "";
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun if (xen_initial_domain())
37*4882a593Smuzhiyun tmp = "control_d\n";
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp));
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static const struct file_operations capabilities_file_ops = {
43*4882a593Smuzhiyun .read = capabilities_read,
44*4882a593Smuzhiyun .llseek = default_llseek,
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
xenfs_fill_super(struct super_block * sb,struct fs_context * fc)47*4882a593Smuzhiyun static int xenfs_fill_super(struct super_block *sb, struct fs_context *fc)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun static const struct tree_descr xenfs_files[] = {
50*4882a593Smuzhiyun [2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
51*4882a593Smuzhiyun { "capabilities", &capabilities_file_ops, S_IRUGO },
52*4882a593Smuzhiyun { "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
53*4882a593Smuzhiyun {""},
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static const struct tree_descr xenfs_init_files[] = {
57*4882a593Smuzhiyun [2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
58*4882a593Smuzhiyun { "capabilities", &capabilities_file_ops, S_IRUGO },
59*4882a593Smuzhiyun { "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
60*4882a593Smuzhiyun { "xsd_kva", &xsd_kva_file_ops, S_IRUSR|S_IWUSR},
61*4882a593Smuzhiyun { "xsd_port", &xsd_port_file_ops, S_IRUSR|S_IWUSR},
62*4882a593Smuzhiyun #ifdef CONFIG_XEN_SYMS
63*4882a593Smuzhiyun { "xensyms", &xensyms_ops, S_IRUSR},
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun {""},
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun return simple_fill_super(sb, XENFS_SUPER_MAGIC,
69*4882a593Smuzhiyun xen_initial_domain() ? xenfs_init_files : xenfs_files);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
xenfs_get_tree(struct fs_context * fc)72*4882a593Smuzhiyun static int xenfs_get_tree(struct fs_context *fc)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun return get_tree_single(fc, xenfs_fill_super);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun static const struct fs_context_operations xenfs_context_ops = {
78*4882a593Smuzhiyun .get_tree = xenfs_get_tree,
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
xenfs_init_fs_context(struct fs_context * fc)81*4882a593Smuzhiyun static int xenfs_init_fs_context(struct fs_context *fc)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun fc->ops = &xenfs_context_ops;
84*4882a593Smuzhiyun return 0;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun static struct file_system_type xenfs_type = {
88*4882a593Smuzhiyun .owner = THIS_MODULE,
89*4882a593Smuzhiyun .name = "xenfs",
90*4882a593Smuzhiyun .init_fs_context = xenfs_init_fs_context,
91*4882a593Smuzhiyun .kill_sb = kill_litter_super,
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun MODULE_ALIAS_FS("xenfs");
94*4882a593Smuzhiyun
xenfs_init(void)95*4882a593Smuzhiyun static int __init xenfs_init(void)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun if (xen_domain())
98*4882a593Smuzhiyun return register_filesystem(&xenfs_type);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
xenfs_exit(void)103*4882a593Smuzhiyun static void __exit xenfs_exit(void)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun if (xen_domain())
106*4882a593Smuzhiyun unregister_filesystem(&xenfs_type);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun module_init(xenfs_init);
110*4882a593Smuzhiyun module_exit(xenfs_exit);
111*4882a593Smuzhiyun
112