blob: 8756ab0daa8b3123bb4937dc128920fa0471e130 [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>
Jonathan Corbetc0bed682008-05-16 13:54:46 -060022#include <linux/smp_lock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.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
Jonathan Corbetc0bed682008-05-16 13:54:46 -060058 lock_kernel();
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080059 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 /*
62 * All we need to do on open is check that the device is bound.
63 */
64 bdev = raw_devices[minor].binding;
65 err = -ENODEV;
66 if (!bdev)
67 goto out;
68 igrab(bdev->bd_inode);
Al Viro572c4892007-10-08 13:24:05 -040069 err = blkdev_get(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (err)
71 goto out;
72 err = bd_claim(bdev, raw_open);
73 if (err)
74 goto out1;
Martin K. Petersene1defc42009-05-22 17:17:49 -040075 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (err)
77 goto out2;
78 filp->f_flags |= O_DIRECT;
79 filp->f_mapping = bdev->bd_inode->i_mapping;
80 if (++raw_devices[minor].inuse == 1)
Josef Sipeka7113a92006-12-08 02:36:55 -080081 filp->f_path.dentry->d_inode->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 bdev->bd_inode->i_mapping;
83 filp->private_data = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080084 mutex_unlock(&raw_mutex);
Jonathan Corbetc0bed682008-05-16 13:54:46 -060085 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87
88out2:
89 bd_release(bdev);
90out1:
Al Viro9a1c3542008-02-22 20:40:24 -050091 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080093 mutex_unlock(&raw_mutex);
Dan Carpenter996ff682009-03-27 13:28:48 +030094 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return err;
96}
97
98/*
99 * When the final fd which refers to this character-special node is closed, we
100 * make its ->mapping point back at its own i_data.
101 */
102static int raw_release(struct inode *inode, struct file *filp)
103{
104 const int minor= iminor(inode);
105 struct block_device *bdev;
106
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800107 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 bdev = raw_devices[minor].binding;
109 if (--raw_devices[minor].inuse == 0) {
110 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
111 inode->i_mapping = &inode->i_data;
112 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
113 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800114 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500117 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
121/*
122 * Forward ioctls to the underlying block device.
123 */
124static int
125raw_ioctl(struct inode *inode, struct file *filp,
126 unsigned int command, unsigned long arg)
127{
128 struct block_device *bdev = filp->private_data;
129
Al Viro56b26ad2008-09-19 03:17:36 -0400130 return blkdev_ioctl(bdev, 0, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static void bind_device(struct raw_config_request *rq)
134{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700135 device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700136 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL,
137 "raw%d", rq->raw_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140/*
141 * Deal with ioctls against the raw-device control interface, to bind
142 * and unbind other raw devices.
143 */
144static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
145 unsigned int command, unsigned long arg)
146{
147 struct raw_config_request rq;
148 struct raw_device_data *rawdev;
149 int err = 0;
150
151 switch (command) {
152 case RAW_SETBIND:
153 case RAW_GETBIND:
154
155 /* First, find out which raw minor we want */
156
157 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) {
158 err = -EFAULT;
159 goto out;
160 }
161
Jeff Moyer38584c12007-02-10 01:46:10 -0800162 if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = -EINVAL;
164 goto out;
165 }
166 rawdev = &raw_devices[rq.raw_minor];
167
168 if (command == RAW_SETBIND) {
169 dev_t dev;
170
171 /*
172 * This is like making block devices, so demand the
173 * same capability
174 */
175 if (!capable(CAP_SYS_ADMIN)) {
176 err = -EPERM;
177 goto out;
178 }
179
180 /*
181 * For now, we don't need to check that the underlying
182 * block device is present or not: we can do that when
183 * the raw device is opened. Just check that the
184 * major/minor numbers make sense.
185 */
186
187 dev = MKDEV(rq.block_major, rq.block_minor);
188 if ((rq.block_major == 0 && rq.block_minor != 0) ||
189 MAJOR(dev) != rq.block_major ||
190 MINOR(dev) != rq.block_minor) {
191 err = -EINVAL;
192 goto out;
193 }
194
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800195 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (rawdev->inuse) {
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800197 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 err = -EBUSY;
199 goto out;
200 }
201 if (rawdev->binding) {
202 bdput(rawdev->binding);
203 module_put(THIS_MODULE);
204 }
205 if (rq.block_major == 0 && rq.block_minor == 0) {
206 /* unbind */
207 rawdev->binding = NULL;
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700208 device_destroy(raw_class,
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800209 MKDEV(RAW_MAJOR, rq.raw_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else {
211 rawdev->binding = bdget(dev);
212 if (rawdev->binding == NULL)
213 err = -ENOMEM;
214 else {
215 __module_get(THIS_MODULE);
216 bind_device(&rq);
217 }
218 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800219 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
221 struct block_device *bdev;
222
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800223 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 bdev = rawdev->binding;
225 if (bdev) {
226 rq.block_major = MAJOR(bdev->bd_dev);
227 rq.block_minor = MINOR(bdev->bd_dev);
228 } else {
229 rq.block_major = rq.block_minor = 0;
230 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800231 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
233 err = -EFAULT;
234 goto out;
235 }
236 }
237 break;
238 default:
239 err = -EINVAL;
240 break;
241 }
242out:
243 return err;
244}
245
Arjan van de Ven62322d22006-07-03 00:24:21 -0700246static const struct file_operations raw_fops = {
Badari Pulavarty543ade12006-09-30 23:28:48 -0700247 .read = do_sync_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .aio_read = generic_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700249 .write = do_sync_write,
Christoph Hellwigeef99382009-08-20 17:43:41 +0200250 .aio_write = blkdev_aio_write,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700251 .fsync = blkdev_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .open = raw_open,
253 .release= raw_release,
254 .ioctl = raw_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .owner = THIS_MODULE,
256};
257
Arjan van de Ven62322d22006-07-03 00:24:21 -0700258static const struct file_operations raw_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .ioctl = raw_ctl_ioctl,
260 .open = raw_open,
261 .owner = THIS_MODULE,
262};
263
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700264static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Kay Sieverse454cea2009-09-18 23:01:12 +0200266static char *raw_devnode(struct device *dev, mode_t *mode)
Kay Sievers6fd46932009-04-30 15:23:42 +0200267{
268 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int __init raw_init(void)
272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700276 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
277 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto error;
279
280 cdev_init(&raw_cdev, &raw_fops);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700281 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
282 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 kobject_put(&raw_cdev.kobj);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700284 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800287 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (IS_ERR(raw_class)) {
289 printk(KERN_ERR "Error creating raw class.\n");
290 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700291 ret = PTR_ERR(raw_class);
292 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200294 raw_class->devnode = raw_devnode;
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700295 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700299error_region:
300 unregister_chrdev_region(dev, MAX_RAW_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301error:
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static void __exit raw_exit(void)
306{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700307 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800308 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 cdev_del(&raw_cdev);
310 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
311}
312
313module_init(raw_init);
314module_exit(raw_exit);
315MODULE_LICENSE("GPL");