raw.c 8.5 KB

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