Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 15 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 16 | #include <xen/xen.h> |
| 17 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 18 | #include "xenfs.h" |
Bastian Blank | d8414d3 | 2011-12-16 11:34:33 -0500 | [diff] [blame] | 19 | #include "../privcmd.h" |
Bastian Blank | 2fb3683 | 2011-12-10 19:29:47 +0100 | [diff] [blame] | 20 | #include "../xenbus/xenbus_comms.h" |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 21 | |
| 22 | #include <asm/xen/hypervisor.h> |
| 23 | |
| 24 | MODULE_DESCRIPTION("Xen filesystem"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 27 | static struct inode *xenfs_make_inode(struct super_block *sb, int mode) |
| 28 | { |
| 29 | struct inode *ret = new_inode(sb); |
| 30 | |
| 31 | if (ret) { |
| 32 | ret->i_mode = mode; |
Eric W. Biederman | 9a11f45 | 2012-02-07 16:29:19 -0800 | [diff] [blame^] | 33 | ret->i_uid = GLOBAL_ROOT_UID; |
| 34 | ret->i_gid = GLOBAL_ROOT_GID; |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 35 | ret->i_blocks = 0; |
| 36 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 37 | } |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | static struct dentry *xenfs_create_file(struct super_block *sb, |
| 42 | struct dentry *parent, |
| 43 | const char *name, |
| 44 | const struct file_operations *fops, |
| 45 | void *data, |
| 46 | int mode) |
| 47 | { |
| 48 | struct dentry *dentry; |
| 49 | struct inode *inode; |
| 50 | |
| 51 | dentry = d_alloc_name(parent, name); |
| 52 | if (!dentry) |
| 53 | return NULL; |
| 54 | |
| 55 | inode = xenfs_make_inode(sb, S_IFREG | mode); |
| 56 | if (!inode) { |
| 57 | dput(dentry); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | inode->i_fop = fops; |
| 62 | inode->i_private = data; |
| 63 | |
| 64 | d_add(dentry, inode); |
| 65 | return dentry; |
| 66 | } |
| 67 | |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 68 | static ssize_t capabilities_read(struct file *file, char __user *buf, |
| 69 | size_t size, loff_t *off) |
| 70 | { |
| 71 | char *tmp = ""; |
| 72 | |
| 73 | if (xen_initial_domain()) |
| 74 | tmp = "control_d\n"; |
| 75 | |
| 76 | return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp)); |
| 77 | } |
| 78 | |
| 79 | static const struct file_operations capabilities_file_ops = { |
| 80 | .read = capabilities_read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 81 | .llseek = default_llseek, |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 84 | static int xenfs_fill_super(struct super_block *sb, void *data, int silent) |
| 85 | { |
| 86 | static struct tree_descr xenfs_files[] = { |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 87 | [1] = {}, |
Bastian Blank | 2fb3683 | 2011-12-10 19:29:47 +0100 | [diff] [blame] | 88 | { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR }, |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 89 | { "capabilities", &capabilities_file_ops, S_IRUGO }, |
Bastian Blank | d8414d3 | 2011-12-16 11:34:33 -0500 | [diff] [blame] | 90 | { "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR }, |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 91 | {""}, |
| 92 | }; |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 93 | int rc; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 94 | |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 95 | rc = simple_fill_super(sb, XENFS_SUPER_MAGIC, xenfs_files); |
| 96 | if (rc < 0) |
| 97 | return rc; |
| 98 | |
| 99 | if (xen_initial_domain()) { |
| 100 | xenfs_create_file(sb, sb->s_root, "xsd_kva", |
| 101 | &xsd_kva_file_ops, NULL, S_IRUSR|S_IWUSR); |
| 102 | xenfs_create_file(sb, sb->s_root, "xsd_port", |
| 103 | &xsd_port_file_ops, NULL, S_IRUSR|S_IWUSR); |
| 104 | } |
| 105 | |
| 106 | return rc; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jeremy Fitzhardinge | fe61f1d | 2010-11-16 11:06:46 -0800 | [diff] [blame] | 109 | static struct dentry *xenfs_mount(struct file_system_type *fs_type, |
| 110 | int flags, const char *dev_name, |
| 111 | void *data) |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 112 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 113 | return mount_single(fs_type, flags, data, xenfs_fill_super); |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static struct file_system_type xenfs_type = { |
| 117 | .owner = THIS_MODULE, |
| 118 | .name = "xenfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 119 | .mount = xenfs_mount, |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 120 | .kill_sb = kill_litter_super, |
| 121 | }; |
| 122 | |
| 123 | static int __init xenfs_init(void) |
| 124 | { |
Jeremy Fitzhardinge | 9045d47 | 2010-11-18 17:14:46 -0800 | [diff] [blame] | 125 | if (xen_domain()) |
| 126 | return register_filesystem(&xenfs_type); |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 127 | |
Jeremy Fitzhardinge | 9045d47 | 2010-11-18 17:14:46 -0800 | [diff] [blame] | 128 | printk(KERN_INFO "XENFS: not registering filesystem on non-xen platform\n"); |
| 129 | return 0; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void __exit xenfs_exit(void) |
| 133 | { |
Jeremy Fitzhardinge | 43df95c | 2010-07-21 22:51:39 -0700 | [diff] [blame] | 134 | if (xen_domain()) |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 135 | unregister_filesystem(&xenfs_type); |
| 136 | } |
| 137 | |
| 138 | module_init(xenfs_init); |
| 139 | module_exit(xenfs_exit); |
| 140 | |