blob: 5d52a1f4738c760c5033f8a97c0bebb93eb745ea [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/char/raw.c
4 *
5 * Front-end raw character devices. These can be bound to any block
6 * devices to provide genuine Unix raw character device semantics.
7 *
8 * We reserve minor number 0 for a control interface. ioctl()s on this
9 * device are used to bind the other minor numbers to block devices.
10 */
11
12#include <linux/init.h>
13#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/major.h>
15#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040016#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/raw.h>
19#include <linux/capability.h>
20#include <linux/uio.h>
21#include <linux/cdev.h>
22#include <linux/device.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080023#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/gfp.h>
Al Viroc4a047272009-08-24 22:42:56 +000025#include <linux/compat.h>
Jan Kara0078bff2011-04-29 00:24:29 +020026#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct raw_device_data {
Christoph Hellwig5a56ad72020-09-21 09:19:51 +020031 dev_t binding;
32 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int inuse;
34};
35
gregkh@suse.deca8eca62005-03-23 09:53:09 -080036static struct class *raw_class;
Jan Kara0078bff2011-04-29 00:24:29 +020037static struct raw_device_data *raw_devices;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080038static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070039static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Masahiro Yamada04aaca12020-06-17 17:33:13 +090041static int max_raw_minors = CONFIG_MAX_RAW_DEVS;
Jan Kara0078bff2011-04-29 00:24:29 +020042
43module_param(max_raw_minors, int, 0);
44MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Open/close code for raw IO.
48 *
49 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
50 * point at the blockdev's address_space and set the file handle to use
51 * O_DIRECT.
52 *
53 * Set the device's soft blocksize to the minimum possible. This gives the
54 * finest possible alignment and has no adverse impact on performance.
55 */
56static int raw_open(struct inode *inode, struct file *filp)
57{
58 const int minor = iminor(inode);
59 struct block_device *bdev;
60 int err;
61
62 if (minor == 0) { /* It is the control device */
63 filp->f_op = &raw_ctl_fops;
64 return 0;
65 }
66
Christoph Hellwigc4823982020-08-19 09:35:33 +020067 pr_warn_ratelimited(
68 "process %s (pid %d) is using the deprecated raw device\n"
69 "support will be removed in Linux 5.14.\n",
70 current->comm, current->pid);
71
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080072 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /*
75 * All we need to do on open is check that the device is bound.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 err = -ENODEV;
Christoph Hellwig5a56ad72020-09-21 09:19:51 +020078 if (!raw_devices[minor].binding)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 goto out;
Christoph Hellwig5a56ad72020-09-21 09:19:51 +020080 bdev = blkdev_get_by_dev(raw_devices[minor].binding,
81 filp->f_mode | FMODE_EXCL, raw_open);
82 if (IS_ERR(bdev)) {
83 err = PTR_ERR(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto out;
Christoph Hellwig5a56ad72020-09-21 09:19:51 +020085 }
Martin K. Petersene1defc42009-05-22 17:17:49 -040086 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (err)
Tejun Heoe525fd82010-11-13 11:55:17 +010088 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 filp->f_flags |= O_DIRECT;
90 filp->f_mapping = bdev->bd_inode->i_mapping;
91 if (++raw_devices[minor].inuse == 1)
Al Viro496ad9a2013-01-23 17:07:38 -050092 file_inode(filp)->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 bdev->bd_inode->i_mapping;
94 filp->private_data = bdev;
Christoph Hellwig5a56ad72020-09-21 09:19:51 +020095 raw_devices[minor].bdev = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080096 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099out1:
Tejun Heoe525fd82010-11-13 11:55:17 +0100100 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800102 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return err;
104}
105
106/*
107 * When the final fd which refers to this character-special node is closed, we
108 * make its ->mapping point back at its own i_data.
109 */
110static int raw_release(struct inode *inode, struct file *filp)
111{
112 const int minor= iminor(inode);
113 struct block_device *bdev;
114
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800115 mutex_lock(&raw_mutex);
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200116 bdev = raw_devices[minor].bdev;
Christoph Hellwigb83ae6d2015-01-14 10:42:37 +0100117 if (--raw_devices[minor].inuse == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
119 inode->i_mapping = &inode->i_data;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800120 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Tejun Heoe525fd82010-11-13 11:55:17 +0100122 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124}
125
126/*
127 * Forward ioctls to the underlying block device.
128 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200129static long
130raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct block_device *bdev = filp->private_data;
Al Viroc4a047272009-08-24 22:42:56 +0000133 return blkdev_ioctl(bdev, 0, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Al Viroc4a047272009-08-24 22:42:56 +0000136static int bind_set(int number, u64 major, u64 minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Al Viroc4a047272009-08-24 22:42:56 +0000138 dev_t dev = MKDEV(major, minor);
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200139 dev_t raw = MKDEV(RAW_MAJOR, number);
Al Viroc4a047272009-08-24 22:42:56 +0000140 struct raw_device_data *rawdev;
141 int err = 0;
142
Jan Kara0078bff2011-04-29 00:24:29 +0200143 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a047272009-08-24 22:42:56 +0000144 return -EINVAL;
145
146 if (MAJOR(dev) != major || MINOR(dev) != minor)
147 return -EINVAL;
148
149 rawdev = &raw_devices[number];
150
151 /*
152 * This is like making block devices, so demand the
153 * same capability
154 */
155 if (!capable(CAP_SYS_ADMIN))
156 return -EPERM;
157
158 /*
159 * For now, we don't need to check that the underlying
160 * block device is present or not: we can do that when
161 * the raw device is opened. Just check that the
162 * major/minor numbers make sense.
163 */
164
165 if (MAJOR(dev) == 0 && dev != 0)
166 return -EINVAL;
167
168 mutex_lock(&raw_mutex);
169 if (rawdev->inuse) {
170 mutex_unlock(&raw_mutex);
171 return -EBUSY;
172 }
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200173 if (rawdev->binding)
Al Viroc4a047272009-08-24 22:42:56 +0000174 module_put(THIS_MODULE);
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200175
176 rawdev->binding = dev;
Al Viroc4a047272009-08-24 22:42:56 +0000177 if (!dev) {
178 /* unbind */
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200179 device_destroy(raw_class, raw);
Al Viroc4a047272009-08-24 22:42:56 +0000180 } else {
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200181 __module_get(THIS_MODULE);
182 device_destroy(raw_class, raw);
183 device_create(raw_class, NULL, raw, NULL, "raw%d", number);
Al Viroc4a047272009-08-24 22:42:56 +0000184 }
185 mutex_unlock(&raw_mutex);
186 return err;
187}
188
189static int bind_get(int number, dev_t *dev)
190{
Paul Bolle5bbb2ae2014-02-04 23:23:12 +0100191 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a047272009-08-24 22:42:56 +0000192 return -EINVAL;
Christoph Hellwig5a56ad72020-09-21 09:19:51 +0200193 *dev = raw_devices[number].binding;
Al Viroc4a047272009-08-24 22:42:56 +0000194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * Deal with ioctls against the raw-device control interface, to bind
199 * and unbind other raw devices.
200 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200201static long raw_ctl_ioctl(struct file *filp, unsigned int command,
202 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct raw_config_request rq;
Al Viroc4a047272009-08-24 22:42:56 +0000205 dev_t dev;
206 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 switch (command) {
209 case RAW_SETBIND:
Al Viroc4a047272009-08-24 22:42:56 +0000210 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
211 return -EFAULT;
212
213 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 case RAW_GETBIND:
Al Viroc4a047272009-08-24 22:42:56 +0000216 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
217 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Al Viroc4a047272009-08-24 22:42:56 +0000219 err = bind_get(rq.raw_minor, &dev);
220 if (err)
221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Al Viroc4a047272009-08-24 22:42:56 +0000223 rq.block_major = MAJOR(dev);
224 rq.block_minor = MINOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Al Viroc4a047272009-08-24 22:42:56 +0000226 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
227 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Al Viroc4a047272009-08-24 22:42:56 +0000229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Al Viroc4a047272009-08-24 22:42:56 +0000231
232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Al Viroc4a047272009-08-24 22:42:56 +0000235#ifdef CONFIG_COMPAT
236struct raw32_config_request {
237 compat_int_t raw_minor;
238 compat_u64 block_major;
239 compat_u64 block_minor;
240};
241
242static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
243 unsigned long arg)
244{
245 struct raw32_config_request __user *user_req = compat_ptr(arg);
246 struct raw32_config_request rq;
247 dev_t dev;
248 int err = 0;
249
250 switch (cmd) {
251 case RAW_SETBIND:
252 if (copy_from_user(&rq, user_req, sizeof(rq)))
253 return -EFAULT;
254
255 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
256
257 case RAW_GETBIND:
258 if (copy_from_user(&rq, user_req, sizeof(rq)))
259 return -EFAULT;
260
261 err = bind_get(rq.raw_minor, &dev);
262 if (err)
263 return err;
264
265 rq.block_major = MAJOR(dev);
266 rq.block_minor = MINOR(dev);
267
268 if (copy_to_user(user_req, &rq, sizeof(rq)))
269 return -EFAULT;
270
271 return 0;
272 }
273
274 return -EINVAL;
275}
276#endif
277
Arjan van de Ven62322d22006-07-03 00:24:21 -0700278static const struct file_operations raw_fops = {
David Jefferyb2de5252014-09-29 10:21:10 -0400279 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -0400280 .write_iter = blkdev_write_iter,
Arnd Bergmann55929332010-04-27 00:24:05 +0200281 .fsync = blkdev_fsync,
282 .open = raw_open,
283 .release = raw_release,
284 .unlocked_ioctl = raw_ioctl,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200285 .llseek = default_llseek,
Arnd Bergmann55929332010-04-27 00:24:05 +0200286 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
Arjan van de Ven62322d22006-07-03 00:24:21 -0700289static const struct file_operations raw_ctl_fops = {
Arnd Bergmann55929332010-04-27 00:24:05 +0200290 .unlocked_ioctl = raw_ctl_ioctl,
Al Viroc4a047272009-08-24 22:42:56 +0000291#ifdef CONFIG_COMPAT
292 .compat_ioctl = raw_ctl_compat_ioctl,
293#endif
Arnd Bergmann55929332010-04-27 00:24:05 +0200294 .open = raw_open,
295 .owner = THIS_MODULE,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200296 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700299static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Al Viro2c9ede52011-07-23 20:24:48 -0400301static char *raw_devnode(struct device *dev, umode_t *mode)
Kay Sievers6fd46932009-04-30 15:23:42 +0200302{
303 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int __init raw_init(void)
307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700309 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Jan Kara0078bff2011-04-29 00:24:29 +0200311 if (max_raw_minors < 1 || max_raw_minors > 65536) {
Masahiro Yamada04aaca12020-06-17 17:33:13 +0900312 pr_warn("raw: invalid max_raw_minors (must be between 1 and 65536), using %d\n",
313 CONFIG_MAX_RAW_DEVS);
314 max_raw_minors = CONFIG_MAX_RAW_DEVS;
Jan Kara0078bff2011-04-29 00:24:29 +0200315 }
316
Kees Cookfad953c2018-06-12 14:27:37 -0700317 raw_devices = vzalloc(array_size(max_raw_minors,
318 sizeof(struct raw_device_data)));
Jan Kara0078bff2011-04-29 00:24:29 +0200319 if (!raw_devices) {
320 printk(KERN_ERR "Not enough memory for raw device structures\n");
321 ret = -ENOMEM;
322 goto error;
323 }
Jan Kara0078bff2011-04-29 00:24:29 +0200324
325 ret = register_chrdev_region(dev, max_raw_minors, "raw");
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700326 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto error;
328
329 cdev_init(&raw_cdev, &raw_fops);
Jan Kara0078bff2011-04-29 00:24:29 +0200330 ret = cdev_add(&raw_cdev, dev, max_raw_minors);
Bhumika Goyal202cdb62015-12-22 00:11:11 +0530331 if (ret)
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700332 goto error_region;
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800333 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (IS_ERR(raw_class)) {
335 printk(KERN_ERR "Error creating raw class.\n");
336 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700337 ret = PTR_ERR(raw_class);
338 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200340 raw_class->devnode = raw_devnode;
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700341 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700345error_region:
Jan Kara0078bff2011-04-29 00:24:29 +0200346 unregister_chrdev_region(dev, max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347error:
Jan Kara0078bff2011-04-29 00:24:29 +0200348 vfree(raw_devices);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static void __exit raw_exit(void)
353{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700354 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800355 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 cdev_del(&raw_cdev);
Jan Kara0078bff2011-04-29 00:24:29 +0200357 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360module_init(raw_init);
361module_exit(raw_exit);
362MODULE_LICENSE("GPL");