Christoph Hellwig | 8c16567 | 2019-04-30 14:42:39 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 2 | /* |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 3 | * bsg.c - block layer implementation of the sg v4 interface |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 4 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/file.h> |
| 8 | #include <linux/blkdev.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 9 | #include <linux/cdev.h> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 10 | #include <linux/jiffies.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 11 | #include <linux/percpu.h> |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 12 | #include <linux/idr.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 13 | #include <linux/bsg.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 15 | |
| 16 | #include <scsi/scsi.h> |
| 17 | #include <scsi/scsi_ioctl.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 18 | #include <scsi/sg.h> |
| 19 | |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 20 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 21 | #define BSG_VERSION "0.4" |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 22 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 23 | struct bsg_device { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 24 | struct request_queue *queue; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 25 | struct device device; |
| 26 | struct cdev cdev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 27 | int max_queue; |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 28 | unsigned int timeout; |
| 29 | unsigned int reserved_size; |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 30 | bsg_sg_io_fn *sg_io_fn; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 33 | static inline struct bsg_device *to_bsg_device(struct inode *inode) |
| 34 | { |
| 35 | return container_of(inode->i_cdev, struct bsg_device, cdev); |
| 36 | } |
| 37 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 38 | #define BSG_DEFAULT_CMDS 64 |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 39 | #define BSG_MAX_DEVS 32768 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 40 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 41 | static DEFINE_IDA(bsg_minor_ida); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 42 | static struct class *bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 43 | static int bsg_major; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 44 | |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 45 | static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr) |
| 46 | { |
| 47 | unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT; |
| 48 | |
| 49 | if (hdr->timeout) |
| 50 | timeout = msecs_to_jiffies(hdr->timeout); |
| 51 | else if (bd->timeout) |
| 52 | timeout = bd->timeout; |
| 53 | |
| 54 | return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT); |
| 55 | } |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 56 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 57 | static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg) |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 58 | { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 59 | struct sg_io_v4 hdr; |
Christoph Hellwig | 17cb960 | 2018-03-13 17:28:41 +0100 | [diff] [blame] | 60 | int ret; |
| 61 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 62 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 63 | return -EFAULT; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 64 | if (hdr.guard != 'Q') |
| 65 | return -EINVAL; |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 66 | ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr)); |
Christoph Hellwig | 972248e | 2019-01-29 09:32:03 +0100 | [diff] [blame] | 67 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 68 | return -EFAULT; |
| 69 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 72 | static int bsg_open(struct inode *inode, struct file *file) |
| 73 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 74 | if (!blk_get_queue(to_bsg_device(inode)->queue)) |
| 75 | return -ENXIO; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int bsg_release(struct inode *inode, struct file *file) |
| 80 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 81 | blk_put_queue(to_bsg_device(inode)->queue); |
| 82 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 85 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
| 86 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 87 | return put_user(READ_ONCE(bd->max_queue), uarg); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
| 91 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 92 | int max_queue; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 93 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 94 | if (get_user(max_queue, uarg)) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 95 | return -EFAULT; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 96 | if (max_queue < 1) |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 97 | return -EINVAL; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 98 | WRITE_ONCE(bd->max_queue, max_queue); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 102 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 103 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 104 | struct bsg_device *bd = to_bsg_device(file_inode(file)); |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 105 | struct request_queue *q = bd->queue; |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 106 | void __user *uarg = (void __user *) arg; |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 107 | int __user *intp = uarg; |
| 108 | int val; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 109 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 110 | switch (cmd) { |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 111 | /* |
| 112 | * Our own ioctls |
| 113 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 114 | case SG_GET_COMMAND_Q: |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 115 | return bsg_get_command_q(bd, uarg); |
| 116 | case SG_SET_COMMAND_Q: |
| 117 | return bsg_set_command_q(bd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * SCSI/sg ioctls |
| 121 | */ |
| 122 | case SG_GET_VERSION_NUM: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 123 | return put_user(30527, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 124 | case SCSI_IOCTL_GET_IDLUN: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 125 | return put_user(0, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 126 | case SCSI_IOCTL_GET_BUS_NUMBER: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 127 | return put_user(0, intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 128 | case SG_SET_TIMEOUT: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 129 | if (get_user(val, intp)) |
| 130 | return -EFAULT; |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 131 | bd->timeout = clock_t_to_jiffies(val); |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 132 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 133 | case SG_GET_TIMEOUT: |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 134 | return jiffies_to_clock_t(bd->timeout); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 135 | case SG_GET_RESERVED_SIZE: |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 136 | return put_user(min(bd->reserved_size, queue_max_bytes(q)), |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 137 | intp); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 138 | case SG_SET_RESERVED_SIZE: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 139 | if (get_user(val, intp)) |
| 140 | return -EFAULT; |
| 141 | if (val < 0) |
| 142 | return -EINVAL; |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 143 | bd->reserved_size = |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 144 | min_t(unsigned int, val, queue_max_bytes(q)); |
| 145 | return 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 146 | case SG_EMULATED_HOST: |
Christoph Hellwig | d52fe8f | 2021-07-24 09:20:22 +0200 | [diff] [blame] | 147 | return put_user(1, intp); |
Christoph Hellwig | ccf3209 | 2018-11-09 20:12:25 +0100 | [diff] [blame] | 148 | case SG_IO: |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 149 | return bsg_sg_io(bd, file->f_mode, uarg); |
Christoph Hellwig | beec64d | 2021-07-24 09:20:10 +0200 | [diff] [blame] | 150 | case SCSI_IOCTL_SEND_COMMAND: |
| 151 | pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n", |
| 152 | current->comm); |
| 153 | return -EINVAL; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 154 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 155 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 159 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 160 | .open = bsg_open, |
| 161 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 162 | .unlocked_ioctl = bsg_ioctl, |
Arnd Bergmann | fe0da4e | 2019-03-15 17:13:06 +0100 | [diff] [blame] | 163 | .compat_ioctl = compat_ptr_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 164 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 165 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 168 | static void bsg_device_release(struct device *dev) |
| 169 | { |
| 170 | struct bsg_device *bd = container_of(dev, struct bsg_device, device); |
| 171 | |
| 172 | ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); |
| 173 | kfree(bd); |
| 174 | } |
| 175 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 176 | void bsg_unregister_queue(struct bsg_device *bd) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 177 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 178 | if (bd->queue->kobj.sd) |
| 179 | sysfs_remove_link(&bd->queue->kobj, "bsg"); |
| 180 | cdev_device_del(&bd->cdev, &bd->device); |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 181 | put_device(&bd->device); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 182 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 183 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 184 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 185 | struct bsg_device *bsg_register_queue(struct request_queue *q, |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 186 | struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 187 | { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 188 | struct bsg_device *bd; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 189 | int ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 190 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 191 | bd = kzalloc(sizeof(*bd), GFP_KERNEL); |
| 192 | if (!bd) |
| 193 | return ERR_PTR(-ENOMEM); |
| 194 | bd->max_queue = BSG_DEFAULT_CMDS; |
Christoph Hellwig | 1e61c1a | 2021-07-29 08:48:44 +0200 | [diff] [blame] | 195 | bd->reserved_size = INT_MAX; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 196 | bd->queue = q; |
Christoph Hellwig | 75ca564 | 2021-07-29 08:48:45 +0200 | [diff] [blame] | 197 | bd->sg_io_fn = sg_io_fn; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 198 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 199 | ret = ida_simple_get(&bsg_minor_ida, 0, BSG_MAX_DEVS, GFP_KERNEL); |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 200 | if (ret < 0) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 201 | if (ret == -ENOSPC) |
| 202 | dev_err(parent, "bsg: too many bsg devices\n"); |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 203 | kfree(bd); |
| 204 | return ERR_PTR(ret); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 205 | } |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 206 | bd->device.devt = MKDEV(bsg_major, ret); |
| 207 | bd->device.class = bsg_class; |
| 208 | bd->device.parent = parent; |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 209 | bd->device.release = bsg_device_release; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 210 | dev_set_name(&bd->device, "%s", name); |
| 211 | device_initialize(&bd->device); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 212 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 213 | cdev_init(&bd->cdev, &bsg_fops); |
| 214 | bd->cdev.owner = THIS_MODULE; |
| 215 | ret = cdev_device_add(&bd->cdev, &bd->device); |
| 216 | if (ret) |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 217 | goto out_put_device; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 218 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 219 | if (q->kobj.sd) { |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 220 | ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg"); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 221 | if (ret) |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 222 | goto out_device_del; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 225 | return bd; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 226 | |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 227 | out_device_del: |
| 228 | cdev_device_del(&bd->cdev, &bd->device); |
Zenghui Yu | 1a0db77 | 2021-09-11 18:53:06 +0800 | [diff] [blame] | 229 | out_put_device: |
| 230 | put_device(&bd->device); |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 231 | return ERR_PTR(ret); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 232 | } |
Christoph Hellwig | 7801104 | 2021-07-24 09:20:23 +0200 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(bsg_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 234 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 235 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 236 | { |
| 237 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 238 | } |
| 239 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 240 | static int __init bsg_init(void) |
| 241 | { |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 242 | dev_t devid; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 243 | int ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 244 | |
| 245 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Christoph Hellwig | 28519c8 | 2018-07-12 10:09:59 +0200 | [diff] [blame] | 246 | if (IS_ERR(bsg_class)) |
| 247 | return PTR_ERR(bsg_class); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 248 | bsg_class->devnode = bsg_devnode; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 249 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 250 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 251 | if (ret) |
| 252 | goto destroy_bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 253 | bsg_major = MAJOR(devid); |
| 254 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 255 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 256 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 257 | return 0; |
Christoph Hellwig | ead09dd | 2021-07-29 08:48:42 +0200 | [diff] [blame] | 258 | |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 259 | destroy_bsg_class: |
| 260 | class_destroy(bsg_class); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 261 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 265 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 266 | MODULE_LICENSE("GPL"); |
| 267 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 268 | device_initcall(bsg_init); |