- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_dev_backend.c
1 #include <linux/slab.h>
2 #include <linux/types.h>
3 #include <linux/mm.h>
4 #include <linux/fs.h>
5 #include <linux/miscdevice.h>
6 #include <linux/module.h>
7 #include <linux/capability.h>
8
9 #include <xen/xen.h>
10 #ifdef CONFIG_PARAVIRT_XEN
11 #include <xen/page.h>
12 #endif
13 #include <xen/xenbus_dev.h>
14
15 #include "xenbus_comms.h"
16
17 MODULE_LICENSE("GPL");
18
19 static int xenbus_backend_open(struct inode *inode, struct file *filp)
20 {
21         if (!capable(CAP_SYS_ADMIN))
22                 return -EPERM;
23
24         return nonseekable_open(inode, filp);
25 }
26
27 static long xenbus_backend_ioctl(struct file *file, unsigned int cmd, unsigned long data)
28 {
29         if (!capable(CAP_SYS_ADMIN))
30                 return -EPERM;
31
32         switch (cmd) {
33                 case IOCTL_XENBUS_BACKEND_EVTCHN:
34                         if (xen_store_evtchn > 0)
35                                 return xen_store_evtchn;
36                         return -ENODEV;
37
38                 default:
39                         return -ENOTTY;
40         }
41 }
42
43 static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
44 {
45         size_t size = vma->vm_end - vma->vm_start;
46
47         if (!capable(CAP_SYS_ADMIN))
48                 return -EPERM;
49
50         if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
51                 return -EINVAL;
52
53         if (remap_pfn_range(vma, vma->vm_start,
54                             PFN_DOWN(__pa(xen_store_interface)),
55                             size, vma->vm_page_prot))
56                 return -EAGAIN;
57
58         return 0;
59 }
60
61 const struct file_operations xenbus_backend_fops = {
62         .open = xenbus_backend_open,
63         .mmap = xenbus_backend_mmap,
64         .unlocked_ioctl = xenbus_backend_ioctl,
65 };
66
67 static struct miscdevice xenbus_backend_dev = {
68         .minor = MISC_DYNAMIC_MINOR,
69         .name = "xen/xenbus_backend",
70         .fops = &xenbus_backend_fops,
71 };
72
73 static int __init xenbus_backend_init(void)
74 {
75         int err;
76
77         if (!xen_initial_domain())
78                 return -ENODEV;
79
80         err = misc_register(&xenbus_backend_dev);
81         if (err)
82                 printk(KERN_ERR "Could not register xenbus backend device\n");
83         return err;
84 }
85
86 static void __exit xenbus_backend_exit(void)
87 {
88         misc_deregister(&xenbus_backend_dev);
89 }
90
91 module_init(xenbus_backend_init);
92 module_exit(xenbus_backend_exit);