bsg.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 32768
  35. static DEFINE_IDA(bsg_minor_ida);
  36. static 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, fmode_t mode, void __user *uarg)
  48. {
  49. struct sg_io_v4 hdr;
  50. int ret;
  51. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  52. return -EFAULT;
  53. if (hdr.guard != 'Q')
  54. return -EINVAL;
  55. ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr));
  56. if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
  57. return -EFAULT;
  58. return ret;
  59. }
  60. static int bsg_open(struct inode *inode, struct file *file)
  61. {
  62. if (!blk_get_queue(to_bsg_device(inode)->queue))
  63. return -ENXIO;
  64. return 0;
  65. }
  66. static int bsg_release(struct inode *inode, struct file *file)
  67. {
  68. blk_put_queue(to_bsg_device(inode)->queue);
  69. return 0;
  70. }
  71. static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
  72. {
  73. return put_user(READ_ONCE(bd->max_queue), uarg);
  74. }
  75. static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
  76. {
  77. int max_queue;
  78. if (get_user(max_queue, uarg))
  79. return -EFAULT;
  80. if (max_queue < 1)
  81. return -EINVAL;
  82. WRITE_ONCE(bd->max_queue, max_queue);
  83. return 0;
  84. }
  85. static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  86. {
  87. struct bsg_device *bd = to_bsg_device(file_inode(file));
  88. struct request_queue *q = bd->queue;
  89. void __user *uarg = (void __user *) arg;
  90. int __user *intp = uarg;
  91. int val;
  92. switch (cmd) {
  93. /*
  94. * Our own ioctls
  95. */
  96. case SG_GET_COMMAND_Q:
  97. return bsg_get_command_q(bd, uarg);
  98. case SG_SET_COMMAND_Q:
  99. return bsg_set_command_q(bd, uarg);
  100. /*
  101. * SCSI/sg ioctls
  102. */
  103. case SG_GET_VERSION_NUM:
  104. return put_user(30527, intp);
  105. case SCSI_IOCTL_GET_IDLUN:
  106. return put_user(0, intp);
  107. case SCSI_IOCTL_GET_BUS_NUMBER:
  108. return put_user(0, intp);
  109. case SG_SET_TIMEOUT:
  110. if (get_user(val, intp))
  111. return -EFAULT;
  112. bd->timeout = clock_t_to_jiffies(val);
  113. return 0;
  114. case SG_GET_TIMEOUT:
  115. return jiffies_to_clock_t(bd->timeout);
  116. case SG_GET_RESERVED_SIZE:
  117. return put_user(min(bd->reserved_size, queue_max_bytes(q)),
  118. intp);
  119. case SG_SET_RESERVED_SIZE:
  120. if (get_user(val, intp))
  121. return -EFAULT;
  122. if (val < 0)
  123. return -EINVAL;
  124. bd->reserved_size =
  125. min_t(unsigned int, val, queue_max_bytes(q));
  126. return 0;
  127. case SG_EMULATED_HOST:
  128. return put_user(1, intp);
  129. case SG_IO:
  130. return bsg_sg_io(bd, file->f_mode, uarg);
  131. case SCSI_IOCTL_SEND_COMMAND:
  132. pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
  133. current->comm);
  134. return -EINVAL;
  135. default:
  136. return -ENOTTY;
  137. }
  138. }
  139. static const struct file_operations bsg_fops = {
  140. .open = bsg_open,
  141. .release = bsg_release,
  142. .unlocked_ioctl = bsg_ioctl,
  143. .compat_ioctl = compat_ptr_ioctl,
  144. .owner = THIS_MODULE,
  145. .llseek = default_llseek,
  146. };
  147. static void bsg_device_release(struct device *dev)
  148. {
  149. struct bsg_device *bd = container_of(dev, struct bsg_device, device);
  150. ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
  151. kfree(bd);
  152. }
  153. void bsg_unregister_queue(struct bsg_device *bd)
  154. {
  155. if (bd->queue->kobj.sd)
  156. sysfs_remove_link(&bd->queue->kobj, "bsg");
  157. cdev_device_del(&bd->cdev, &bd->device);
  158. put_device(&bd->device);
  159. }
  160. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  161. struct bsg_device *bsg_register_queue(struct request_queue *q,
  162. struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
  163. {
  164. struct bsg_device *bd;
  165. int ret;
  166. bd = kzalloc(sizeof(*bd), GFP_KERNEL);
  167. if (!bd)
  168. return ERR_PTR(-ENOMEM);
  169. bd->max_queue = BSG_DEFAULT_CMDS;
  170. bd->reserved_size = INT_MAX;
  171. bd->queue = q;
  172. bd->sg_io_fn = sg_io_fn;
  173. ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
  174. if (ret < 0) {
  175. if (ret == -ENOSPC)
  176. dev_err(parent, "bsg: too many bsg devices\n");
  177. kfree(bd);
  178. return ERR_PTR(ret);
  179. }
  180. bd->device.devt = MKDEV(bsg_major, ret);
  181. bd->device.class = bsg_class;
  182. bd->device.parent = parent;
  183. bd->device.release = bsg_device_release;
  184. dev_set_name(&bd->device, "%s", name);
  185. device_initialize(&bd->device);
  186. cdev_init(&bd->cdev, &bsg_fops);
  187. bd->cdev.owner = THIS_MODULE;
  188. ret = cdev_device_add(&bd->cdev, &bd->device);
  189. if (ret)
  190. goto out_put_device;
  191. if (q->kobj.sd) {
  192. ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg");
  193. if (ret)
  194. goto out_device_del;
  195. }
  196. return bd;
  197. out_device_del:
  198. cdev_device_del(&bd->cdev, &bd->device);
  199. out_put_device:
  200. put_device(&bd->device);
  201. return ERR_PTR(ret);
  202. }
  203. EXPORT_SYMBOL_GPL(bsg_register_queue);
  204. static char *bsg_devnode(struct device *dev, umode_t *mode)
  205. {
  206. return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
  207. }
  208. static int __init bsg_init(void)
  209. {
  210. dev_t devid;
  211. int ret;
  212. bsg_class = class_create(THIS_MODULE, "bsg");
  213. if (IS_ERR(bsg_class))
  214. return PTR_ERR(bsg_class);
  215. bsg_class->devnode = bsg_devnode;
  216. ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
  217. if (ret)
  218. goto destroy_bsg_class;
  219. bsg_major = MAJOR(devid);
  220. printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
  221. " loaded (major %d)\n", bsg_major);
  222. return 0;
  223. destroy_bsg_class:
  224. class_destroy(bsg_class);
  225. return ret;
  226. }
  227. MODULE_AUTHOR("Jens Axboe");
  228. MODULE_DESCRIPTION(BSG_DESCRIPTION);
  229. MODULE_LICENSE("GPL");
  230. device_initcall(bsg_init);