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>
24 #include <linux/vmalloc.h>
26 #include <asm/uaccess.h>
28 struct raw_device_data
{
29 struct block_device
*binding
;
33 static struct class *raw_class
;
34 static struct raw_device_data
*raw_devices
;
35 static DEFINE_MUTEX(raw_mutex
);
36 static const struct file_operations raw_ctl_fops
; /* forward declaration */
38 static int max_raw_minors
= MAX_RAW_MINORS
;
40 module_param(max_raw_minors
, int, 0);
41 MODULE_PARM_DESC(max_raw_minors
, "Maximum number of raw devices (1-65536)");
44 * Open/close code for raw IO.
46 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
47 * point at the blockdev's address_space and set the file handle to use
50 * Set the device's soft blocksize to the minimum possible. This gives the
51 * finest possible alignment and has no adverse impact on performance.
53 static int raw_open(struct inode
*inode
, struct file
*filp
)
55 const int minor
= iminor(inode
);
56 struct block_device
*bdev
;
59 if (minor
== 0) { /* It is the control device */
60 filp
->f_op
= &raw_ctl_fops
;
64 mutex_lock(&raw_mutex
);
67 * All we need to do on open is check that the device is bound.
69 bdev
= raw_devices
[minor
].binding
;
73 igrab(bdev
->bd_inode
);
74 err
= blkdev_get(bdev
, filp
->f_mode
| FMODE_EXCL
, raw_open
);
77 err
= set_blocksize(bdev
, bdev_logical_block_size(bdev
));
80 filp
->f_flags
|= O_DIRECT
;
81 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
82 if (++raw_devices
[minor
].inuse
== 1)
83 file_inode(filp
)->i_mapping
=
84 bdev
->bd_inode
->i_mapping
;
85 filp
->private_data
= bdev
;
86 mutex_unlock(&raw_mutex
);
90 blkdev_put(bdev
, filp
->f_mode
| FMODE_EXCL
);
92 mutex_unlock(&raw_mutex
);
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.
100 static int raw_release(struct inode
*inode
, struct file
*filp
)
102 const int minor
= iminor(inode
);
103 struct block_device
*bdev
;
105 mutex_lock(&raw_mutex
);
106 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 mutex_unlock(&raw_mutex
);
112 blkdev_put(bdev
, filp
->f_mode
| FMODE_EXCL
);
117 * Forward ioctls to the underlying block device.
120 raw_ioctl(struct file
*filp
, unsigned int command
, unsigned long arg
)
122 struct block_device
*bdev
= filp
->private_data
;
123 return blkdev_ioctl(bdev
, 0, command
, arg
);
126 static int bind_set(int number
, u64 major
, u64 minor
)
128 dev_t dev
= MKDEV(major
, minor
);
129 struct raw_device_data
*rawdev
;
132 if (number
<= 0 || number
>= max_raw_minors
)
135 if (MAJOR(dev
) != major
|| MINOR(dev
) != minor
)
138 rawdev
= &raw_devices
[number
];
141 * This is like making block devices, so demand the
144 if (!capable(CAP_SYS_ADMIN
))
148 * For now, we don't need to check that the underlying
149 * block device is present or not: we can do that when
150 * the raw device is opened. Just check that the
151 * major/minor numbers make sense.
154 if (MAJOR(dev
) == 0 && dev
!= 0)
157 mutex_lock(&raw_mutex
);
159 mutex_unlock(&raw_mutex
);
162 if (rawdev
->binding
) {
163 bdput(rawdev
->binding
);
164 module_put(THIS_MODULE
);
168 rawdev
->binding
= NULL
;
169 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, number
));
171 rawdev
->binding
= bdget(dev
);
172 if (rawdev
->binding
== NULL
) {
175 dev_t raw
= MKDEV(RAW_MAJOR
, number
);
176 __module_get(THIS_MODULE
);
177 device_destroy(raw_class
, raw
);
178 device_create(raw_class
, NULL
, raw
, NULL
,
182 mutex_unlock(&raw_mutex
);
186 static int bind_get(int number
, dev_t
*dev
)
188 struct raw_device_data
*rawdev
;
189 struct block_device
*bdev
;
191 if (number
<= 0 || number
>= max_raw_minors
)
194 rawdev
= &raw_devices
[number
];
196 mutex_lock(&raw_mutex
);
197 bdev
= rawdev
->binding
;
198 *dev
= bdev
? bdev
->bd_dev
: 0;
199 mutex_unlock(&raw_mutex
);
204 * Deal with ioctls against the raw-device control interface, to bind
205 * and unbind other raw devices.
207 static long raw_ctl_ioctl(struct file
*filp
, unsigned int command
,
210 struct raw_config_request rq
;
216 if (copy_from_user(&rq
, (void __user
*) arg
, sizeof(rq
)))
219 return bind_set(rq
.raw_minor
, rq
.block_major
, rq
.block_minor
);
222 if (copy_from_user(&rq
, (void __user
*) arg
, sizeof(rq
)))
225 err
= bind_get(rq
.raw_minor
, &dev
);
229 rq
.block_major
= MAJOR(dev
);
230 rq
.block_minor
= MINOR(dev
);
232 if (copy_to_user((void __user
*)arg
, &rq
, sizeof(rq
)))
242 struct raw32_config_request
{
243 compat_int_t raw_minor
;
244 compat_u64 block_major
;
245 compat_u64 block_minor
;
248 static long raw_ctl_compat_ioctl(struct file
*file
, unsigned int cmd
,
251 struct raw32_config_request __user
*user_req
= compat_ptr(arg
);
252 struct raw32_config_request rq
;
258 if (copy_from_user(&rq
, user_req
, sizeof(rq
)))
261 return bind_set(rq
.raw_minor
, rq
.block_major
, rq
.block_minor
);
264 if (copy_from_user(&rq
, user_req
, sizeof(rq
)))
267 err
= bind_get(rq
.raw_minor
, &dev
);
271 rq
.block_major
= MAJOR(dev
);
272 rq
.block_minor
= MINOR(dev
);
274 if (copy_to_user(user_req
, &rq
, sizeof(rq
)))
284 static const struct file_operations raw_fops
= {
285 .read
= new_sync_read
,
286 .read_iter
= blkdev_read_iter
,
287 .write
= new_sync_write
,
288 .write_iter
= blkdev_write_iter
,
289 .fsync
= blkdev_fsync
,
291 .release
= raw_release
,
292 .unlocked_ioctl
= raw_ioctl
,
293 .llseek
= default_llseek
,
294 .owner
= THIS_MODULE
,
297 static const struct file_operations raw_ctl_fops
= {
298 .unlocked_ioctl
= raw_ctl_ioctl
,
300 .compat_ioctl
= raw_ctl_compat_ioctl
,
303 .owner
= THIS_MODULE
,
304 .llseek
= noop_llseek
,
307 static struct cdev raw_cdev
;
309 static char *raw_devnode(struct device
*dev
, umode_t
*mode
)
311 return kasprintf(GFP_KERNEL
, "raw/%s", dev_name(dev
));
314 static int __init
raw_init(void)
316 dev_t dev
= MKDEV(RAW_MAJOR
, 0);
319 if (max_raw_minors
< 1 || max_raw_minors
> 65536) {
320 printk(KERN_WARNING
"raw: invalid max_raw_minors (must be"
321 " between 1 and 65536), using %d\n", MAX_RAW_MINORS
);
322 max_raw_minors
= MAX_RAW_MINORS
;
325 raw_devices
= vzalloc(sizeof(struct raw_device_data
) * max_raw_minors
);
327 printk(KERN_ERR
"Not enough memory for raw device structures\n");
332 ret
= register_chrdev_region(dev
, max_raw_minors
, "raw");
336 cdev_init(&raw_cdev
, &raw_fops
);
337 ret
= cdev_add(&raw_cdev
, dev
, max_raw_minors
);
342 raw_class
= class_create(THIS_MODULE
, "raw");
343 if (IS_ERR(raw_class
)) {
344 printk(KERN_ERR
"Error creating raw class.\n");
346 ret
= PTR_ERR(raw_class
);
349 raw_class
->devnode
= raw_devnode
;
350 device_create(raw_class
, NULL
, MKDEV(RAW_MAJOR
, 0), NULL
, "rawctl");
355 unregister_chrdev_region(dev
, max_raw_minors
);
361 static void __exit
raw_exit(void)
363 device_destroy(raw_class
, MKDEV(RAW_MAJOR
, 0));
364 class_destroy(raw_class
);
366 unregister_chrdev_region(MKDEV(RAW_MAJOR
, 0), max_raw_minors
);
369 module_init(raw_init
);
370 module_exit(raw_exit
);
371 MODULE_LICENSE("GPL");