raw.c 7.9 KB

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