raw.c 8.3 KB

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