d6c4d7ca8a38afd9e03d9301ea006e9e91e616b2
[linux-flexiantxendom0-3.2.10.git] / drivers / char / raw.c
1 /*
2  * linux/drivers/char/raw.c
3  *
4  * Front-end raw character devices.  These can be bound to any block
5  * devices to provide genuine Unix raw character device semantics.
6  *
7  * We reserve minor number 0 for a control interface.  ioctl()s on this
8  * device are used to bind the other minor numbers to block devices.
9  */
10
11 #include <linux/init.h>
12 #include <linux/fs.h>
13 #include <linux/devfs_fs_kernel.h>
14 #include <linux/major.h>
15 #include <linux/blkdev.h>
16 #include <linux/module.h>
17 #include <linux/raw.h>
18 #include <linux/capability.h>
19 #include <linux/uio.h>
20
21 #include <asm/uaccess.h>
22
23 struct raw_device_data {
24         struct block_device *binding;
25         int inuse;
26 };
27
28 static struct raw_device_data raw_devices[MAX_RAW_MINORS];
29 static DECLARE_MUTEX(raw_mutex);
30 static struct file_operations raw_ctl_fops;          /* forward declaration */
31
32 /*
33  * Open/close code for raw IO.
34  *
35  * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
36  * point at the blockdev's address_space and set the file handle to use
37  * O_DIRECT.
38  *
39  * Set the device's soft blocksize to the minimum possible.  This gives the
40  * finest possible alignment and has no adverse impact on performance.
41  */
42 static int raw_open(struct inode *inode, struct file *filp)
43 {
44         const int minor = iminor(inode);
45         struct block_device *bdev;
46         int err;
47
48         if (minor == 0) {       /* It is the control device */
49                 filp->f_op = &raw_ctl_fops;
50                 return 0;
51         }
52
53         down(&raw_mutex);
54
55         /*
56          * All we need to do on open is check that the device is bound.
57          */
58         bdev = raw_devices[minor].binding;
59         err = -ENODEV;
60         if (!bdev)
61                 goto out;
62         igrab(bdev->bd_inode);
63         err = blkdev_get(bdev, filp->f_mode, 0, BDEV_RAW);
64         if (err)
65                 goto out;
66         err = bd_claim(bdev, raw_open);
67         if (err)
68                 goto out1;
69         err = set_blocksize(bdev, bdev_hardsect_size(bdev));
70         if (err)
71                 goto out2;
72         filp->f_flags |= O_DIRECT;
73         filp->f_mapping = bdev->bd_inode->i_mapping;
74         if (++raw_devices[minor].inuse == 1)
75                 filp->f_dentry->d_inode->i_mapping =
76                         bdev->bd_inode->i_mapping;
77         filp->private_data = bdev;
78         up(&raw_mutex);
79         return 0;
80
81 out2:
82         bd_release(bdev);
83 out1:
84         blkdev_put(bdev, BDEV_RAW);
85 out:
86         up(&raw_mutex);
87         return err;
88 }
89
90 /*
91  * When the final fd which refers to this character-special node is closed, we
92  * make its ->mapping point back at its own i_data.
93  */
94 static int raw_release(struct inode *inode, struct file *filp)
95 {
96         const int minor= iminor(inode);
97         struct block_device *bdev;
98
99         down(&raw_mutex);
100         bdev = raw_devices[minor].binding;
101         if (--raw_devices[minor].inuse == 0) {
102                 /* Here  inode->i_mapping == bdev->bd_inode->i_mapping  */
103                 inode->i_mapping = &inode->i_data;
104                 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
105         }
106         up(&raw_mutex);
107
108         bd_release(bdev);
109         blkdev_put(bdev, BDEV_RAW);
110         return 0;
111 }
112
113 /*
114  * Forward ioctls to the underlying block device.
115  */
116 static int
117 raw_ioctl(struct inode *inode, struct file *filp,
118                   unsigned int command, unsigned long arg)
119 {
120         struct block_device *bdev = filp->private_data;
121
122         return ioctl_by_bdev(bdev, command, arg);
123 }
124
125 /*
126  * Deal with ioctls against the raw-device control interface, to bind
127  * and unbind other raw devices.
128  */
129 static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
130                         unsigned int command, unsigned long arg)
131 {
132         struct raw_config_request rq;
133         struct raw_device_data *rawdev;
134         int err = 0;
135
136         switch (command) {
137         case RAW_SETBIND:
138         case RAW_GETBIND:
139
140                 /* First, find out which raw minor we want */
141
142                 if (copy_from_user(&rq, (void *) arg, sizeof(rq))) {
143                         err = -EFAULT;
144                         goto out;
145                 }
146
147                 if (rq.raw_minor < 0 || rq.raw_minor >= MAX_RAW_MINORS) {
148                         err = -EINVAL;
149                         goto out;
150                 }
151                 rawdev = &raw_devices[rq.raw_minor];
152
153                 if (command == RAW_SETBIND) {
154                         dev_t dev;
155
156                         /*
157                          * This is like making block devices, so demand the
158                          * same capability
159                          */
160                         if (!capable(CAP_SYS_ADMIN)) {
161                                 err = -EPERM;
162                                 goto out;
163                         }
164
165                         /*
166                          * For now, we don't need to check that the underlying
167                          * block device is present or not: we can do that when
168                          * the raw device is opened.  Just check that the
169                          * major/minor numbers make sense.
170                          */
171
172                         dev = MKDEV(rq.block_major, rq.block_minor);
173                         if ((rq.block_major == 0 && rq.block_minor != 0) ||
174                                         MAJOR(dev) != rq.block_major ||
175                                         MINOR(dev) != rq.block_minor) {
176                                 err = -EINVAL;
177                                 goto out;
178                         }
179
180                         down(&raw_mutex);
181                         if (rawdev->inuse) {
182                                 up(&raw_mutex);
183                                 err = -EBUSY;
184                                 goto out;
185                         }
186                         if (rawdev->binding) {
187                                 bdput(rawdev->binding);
188                                 module_put(THIS_MODULE);
189                         }
190                         if (rq.block_major == 0 && rq.block_minor == 0) {
191                                 /* unbind */
192                                 rawdev->binding = NULL;
193                         } else {
194                                 rawdev->binding = bdget(dev);
195                                 if (rawdev->binding == NULL)
196                                         err = -ENOMEM;
197                                 else
198                                         __module_get(THIS_MODULE);
199                         }
200                         up(&raw_mutex);
201                 } else {
202                         struct block_device *bdev;
203
204                         down(&raw_mutex);
205                         bdev = rawdev->binding;
206                         if (bdev) {
207                                 rq.block_major = MAJOR(bdev->bd_dev);
208                                 rq.block_minor = MINOR(bdev->bd_dev);
209                         } else {
210                                 rq.block_major = rq.block_minor = 0;
211                         }
212                         up(&raw_mutex);
213                         if (copy_to_user((void *)arg, &rq, sizeof(rq))) {
214                                 err = -EFAULT;
215                                 goto out;
216                         }
217                 }
218                 break;
219         default:
220                 err = -EINVAL;
221                 break;
222         }
223 out:
224         return err;
225 }
226
227 static ssize_t raw_file_write(struct file *file, const char *buf,
228                                    size_t count, loff_t *ppos)
229 {
230         struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
231
232         return generic_file_write_nolock(file, &local_iov, 1, ppos);
233 }
234
235 static ssize_t raw_file_aio_write(struct kiocb *iocb, const char *buf,
236                                         size_t count, loff_t pos)
237 {
238         struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
239
240         return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
241 }
242
243
244 static struct file_operations raw_fops = {
245         .read   =       generic_file_read,
246         .aio_read =     generic_file_aio_read,
247         .write  =       raw_file_write,
248         .aio_write =    raw_file_aio_write,
249         .open   =       raw_open,
250         .release=       raw_release,
251         .ioctl  =       raw_ioctl,
252         .readv  =       generic_file_readv,
253         .writev =       generic_file_writev,
254         .owner  =       THIS_MODULE,
255 };
256
257 static struct file_operations raw_ctl_fops = {
258         .ioctl  =       raw_ctl_ioctl,
259         .open   =       raw_open,
260         .owner  =       THIS_MODULE,
261 };
262
263 static int __init raw_init(void)
264 {
265         int i;
266
267         register_chrdev(RAW_MAJOR, "raw", &raw_fops);
268         devfs_mk_cdev(MKDEV(RAW_MAJOR, 0),
269                       S_IFCHR | S_IRUGO | S_IWUGO,
270                       "raw/rawctl");
271         for (i = 1; i < MAX_RAW_MINORS; i++)
272                 devfs_mk_cdev(MKDEV(RAW_MAJOR, i),
273                               S_IFCHR | S_IRUGO | S_IWUGO,
274                               "raw/raw%d", i);
275         return 0;
276 }
277
278 static void __exit raw_exit(void)
279 {
280         int i;
281
282         for (i = 1; i < MAX_RAW_MINORS; i++)
283                 devfs_remove("raw/raw%d", i);
284         devfs_remove("raw/rawctl");
285         devfs_remove("raw");
286         unregister_chrdev(RAW_MAJOR, "raw");
287 }
288
289 module_init(raw_init);
290 module_exit(raw_exit);
291 MODULE_LICENSE("GPL");