raw.c 7.9 KB

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