raw.c 7.2 KB

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