blob: 23f1cca5a2e9b7e65024cdc53e5d0ca250d352d3 [file] [log] [blame]
Alex Zeffertt1107ba82009-01-07 18:07:11 -08001/*
2 * xenfs.c - a filesystem for passing info between the a domain and
3 * the hypervisor.
4 *
5 * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
6 * and /proc/xen compatibility mount point.
7 * Turned xenfs into a loadable module.
8 */
9
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/magic.h>
Jeremy Fitzhardinge24a89b52009-02-09 12:05:49 -080015#include <linux/mm.h>
16#include <linux/backing-dev.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080017
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070018#include <xen/xen.h>
19
Alex Zeffertt1107ba82009-01-07 18:07:11 -080020#include "xenfs.h"
21
22#include <asm/xen/hypervisor.h>
23
24MODULE_DESCRIPTION("Xen filesystem");
25MODULE_LICENSE("GPL");
26
Jeremy Fitzhardinge24a89b52009-02-09 12:05:49 -080027static int xenfs_set_page_dirty(struct page *page)
28{
29 if (!PageDirty(page))
30 SetPageDirty(page);
31 return 0;
32}
33
34static const struct address_space_operations xenfs_aops = {
35 .set_page_dirty = xenfs_set_page_dirty,
36};
37
38static struct backing_dev_info xenfs_backing_dev_info = {
39 .ra_pages = 0, /* No readahead */
40 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
41};
42
Ian Campbell655d4062009-02-06 18:46:48 -080043static struct inode *xenfs_make_inode(struct super_block *sb, int mode)
44{
45 struct inode *ret = new_inode(sb);
46
47 if (ret) {
48 ret->i_mode = mode;
Jeremy Fitzhardinge24a89b52009-02-09 12:05:49 -080049 ret->i_mapping->a_ops = &xenfs_aops;
50 ret->i_mapping->backing_dev_info = &xenfs_backing_dev_info;
Ian Campbell655d4062009-02-06 18:46:48 -080051 ret->i_uid = ret->i_gid = 0;
52 ret->i_blocks = 0;
53 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
54 }
55 return ret;
56}
57
58static struct dentry *xenfs_create_file(struct super_block *sb,
59 struct dentry *parent,
60 const char *name,
61 const struct file_operations *fops,
62 void *data,
63 int mode)
64{
65 struct dentry *dentry;
66 struct inode *inode;
67
68 dentry = d_alloc_name(parent, name);
69 if (!dentry)
70 return NULL;
71
72 inode = xenfs_make_inode(sb, S_IFREG | mode);
73 if (!inode) {
74 dput(dentry);
75 return NULL;
76 }
77
78 inode->i_fop = fops;
79 inode->i_private = data;
80
81 d_add(dentry, inode);
82 return dentry;
83}
84
Jeremy Fitzhardinge818fd202009-02-06 18:46:47 -080085static ssize_t capabilities_read(struct file *file, char __user *buf,
86 size_t size, loff_t *off)
87{
88 char *tmp = "";
89
90 if (xen_initial_domain())
91 tmp = "control_d\n";
92
93 return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp));
94}
95
96static const struct file_operations capabilities_file_ops = {
97 .read = capabilities_read,
98};
99
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800100static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
101{
102 static struct tree_descr xenfs_files[] = {
Jeremy Fitzhardinge818fd202009-02-06 18:46:47 -0800103 [1] = {},
104 { "xenbus", &xenbus_file_ops, S_IRUSR|S_IWUSR },
105 { "capabilities", &capabilities_file_ops, S_IRUGO },
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800106 {""},
107 };
Ian Campbell655d4062009-02-06 18:46:48 -0800108 int rc;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800109
Ian Campbell655d4062009-02-06 18:46:48 -0800110 rc = simple_fill_super(sb, XENFS_SUPER_MAGIC, xenfs_files);
111 if (rc < 0)
112 return rc;
113
114 if (xen_initial_domain()) {
115 xenfs_create_file(sb, sb->s_root, "xsd_kva",
116 &xsd_kva_file_ops, NULL, S_IRUSR|S_IWUSR);
117 xenfs_create_file(sb, sb->s_root, "xsd_port",
118 &xsd_port_file_ops, NULL, S_IRUSR|S_IWUSR);
Jeremy Fitzhardinge1c5de192009-02-09 12:05:49 -0800119 xenfs_create_file(sb, sb->s_root, "privcmd",
120 &privcmd_file_ops, NULL, S_IRUSR|S_IWUSR);
Ian Campbell655d4062009-02-06 18:46:48 -0800121 }
122
123 return rc;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800124}
125
126static int xenfs_get_sb(struct file_system_type *fs_type,
127 int flags, const char *dev_name,
128 void *data, struct vfsmount *mnt)
129{
130 return get_sb_single(fs_type, flags, data, xenfs_fill_super, mnt);
131}
132
133static struct file_system_type xenfs_type = {
134 .owner = THIS_MODULE,
135 .name = "xenfs",
136 .get_sb = xenfs_get_sb,
137 .kill_sb = kill_litter_super,
138};
139
140static int __init xenfs_init(void)
141{
Jeremy Fitzhardinge24a89b52009-02-09 12:05:49 -0800142 int err;
143 if (!xen_domain()) {
144 printk(KERN_INFO "xenfs: not registering filesystem on non-xen platform\n");
145 return 0;
146 }
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800147
Jeremy Fitzhardinge24a89b52009-02-09 12:05:49 -0800148 err = register_filesystem(&xenfs_type);
149 if (err) {
150 printk(KERN_ERR "xenfs: Unable to register filesystem!\n");
151 goto out;
152 }
153
154 err = bdi_init(&xenfs_backing_dev_info);
155 if (err)
156 unregister_filesystem(&xenfs_type);
157
158 out:
159
160 return err;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800161}
162
163static void __exit xenfs_exit(void)
164{
Jeremy Fitzhardinge43df95c2010-07-21 22:51:39 -0700165 if (xen_domain())
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800166 unregister_filesystem(&xenfs_type);
167}
168
169module_init(xenfs_init);
170module_exit(xenfs_exit);
171