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/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>
21 #include <linux/mutex.h>
22 #include <linux/smp_lock.h>
23 #include <linux/gfp.h>
25 #include <asm/uaccess.h>
27 struct raw_device_data
{
28 struct block_device
*binding
;
32 static struct class *raw_class
;
33 static struct raw_device_data raw_devices
[MAX_RAW_MINORS
];
34 static DEFINE_MUTEX(raw_mutex
);
35 static const struct file_operations raw_ctl_fops
; /* forward declaration */
38 * Open/close code for raw IO.
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
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.
47 static int raw_open(struct inode
*inode
, struct file
*filp
)
49 const int minor
= iminor(inode
);
50 struct block_device
*bdev
;
53 if (minor
== 0) { /* It is the control device */
54 filp
->f_op
= &raw_ctl_fops
;
59 mutex_lock(&raw_mutex
);
62 * All we need to do on open is check that the device is bound.
64 bdev
= raw_devices
[minor
].binding
;
68 igrab(bdev
->bd_inode
);
69 err
= blkdev_get(bdev
, filp
->f_mode
);
72 err
= bd_claim(bdev
, raw_open
);
75 err
= set_blocksize(bdev
, bdev_logical_block_size(bdev
));
78 filp
->f_flags
|= O_DIRECT
;
79 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
80 if (++raw_devices
[minor
].inuse
== 1)
81 filp
->f_path
.dentry
->d_inode
->i_mapping
=
82 bdev
->bd_inode
->i_mapping
;
83 filp
->private_data
= bdev
;
84 mutex_unlock(&raw_mutex
);
91 blkdev_put(bdev
, filp
->f_mode
);
93 mutex_unlock(&raw_mutex
);
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.
102 static int raw_release(struct inode
*inode
, struct file
*filp
)
104 const int minor
= iminor(inode
);
105 struct block_device
*bdev
;
107 mutex_lock(&raw_mutex
);
108 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
;
114 mutex_unlock(&raw_mutex
);
117 blkdev_put(bdev
, filp
->f_mode
);
122 * Forward ioctls to the underlying block device.
125 raw_ioctl(struct inode
*inode
, struct file
*filp
,
126 unsigned int command
, unsigned long arg
)
128 struct block_device
*bdev
= filp
->private_data
;
130 return blkdev_ioctl(bdev
, 0, command
, arg
);
133 static void bind_device(struct raw_config_request
*rq
)
135 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, rq
->raw_minor
));
136 device_create(raw_class
, NULL
, MKDEV(RAW_MAJOR
, rq
->raw_minor
), NULL
,
137 "raw%d", rq
->raw_minor
);
141 * Deal with ioctls against the raw-device control interface, to bind
142 * and unbind other raw devices.
144 static int raw_ctl_ioctl(struct inode
*inode
, struct file
*filp
,
145 unsigned int command
, unsigned long arg
)
147 struct raw_config_request rq
;
148 struct raw_device_data
*rawdev
;
155 /* First, find out which raw minor we want */
157 if (copy_from_user(&rq
, (void __user
*) arg
, sizeof(rq
))) {
162 if (rq
.raw_minor
<= 0 || rq
.raw_minor
>= MAX_RAW_MINORS
) {
166 rawdev
= &raw_devices
[rq
.raw_minor
];
168 if (command
== RAW_SETBIND
) {
172 * This is like making block devices, so demand the
175 if (!capable(CAP_SYS_ADMIN
)) {
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.
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
) {
195 mutex_lock(&raw_mutex
);
197 mutex_unlock(&raw_mutex
);
201 if (rawdev
->binding
) {
202 bdput(rawdev
->binding
);
203 module_put(THIS_MODULE
);
205 if (rq
.block_major
== 0 && rq
.block_minor
== 0) {
207 rawdev
->binding
= NULL
;
208 device_destroy(raw_class
,
209 MKDEV(RAW_MAJOR
, rq
.raw_minor
));
211 rawdev
->binding
= bdget(dev
);
212 if (rawdev
->binding
== NULL
)
215 __module_get(THIS_MODULE
);
219 mutex_unlock(&raw_mutex
);
221 struct block_device
*bdev
;
223 mutex_lock(&raw_mutex
);
224 bdev
= rawdev
->binding
;
226 rq
.block_major
= MAJOR(bdev
->bd_dev
);
227 rq
.block_minor
= MINOR(bdev
->bd_dev
);
229 rq
.block_major
= rq
.block_minor
= 0;
231 mutex_unlock(&raw_mutex
);
232 if (copy_to_user((void __user
*)arg
, &rq
, sizeof(rq
))) {
246 static const struct file_operations raw_fops
= {
247 .read
= do_sync_read
,
248 .aio_read
= generic_file_aio_read
,
249 .write
= do_sync_write
,
250 .aio_write
= blkdev_aio_write
,
251 .fsync
= blkdev_fsync
,
253 .release
= raw_release
,
255 .owner
= THIS_MODULE
,
258 static const struct file_operations raw_ctl_fops
= {
259 .ioctl
= raw_ctl_ioctl
,
261 .owner
= THIS_MODULE
,
264 static struct cdev raw_cdev
;
266 static char *raw_devnode(struct device
*dev
, mode_t
*mode
)
268 return kasprintf(GFP_KERNEL
, "raw/%s", dev_name(dev
));
271 static int __init
raw_init(void)
273 dev_t dev
= MKDEV(RAW_MAJOR
, 0);
276 ret
= register_chrdev_region(dev
, MAX_RAW_MINORS
, "raw");
280 cdev_init(&raw_cdev
, &raw_fops
);
281 ret
= cdev_add(&raw_cdev
, dev
, MAX_RAW_MINORS
);
283 kobject_put(&raw_cdev
.kobj
);
287 raw_class
= class_create(THIS_MODULE
, "raw");
288 if (IS_ERR(raw_class
)) {
289 printk(KERN_ERR
"Error creating raw class.\n");
291 ret
= PTR_ERR(raw_class
);
294 raw_class
->devnode
= raw_devnode
;
295 device_create(raw_class
, NULL
, MKDEV(RAW_MAJOR
, 0), NULL
, "rawctl");
300 unregister_chrdev_region(dev
, MAX_RAW_MINORS
);
305 static void __exit
raw_exit(void)
307 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, 0));
308 class_destroy(raw_class
);
310 unregister_chrdev_region(MKDEV(RAW_MAJOR
, 0), MAX_RAW_MINORS
);
313 module_init(raw_init
);
314 module_exit(raw_exit
);
315 MODULE_LICENSE("GPL");