blob: bbfa0e241cba38928c8a5b5157a7a2a3ac178d2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/major.h>
14#include <linux/blkdev.h>
15#include <linux/module.h>
16#include <linux/raw.h>
17#include <linux/capability.h>
18#include <linux/uio.h>
19#include <linux/cdev.h>
20#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24
25struct raw_device_data {
26 struct block_device *binding;
27 int inuse;
28};
29
gregkh@suse.deca8eca62005-03-23 09:53:09 -080030static struct class *raw_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static struct raw_device_data raw_devices[MAX_RAW_MINORS];
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080032static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070033static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * Open/close code for raw IO.
37 *
38 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
39 * point at the blockdev's address_space and set the file handle to use
40 * O_DIRECT.
41 *
42 * Set the device's soft blocksize to the minimum possible. This gives the
43 * finest possible alignment and has no adverse impact on performance.
44 */
45static int raw_open(struct inode *inode, struct file *filp)
46{
47 const int minor = iminor(inode);
48 struct block_device *bdev;
49 int err;
50
51 if (minor == 0) { /* It is the control device */
52 filp->f_op = &raw_ctl_fops;
53 return 0;
54 }
55
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080056 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /*
59 * All we need to do on open is check that the device is bound.
60 */
61 bdev = raw_devices[minor].binding;
62 err = -ENODEV;
63 if (!bdev)
64 goto out;
65 igrab(bdev->bd_inode);
66 err = blkdev_get(bdev, filp->f_mode, 0);
67 if (err)
68 goto out;
69 err = bd_claim(bdev, raw_open);
70 if (err)
71 goto out1;
72 err = set_blocksize(bdev, bdev_hardsect_size(bdev));
73 if (err)
74 goto out2;
75 filp->f_flags |= O_DIRECT;
76 filp->f_mapping = bdev->bd_inode->i_mapping;
77 if (++raw_devices[minor].inuse == 1)
Josef Sipeka7113a92006-12-08 02:36:55 -080078 filp->f_path.dentry->d_inode->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 bdev->bd_inode->i_mapping;
80 filp->private_data = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080081 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83
84out2:
85 bd_release(bdev);
86out1:
87 blkdev_put(bdev);
88out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080089 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return err;
91}
92
93/*
94 * When the final fd which refers to this character-special node is closed, we
95 * make its ->mapping point back at its own i_data.
96 */
97static int raw_release(struct inode *inode, struct file *filp)
98{
99 const int minor= iminor(inode);
100 struct block_device *bdev;
101
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800102 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 bdev = raw_devices[minor].binding;
104 if (--raw_devices[minor].inuse == 0) {
105 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
106 inode->i_mapping = &inode->i_data;
107 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
108 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800109 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 bd_release(bdev);
112 blkdev_put(bdev);
113 return 0;
114}
115
116/*
117 * Forward ioctls to the underlying block device.
118 */
119static int
120raw_ioctl(struct inode *inode, struct file *filp,
121 unsigned int command, unsigned long arg)
122{
123 struct block_device *bdev = filp->private_data;
124
Stephen Tweediee72022e2005-05-18 11:22:31 -0400125 return blkdev_ioctl(bdev->bd_inode, NULL, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void bind_device(struct raw_config_request *rq)
129{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700130 device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
131 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor),
132 "raw%d", rq->raw_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135/*
136 * Deal with ioctls against the raw-device control interface, to bind
137 * and unbind other raw devices.
138 */
139static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
140 unsigned int command, unsigned long arg)
141{
142 struct raw_config_request rq;
143 struct raw_device_data *rawdev;
144 int err = 0;
145
146 switch (command) {
147 case RAW_SETBIND:
148 case RAW_GETBIND:
149
150 /* First, find out which raw minor we want */
151
152 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) {
153 err = -EFAULT;
154 goto out;
155 }
156
Jeff Moyer38584c12007-02-10 01:46:10 -0800157 if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 err = -EINVAL;
159 goto out;
160 }
161 rawdev = &raw_devices[rq.raw_minor];
162
163 if (command == RAW_SETBIND) {
164 dev_t dev;
165
166 /*
167 * This is like making block devices, so demand the
168 * same capability
169 */
170 if (!capable(CAP_SYS_ADMIN)) {
171 err = -EPERM;
172 goto out;
173 }
174
175 /*
176 * For now, we don't need to check that the underlying
177 * block device is present or not: we can do that when
178 * the raw device is opened. Just check that the
179 * major/minor numbers make sense.
180 */
181
182 dev = MKDEV(rq.block_major, rq.block_minor);
183 if ((rq.block_major == 0 && rq.block_minor != 0) ||
184 MAJOR(dev) != rq.block_major ||
185 MINOR(dev) != rq.block_minor) {
186 err = -EINVAL;
187 goto out;
188 }
189
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800190 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (rawdev->inuse) {
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800192 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 err = -EBUSY;
194 goto out;
195 }
196 if (rawdev->binding) {
197 bdput(rawdev->binding);
198 module_put(THIS_MODULE);
199 }
200 if (rq.block_major == 0 && rq.block_minor == 0) {
201 /* unbind */
202 rawdev->binding = NULL;
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700203 device_destroy(raw_class,
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800204 MKDEV(RAW_MAJOR, rq.raw_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
206 rawdev->binding = bdget(dev);
207 if (rawdev->binding == NULL)
208 err = -ENOMEM;
209 else {
210 __module_get(THIS_MODULE);
211 bind_device(&rq);
212 }
213 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800214 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 } else {
216 struct block_device *bdev;
217
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800218 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 bdev = rawdev->binding;
220 if (bdev) {
221 rq.block_major = MAJOR(bdev->bd_dev);
222 rq.block_minor = MINOR(bdev->bd_dev);
223 } else {
224 rq.block_major = rq.block_minor = 0;
225 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800226 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
228 err = -EFAULT;
229 goto out;
230 }
231 }
232 break;
233 default:
234 err = -EINVAL;
235 break;
236 }
237out:
238 return err;
239}
240
Arjan van de Ven62322d22006-07-03 00:24:21 -0700241static const struct file_operations raw_fops = {
Badari Pulavarty543ade12006-09-30 23:28:48 -0700242 .read = do_sync_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .aio_read = generic_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700244 .write = do_sync_write,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700245 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .open = raw_open,
247 .release= raw_release,
248 .ioctl = raw_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .owner = THIS_MODULE,
250};
251
Arjan van de Ven62322d22006-07-03 00:24:21 -0700252static const struct file_operations raw_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .ioctl = raw_ctl_ioctl,
254 .open = raw_open,
255 .owner = THIS_MODULE,
256};
257
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700258static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260static int __init raw_init(void)
261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700265 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
266 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto error;
268
269 cdev_init(&raw_cdev, &raw_fops);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700270 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
271 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 kobject_put(&raw_cdev.kobj);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700273 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800276 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (IS_ERR(raw_class)) {
278 printk(KERN_ERR "Error creating raw class.\n");
279 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700280 ret = PTR_ERR(raw_class);
281 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700283 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700287error_region:
288 unregister_chrdev_region(dev, MAX_RAW_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289error:
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700290 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static void __exit raw_exit(void)
294{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700295 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800296 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cdev_del(&raw_cdev);
298 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
299}
300
301module_init(raw_init);
302module_exit(raw_exit);
303MODULE_LICENSE("GPL");