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> |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 15 | #include <linux/mm.h> |
| 16 | #include <linux/backing-dev.h> |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 17 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 18 | #include <xen/xen.h> |
| 19 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 20 | #include "xenfs.h" |
| 21 | |
| 22 | #include <asm/xen/hypervisor.h> |
| 23 | |
| 24 | MODULE_DESCRIPTION("Xen filesystem"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 27 | static int xenfs_set_page_dirty(struct page *page) |
| 28 | { |
Jeremy Fitzhardinge | 35f8c1c | 2009-03-06 09:56:26 -0800 | [diff] [blame] | 29 | return !TestSetPageDirty(page); |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static const struct address_space_operations xenfs_aops = { |
| 33 | .set_page_dirty = xenfs_set_page_dirty, |
| 34 | }; |
| 35 | |
| 36 | static struct backing_dev_info xenfs_backing_dev_info = { |
| 37 | .ra_pages = 0, /* No readahead */ |
| 38 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
| 39 | }; |
| 40 | |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 41 | static struct inode *xenfs_make_inode(struct super_block *sb, int mode) |
| 42 | { |
| 43 | struct inode *ret = new_inode(sb); |
| 44 | |
| 45 | if (ret) { |
| 46 | ret->i_mode = mode; |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 47 | ret->i_mapping->a_ops = &xenfs_aops; |
| 48 | ret->i_mapping->backing_dev_info = &xenfs_backing_dev_info; |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 49 | ret->i_uid = ret->i_gid = 0; |
| 50 | ret->i_blocks = 0; |
| 51 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 52 | } |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static struct dentry *xenfs_create_file(struct super_block *sb, |
| 57 | struct dentry *parent, |
| 58 | const char *name, |
| 59 | const struct file_operations *fops, |
| 60 | void *data, |
| 61 | int mode) |
| 62 | { |
| 63 | struct dentry *dentry; |
| 64 | struct inode *inode; |
| 65 | |
| 66 | dentry = d_alloc_name(parent, name); |
| 67 | if (!dentry) |
| 68 | return NULL; |
| 69 | |
| 70 | inode = xenfs_make_inode(sb, S_IFREG | mode); |
| 71 | if (!inode) { |
| 72 | dput(dentry); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | inode->i_fop = fops; |
| 77 | inode->i_private = data; |
| 78 | |
| 79 | d_add(dentry, inode); |
| 80 | return dentry; |
| 81 | } |
| 82 | |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 83 | static ssize_t capabilities_read(struct file *file, char __user *buf, |
| 84 | size_t size, loff_t *off) |
| 85 | { |
| 86 | char *tmp = ""; |
| 87 | |
| 88 | if (xen_initial_domain()) |
| 89 | tmp = "control_d\n"; |
| 90 | |
| 91 | return simple_read_from_buffer(buf, size, off, tmp, strlen(tmp)); |
| 92 | } |
| 93 | |
| 94 | static const struct file_operations capabilities_file_ops = { |
| 95 | .read = capabilities_read, |
| 96 | }; |
| 97 | |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 98 | static int xenfs_fill_super(struct super_block *sb, void *data, int silent) |
| 99 | { |
| 100 | static struct tree_descr xenfs_files[] = { |
Jeremy Fitzhardinge | 818fd20 | 2009-02-06 18:46:47 -0800 | [diff] [blame] | 101 | [1] = {}, |
| 102 | { "xenbus", &xenbus_file_ops, S_IRUSR|S_IWUSR }, |
| 103 | { "capabilities", &capabilities_file_ops, S_IRUGO }, |
Jeremy Fitzhardinge | 9387377 | 2010-10-06 08:51:32 -0700 | [diff] [blame^] | 104 | { "privcmd", &privcmd_file_ops, S_IRUSR|S_IWUSR }, |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 105 | {""}, |
| 106 | }; |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 107 | int rc; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 108 | |
Ian Campbell | 655d406 | 2009-02-06 18:46:48 -0800 | [diff] [blame] | 109 | rc = simple_fill_super(sb, XENFS_SUPER_MAGIC, xenfs_files); |
| 110 | if (rc < 0) |
| 111 | return rc; |
| 112 | |
| 113 | if (xen_initial_domain()) { |
| 114 | xenfs_create_file(sb, sb->s_root, "xsd_kva", |
| 115 | &xsd_kva_file_ops, NULL, S_IRUSR|S_IWUSR); |
| 116 | xenfs_create_file(sb, sb->s_root, "xsd_port", |
| 117 | &xsd_port_file_ops, NULL, S_IRUSR|S_IWUSR); |
| 118 | } |
| 119 | |
| 120 | return rc; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int xenfs_get_sb(struct file_system_type *fs_type, |
| 124 | int flags, const char *dev_name, |
| 125 | void *data, struct vfsmount *mnt) |
| 126 | { |
| 127 | return get_sb_single(fs_type, flags, data, xenfs_fill_super, mnt); |
| 128 | } |
| 129 | |
| 130 | static struct file_system_type xenfs_type = { |
| 131 | .owner = THIS_MODULE, |
| 132 | .name = "xenfs", |
| 133 | .get_sb = xenfs_get_sb, |
| 134 | .kill_sb = kill_litter_super, |
| 135 | }; |
| 136 | |
| 137 | static int __init xenfs_init(void) |
| 138 | { |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 139 | int err; |
| 140 | if (!xen_domain()) { |
| 141 | printk(KERN_INFO "xenfs: not registering filesystem on non-xen platform\n"); |
| 142 | return 0; |
| 143 | } |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 144 | |
Jeremy Fitzhardinge | 24a89b5 | 2009-02-09 12:05:49 -0800 | [diff] [blame] | 145 | err = register_filesystem(&xenfs_type); |
| 146 | if (err) { |
| 147 | printk(KERN_ERR "xenfs: Unable to register filesystem!\n"); |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | err = bdi_init(&xenfs_backing_dev_info); |
| 152 | if (err) |
| 153 | unregister_filesystem(&xenfs_type); |
| 154 | |
| 155 | out: |
| 156 | |
| 157 | return err; |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void __exit xenfs_exit(void) |
| 161 | { |
Jeremy Fitzhardinge | 43df95c | 2010-07-21 22:51:39 -0700 | [diff] [blame] | 162 | if (xen_domain()) |
Alex Zeffertt | 1107ba8 | 2009-01-07 18:07:11 -0800 | [diff] [blame] | 163 | unregister_filesystem(&xenfs_type); |
| 164 | } |
| 165 | |
| 166 | module_init(xenfs_init); |
| 167 | module_exit(xenfs_exit); |
| 168 | |