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>
21 #include <asm/uaccess.h>
23 struct raw_device_data {
24 struct block_device *binding;
28 static struct raw_device_data raw_devices[MAX_RAW_MINORS];
29 static DECLARE_MUTEX(raw_mutex);
30 static struct file_operations raw_ctl_fops; /* forward declaration */
33 * Open/close code for raw IO.
35 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
36 * point at the blockdev's address_space and set the file handle to use
39 * Set the device's soft blocksize to the minimum possible. This gives the
40 * finest possible alignment and has no adverse impact on performance.
42 static int raw_open(struct inode *inode, struct file *filp)
44 const int minor = iminor(inode);
45 struct block_device *bdev;
48 if (minor == 0) { /* It is the control device */
49 filp->f_op = &raw_ctl_fops;
56 * All we need to do on open is check that the device is bound.
58 bdev = raw_devices[minor].binding;
62 igrab(bdev->bd_inode);
63 err = blkdev_get(bdev, filp->f_mode, 0, BDEV_RAW);
66 err = bd_claim(bdev, raw_open);
69 err = set_blocksize(bdev, bdev_hardsect_size(bdev));
72 filp->f_flags |= O_DIRECT;
73 filp->f_mapping = bdev->bd_inode->i_mapping;
74 if (++raw_devices[minor].inuse == 1)
75 filp->f_dentry->d_inode->i_mapping =
76 bdev->bd_inode->i_mapping;
77 filp->private_data = bdev;
84 blkdev_put(bdev, BDEV_RAW);
91 * When the final fd which refers to this character-special node is closed, we
92 * make its ->mapping point back at its own i_data.
94 static int raw_release(struct inode *inode, struct file *filp)
96 const int minor= iminor(inode);
97 struct block_device *bdev;
100 bdev = raw_devices[minor].binding;
101 if (--raw_devices[minor].inuse == 0) {
102 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
103 inode->i_mapping = &inode->i_data;
104 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
109 blkdev_put(bdev, BDEV_RAW);
114 * Forward ioctls to the underlying block device.
117 raw_ioctl(struct inode *inode, struct file *filp,
118 unsigned int command, unsigned long arg)
120 struct block_device *bdev = filp->private_data;
122 return ioctl_by_bdev(bdev, command, arg);
126 * Deal with ioctls against the raw-device control interface, to bind
127 * and unbind other raw devices.
129 static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
130 unsigned int command, unsigned long arg)
132 struct raw_config_request rq;
133 struct raw_device_data *rawdev;
140 /* First, find out which raw minor we want */
142 if (copy_from_user(&rq, (void *) arg, sizeof(rq))) {
147 if (rq.raw_minor < 0 || rq.raw_minor >= MAX_RAW_MINORS) {
151 rawdev = &raw_devices[rq.raw_minor];
153 if (command == RAW_SETBIND) {
157 * This is like making block devices, so demand the
160 if (!capable(CAP_SYS_ADMIN)) {
166 * For now, we don't need to check that the underlying
167 * block device is present or not: we can do that when
168 * the raw device is opened. Just check that the
169 * major/minor numbers make sense.
172 dev = MKDEV(rq.block_major, rq.block_minor);
173 if ((rq.block_major == 0 && rq.block_minor != 0) ||
174 MAJOR(dev) != rq.block_major ||
175 MINOR(dev) != rq.block_minor) {
186 if (rawdev->binding) {
187 bdput(rawdev->binding);
188 module_put(THIS_MODULE);
190 if (rq.block_major == 0 && rq.block_minor == 0) {
192 rawdev->binding = NULL;
194 rawdev->binding = bdget(dev);
195 if (rawdev->binding == NULL)
198 __module_get(THIS_MODULE);
202 struct block_device *bdev;
205 bdev = rawdev->binding;
207 rq.block_major = MAJOR(bdev->bd_dev);
208 rq.block_minor = MINOR(bdev->bd_dev);
210 rq.block_major = rq.block_minor = 0;
213 if (copy_to_user((void *)arg, &rq, sizeof(rq))) {
227 static ssize_t raw_file_write(struct file *file, const char *buf,
228 size_t count, loff_t *ppos)
230 struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
232 return generic_file_write_nolock(file, &local_iov, 1, ppos);
235 static ssize_t raw_file_aio_write(struct kiocb *iocb, const char *buf,
236 size_t count, loff_t pos)
238 struct iovec local_iov = { .iov_base = (void *)buf, .iov_len = count };
240 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
244 static struct file_operations raw_fops = {
245 .read = generic_file_read,
246 .aio_read = generic_file_aio_read,
247 .write = raw_file_write,
248 .aio_write = raw_file_aio_write,
250 .release= raw_release,
252 .readv = generic_file_readv,
253 .writev = generic_file_writev,
254 .owner = THIS_MODULE,
257 static struct file_operations raw_ctl_fops = {
258 .ioctl = raw_ctl_ioctl,
260 .owner = THIS_MODULE,
263 static int __init raw_init(void)
267 register_chrdev(RAW_MAJOR, "raw", &raw_fops);
268 devfs_mk_cdev(MKDEV(RAW_MAJOR, 0),
269 S_IFCHR | S_IRUGO | S_IWUGO,
271 for (i = 1; i < MAX_RAW_MINORS; i++)
272 devfs_mk_cdev(MKDEV(RAW_MAJOR, i),
273 S_IFCHR | S_IRUGO | S_IWUGO,
278 static void __exit raw_exit(void)
282 for (i = 1; i < MAX_RAW_MINORS; i++)
283 devfs_remove("raw/raw%d", i);
284 devfs_remove("raw/rawctl");
286 unregister_chrdev(RAW_MAJOR, "raw");
289 module_init(raw_init);
290 module_exit(raw_exit);
291 MODULE_LICENSE("GPL");