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/gfp.h>
23 #include <linux/compat.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
;
58 mutex_lock(&raw_mutex
);
61 * All we need to do on open is check that the device is bound.
63 bdev
= raw_devices
[minor
].binding
;
67 igrab(bdev
->bd_inode
);
68 err
= blkdev_get(bdev
, filp
->f_mode
| FMODE_EXCL
, raw_open
);
71 err
= set_blocksize(bdev
, bdev_logical_block_size(bdev
));
74 filp
->f_flags
|= O_DIRECT
;
75 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
76 if (++raw_devices
[minor
].inuse
== 1)
77 filp
->f_path
.dentry
->d_inode
->i_mapping
=
78 bdev
->bd_inode
->i_mapping
;
79 filp
->private_data
= bdev
;
80 mutex_unlock(&raw_mutex
);
84 blkdev_put(bdev
, filp
->f_mode
| FMODE_EXCL
);
86 mutex_unlock(&raw_mutex
);
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
;
99 mutex_lock(&raw_mutex
);
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
;
106 mutex_unlock(&raw_mutex
);
108 blkdev_put(bdev
, filp
->f_mode
| FMODE_EXCL
);
113 * Forward ioctls to the underlying block device.
116 raw_ioctl(struct file
*filp
, unsigned int command
, unsigned long arg
)
118 struct block_device
*bdev
= filp
->private_data
;
119 return blkdev_ioctl(bdev
, 0, command
, arg
);
122 static int bind_set(int number
, u64 major
, u64 minor
)
124 dev_t dev
= MKDEV(major
, minor
);
125 struct raw_device_data
*rawdev
;
128 if (number
<= 0 || number
>= MAX_RAW_MINORS
)
131 if (MAJOR(dev
) != major
|| MINOR(dev
) != minor
)
134 rawdev
= &raw_devices
[number
];
137 * This is like making block devices, so demand the
140 if (!capable(CAP_SYS_ADMIN
))
144 * For now, we don't need to check that the underlying
145 * block device is present or not: we can do that when
146 * the raw device is opened. Just check that the
147 * major/minor numbers make sense.
150 if (MAJOR(dev
) == 0 && dev
!= 0)
153 mutex_lock(&raw_mutex
);
155 mutex_unlock(&raw_mutex
);
158 if (rawdev
->binding
) {
159 bdput(rawdev
->binding
);
160 module_put(THIS_MODULE
);
164 rawdev
->binding
= NULL
;
165 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, number
));
167 rawdev
->binding
= bdget(dev
);
168 if (rawdev
->binding
== NULL
) {
171 dev_t raw
= MKDEV(RAW_MAJOR
, number
);
172 __module_get(THIS_MODULE
);
173 device_destroy(raw_class
, raw
);
174 device_create(raw_class
, NULL
, raw
, NULL
,
178 mutex_unlock(&raw_mutex
);
182 static int bind_get(int number
, dev_t
*dev
)
184 struct raw_device_data
*rawdev
;
185 struct block_device
*bdev
;
187 if (number
<= 0 || number
>= MAX_RAW_MINORS
)
190 rawdev
= &raw_devices
[number
];
192 mutex_lock(&raw_mutex
);
193 bdev
= rawdev
->binding
;
194 *dev
= bdev
? bdev
->bd_dev
: 0;
195 mutex_unlock(&raw_mutex
);
200 * Deal with ioctls against the raw-device control interface, to bind
201 * and unbind other raw devices.
203 static long raw_ctl_ioctl(struct file
*filp
, unsigned int command
,
206 struct raw_config_request rq
;
212 if (copy_from_user(&rq
, (void __user
*) arg
, sizeof(rq
)))
215 return bind_set(rq
.raw_minor
, rq
.block_major
, rq
.block_minor
);
218 if (copy_from_user(&rq
, (void __user
*) arg
, sizeof(rq
)))
221 err
= bind_get(rq
.raw_minor
, &dev
);
225 rq
.block_major
= MAJOR(dev
);
226 rq
.block_minor
= MINOR(dev
);
228 if (copy_to_user((void __user
*)arg
, &rq
, sizeof(rq
)))
238 struct raw32_config_request
{
239 compat_int_t raw_minor
;
240 compat_u64 block_major
;
241 compat_u64 block_minor
;
244 static long raw_ctl_compat_ioctl(struct file
*file
, unsigned int cmd
,
247 struct raw32_config_request __user
*user_req
= compat_ptr(arg
);
248 struct raw32_config_request rq
;
254 if (copy_from_user(&rq
, user_req
, sizeof(rq
)))
257 return bind_set(rq
.raw_minor
, rq
.block_major
, rq
.block_minor
);
260 if (copy_from_user(&rq
, user_req
, sizeof(rq
)))
263 err
= bind_get(rq
.raw_minor
, &dev
);
267 rq
.block_major
= MAJOR(dev
);
268 rq
.block_minor
= MINOR(dev
);
270 if (copy_to_user(user_req
, &rq
, sizeof(rq
)))
280 static const struct file_operations raw_fops
= {
281 .read
= do_sync_read
,
282 .aio_read
= generic_file_aio_read
,
283 .write
= do_sync_write
,
284 .aio_write
= blkdev_aio_write
,
285 .fsync
= blkdev_fsync
,
287 .release
= raw_release
,
288 .unlocked_ioctl
= raw_ioctl
,
289 .llseek
= default_llseek
,
290 .owner
= THIS_MODULE
,
293 static const struct file_operations raw_ctl_fops
= {
294 .unlocked_ioctl
= raw_ctl_ioctl
,
296 .compat_ioctl
= raw_ctl_compat_ioctl
,
299 .owner
= THIS_MODULE
,
300 .llseek
= noop_llseek
,
303 static struct cdev raw_cdev
;
305 static char *raw_devnode(struct device
*dev
, mode_t
*mode
)
307 return kasprintf(GFP_KERNEL
, "raw/%s", dev_name(dev
));
310 static int __init
raw_init(void)
312 dev_t dev
= MKDEV(RAW_MAJOR
, 0);
315 ret
= register_chrdev_region(dev
, MAX_RAW_MINORS
, "raw");
319 cdev_init(&raw_cdev
, &raw_fops
);
320 ret
= cdev_add(&raw_cdev
, dev
, MAX_RAW_MINORS
);
322 kobject_put(&raw_cdev
.kobj
);
326 raw_class
= class_create(THIS_MODULE
, "raw");
327 if (IS_ERR(raw_class
)) {
328 printk(KERN_ERR
"Error creating raw class.\n");
330 ret
= PTR_ERR(raw_class
);
333 raw_class
->devnode
= raw_devnode
;
334 device_create(raw_class
, NULL
, MKDEV(RAW_MAJOR
, 0), NULL
, "rawctl");
339 unregister_chrdev_region(dev
, MAX_RAW_MINORS
);
344 static void __exit
raw_exit(void)
346 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, 0));
347 class_destroy(raw_class
);
349 unregister_chrdev_region(MKDEV(RAW_MAJOR
, 0), MAX_RAW_MINORS
);
352 module_init(raw_init
);
353 module_exit(raw_exit
);
354 MODULE_LICENSE("GPL");