blob: 24b2b9160aa659ee1f8e0de3b13eabf2985db9d8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Al Viroc4a04722009-08-24 22:42:56 +000023#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26
27struct raw_device_data {
28 struct block_device *binding;
29 int inuse;
30};
31
gregkh@suse.deca8eca62005-03-23 09:53:09 -080032static struct class *raw_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static struct raw_device_data raw_devices[MAX_RAW_MINORS];
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080034static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070035static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Open/close code for raw IO.
39 *
40 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
41 * point at the blockdev's address_space and set the file handle to use
42 * O_DIRECT.
43 *
44 * Set the device's soft blocksize to the minimum possible. This gives the
45 * finest possible alignment and has no adverse impact on performance.
46 */
47static int raw_open(struct inode *inode, struct file *filp)
48{
49 const int minor = iminor(inode);
50 struct block_device *bdev;
51 int err;
52
53 if (minor == 0) { /* It is the control device */
54 filp->f_op = &raw_ctl_fops;
55 return 0;
56 }
57
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080058 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /*
61 * All we need to do on open is check that the device is bound.
62 */
63 bdev = raw_devices[minor].binding;
64 err = -ENODEV;
65 if (!bdev)
66 goto out;
67 igrab(bdev->bd_inode);
Al Viro572c4892007-10-08 13:24:05 -040068 err = blkdev_get(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (err)
70 goto out;
71 err = bd_claim(bdev, raw_open);
72 if (err)
73 goto out1;
Martin K. Petersene1defc42009-05-22 17:17:49 -040074 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (err)
76 goto out2;
77 filp->f_flags |= O_DIRECT;
78 filp->f_mapping = bdev->bd_inode->i_mapping;
79 if (++raw_devices[minor].inuse == 1)
Josef Sipeka7113a92006-12-08 02:36:55 -080080 filp->f_path.dentry->d_inode->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 bdev->bd_inode->i_mapping;
82 filp->private_data = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080083 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85
86out2:
87 bd_release(bdev);
88out1:
Al Viro9a1c3542008-02-22 20:40:24 -050089 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080091 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return err;
93}
94
95/*
96 * When the final fd which refers to this character-special node is closed, we
97 * make its ->mapping point back at its own i_data.
98 */
99static int raw_release(struct inode *inode, struct file *filp)
100{
101 const int minor= iminor(inode);
102 struct block_device *bdev;
103
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800104 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 bdev = raw_devices[minor].binding;
106 if (--raw_devices[minor].inuse == 0) {
107 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
108 inode->i_mapping = &inode->i_data;
109 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
110 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800111 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500114 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
118/*
119 * Forward ioctls to the underlying block device.
120 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200121static long
122raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct block_device *bdev = filp->private_data;
Al Viroc4a04722009-08-24 22:42:56 +0000125 return blkdev_ioctl(bdev, 0, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Al Viroc4a04722009-08-24 22:42:56 +0000128static int bind_set(int number, u64 major, u64 minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Al Viroc4a04722009-08-24 22:42:56 +0000130 dev_t dev = MKDEV(major, minor);
131 struct raw_device_data *rawdev;
132 int err = 0;
133
134 if (number <= 0 || number >= MAX_RAW_MINORS)
135 return -EINVAL;
136
137 if (MAJOR(dev) != major || MINOR(dev) != minor)
138 return -EINVAL;
139
140 rawdev = &raw_devices[number];
141
142 /*
143 * This is like making block devices, so demand the
144 * same capability
145 */
146 if (!capable(CAP_SYS_ADMIN))
147 return -EPERM;
148
149 /*
150 * For now, we don't need to check that the underlying
151 * block device is present or not: we can do that when
152 * the raw device is opened. Just check that the
153 * major/minor numbers make sense.
154 */
155
156 if (MAJOR(dev) == 0 && dev != 0)
157 return -EINVAL;
158
159 mutex_lock(&raw_mutex);
160 if (rawdev->inuse) {
161 mutex_unlock(&raw_mutex);
162 return -EBUSY;
163 }
164 if (rawdev->binding) {
165 bdput(rawdev->binding);
166 module_put(THIS_MODULE);
167 }
168 if (!dev) {
169 /* unbind */
170 rawdev->binding = NULL;
171 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
172 } else {
173 rawdev->binding = bdget(dev);
174 if (rawdev->binding == NULL) {
175 err = -ENOMEM;
176 } else {
177 dev_t raw = MKDEV(RAW_MAJOR, number);
178 __module_get(THIS_MODULE);
179 device_destroy(raw_class, raw);
180 device_create(raw_class, NULL, raw, NULL,
181 "raw%d", number);
182 }
183 }
184 mutex_unlock(&raw_mutex);
185 return err;
186}
187
188static int bind_get(int number, dev_t *dev)
189{
190 struct raw_device_data *rawdev;
191 struct block_device *bdev;
192
193 if (number <= 0 || number >= MAX_RAW_MINORS)
194 return -EINVAL;
195
196 rawdev = &raw_devices[number];
197
198 mutex_lock(&raw_mutex);
199 bdev = rawdev->binding;
200 *dev = bdev ? bdev->bd_dev : 0;
201 mutex_unlock(&raw_mutex);
202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/*
206 * Deal with ioctls against the raw-device control interface, to bind
207 * and unbind other raw devices.
208 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200209static long raw_ctl_ioctl(struct file *filp, unsigned int command,
210 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct raw_config_request rq;
Al Viroc4a04722009-08-24 22:42:56 +0000213 dev_t dev;
214 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 switch (command) {
217 case RAW_SETBIND:
Al Viroc4a04722009-08-24 22:42:56 +0000218 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
219 return -EFAULT;
220
221 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 case RAW_GETBIND:
Al Viroc4a04722009-08-24 22:42:56 +0000224 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
225 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Al Viroc4a04722009-08-24 22:42:56 +0000227 err = bind_get(rq.raw_minor, &dev);
228 if (err)
229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Al Viroc4a04722009-08-24 22:42:56 +0000231 rq.block_major = MAJOR(dev);
232 rq.block_minor = MINOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Al Viroc4a04722009-08-24 22:42:56 +0000234 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
235 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Al Viroc4a04722009-08-24 22:42:56 +0000237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Al Viroc4a04722009-08-24 22:42:56 +0000239
240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Al Viroc4a04722009-08-24 22:42:56 +0000243#ifdef CONFIG_COMPAT
244struct raw32_config_request {
245 compat_int_t raw_minor;
246 compat_u64 block_major;
247 compat_u64 block_minor;
248};
249
250static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
251 unsigned long arg)
252{
253 struct raw32_config_request __user *user_req = compat_ptr(arg);
254 struct raw32_config_request rq;
255 dev_t dev;
256 int err = 0;
257
258 switch (cmd) {
259 case RAW_SETBIND:
260 if (copy_from_user(&rq, user_req, sizeof(rq)))
261 return -EFAULT;
262
263 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
264
265 case RAW_GETBIND:
266 if (copy_from_user(&rq, user_req, sizeof(rq)))
267 return -EFAULT;
268
269 err = bind_get(rq.raw_minor, &dev);
270 if (err)
271 return err;
272
273 rq.block_major = MAJOR(dev);
274 rq.block_minor = MINOR(dev);
275
276 if (copy_to_user(user_req, &rq, sizeof(rq)))
277 return -EFAULT;
278
279 return 0;
280 }
281
282 return -EINVAL;
283}
284#endif
285
Arjan van de Ven62322d22006-07-03 00:24:21 -0700286static const struct file_operations raw_fops = {
Arnd Bergmann55929332010-04-27 00:24:05 +0200287 .read = do_sync_read,
288 .aio_read = generic_file_aio_read,
289 .write = do_sync_write,
290 .aio_write = blkdev_aio_write,
291 .fsync = blkdev_fsync,
292 .open = raw_open,
293 .release = raw_release,
294 .unlocked_ioctl = raw_ioctl,
295 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
Arjan van de Ven62322d22006-07-03 00:24:21 -0700298static const struct file_operations raw_ctl_fops = {
Arnd Bergmann55929332010-04-27 00:24:05 +0200299 .unlocked_ioctl = raw_ctl_ioctl,
Al Viroc4a04722009-08-24 22:42:56 +0000300#ifdef CONFIG_COMPAT
301 .compat_ioctl = raw_ctl_compat_ioctl,
302#endif
Arnd Bergmann55929332010-04-27 00:24:05 +0200303 .open = raw_open,
304 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700307static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Kay Sieverse454cea2009-09-18 23:01:12 +0200309static char *raw_devnode(struct device *dev, mode_t *mode)
Kay Sievers6fd46932009-04-30 15:23:42 +0200310{
311 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int __init raw_init(void)
315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700317 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700319 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
320 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto error;
322
323 cdev_init(&raw_cdev, &raw_fops);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700324 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
325 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 kobject_put(&raw_cdev.kobj);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700327 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800330 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (IS_ERR(raw_class)) {
332 printk(KERN_ERR "Error creating raw class.\n");
333 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700334 ret = PTR_ERR(raw_class);
335 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200337 raw_class->devnode = raw_devnode;
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700338 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700342error_region:
343 unregister_chrdev_region(dev, MAX_RAW_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344error:
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700345 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static void __exit raw_exit(void)
349{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700350 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800351 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 cdev_del(&raw_cdev);
353 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
354}
355
356module_init(raw_init);
357module_exit(raw_exit);
358MODULE_LICENSE("GPL");