hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/char/raw.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/drivers/char/raw.c
34 *
....@@ -27,7 +28,8 @@
2728 #include <linux/uaccess.h>
2829
2930 struct raw_device_data {
30
- struct block_device *binding;
31
+ dev_t binding;
32
+ struct block_device *bdev;
3133 int inuse;
3234 };
3335
....@@ -36,7 +38,7 @@
3638 static DEFINE_MUTEX(raw_mutex);
3739 static const struct file_operations raw_ctl_fops; /* forward declaration */
3840
39
-static int max_raw_minors = MAX_RAW_MINORS;
41
+static int max_raw_minors = CONFIG_MAX_RAW_DEVS;
4042
4143 module_param(max_raw_minors, int, 0);
4244 MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
....@@ -62,19 +64,25 @@
6264 return 0;
6365 }
6466
67
+ 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
+
6572 mutex_lock(&raw_mutex);
6673
6774 /*
6875 * All we need to do on open is check that the device is bound.
6976 */
70
- bdev = raw_devices[minor].binding;
7177 err = -ENODEV;
72
- if (!bdev)
78
+ if (!raw_devices[minor].binding)
7379 goto out;
74
- bdgrab(bdev);
75
- err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
76
- if (err)
80
+ 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);
7784 goto out;
85
+ }
7886 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
7987 if (err)
8088 goto out1;
....@@ -84,6 +92,7 @@
8492 file_inode(filp)->i_mapping =
8593 bdev->bd_inode->i_mapping;
8694 filp->private_data = bdev;
95
+ raw_devices[minor].bdev = bdev;
8796 mutex_unlock(&raw_mutex);
8897 return 0;
8998
....@@ -104,7 +113,7 @@
104113 struct block_device *bdev;
105114
106115 mutex_lock(&raw_mutex);
107
- bdev = raw_devices[minor].binding;
116
+ bdev = raw_devices[minor].bdev;
108117 if (--raw_devices[minor].inuse == 0)
109118 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
110119 inode->i_mapping = &inode->i_data;
....@@ -127,6 +136,7 @@
127136 static int bind_set(int number, u64 major, u64 minor)
128137 {
129138 dev_t dev = MKDEV(major, minor);
139
+ dev_t raw = MKDEV(RAW_MAJOR, number);
130140 struct raw_device_data *rawdev;
131141 int err = 0;
132142
....@@ -160,25 +170,17 @@
160170 mutex_unlock(&raw_mutex);
161171 return -EBUSY;
162172 }
163
- if (rawdev->binding) {
164
- bdput(rawdev->binding);
173
+ if (rawdev->binding)
165174 module_put(THIS_MODULE);
166
- }
175
+
176
+ rawdev->binding = dev;
167177 if (!dev) {
168178 /* unbind */
169
- rawdev->binding = NULL;
170
- device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
179
+ device_destroy(raw_class, raw);
171180 } 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
- }
181
+ __module_get(THIS_MODULE);
182
+ device_destroy(raw_class, raw);
183
+ device_create(raw_class, NULL, raw, NULL, "raw%d", number);
182184 }
183185 mutex_unlock(&raw_mutex);
184186 return err;
....@@ -186,18 +188,9 @@
186188
187189 static int bind_get(int number, dev_t *dev)
188190 {
189
- struct raw_device_data *rawdev;
190
- struct block_device *bdev;
191
-
192191 if (number <= 0 || number >= max_raw_minors)
193192 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);
193
+ *dev = raw_devices[number].binding;
201194 return 0;
202195 }
203196
....@@ -316,9 +309,9 @@
316309 int ret;
317310
318311 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;
312
+ 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;
322315 }
323316
324317 raw_devices = vzalloc(array_size(max_raw_minors,