raw.c 6.9 KB

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