bsg.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bsg.c - block layer implementation of the sg v4 interface
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/file.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/cdev.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/percpu.h>
  12. #include <linux/idr.h>
  13. #include <linux/bsg.h>
  14. #include <linux/slab.h>
  15. #include <scsi/scsi.h>
  16. #include <scsi/scsi_ioctl.h>
  17. #include <scsi/sg.h>
  18. #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
  19. #define BSG_VERSION "0.4"
  20. struct bsg_device {
  21. struct request_queue *queue;
  22. struct device device;
  23. struct cdev cdev;
  24. int max_queue;
  25. unsigned int timeout;
  26. unsigned int reserved_size;
  27. bsg_sg_io_fn *sg_io_fn;
  28. };
  29. static inline struct bsg_device *to_bsg_device(struct inode *inode)
  30. {
  31. return container_of(inode->i_cdev, struct bsg_device, cdev);
  32. }
  33. #define BSG_DEFAULT_CMDS 64
  34. #define BSG_MAX_DEVS (1 << MINORBITS)
  35. static DEFINE_IDA(bsg_minor_ida);
  36. static const struct class bsg_class;
  37. static int bsg_major;
  38. static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
  39. {
  40. unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
  41. if (hdr->timeout)
  42. timeout = msecs_to_jiffies(hdr->timeout);
  43. else if (bd->timeout)
  44. timeout = bd->timeout;
  45. return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
  46. }
  47. static int bsg_sg_io(struct bsg_device *bd, bool open_for_write,
  48. void __user *uarg)
  49. {
  50. struct sg_io_v4 hdr;
  51. int ret;
  52. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  53. return -EFAULT;
  54. if (hdr.guard != 'Q')
  55. return -EINVAL;
  56. ret = bd->sg_io_fn(bd->queue, &hdr, open_for_write,
  57. bsg_timeout(bd, &hdr));
  58. if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
  59. return -EFAULT;
  60. return ret;
  61. }
  62. static int bsg_open(struct inode *inode, struct file *file)
  63. {
  64. if (!blk_get_queue(to_bsg_device(inode)->queue))
  65. return -ENXIO;
  66. return 0;
  67. }
  68. static int bsg_release(struct inode *inode, struct file *file)
  69. {
  70. blk_put_queue(to_bsg_device(inode)->queue);
  71. return 0;
  72. }
  73. static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
  74. {
  75. return put_user(READ_ONCE(bd->max_queue), uarg);
  76. }
  77. static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
  78. {
  79. int max_queue;
  80. if (get_user(max_queue, uarg))
  81. return -EFAULT;
  82. if (max_queue < 1)
  83. return -EINVAL;
  84. WRITE_ONCE(bd->max_queue, max_queue);
  85. return 0;
  86. }
  87. static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  88. {
  89. struct bsg_device *bd = to_bsg_device(file_inode(file));
  90. struct request_queue *q = bd->queue;
  91. void __user *uarg = (void __user *) arg;
  92. int __user *intp = uarg;
  93. int val;
  94. switch (cmd) {
  95. /*
  96. * Our own ioctls
  97. */
  98. case SG_GET_COMMAND_Q:
  99. return bsg_get_command_q(bd, uarg);
  100. case SG_SET_COMMAND_Q:
  101. return bsg_set_command_q(bd, uarg);
  102. /*
  103. * SCSI/sg ioctls
  104. */
  105. case SG_GET_VERSION_NUM:
  106. return put_user(30527, intp);
  107. case SCSI_IOCTL_GET_IDLUN:
  108. return put_user(0, intp);
  109. case SCSI_IOCTL_GET_BUS_NUMBER:
  110. return put_user(0, intp);
  111. case SG_SET_TIMEOUT:
  112. if (get_user(val, intp))
  113. return -EFAULT;
  114. bd->timeout = clock_t_to_jiffies(val);
  115. return 0;
  116. case SG_GET_TIMEOUT:
  117. return jiffies_to_clock_t(bd->timeout);
  118. case SG_GET_RESERVED_SIZE:
  119. return put_user(min(bd->reserved_size, queue_max_bytes(q)),
  120. intp);
  121. case SG_SET_RESERVED_SIZE:
  122. if (get_user(val, intp))
  123. return -EFAULT;
  124. if (val < 0)
  125. return -EINVAL;
  126. bd->reserved_size =
  127. min_t(unsigned int, val, queue_max_bytes(q));
  128. return 0;
  129. case SG_EMULATED_HOST:
  130. return put_user(1, intp);
  131. case SG_IO:
  132. return bsg_sg_io(bd, file->f_mode & FMODE_WRITE, uarg);
  133. case SCSI_IOCTL_SEND_COMMAND:
  134. pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
  135. current->comm);
  136. return -EINVAL;
  137. default:
  138. return -ENOTTY;
  139. }
  140. }
  141. static const struct file_operations bsg_fops = {
  142. .open = bsg_open,
  143. .release = bsg_release,
  144. .unlocked_ioctl = bsg_ioctl,
  145. .compat_ioctl = compat_ptr_ioctl,
  146. .owner = THIS_MODULE,
  147. .llseek = default_llseek,
  148. };
  149. static void bsg_device_release(struct device *dev)
  150. {
  151. struct bsg_device *bd = container_of(dev, struct bsg_device, device);
  152. ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
  153. kfree(bd);
  154. }
  155. void bsg_unregister_queue(struct bsg_device *bd)
  156. {
  157. struct gendisk *disk = bd->queue->disk;
  158. if (disk && disk->queue_kobj.sd)
  159. sysfs_remove_link(&disk->queue_kobj, "bsg");
  160. cdev_device_del(&bd->cdev, &bd->device);
  161. put_device(&bd->device);
  162. }
  163. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  164. struct bsg_device *bsg_register_queue(struct request_queue *q,
  165. struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
  166. {
  167. struct bsg_device *bd;
  168. int ret;
  169. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  170. if (!bd)
  171. return ERR_PTR(-ENOMEM);
  172. bd->max_queue = BSG_DEFAULT_CMDS;
  173. bd->reserved_size = INT_MAX;
  174. bd->queue = q;
  175. bd->sg_io_fn = sg_io_fn;
  176. ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
  177. if (ret < 0) {
  178. if (ret == -ENOSPC)
  179. dev_err(parent, "bsg: too many bsg devices\n");
  180. kfree(bd);
  181. return ERR_PTR(ret);
  182. }
  183. bd->device.devt = MKDEV(bsg_major, ret);
  184. bd->device.class = &bsg_class;
  185. bd->device.parent = parent;
  186. bd->device.release = bsg_device_release;
  187. dev_set_name(&bd->device, "%s", name);
  188. device_initialize(&bd->device);
  189. cdev_init(&bd->cdev, &bsg_fops);
  190. bd->cdev.owner = THIS_MODULE;
  191. ret = cdev_device_add(&bd->cdev, &bd->device);
  192. if (ret)
  193. goto out_put_device;
  194. if (q->disk && q->disk->queue_kobj.sd) {
  195. ret = sysfs_create_link(&q->disk->queue_kobj, &bd->device.kobj,
  196. "bsg");
  197. if (ret)
  198. goto out_device_del;
  199. }
  200. return bd;
  201. out_device_del:
  202. cdev_device_del(&bd->cdev, &bd->device);
  203. out_put_device:
  204. put_device(&bd->device);
  205. return ERR_PTR(ret);
  206. }
  207. EXPORT_SYMBOL_GPL(bsg_register_queue);
  208. static char *bsg_devnode(const struct device *dev, umode_t *mode)
  209. {
  210. return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
  211. }
  212. static const struct class bsg_class = {
  213. .name = "bsg",
  214. .devnode = bsg_devnode,
  215. };
  216. static int __init bsg_init(void)
  217. {
  218. dev_t devid;
  219. int ret;
  220. ret = class_register(&bsg_class);
  221. if (ret)
  222. return ret;
  223. ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
  224. if (ret)
  225. goto destroy_bsg_class;
  226. bsg_major = MAJOR(devid);
  227. printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
  228. " loaded (major %d)\n", bsg_major);
  229. return 0;
  230. destroy_bsg_class:
  231. class_unregister(&bsg_class);
  232. return ret;
  233. }
  234. MODULE_AUTHOR("Jens Axboe");
  235. MODULE_DESCRIPTION(BSG_DESCRIPTION);
  236. MODULE_LICENSE("GPL");
  237. device_initcall(bsg_init);