raw.c 8.3 KB

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