blob: ccf5bd528642da64a860f0b9660c80a472371926 [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 {
31 struct block_device *binding;
32 int inuse;
33};
34
gregkh@suse.deca8eca62005-03-23 09:53:09 -080035static struct class *raw_class;
Jan Kara0078bff2011-04-29 00:24:29 +020036static struct raw_device_data *raw_devices;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080037static DEFINE_MUTEX(raw_mutex);
Arjan van de Ven62322d22006-07-03 00:24:21 -070038static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Masahiro Yamada04aaca12020-06-17 17:33:13 +090040static int max_raw_minors = CONFIG_MAX_RAW_DEVS;
Jan Kara0078bff2011-04-29 00:24:29 +020041
42module_param(max_raw_minors, int, 0);
43MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Open/close code for raw IO.
47 *
48 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
49 * point at the blockdev's address_space and set the file handle to use
50 * O_DIRECT.
51 *
52 * Set the device's soft blocksize to the minimum possible. This gives the
53 * finest possible alignment and has no adverse impact on performance.
54 */
55static int raw_open(struct inode *inode, struct file *filp)
56{
57 const int minor = iminor(inode);
58 struct block_device *bdev;
59 int err;
60
61 if (minor == 0) { /* It is the control device */
62 filp->f_op = &raw_ctl_fops;
63 return 0;
64 }
65
Christoph Hellwigc4823982020-08-19 09:35:33 +020066 pr_warn_ratelimited(
67 "process %s (pid %d) is using the deprecated raw device\n"
68 "support will be removed in Linux 5.14.\n",
69 current->comm, current->pid);
70
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080071 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /*
74 * All we need to do on open is check that the device is bound.
75 */
76 bdev = raw_devices[minor].binding;
77 err = -ENODEV;
78 if (!bdev)
79 goto out;
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +010080 bdgrab(bdev);
Tejun Heoe525fd82010-11-13 11:55:17 +010081 err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (err)
83 goto out;
Martin K. Petersene1defc42009-05-22 17:17:49 -040084 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (err)
Tejun Heoe525fd82010-11-13 11:55:17 +010086 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 filp->f_flags |= O_DIRECT;
88 filp->f_mapping = bdev->bd_inode->i_mapping;
89 if (++raw_devices[minor].inuse == 1)
Al Viro496ad9a2013-01-23 17:07:38 -050090 file_inode(filp)->i_mapping =
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 bdev->bd_inode->i_mapping;
92 filp->private_data = bdev;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080093 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096out1:
Tejun Heoe525fd82010-11-13 11:55:17 +010097 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098out:
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080099 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return err;
101}
102
103/*
104 * When the final fd which refers to this character-special node is closed, we
105 * make its ->mapping point back at its own i_data.
106 */
107static int raw_release(struct inode *inode, struct file *filp)
108{
109 const int minor= iminor(inode);
110 struct block_device *bdev;
111
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800112 mutex_lock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 bdev = raw_devices[minor].binding;
Christoph Hellwigb83ae6d2015-01-14 10:42:37 +0100114 if (--raw_devices[minor].inuse == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
116 inode->i_mapping = &inode->i_data;
Arjan van de Ven8ed965d2006-03-23 03:00:21 -0800117 mutex_unlock(&raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Tejun Heoe525fd82010-11-13 11:55:17 +0100119 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
123/*
124 * Forward ioctls to the underlying block device.
125 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200126static long
127raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct block_device *bdev = filp->private_data;
Al Viroc4a047272009-08-24 22:42:56 +0000130 return blkdev_ioctl(bdev, 0, command, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Al Viroc4a047272009-08-24 22:42:56 +0000133static int bind_set(int number, u64 major, u64 minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Al Viroc4a047272009-08-24 22:42:56 +0000135 dev_t dev = MKDEV(major, minor);
136 struct raw_device_data *rawdev;
137 int err = 0;
138
Jan Kara0078bff2011-04-29 00:24:29 +0200139 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a047272009-08-24 22:42:56 +0000140 return -EINVAL;
141
142 if (MAJOR(dev) != major || MINOR(dev) != minor)
143 return -EINVAL;
144
145 rawdev = &raw_devices[number];
146
147 /*
148 * This is like making block devices, so demand the
149 * same capability
150 */
151 if (!capable(CAP_SYS_ADMIN))
152 return -EPERM;
153
154 /*
155 * For now, we don't need to check that the underlying
156 * block device is present or not: we can do that when
157 * the raw device is opened. Just check that the
158 * major/minor numbers make sense.
159 */
160
161 if (MAJOR(dev) == 0 && dev != 0)
162 return -EINVAL;
163
164 mutex_lock(&raw_mutex);
165 if (rawdev->inuse) {
166 mutex_unlock(&raw_mutex);
167 return -EBUSY;
168 }
169 if (rawdev->binding) {
170 bdput(rawdev->binding);
171 module_put(THIS_MODULE);
172 }
173 if (!dev) {
174 /* unbind */
175 rawdev->binding = NULL;
176 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
177 } else {
178 rawdev->binding = bdget(dev);
179 if (rawdev->binding == NULL) {
180 err = -ENOMEM;
181 } else {
182 dev_t raw = MKDEV(RAW_MAJOR, number);
183 __module_get(THIS_MODULE);
184 device_destroy(raw_class, raw);
185 device_create(raw_class, NULL, raw, NULL,
186 "raw%d", number);
187 }
188 }
189 mutex_unlock(&raw_mutex);
190 return err;
191}
192
193static int bind_get(int number, dev_t *dev)
194{
195 struct raw_device_data *rawdev;
196 struct block_device *bdev;
197
Paul Bolle5bbb2ae2014-02-04 23:23:12 +0100198 if (number <= 0 || number >= max_raw_minors)
Al Viroc4a047272009-08-24 22:42:56 +0000199 return -EINVAL;
200
201 rawdev = &raw_devices[number];
202
203 mutex_lock(&raw_mutex);
204 bdev = rawdev->binding;
205 *dev = bdev ? bdev->bd_dev : 0;
206 mutex_unlock(&raw_mutex);
207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/*
211 * Deal with ioctls against the raw-device control interface, to bind
212 * and unbind other raw devices.
213 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200214static long raw_ctl_ioctl(struct file *filp, unsigned int command,
215 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct raw_config_request rq;
Al Viroc4a047272009-08-24 22:42:56 +0000218 dev_t dev;
219 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 switch (command) {
222 case RAW_SETBIND:
Al Viroc4a047272009-08-24 22:42:56 +0000223 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
224 return -EFAULT;
225
226 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case RAW_GETBIND:
Al Viroc4a047272009-08-24 22:42:56 +0000229 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
230 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Al Viroc4a047272009-08-24 22:42:56 +0000232 err = bind_get(rq.raw_minor, &dev);
233 if (err)
234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Al Viroc4a047272009-08-24 22:42:56 +0000236 rq.block_major = MAJOR(dev);
237 rq.block_minor = MINOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Al Viroc4a047272009-08-24 22:42:56 +0000239 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
240 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Al Viroc4a047272009-08-24 22:42:56 +0000242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Al Viroc4a047272009-08-24 22:42:56 +0000244
245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Al Viroc4a047272009-08-24 22:42:56 +0000248#ifdef CONFIG_COMPAT
249struct raw32_config_request {
250 compat_int_t raw_minor;
251 compat_u64 block_major;
252 compat_u64 block_minor;
253};
254
255static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
256 unsigned long arg)
257{
258 struct raw32_config_request __user *user_req = compat_ptr(arg);
259 struct raw32_config_request rq;
260 dev_t dev;
261 int err = 0;
262
263 switch (cmd) {
264 case RAW_SETBIND:
265 if (copy_from_user(&rq, user_req, sizeof(rq)))
266 return -EFAULT;
267
268 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
269
270 case RAW_GETBIND:
271 if (copy_from_user(&rq, user_req, sizeof(rq)))
272 return -EFAULT;
273
274 err = bind_get(rq.raw_minor, &dev);
275 if (err)
276 return err;
277
278 rq.block_major = MAJOR(dev);
279 rq.block_minor = MINOR(dev);
280
281 if (copy_to_user(user_req, &rq, sizeof(rq)))
282 return -EFAULT;
283
284 return 0;
285 }
286
287 return -EINVAL;
288}
289#endif
290
Arjan van de Ven62322d22006-07-03 00:24:21 -0700291static const struct file_operations raw_fops = {
David Jefferyb2de5252014-09-29 10:21:10 -0400292 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -0400293 .write_iter = blkdev_write_iter,
Arnd Bergmann55929332010-04-27 00:24:05 +0200294 .fsync = blkdev_fsync,
295 .open = raw_open,
296 .release = raw_release,
297 .unlocked_ioctl = raw_ioctl,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200298 .llseek = default_llseek,
Arnd Bergmann55929332010-04-27 00:24:05 +0200299 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Arjan van de Ven62322d22006-07-03 00:24:21 -0700302static const struct file_operations raw_ctl_fops = {
Arnd Bergmann55929332010-04-27 00:24:05 +0200303 .unlocked_ioctl = raw_ctl_ioctl,
Al Viroc4a047272009-08-24 22:42:56 +0000304#ifdef CONFIG_COMPAT
305 .compat_ioctl = raw_ctl_compat_ioctl,
306#endif
Arnd Bergmann55929332010-04-27 00:24:05 +0200307 .open = raw_open,
308 .owner = THIS_MODULE,
Arnd Bergmanncb3b9cf2010-07-06 23:03:55 +0200309 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700312static struct cdev raw_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Al Viro2c9ede52011-07-23 20:24:48 -0400314static char *raw_devnode(struct device *dev, umode_t *mode)
Kay Sievers6fd46932009-04-30 15:23:42 +0200315{
316 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int __init raw_init(void)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dev_t dev = MKDEV(RAW_MAJOR, 0);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700322 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Jan Kara0078bff2011-04-29 00:24:29 +0200324 if (max_raw_minors < 1 || max_raw_minors > 65536) {
Masahiro Yamada04aaca12020-06-17 17:33:13 +0900325 pr_warn("raw: invalid max_raw_minors (must be between 1 and 65536), using %d\n",
326 CONFIG_MAX_RAW_DEVS);
327 max_raw_minors = CONFIG_MAX_RAW_DEVS;
Jan Kara0078bff2011-04-29 00:24:29 +0200328 }
329
Kees Cookfad953c2018-06-12 14:27:37 -0700330 raw_devices = vzalloc(array_size(max_raw_minors,
331 sizeof(struct raw_device_data)));
Jan Kara0078bff2011-04-29 00:24:29 +0200332 if (!raw_devices) {
333 printk(KERN_ERR "Not enough memory for raw device structures\n");
334 ret = -ENOMEM;
335 goto error;
336 }
Jan Kara0078bff2011-04-29 00:24:29 +0200337
338 ret = register_chrdev_region(dev, max_raw_minors, "raw");
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700339 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto error;
341
342 cdev_init(&raw_cdev, &raw_fops);
Jan Kara0078bff2011-04-29 00:24:29 +0200343 ret = cdev_add(&raw_cdev, dev, max_raw_minors);
Bhumika Goyal202cdb62015-12-22 00:11:11 +0530344 if (ret)
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700345 goto error_region;
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800346 raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (IS_ERR(raw_class)) {
348 printk(KERN_ERR "Error creating raw class.\n");
349 cdev_del(&raw_cdev);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700350 ret = PTR_ERR(raw_class);
351 goto error_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200353 raw_class->devnode = raw_devnode;
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700354 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700358error_region:
Jan Kara0078bff2011-04-29 00:24:29 +0200359 unregister_chrdev_region(dev, max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360error:
Jan Kara0078bff2011-04-29 00:24:29 +0200361 vfree(raw_devices);
Rolf Eike Beer3e26a422006-09-29 02:00:46 -0700362 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static void __exit raw_exit(void)
366{
Greg Kroah-Hartman38ca6c32006-08-07 22:19:37 -0700367 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800368 class_destroy(raw_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 cdev_del(&raw_cdev);
Jan Kara0078bff2011-04-29 00:24:29 +0200370 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373module_init(raw_init);
374module_exit(raw_exit);
375MODULE_LICENSE("GPL");