2 * linux/drivers/char/raw.c
4 * Front-end raw character devices. These can be bound to any block
5 * devices to provide genuine Unix raw character device semantics.
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.
11 #include <linux/init.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 #include <linux/cdev.h>
22 #include <asm/uaccess.h>
24 struct raw_device_data {
25 struct block_device *binding;
29 static struct raw_device_data raw_devices[MAX_RAW_MINORS];
30 static DECLARE_MUTEX(raw_mutex);
31 static struct file_operations raw_ctl_fops; /* forward declaration */
34 * Open/close code for raw IO.
36 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
37 * point at the blockdev's address_space and set the file handle to use
40 * Set the device's soft blocksize to the minimum possible. This gives the
41 * finest possible alignment and has no adverse impact on performance.
43 static int raw_open(struct inode *inode, struct file *filp)
45 const int minor = iminor(inode);
46 struct block_device *bdev;
49 if (minor == 0) { /* It is the control device */
50 filp->f_op = &raw_ctl_fops;
57 * All we need to do on open is check that the device is bound.
59 bdev = raw_devices[minor].binding;
63 igrab(bdev->bd_inode);
64 err = blkdev_get(bdev, filp->f_mode, 0);
67 err = bd_claim(bdev, raw_open);
70 err = set_blocksize(bdev, bdev_hardsect_size(bdev));
73 filp->f_flags |= O_DIRECT;
74 filp->f_mapping = bdev->bd_inode->i_mapping;
75 if (++raw_devices[minor].inuse == 1)
76 filp->f_dentry->d_inode->i_mapping =
77 bdev->bd_inode->i_mapping;
78 filp->private_data = bdev;
92 * When the final fd which refers to this character-special node is closed, we
93 * make its ->mapping point back at its own i_data.
95 static int raw_release(struct inode *inode, struct file *filp)
97 const int minor= iminor(inode);
98 struct block_device *bdev;
101 bdev = raw_devices[minor].binding;
102 if (--raw_devices[minor].inuse == 0) {
103 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
104 inode->i_mapping = &inode->i_data;
105 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
115 * Forward ioctls to the underlying block device.
118 raw_ioctl(struct inode *inode, struct file *filp,
119 unsigned int command, unsigned long arg)
121 struct block_device *bdev = filp->private_data;
123 return ioctl_by_bdev(bdev, command, arg);
127 * Deal with ioctls against the raw-device control interface, to bind
128 * and unbind other raw devices.
130 static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
131 unsigned int command, unsigned long arg)
133 struct raw_config_request rq;
134 struct raw_device_data *rawdev;
141 /* First, find out which raw minor we want */
143 if (copy_from_user(&rq, (void *) arg, sizeof(rq))) {
148 if (rq.raw_minor < 0 || rq.raw_minor >= MAX_RAW_MINORS) {
152 rawdev = &raw_devices[rq.raw_minor];
154 if (command == RAW_SETBIND) {
158 * This is like making block devices, so demand the
161 if (!capable(CAP_SYS_ADMIN)) {
167 * For now, we don't need to check that the underlying
168 * block device is present or not: we can do that when
169 * the raw device is opened. Just check that the
170 * major/minor numbers make sense.
173 dev = MKDEV(rq.block_major, rq.block_minor);
174 if ((rq.block_major == 0 && rq.block_minor != 0) ||
175 MAJOR(dev) != rq.block_major ||
176 MINOR(dev) != rq.block_minor) {
187 if (rawdev->binding) {
188 bdput(rawdev->binding);
189 module_put(THIS_MODULE);
191 if (rq.block_major == 0 && rq.block_minor == 0) {
193 rawdev->binding = NULL;
195 rawdev->binding = bdget(dev);
196 if (rawdev->binding == NULL)
199 __module_get(THIS_MODULE);
203 struct block_device *bdev;
206 bdev = rawdev->binding;
208 rq.block_major = MAJOR(bdev->bd_dev);
209 rq.block_minor = MINOR(bdev->bd_dev);
211 rq.block_major = rq.block_minor = 0;
214 if (copy_to_user((void *)arg, &rq, sizeof(rq))) {
228 static ssize_t raw_file_write(struct file *file, const char *buf,
229 size_t count, loff_t *ppos)
231 struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
233 return generic_file_write_nolock(file, &local_iov, 1, ppos);
236 static ssize_t raw_file_aio_write(struct kiocb *iocb, const char *buf,
237 size_t count, loff_t pos)
239 struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
241 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
245 static struct file_operations raw_fops = {
246 .read = generic_file_read,
247 .aio_read = generic_file_aio_read,
248 .write = raw_file_write,
249 .aio_write = raw_file_aio_write,
251 .release= raw_release,
253 .readv = generic_file_readv,
254 .writev = generic_file_writev,
255 .owner = THIS_MODULE,
258 static struct file_operations raw_ctl_fops = {
259 .ioctl = raw_ctl_ioctl,
261 .owner = THIS_MODULE,
264 static struct cdev raw_cdev = {
265 .kobj = {.name = "raw", },
266 .owner = THIS_MODULE,
269 static int __init raw_init(void)
272 dev_t dev = MKDEV(RAW_MAJOR, 0);
274 if (register_chrdev_region(dev, MAX_RAW_MINORS, "raw"))
277 cdev_init(&raw_cdev, &raw_fops);
278 if (cdev_add(&raw_cdev, dev, MAX_RAW_MINORS)) {
279 kobject_put(&raw_cdev.kobj);
280 unregister_chrdev_region(dev, MAX_RAW_MINORS);
284 devfs_mk_cdev(MKDEV(RAW_MAJOR, 0),
285 S_IFCHR | S_IRUGO | S_IWUGO,
287 for (i = 1; i < MAX_RAW_MINORS; i++)
288 devfs_mk_cdev(MKDEV(RAW_MAJOR, i),
289 S_IFCHR | S_IRUGO | S_IWUGO,
294 printk(KERN_ERR "error register raw device\n");
298 static void __exit raw_exit(void)
302 for (i = 1; i < MAX_RAW_MINORS; i++)
303 devfs_remove("raw/raw%d", i);
304 devfs_remove("raw/rawctl");
307 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
310 module_init(raw_init);
311 module_exit(raw_exit);
312 MODULE_LICENSE("GPL");