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