blob: e83b2adc014ad92c374a1e136a158612d3fd1883 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/raw.c
3 *
4 * Front-end raw character devices. These can be bound to any block
5 * devices to provide genuine Unix raw character device semantics.
6 *
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.
9 */
10
11#include <linux/init.h>
12#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/major.h>
14#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040015#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/raw.h>
18#include <linux/capability.h>
19#include <linux/uio.h>
20#include <linux/cdev.h>
21#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080022#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Al Viroc4a04722009-08-24 22:42:56 +000024#include <linux/compat.h>
Jan Kara0078bff2011-04-29 00:24:29 +020025#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/uaccess.h>
28
29struct raw_device_data {
30 struct block_device *binding;
31 int inuse;
32};
33
gregkh@suse.deca8eca62005-03-23 09:53:09 -080034static struct class *raw_class;
Jan Kara0078bff2011-04-29 00:24:29 +020035static struct raw_device_data *raw_devices;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080036static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070037static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jan Kara0078bff2011-04-29 00:24:29 +020039static int max_raw_minors = MAX_RAW_MINORS;
40
41module_param(max_raw_minors, int, 0);
42MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Open/close code for raw IO.
46 *
47 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
48 * point at the blockdev's address_space and set the file handle to use
49 * O_DIRECT.
50 *
51 * Set the device's soft blocksize to the minimum possible. This gives the
52 * finest possible alignment and has no adverse impact on performance.
53 */
54static int raw_open(struct inode *inode, struct file *filp)
55{
56 const int minor = iminor(inode);
57 struct block_device *bdev;
58 int err;
59
60 if (minor == 0) { /* It is the control device */
61 filp->f_op = &raw_ctl_fops;
62 return 0;
63 }
64
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080065 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /*
68 * All we need to do on open is check that the device is bound.
69 */
70 bdev = raw_devices[minor].binding;
71 err = -ENODEV;
72 if (!bdev)
73 goto out;
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +010074 bdgrab(bdev);
Tejun Heoe525fd82010-11-13 11:55:17 +010075 err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (err)
77 goto out;
Martin K. Petersene1defc42009-05-22 17:17:49 -040078 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (err)
Tejun Heoe525fd82010-11-13 11:55:17 +010080 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 filp->f_flags |= O_DIRECT;
82 filp->f_mapping = bdev->bd_inode->i_mapping;
83 if (++raw_devices[minor].inuse == 1)
Al Viro496ad9a2013-01-23 17:07:38 -050084 file_inode(filp)->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 bdev->bd_inode->i_mapping;
86 filp->private_data = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080087 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090out1:
Tejun Heoe525fd82010-11-13 11:55:17 +010091 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080093 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return err;
95}
96
97/*
98 * When the final fd which refers to this character-special node is closed, we
99 * make its ->mapping point back at its own i_data.
100 */
101static int raw_release(struct inode *inode, struct file *filp)
102{
103 const int minor= iminor(inode);
104 struct block_device *bdev;
105
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800106 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 bdev = raw_devices[minor].binding;
Christoph Hellwigb83ae6d2015-01-14 10:42:37 +0100108 if (--raw_devices[minor].inuse == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
110 inode->i_mapping = &inode->i_data;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800111 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Tejun Heoe525fd82010-11-13 11:55:17 +0100113 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116
117/*
118 * Forward ioctls to the underlying block device.
119 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200120static long
121raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct block_device *bdev = filp->private_data;
Al Viroc4a04722009-08-24 22:42:56 +0000124 return blkdev_ioctl(bdev, 0, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Al Viroc4a04722009-08-24 22:42:56 +0000127static int bind_set(int number, u64 major, u64 minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Al Viroc4a04722009-08-24 22:42:56 +0000129 dev_t dev = MKDEV(major, minor);
130 struct raw_device_data *rawdev;
131 int err = 0;
132
Jan Kara0078bff2011-04-29 00:24:29 +0200133 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a04722009-08-24 22:42:56 +0000134 return -EINVAL;
135
136 if (MAJOR(dev) != major || MINOR(dev) != minor)
137 return -EINVAL;
138
139 rawdev = &raw_devices[number];
140
141 /*
142 * This is like making block devices, so demand the
143 * same capability
144 */
145 if (!capable(CAP_SYS_ADMIN))
146 return -EPERM;
147
148 /*
149 * For now, we don't need to check that the underlying
150 * block device is present or not: we can do that when
151 * the raw device is opened. Just check that the
152 * major/minor numbers make sense.
153 */
154
155 if (MAJOR(dev) == 0 && dev != 0)
156 return -EINVAL;
157
158 mutex_lock(&raw_mutex);
159 if (rawdev->inuse) {
160 mutex_unlock(&raw_mutex);
161 return -EBUSY;
162 }
163 if (rawdev->binding) {
164 bdput(rawdev->binding);
165 module_put(THIS_MODULE);
166 }
167 if (!dev) {
168 /* unbind */
169 rawdev->binding = NULL;
170 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
171 } else {
172 rawdev->binding = bdget(dev);
173 if (rawdev->binding == NULL) {
174 err = -ENOMEM;
175 } else {
176 dev_t raw = MKDEV(RAW_MAJOR, number);
177 __module_get(THIS_MODULE);
178 device_destroy(raw_class, raw);
179 device_create(raw_class, NULL, raw, NULL,
180 "raw%d", number);
181 }
182 }
183 mutex_unlock(&raw_mutex);
184 return err;
185}
186
187static int bind_get(int number, dev_t *dev)
188{
189 struct raw_device_data *rawdev;
190 struct block_device *bdev;
191
Paul Bolle5bbb2ae2014-02-04 23:23:12 +0100192 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a04722009-08-24 22:42:56 +0000193 return -EINVAL;
194
195 rawdev = &raw_devices[number];
196
197 mutex_lock(&raw_mutex);
198 bdev = rawdev->binding;
199 *dev = bdev ? bdev->bd_dev : 0;
200 mutex_unlock(&raw_mutex);
201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
205 * Deal with ioctls against the raw-device control interface, to bind
206 * and unbind other raw devices.
207 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200208static long raw_ctl_ioctl(struct file *filp, unsigned int command,
209 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct raw_config_request rq;
Al Viroc4a04722009-08-24 22:42:56 +0000212 dev_t dev;
213 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 switch (command) {
216 case RAW_SETBIND:
Al Viroc4a04722009-08-24 22:42:56 +0000217 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
218 return -EFAULT;
219
220 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 case RAW_GETBIND:
Al Viroc4a04722009-08-24 22:42:56 +0000223 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
224 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Al Viroc4a04722009-08-24 22:42:56 +0000226 err = bind_get(rq.raw_minor, &dev);
227 if (err)
228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Al Viroc4a04722009-08-24 22:42:56 +0000230 rq.block_major = MAJOR(dev);
231 rq.block_minor = MINOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Al Viroc4a04722009-08-24 22:42:56 +0000233 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
234 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Al Viroc4a04722009-08-24 22:42:56 +0000236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Al Viroc4a04722009-08-24 22:42:56 +0000238
239 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Al Viroc4a04722009-08-24 22:42:56 +0000242#ifdef CONFIG_COMPAT
243struct raw32_config_request {
244 compat_int_t raw_minor;
245 compat_u64 block_major;
246 compat_u64 block_minor;
247};
248
249static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
250 unsigned long arg)
251{
252 struct raw32_config_request __user *user_req = compat_ptr(arg);
253 struct raw32_config_request rq;
254 dev_t dev;
255 int err = 0;
256
257 switch (cmd) {
258 case RAW_SETBIND:
259 if (copy_from_user(&rq, user_req, sizeof(rq)))
260 return -EFAULT;
261
262 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
263
264 case RAW_GETBIND:
265 if (copy_from_user(&rq, user_req, sizeof(rq)))
266 return -EFAULT;
267
268 err = bind_get(rq.raw_minor, &dev);
269 if (err)
270 return err;
271
272 rq.block_major = MAJOR(dev);
273 rq.block_minor = MINOR(dev);
274
275 if (copy_to_user(user_req, &rq, sizeof(rq)))
276 return -EFAULT;
277
278 return 0;
279 }
280
281 return -EINVAL;
282}
283#endif
284
Arjan van de Ven62322d22006-07-03 00:24:21 -0700285static const struct file_operations raw_fops = {
David Jefferyb2de5252014-09-29 10:21:10 -0400286 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -0400287 .write_iter = blkdev_write_iter,
Arnd Bergmann55929332010-04-27 00:24:05 +0200288 .fsync = blkdev_fsync,
289 .open = raw_open,
290 .release = raw_release,
291 .unlocked_ioctl = raw_ioctl,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200292 .llseek = default_llseek,
Arnd Bergmann55929332010-04-27 00:24:05 +0200293 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294};
295
Arjan van de Ven62322d22006-07-03 00:24:21 -0700296static const struct file_operations raw_ctl_fops = {
Arnd Bergmann55929332010-04-27 00:24:05 +0200297 .unlocked_ioctl = raw_ctl_ioctl,
Al Viroc4a04722009-08-24 22:42:56 +0000298#ifdef CONFIG_COMPAT
299 .compat_ioctl = raw_ctl_compat_ioctl,
300#endif
Arnd Bergmann55929332010-04-27 00:24:05 +0200301 .open = raw_open,
302 .owner = THIS_MODULE,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200303 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700306static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Al Viro2c9ede52011-07-23 20:24:48 -0400308static char *raw_devnode(struct device *dev, umode_t *mode)
Kay Sievers6fd46932009-04-30 15:23:42 +0200309{
310 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int __init raw_init(void)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700316 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jan Kara0078bff2011-04-29 00:24:29 +0200318 if (max_raw_minors < 1 || max_raw_minors > 65536) {
319 printk(KERN_WARNING "raw: invalid max_raw_minors (must be"
320 " between 1 and 65536), using %d\n", MAX_RAW_MINORS);
321 max_raw_minors = MAX_RAW_MINORS;
322 }
323
Joe Perches8e03bd62011-05-28 10:36:25 -0700324 raw_devices = vzalloc(sizeof(struct raw_device_data) * max_raw_minors);
Jan Kara0078bff2011-04-29 00:24:29 +0200325 if (!raw_devices) {
326 printk(KERN_ERR "Not enough memory for raw device structures\n");
327 ret = -ENOMEM;
328 goto error;
329 }
Jan Kara0078bff2011-04-29 00:24:29 +0200330
331 ret = register_chrdev_region(dev, max_raw_minors, "raw");
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700332 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto error;
334
335 cdev_init(&raw_cdev, &raw_fops);
Jan Kara0078bff2011-04-29 00:24:29 +0200336 ret = cdev_add(&raw_cdev, dev, max_raw_minors);
Bhumika Goyal202cdb62015-12-22 00:11:11 +0530337 if (ret)
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700338 goto error_region;
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800339 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (IS_ERR(raw_class)) {
341 printk(KERN_ERR "Error creating raw class.\n");
342 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700343 ret = PTR_ERR(raw_class);
344 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200346 raw_class->devnode = raw_devnode;
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700347 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700351error_region:
Jan Kara0078bff2011-04-29 00:24:29 +0200352 unregister_chrdev_region(dev, max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353error:
Jan Kara0078bff2011-04-29 00:24:29 +0200354 vfree(raw_devices);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static void __exit raw_exit(void)
359{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700360 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800361 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 cdev_del(&raw_cdev);
Jan Kara0078bff2011-04-29 00:24:29 +0200363 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366module_init(raw_init);
367module_exit(raw_exit);
368MODULE_LICENSE("GPL");