blob: bfd59e6bf54ff1aa221b71e4f22d989e9ba98a1a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
25
26struct raw_device_data {
27 struct block_device *binding;
28 int inuse;
29};
30
gregkh@suse.deca8eca62005-03-23 09:53:09 -080031static struct class *raw_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static struct raw_device_data raw_devices[MAX_RAW_MINORS];
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080033static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070034static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * Open/close code for raw IO.
38 *
39 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
40 * point at the blockdev's address_space and set the file handle to use
41 * O_DIRECT.
42 *
43 * Set the device's soft blocksize to the minimum possible. This gives the
44 * finest possible alignment and has no adverse impact on performance.
45 */
46static int raw_open(struct inode *inode, struct file *filp)
47{
48 const int minor = iminor(inode);
49 struct block_device *bdev;
50 int err;
51
52 if (minor == 0) { /* It is the control device */
53 filp->f_op = &raw_ctl_fops;
54 return 0;
55 }
56
Jonathan Corbetc0bed682008-05-16 13:54:46 -060057 lock_kernel();
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);
68 err = blkdev_get(bdev, filp->f_mode, 0);
69 if (err)
70 goto out;
71 err = bd_claim(bdev, raw_open);
72 if (err)
73 goto out1;
74 err = set_blocksize(bdev, bdev_hardsect_size(bdev));
75 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);
Jonathan Corbetc0bed682008-05-16 13:54:46 -060084 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86
87out2:
88 bd_release(bdev);
89out1:
Al Viro9a1c3542008-02-22 20:40:24 -050090 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080092 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return err;
94}
95
96/*
97 * When the final fd which refers to this character-special node is closed, we
98 * make its ->mapping point back at its own i_data.
99 */
100static int raw_release(struct inode *inode, struct file *filp)
101{
102 const int minor= iminor(inode);
103 struct block_device *bdev;
104
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800105 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 bdev = raw_devices[minor].binding;
107 if (--raw_devices[minor].inuse == 0) {
108 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
109 inode->i_mapping = &inode->i_data;
110 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
111 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800112 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500115 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
119/*
120 * Forward ioctls to the underlying block device.
121 */
122static int
123raw_ioctl(struct inode *inode, struct file *filp,
124 unsigned int command, unsigned long arg)
125{
126 struct block_device *bdev = filp->private_data;
127
Stephen Tweediee72022e2005-05-18 11:22:31 -0400128 return blkdev_ioctl(bdev->bd_inode, NULL, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static void bind_device(struct raw_config_request *rq)
132{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700133 device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700134 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL,
135 "raw%d", rq->raw_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/*
139 * Deal with ioctls against the raw-device control interface, to bind
140 * and unbind other raw devices.
141 */
142static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
143 unsigned int command, unsigned long arg)
144{
145 struct raw_config_request rq;
146 struct raw_device_data *rawdev;
147 int err = 0;
148
149 switch (command) {
150 case RAW_SETBIND:
151 case RAW_GETBIND:
152
153 /* First, find out which raw minor we want */
154
155 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) {
156 err = -EFAULT;
157 goto out;
158 }
159
Jeff Moyer38584c12007-02-10 01:46:10 -0800160 if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 err = -EINVAL;
162 goto out;
163 }
164 rawdev = &raw_devices[rq.raw_minor];
165
166 if (command == RAW_SETBIND) {
167 dev_t dev;
168
169 /*
170 * This is like making block devices, so demand the
171 * same capability
172 */
173 if (!capable(CAP_SYS_ADMIN)) {
174 err = -EPERM;
175 goto out;
176 }
177
178 /*
179 * For now, we don't need to check that the underlying
180 * block device is present or not: we can do that when
181 * the raw device is opened. Just check that the
182 * major/minor numbers make sense.
183 */
184
185 dev = MKDEV(rq.block_major, rq.block_minor);
186 if ((rq.block_major == 0 && rq.block_minor != 0) ||
187 MAJOR(dev) != rq.block_major ||
188 MINOR(dev) != rq.block_minor) {
189 err = -EINVAL;
190 goto out;
191 }
192
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800193 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (rawdev->inuse) {
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800195 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 err = -EBUSY;
197 goto out;
198 }
199 if (rawdev->binding) {
200 bdput(rawdev->binding);
201 module_put(THIS_MODULE);
202 }
203 if (rq.block_major == 0 && rq.block_minor == 0) {
204 /* unbind */
205 rawdev->binding = NULL;
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700206 device_destroy(raw_class,
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800207 MKDEV(RAW_MAJOR, rq.raw_minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 } else {
209 rawdev->binding = bdget(dev);
210 if (rawdev->binding == NULL)
211 err = -ENOMEM;
212 else {
213 __module_get(THIS_MODULE);
214 bind_device(&rq);
215 }
216 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800217 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else {
219 struct block_device *bdev;
220
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800221 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 bdev = rawdev->binding;
223 if (bdev) {
224 rq.block_major = MAJOR(bdev->bd_dev);
225 rq.block_minor = MINOR(bdev->bd_dev);
226 } else {
227 rq.block_major = rq.block_minor = 0;
228 }
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800229 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
231 err = -EFAULT;
232 goto out;
233 }
234 }
235 break;
236 default:
237 err = -EINVAL;
238 break;
239 }
240out:
241 return err;
242}
243
Arjan van de Ven62322d22006-07-03 00:24:21 -0700244static const struct file_operations raw_fops = {
Badari Pulavarty543ade12006-09-30 23:28:48 -0700245 .read = do_sync_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .aio_read = generic_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700247 .write = do_sync_write,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700248 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .open = raw_open,
250 .release= raw_release,
251 .ioctl = raw_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .owner = THIS_MODULE,
253};
254
Arjan van de Ven62322d22006-07-03 00:24:21 -0700255static const struct file_operations raw_ctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .ioctl = raw_ctl_ioctl,
257 .open = raw_open,
258 .owner = THIS_MODULE,
259};
260
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700261static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263static int __init raw_init(void)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700266 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700268 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
269 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto error;
271
272 cdev_init(&raw_cdev, &raw_fops);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700273 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
274 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 kobject_put(&raw_cdev.kobj);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700276 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800279 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (IS_ERR(raw_class)) {
281 printk(KERN_ERR "Error creating raw class.\n");
282 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700283 ret = PTR_ERR(raw_class);
284 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700286 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700290error_region:
291 unregister_chrdev_region(dev, MAX_RAW_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292error:
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static void __exit raw_exit(void)
297{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700298 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800299 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 cdev_del(&raw_cdev);
301 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
302}
303
304module_init(raw_init);
305module_exit(raw_exit);
306MODULE_LICENSE("GPL");