xref: /OK3568_Linux_fs/kernel/drivers/char/raw.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * linux/drivers/char/raw.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Front-end raw character devices.  These can be bound to any block
6*4882a593Smuzhiyun  * devices to provide genuine Unix raw character device semantics.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * We reserve minor number 0 for a control interface.  ioctl()s on this
9*4882a593Smuzhiyun  * device are used to bind the other minor numbers to block devices.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/fs.h>
14*4882a593Smuzhiyun #include <linux/major.h>
15*4882a593Smuzhiyun #include <linux/blkdev.h>
16*4882a593Smuzhiyun #include <linux/backing-dev.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/raw.h>
19*4882a593Smuzhiyun #include <linux/capability.h>
20*4882a593Smuzhiyun #include <linux/uio.h>
21*4882a593Smuzhiyun #include <linux/cdev.h>
22*4882a593Smuzhiyun #include <linux/device.h>
23*4882a593Smuzhiyun #include <linux/mutex.h>
24*4882a593Smuzhiyun #include <linux/gfp.h>
25*4882a593Smuzhiyun #include <linux/compat.h>
26*4882a593Smuzhiyun #include <linux/vmalloc.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include <linux/uaccess.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun struct raw_device_data {
31*4882a593Smuzhiyun 	dev_t binding;
32*4882a593Smuzhiyun 	struct block_device *bdev;
33*4882a593Smuzhiyun 	int inuse;
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static struct class *raw_class;
37*4882a593Smuzhiyun static struct raw_device_data *raw_devices;
38*4882a593Smuzhiyun static DEFINE_MUTEX(raw_mutex);
39*4882a593Smuzhiyun static const struct file_operations raw_ctl_fops; /* forward declaration */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static int max_raw_minors = CONFIG_MAX_RAW_DEVS;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun module_param(max_raw_minors, int, 0);
44*4882a593Smuzhiyun MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun  * Open/close code for raw IO.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
50*4882a593Smuzhiyun  * point at the blockdev's address_space and set the file handle to use
51*4882a593Smuzhiyun  * O_DIRECT.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * Set the device's soft blocksize to the minimum possible.  This gives the
54*4882a593Smuzhiyun  * finest possible alignment and has no adverse impact on performance.
55*4882a593Smuzhiyun  */
raw_open(struct inode * inode,struct file * filp)56*4882a593Smuzhiyun static int raw_open(struct inode *inode, struct file *filp)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	const int minor = iminor(inode);
59*4882a593Smuzhiyun 	struct block_device *bdev;
60*4882a593Smuzhiyun 	int err;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (minor == 0) {	/* It is the control device */
63*4882a593Smuzhiyun 		filp->f_op = &raw_ctl_fops;
64*4882a593Smuzhiyun 		return 0;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	pr_warn_ratelimited(
68*4882a593Smuzhiyun 		"process %s (pid %d) is using the deprecated raw device\n"
69*4882a593Smuzhiyun 		"support will be removed in Linux 5.14.\n",
70*4882a593Smuzhiyun 		current->comm, current->pid);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	mutex_lock(&raw_mutex);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/*
75*4882a593Smuzhiyun 	 * All we need to do on open is check that the device is bound.
76*4882a593Smuzhiyun 	 */
77*4882a593Smuzhiyun 	err = -ENODEV;
78*4882a593Smuzhiyun 	if (!raw_devices[minor].binding)
79*4882a593Smuzhiyun 		goto out;
80*4882a593Smuzhiyun 	bdev = blkdev_get_by_dev(raw_devices[minor].binding,
81*4882a593Smuzhiyun 				 filp->f_mode | FMODE_EXCL, raw_open);
82*4882a593Smuzhiyun 	if (IS_ERR(bdev)) {
83*4882a593Smuzhiyun 		err = PTR_ERR(bdev);
84*4882a593Smuzhiyun 		goto out;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 	err = set_blocksize(bdev, bdev_logical_block_size(bdev));
87*4882a593Smuzhiyun 	if (err)
88*4882a593Smuzhiyun 		goto out1;
89*4882a593Smuzhiyun 	filp->f_flags |= O_DIRECT;
90*4882a593Smuzhiyun 	filp->f_mapping = bdev->bd_inode->i_mapping;
91*4882a593Smuzhiyun 	if (++raw_devices[minor].inuse == 1)
92*4882a593Smuzhiyun 		file_inode(filp)->i_mapping =
93*4882a593Smuzhiyun 			bdev->bd_inode->i_mapping;
94*4882a593Smuzhiyun 	filp->private_data = bdev;
95*4882a593Smuzhiyun 	raw_devices[minor].bdev = bdev;
96*4882a593Smuzhiyun 	mutex_unlock(&raw_mutex);
97*4882a593Smuzhiyun 	return 0;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun out1:
100*4882a593Smuzhiyun 	blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
101*4882a593Smuzhiyun out:
102*4882a593Smuzhiyun 	mutex_unlock(&raw_mutex);
103*4882a593Smuzhiyun 	return err;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun  * When the final fd which refers to this character-special node is closed, we
108*4882a593Smuzhiyun  * make its ->mapping point back at its own i_data.
109*4882a593Smuzhiyun  */
raw_release(struct inode * inode,struct file * filp)110*4882a593Smuzhiyun static int raw_release(struct inode *inode, struct file *filp)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	const int minor= iminor(inode);
113*4882a593Smuzhiyun 	struct block_device *bdev;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	mutex_lock(&raw_mutex);
116*4882a593Smuzhiyun 	bdev = raw_devices[minor].bdev;
117*4882a593Smuzhiyun 	if (--raw_devices[minor].inuse == 0)
118*4882a593Smuzhiyun 		/* Here  inode->i_mapping == bdev->bd_inode->i_mapping  */
119*4882a593Smuzhiyun 		inode->i_mapping = &inode->i_data;
120*4882a593Smuzhiyun 	mutex_unlock(&raw_mutex);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
123*4882a593Smuzhiyun 	return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * Forward ioctls to the underlying block device.
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun static long
raw_ioctl(struct file * filp,unsigned int command,unsigned long arg)130*4882a593Smuzhiyun raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	struct block_device *bdev = filp->private_data;
133*4882a593Smuzhiyun 	return blkdev_ioctl(bdev, 0, command, arg);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
bind_set(int number,u64 major,u64 minor)136*4882a593Smuzhiyun static int bind_set(int number, u64 major, u64 minor)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	dev_t dev = MKDEV(major, minor);
139*4882a593Smuzhiyun 	dev_t raw = MKDEV(RAW_MAJOR, number);
140*4882a593Smuzhiyun 	struct raw_device_data *rawdev;
141*4882a593Smuzhiyun 	int err = 0;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (number <= 0 || number >= max_raw_minors)
144*4882a593Smuzhiyun 		return -EINVAL;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (MAJOR(dev) != major || MINOR(dev) != minor)
147*4882a593Smuzhiyun 		return -EINVAL;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	rawdev = &raw_devices[number];
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/*
152*4882a593Smuzhiyun 	 * This is like making block devices, so demand the
153*4882a593Smuzhiyun 	 * same capability
154*4882a593Smuzhiyun 	 */
155*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
156*4882a593Smuzhiyun 		return -EPERM;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/*
159*4882a593Smuzhiyun 	 * For now, we don't need to check that the underlying
160*4882a593Smuzhiyun 	 * block device is present or not: we can do that when
161*4882a593Smuzhiyun 	 * the raw device is opened.  Just check that the
162*4882a593Smuzhiyun 	 * major/minor numbers make sense.
163*4882a593Smuzhiyun 	 */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (MAJOR(dev) == 0 && dev != 0)
166*4882a593Smuzhiyun 		return -EINVAL;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	mutex_lock(&raw_mutex);
169*4882a593Smuzhiyun 	if (rawdev->inuse) {
170*4882a593Smuzhiyun 		mutex_unlock(&raw_mutex);
171*4882a593Smuzhiyun 		return -EBUSY;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 	if (rawdev->binding)
174*4882a593Smuzhiyun 		module_put(THIS_MODULE);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	rawdev->binding = dev;
177*4882a593Smuzhiyun 	if (!dev) {
178*4882a593Smuzhiyun 		/* unbind */
179*4882a593Smuzhiyun 		device_destroy(raw_class, raw);
180*4882a593Smuzhiyun 	} else {
181*4882a593Smuzhiyun 		__module_get(THIS_MODULE);
182*4882a593Smuzhiyun 		device_destroy(raw_class, raw);
183*4882a593Smuzhiyun 		device_create(raw_class, NULL, raw, NULL, "raw%d", number);
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 	mutex_unlock(&raw_mutex);
186*4882a593Smuzhiyun 	return err;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
bind_get(int number,dev_t * dev)189*4882a593Smuzhiyun static int bind_get(int number, dev_t *dev)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	if (number <= 0 || number >= max_raw_minors)
192*4882a593Smuzhiyun 		return -EINVAL;
193*4882a593Smuzhiyun 	*dev = raw_devices[number].binding;
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun  * Deal with ioctls against the raw-device control interface, to bind
199*4882a593Smuzhiyun  * and unbind other raw devices.
200*4882a593Smuzhiyun  */
raw_ctl_ioctl(struct file * filp,unsigned int command,unsigned long arg)201*4882a593Smuzhiyun static long raw_ctl_ioctl(struct file *filp, unsigned int command,
202*4882a593Smuzhiyun 			  unsigned long arg)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct raw_config_request rq;
205*4882a593Smuzhiyun 	dev_t dev;
206*4882a593Smuzhiyun 	int err;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	switch (command) {
209*4882a593Smuzhiyun 	case RAW_SETBIND:
210*4882a593Smuzhiyun 		if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
211*4882a593Smuzhiyun 			return -EFAULT;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	case RAW_GETBIND:
216*4882a593Smuzhiyun 		if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
217*4882a593Smuzhiyun 			return -EFAULT;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 		err = bind_get(rq.raw_minor, &dev);
220*4882a593Smuzhiyun 		if (err)
221*4882a593Smuzhiyun 			return err;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 		rq.block_major = MAJOR(dev);
224*4882a593Smuzhiyun 		rq.block_minor = MINOR(dev);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
227*4882a593Smuzhiyun 			return -EFAULT;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 		return 0;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	return -EINVAL;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
236*4882a593Smuzhiyun struct raw32_config_request {
237*4882a593Smuzhiyun 	compat_int_t	raw_minor;
238*4882a593Smuzhiyun 	compat_u64	block_major;
239*4882a593Smuzhiyun 	compat_u64	block_minor;
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
raw_ctl_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)242*4882a593Smuzhiyun static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
243*4882a593Smuzhiyun 				unsigned long arg)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	struct raw32_config_request __user *user_req = compat_ptr(arg);
246*4882a593Smuzhiyun 	struct raw32_config_request rq;
247*4882a593Smuzhiyun 	dev_t dev;
248*4882a593Smuzhiyun 	int err = 0;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	switch (cmd) {
251*4882a593Smuzhiyun 	case RAW_SETBIND:
252*4882a593Smuzhiyun 		if (copy_from_user(&rq, user_req, sizeof(rq)))
253*4882a593Smuzhiyun 			return -EFAULT;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	case RAW_GETBIND:
258*4882a593Smuzhiyun 		if (copy_from_user(&rq, user_req, sizeof(rq)))
259*4882a593Smuzhiyun 			return -EFAULT;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		err = bind_get(rq.raw_minor, &dev);
262*4882a593Smuzhiyun 		if (err)
263*4882a593Smuzhiyun 			return err;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		rq.block_major = MAJOR(dev);
266*4882a593Smuzhiyun 		rq.block_minor = MINOR(dev);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		if (copy_to_user(user_req, &rq, sizeof(rq)))
269*4882a593Smuzhiyun 			return -EFAULT;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		return 0;
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return -EINVAL;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun #endif
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun static const struct file_operations raw_fops = {
279*4882a593Smuzhiyun 	.read_iter	= blkdev_read_iter,
280*4882a593Smuzhiyun 	.write_iter	= blkdev_write_iter,
281*4882a593Smuzhiyun 	.fsync		= blkdev_fsync,
282*4882a593Smuzhiyun 	.open		= raw_open,
283*4882a593Smuzhiyun 	.release	= raw_release,
284*4882a593Smuzhiyun 	.unlocked_ioctl = raw_ioctl,
285*4882a593Smuzhiyun 	.llseek		= default_llseek,
286*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
287*4882a593Smuzhiyun };
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun static const struct file_operations raw_ctl_fops = {
290*4882a593Smuzhiyun 	.unlocked_ioctl = raw_ctl_ioctl,
291*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
292*4882a593Smuzhiyun 	.compat_ioctl	= raw_ctl_compat_ioctl,
293*4882a593Smuzhiyun #endif
294*4882a593Smuzhiyun 	.open		= raw_open,
295*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
296*4882a593Smuzhiyun 	.llseek		= noop_llseek,
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun static struct cdev raw_cdev;
300*4882a593Smuzhiyun 
raw_devnode(struct device * dev,umode_t * mode)301*4882a593Smuzhiyun static char *raw_devnode(struct device *dev, umode_t *mode)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
raw_init(void)306*4882a593Smuzhiyun static int __init raw_init(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	dev_t dev = MKDEV(RAW_MAJOR, 0);
309*4882a593Smuzhiyun 	int ret;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (max_raw_minors < 1 || max_raw_minors > 65536) {
312*4882a593Smuzhiyun 		pr_warn("raw: invalid max_raw_minors (must be between 1 and 65536), using %d\n",
313*4882a593Smuzhiyun 			CONFIG_MAX_RAW_DEVS);
314*4882a593Smuzhiyun 		max_raw_minors = CONFIG_MAX_RAW_DEVS;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	raw_devices = vzalloc(array_size(max_raw_minors,
318*4882a593Smuzhiyun 					 sizeof(struct raw_device_data)));
319*4882a593Smuzhiyun 	if (!raw_devices) {
320*4882a593Smuzhiyun 		printk(KERN_ERR "Not enough memory for raw device structures\n");
321*4882a593Smuzhiyun 		ret = -ENOMEM;
322*4882a593Smuzhiyun 		goto error;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	ret = register_chrdev_region(dev, max_raw_minors, "raw");
326*4882a593Smuzhiyun 	if (ret)
327*4882a593Smuzhiyun 		goto error;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	cdev_init(&raw_cdev, &raw_fops);
330*4882a593Smuzhiyun 	ret = cdev_add(&raw_cdev, dev, max_raw_minors);
331*4882a593Smuzhiyun 	if (ret)
332*4882a593Smuzhiyun 		goto error_region;
333*4882a593Smuzhiyun 	raw_class = class_create(THIS_MODULE, "raw");
334*4882a593Smuzhiyun 	if (IS_ERR(raw_class)) {
335*4882a593Smuzhiyun 		printk(KERN_ERR "Error creating raw class.\n");
336*4882a593Smuzhiyun 		cdev_del(&raw_cdev);
337*4882a593Smuzhiyun 		ret = PTR_ERR(raw_class);
338*4882a593Smuzhiyun 		goto error_region;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 	raw_class->devnode = raw_devnode;
341*4882a593Smuzhiyun 	device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return 0;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun error_region:
346*4882a593Smuzhiyun 	unregister_chrdev_region(dev, max_raw_minors);
347*4882a593Smuzhiyun error:
348*4882a593Smuzhiyun 	vfree(raw_devices);
349*4882a593Smuzhiyun 	return ret;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
raw_exit(void)352*4882a593Smuzhiyun static void __exit raw_exit(void)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
355*4882a593Smuzhiyun 	class_destroy(raw_class);
356*4882a593Smuzhiyun 	cdev_del(&raw_cdev);
357*4882a593Smuzhiyun 	unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun module_init(raw_init);
361*4882a593Smuzhiyun module_exit(raw_exit);
362*4882a593Smuzhiyun MODULE_LICENSE("GPL");
363