raw.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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);
  61. if (err)
  62. goto out;
  63. err = bd_claim(bdev, raw_open);
  64. if (err)
  65. goto out1;
  66. err = set_blocksize(bdev, bdev_logical_block_size(bdev));
  67. if (err)
  68. goto out2;
  69. filp->f_flags |= O_DIRECT;
  70. filp->f_mapping = bdev->bd_inode->i_mapping;
  71. if (++raw_devices[minor].inuse == 1)
  72. filp->f_path.dentry->d_inode->i_mapping =
  73. bdev->bd_inode->i_mapping;
  74. filp->private_data = bdev;
  75. mutex_unlock(&raw_mutex);
  76. return 0;
  77. out2:
  78. bd_release(bdev);
  79. out1:
  80. blkdev_put(bdev, filp->f_mode);
  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. inode->i_mapping->backing_dev_info = &default_backing_dev_info;
  99. }
  100. mutex_unlock(&raw_mutex);
  101. bd_release(bdev);
  102. blkdev_put(bdev, filp->f_mode);
  103. return 0;
  104. }
  105. /*
  106. * Forward ioctls to the underlying block device.
  107. */
  108. static long
  109. raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
  110. {
  111. struct block_device *bdev = filp->private_data;
  112. return blkdev_ioctl(bdev, 0, command, arg);
  113. }
  114. static int bind_set(int number, u64 major, u64 minor)
  115. {
  116. dev_t dev = MKDEV(major, minor);
  117. struct raw_device_data *rawdev;
  118. int err = 0;
  119. if (number <= 0 || number >= MAX_RAW_MINORS)
  120. return -EINVAL;
  121. if (MAJOR(dev) != major || MINOR(dev) != minor)
  122. return -EINVAL;
  123. rawdev = &raw_devices[number];
  124. /*
  125. * This is like making block devices, so demand the
  126. * same capability
  127. */
  128. if (!capable(CAP_SYS_ADMIN))
  129. return -EPERM;
  130. /*
  131. * For now, we don't need to check that the underlying
  132. * block device is present or not: we can do that when
  133. * the raw device is opened. Just check that the
  134. * major/minor numbers make sense.
  135. */
  136. if (MAJOR(dev) == 0 && dev != 0)
  137. return -EINVAL;
  138. mutex_lock(&raw_mutex);
  139. if (rawdev->inuse) {
  140. mutex_unlock(&raw_mutex);
  141. return -EBUSY;
  142. }
  143. if (rawdev->binding) {
  144. bdput(rawdev->binding);
  145. module_put(THIS_MODULE);
  146. }
  147. if (!dev) {
  148. /* unbind */
  149. rawdev->binding = NULL;
  150. device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
  151. } else {
  152. rawdev->binding = bdget(dev);
  153. if (rawdev->binding == NULL) {
  154. err = -ENOMEM;
  155. } else {
  156. dev_t raw = MKDEV(RAW_MAJOR, number);
  157. __module_get(THIS_MODULE);
  158. device_destroy(raw_class, raw);
  159. device_create(raw_class, NULL, raw, NULL,
  160. "raw%d", number);
  161. }
  162. }
  163. mutex_unlock(&raw_mutex);
  164. return err;
  165. }
  166. static int bind_get(int number, dev_t *dev)
  167. {
  168. struct raw_device_data *rawdev;
  169. struct block_device *bdev;
  170. if (number <= 0 || number >= MAX_RAW_MINORS)
  171. return -EINVAL;
  172. rawdev = &raw_devices[number];
  173. mutex_lock(&raw_mutex);
  174. bdev = rawdev->binding;
  175. *dev = bdev ? bdev->bd_dev : 0;
  176. mutex_unlock(&raw_mutex);
  177. return 0;
  178. }
  179. /*
  180. * Deal with ioctls against the raw-device control interface, to bind
  181. * and unbind other raw devices.
  182. */
  183. static long raw_ctl_ioctl(struct file *filp, unsigned int command,
  184. unsigned long arg)
  185. {
  186. struct raw_config_request rq;
  187. dev_t dev;
  188. int err;
  189. switch (command) {
  190. case RAW_SETBIND:
  191. if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
  192. return -EFAULT;
  193. return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
  194. case RAW_GETBIND:
  195. if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
  196. return -EFAULT;
  197. err = bind_get(rq.raw_minor, &dev);
  198. if (err)
  199. return err;
  200. rq.block_major = MAJOR(dev);
  201. rq.block_minor = MINOR(dev);
  202. if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
  203. return -EFAULT;
  204. return 0;
  205. }
  206. return -EINVAL;
  207. }
  208. #ifdef CONFIG_COMPAT
  209. struct raw32_config_request {
  210. compat_int_t raw_minor;
  211. compat_u64 block_major;
  212. compat_u64 block_minor;
  213. };
  214. static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
  215. unsigned long arg)
  216. {
  217. struct raw32_config_request __user *user_req = compat_ptr(arg);
  218. struct raw32_config_request rq;
  219. dev_t dev;
  220. int err = 0;
  221. switch (cmd) {
  222. case RAW_SETBIND:
  223. if (copy_from_user(&rq, user_req, sizeof(rq)))
  224. return -EFAULT;
  225. return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
  226. case RAW_GETBIND:
  227. if (copy_from_user(&rq, user_req, sizeof(rq)))
  228. return -EFAULT;
  229. err = bind_get(rq.raw_minor, &dev);
  230. if (err)
  231. return err;
  232. rq.block_major = MAJOR(dev);
  233. rq.block_minor = MINOR(dev);
  234. if (copy_to_user(user_req, &rq, sizeof(rq)))
  235. return -EFAULT;
  236. return 0;
  237. }
  238. return -EINVAL;
  239. }
  240. #endif
  241. static const struct file_operations raw_fops = {
  242. .read = do_sync_read,
  243. .aio_read = generic_file_aio_read,
  244. .write = do_sync_write,
  245. .aio_write = blkdev_aio_write,
  246. .fsync = blkdev_fsync,
  247. .open = raw_open,
  248. .release = raw_release,
  249. .unlocked_ioctl = raw_ioctl,
  250. .llseek = default_llseek,
  251. .owner = THIS_MODULE,
  252. };
  253. static const struct file_operations raw_ctl_fops = {
  254. .unlocked_ioctl = raw_ctl_ioctl,
  255. #ifdef CONFIG_COMPAT
  256. .compat_ioctl = raw_ctl_compat_ioctl,
  257. #endif
  258. .open = raw_open,
  259. .owner = THIS_MODULE,
  260. .llseek = noop_llseek,
  261. };
  262. static struct cdev raw_cdev;
  263. static char *raw_devnode(struct device *dev, mode_t *mode)
  264. {
  265. return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
  266. }
  267. static int __init raw_init(void)
  268. {
  269. dev_t dev = MKDEV(RAW_MAJOR, 0);
  270. int ret;
  271. ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
  272. if (ret)
  273. goto error;
  274. cdev_init(&raw_cdev, &raw_fops);
  275. ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
  276. if (ret) {
  277. kobject_put(&raw_cdev.kobj);
  278. goto error_region;
  279. }
  280. raw_class = class_create(THIS_MODULE, "raw");
  281. if (IS_ERR(raw_class)) {
  282. printk(KERN_ERR "Error creating raw class.\n");
  283. cdev_del(&raw_cdev);
  284. ret = PTR_ERR(raw_class);
  285. goto error_region;
  286. }
  287. raw_class->devnode = raw_devnode;
  288. device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
  289. return 0;
  290. error_region:
  291. unregister_chrdev_region(dev, MAX_RAW_MINORS);
  292. error:
  293. return ret;
  294. }
  295. static void __exit raw_exit(void)
  296. {
  297. device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
  298. class_destroy(raw_class);
  299. cdev_del(&raw_cdev);
  300. unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
  301. }
  302. module_init(raw_init);
  303. module_exit(raw_exit);
  304. MODULE_LICENSE("GPL");