| @@ -20,38 +20,29 @@ · 19 unmodified lines |
| 20 | 20 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 21 | 21 | #define BSG_VERSION "0.4" |
| 22 | 22 | |
| 23 | | −#define bsg_dbg(bd, fmt, ...) \ |
| 24 | | − pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) |
| 25 | | − |
| 26 | 23 | struct bsg_device { |
| 27 | 24 | struct request_queue *queue; |
| 28 | | − spinlock_t lock; |
| 29 | | − struct hlist_node dev_list; |
| 30 | | − refcount_t ref_count; |
| 31 | | − char name[20]; |
| 25 | + const struct bsg_ops *ops; |
| 26 | + struct device device; |
| 27 | + struct cdev cdev; |
| 32 | 28 | int max_queue; |
| 33 | 29 | }; |
| 34 | 30 | |
| 31 | +static inline struct bsg_device *to_bsg_device(struct inode *inode) |
| 32 | +{ |
| 33 | + return container_of(inode->i_cdev, struct bsg_device, cdev); |
| 34 | +} |
| 35 | + |
| 35 | 36 | #define BSG_DEFAULT_CMDS 64 |
| 36 | 37 | #define BSG_MAX_DEVS 32768 |
| 37 | 38 | |
| 38 | | −static DEFINE_MUTEX(bsg_mutex); |
| 39 | | −static DEFINE_IDR(bsg_minor_idr); |
| 40 | | − |
| 41 | | −#define BSG_LIST_ARRAY_SIZE 8 |
| 42 | | −static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; |
| 43 | | − |
| 39 | +static DEFINE_IDA(bsg_minor_ida); |
| 44 | 40 | static struct class *bsg_class; |
| 45 | 41 | static int bsg_major; |
| 46 | 42 | |
| 47 | | −static inline struct hlist_head *bsg_dev_idx_hash(int index) |
| 48 | | −{ |
| 49 | | − return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; |
| 50 | | −} |
| 51 | | − |
| 52 | 43 | #define uptr64(val) ((void __user *)(uintptr_t)(val)) |
| 53 | 44 | |
| 54 | | −static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) |
| 45 | +static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg) |
| 55 | 46 | { |
| 56 | 47 | struct request *rq; |
| 57 | 48 | struct bio *bio; |
| @@ -61,21 +52,18 @@ static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) · 3 unmodified lines |
| 61 | 52 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 62 | 53 | return -EFAULT; |
| 63 | 54 | |
| 64 | | − if (!q->bsg_dev.class_dev) |
| 65 | | − return -ENXIO; |
| 66 | | − |
| 67 | 55 | if (hdr.guard != 'Q') |
| 68 | 56 | return -EINVAL; |
| 69 | | − ret = q->bsg_dev.ops->check_proto(&hdr); |
| 57 | + ret = bd->ops->check_proto(&hdr); |
| 70 | 58 | if (ret) |
| 71 | 59 | return ret; |
| 72 | 60 | |
| 73 | | − rq = blk_get_request(q, hdr.dout_xfer_len ? |
| 61 | + rq = blk_get_request(bd->queue, hdr.dout_xfer_len ? |
| 74 | 62 | REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
| 75 | 63 | if (IS_ERR(rq)) |
| 76 | 64 | return PTR_ERR(rq); |
| 77 | 65 | |
| 78 | | − ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); |
| 66 | + ret = bd->ops->fill_hdr(rq, &hdr, mode); |
| 79 | 67 | if (ret) { |
| 80 | 68 | blk_put_request(rq); |
| 81 | 69 | return ret; |
| @@ -83,17 +71,17 @@ static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) · 1 unmodified line |
| 83 | 71 | |
| 84 | 72 | rq->timeout = msecs_to_jiffies(hdr.timeout); |
| 85 | 73 | if (!rq->timeout) |
| 86 | | − rq->timeout = q->sg_timeout; |
| 74 | + rq->timeout = rq->q->sg_timeout; |
| 87 | 75 | if (!rq->timeout) |
| 88 | 76 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
| 89 | 77 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 90 | 78 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
| 91 | 79 | |
| 92 | 80 | if (hdr.dout_xfer_len) { |
| 93 | | − ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), |
| 81 | + ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr.dout_xferp), |
| 94 | 82 | hdr.dout_xfer_len, GFP_KERNEL); |
| 95 | 83 | } else if (hdr.din_xfer_len) { |
| 96 | | − ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), |
| 84 | + ret = blk_rq_map_user(rq->q, rq, NULL, uptr64(hdr.din_xferp), |
| 97 | 85 | hdr.din_xfer_len, GFP_KERNEL); |
| 98 | 86 | } |
| 99 | 87 | |
| @@ -103,171 +91,50 @@ static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) · 3 unmodified lines |
| 103 | 91 | bio = rq->bio; |
| 104 | 92 | |
| 105 | 93 | blk_execute_rq(NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 106 | | − ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); |
| 94 | + ret = bd->ops->complete_rq(rq, &hdr); |
| 107 | 95 | blk_rq_unmap_user(bio); |
| 108 | 96 | |
| 109 | 97 | out_free_rq: |
| 110 | | − rq->q->bsg_dev.ops->free_rq(rq); |
| 98 | + bd->ops->free_rq(rq); |
| 111 | 99 | blk_put_request(rq); |
| 112 | 100 | if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) |
| 113 | 101 | return -EFAULT; |
| 114 | 102 | return ret; |
| 115 | 103 | } |
| 116 | 104 | |
| 117 | | −static struct bsg_device *bsg_alloc_device(void) |
| 118 | | −{ |
| 119 | | − struct bsg_device *bd; |
| 120 | | − |
| 121 | | − bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); |
| 122 | | − if (unlikely(!bd)) |
| 123 | | − return NULL; |
| 124 | | − |
| 125 | | − spin_lock_init(&bd->lock); |
| 126 | | − bd->max_queue = BSG_DEFAULT_CMDS; |
| 127 | | − INIT_HLIST_NODE(&bd->dev_list); |
| 128 | | − return bd; |
| 129 | | −} |
| 130 | | − |
| 131 | | −static int bsg_put_device(struct bsg_device *bd) |
| 132 | | −{ |
| 133 | | − struct request_queue *q = bd->queue; |
| 134 | | − |
| 135 | | − mutex_lock(&bsg_mutex); |
| 136 | | − |
| 137 | | − if (!refcount_dec_and_test(&bd->ref_count)) { |
| 138 | | − mutex_unlock(&bsg_mutex); |
| 139 | | − return 0; |
| 140 | | − } |
| 141 | | − |
| 142 | | − hlist_del(&bd->dev_list); |
| 143 | | − mutex_unlock(&bsg_mutex); |
| 144 | | − |
| 145 | | − bsg_dbg(bd, "tearing down\n"); |
| 146 | | − |
| 147 | | − /* |
| 148 | | − * close can always block |
| 149 | | − */ |
| 150 | | − kfree(bd); |
| 151 | | − blk_put_queue(q); |
| 152 | | − return 0; |
| 153 | | −} |
| 154 | | − |
| 155 | | −static struct bsg_device *bsg_add_device(struct inode *inode, |
| 156 | | − struct request_queue *rq, |
| 157 | | − struct file *file) |
| 158 | | −{ |
| 159 | | − struct bsg_device *bd; |
| 160 | | − unsigned char buf[32]; |
| 161 | | − |
| 162 | | − lockdep_assert_held(&bsg_mutex); |
| 163 | | − |
| 164 | | − if (!blk_get_queue(rq)) |
| 165 | | − return ERR_PTR(-ENXIO); |
| 166 | | − |
| 167 | | − bd = bsg_alloc_device(); |
| 168 | | − if (!bd) { |
| 169 | | − blk_put_queue(rq); |
| 170 | | − return ERR_PTR(-ENOMEM); |
| 171 | | − } |
| 172 | | − |
| 173 | | − bd->queue = rq; |
| 174 | | − |
| 175 | | − refcount_set(&bd->ref_count, 1); |
| 176 | | − hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
| 177 | | − |
| 178 | | − strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); |
| 179 | | − bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
| 180 | | − format_dev_t(buf, inode->i_rdev), bd->max_queue); |
| 181 | | − |
| 182 | | − return bd; |
| 183 | | −} |
| 184 | | − |
| 185 | | −static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) |
| 186 | | −{ |
| 187 | | − struct bsg_device *bd; |
| 188 | | − |
| 189 | | − lockdep_assert_held(&bsg_mutex); |
| 190 | | − |
| 191 | | − hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
| 192 | | − if (bd->queue == q) { |
| 193 | | − refcount_inc(&bd->ref_count); |
| 194 | | − goto found; |
| 195 | | − } |
| 196 | | − } |
| 197 | | − bd = NULL; |
| 198 | | −found: |
| 199 | | − return bd; |
| 200 | | −} |
| 201 | | − |
| 202 | | −static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) |
| 203 | | −{ |
| 204 | | − struct bsg_device *bd; |
| 205 | | − struct bsg_class_device *bcd; |
| 206 | | − |
| 207 | | − /* |
| 208 | | − * find the class device |
| 209 | | − */ |
| 210 | | − mutex_lock(&bsg_mutex); |
| 211 | | − bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
| 212 | | − |
| 213 | | − if (!bcd) { |
| 214 | | − bd = ERR_PTR(-ENODEV); |
| 215 | | − goto out_unlock; |
| 216 | | − } |
| 217 | | − |
| 218 | | − bd = __bsg_get_device(iminor(inode), bcd->queue); |
| 219 | | − if (!bd) |
| 220 | | − bd = bsg_add_device(inode, bcd->queue, file); |
| 221 | | − |
| 222 | | −out_unlock: |
| 223 | | − mutex_unlock(&bsg_mutex); |
| 224 | | − return bd; |
| 225 | | −} |
| 226 | | − |
| 227 | 105 | static int bsg_open(struct inode *inode, struct file *file) |
| 228 | 106 | { |
| 229 | | − struct bsg_device *bd; |
| 230 | | − |
| 231 | | − bd = bsg_get_device(inode, file); |
| 232 | | − |
| 233 | | − if (IS_ERR(bd)) |
| 234 | | − return PTR_ERR(bd); |
| 235 | | − |
| 236 | | − file->private_data = bd; |
| 107 | + if (!blk_get_queue(to_bsg_device(inode)->queue)) |
| 108 | + return -ENXIO; |
| 237 | 109 | return 0; |
| 238 | 110 | } |
| 239 | 111 | |
| 240 | 112 | static int bsg_release(struct inode *inode, struct file *file) |
| 241 | 113 | { |
| 242 | | − struct bsg_device *bd = file->private_data; |
| 243 | | − |
| 244 | | − file->private_data = NULL; |
| 245 | | − return bsg_put_device(bd); |
| 114 | + blk_put_queue(to_bsg_device(inode)->queue); |
| 115 | + return 0; |
| 246 | 116 | } |
| 247 | 117 | |
| 248 | 118 | static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) |
| 249 | 119 | { |
| 250 | | − return put_user(bd->max_queue, uarg); |
| 120 | + return put_user(READ_ONCE(bd->max_queue), uarg); |
| 251 | 121 | } |
| 252 | 122 | |
| 253 | 123 | static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) |
| 254 | 124 | { |
| 255 | | − int queue; |
| 125 | + int max_queue; |
| 256 | 126 | |
| 257 | | − if (get_user(queue, uarg)) |
| 127 | + if (get_user(max_queue, uarg)) |
| 258 | 128 | return -EFAULT; |
| 259 | | − if (queue < 1) |
| 129 | + if (max_queue < 1) |
| 260 | 130 | return -EINVAL; |
| 261 | | − |
| 262 | | − spin_lock_irq(&bd->lock); |
| 263 | | − bd->max_queue = queue; |
| 264 | | − spin_unlock_irq(&bd->lock); |
| 131 | + WRITE_ONCE(bd->max_queue, max_queue); |
| 265 | 132 | return 0; |
| 266 | 133 | } |
| 267 | 134 | |
| 268 | 135 | static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 269 | 136 | { |
| 270 | | − struct bsg_device *bd = file->private_data; |
| 137 | + struct bsg_device *bd = to_bsg_device(file_inode(file)); |
| 271 | 138 | struct request_queue *q = bd->queue; |
| 272 | 139 | void __user *uarg = (void __user *) arg; |
| 273 | 140 | int __user *intp = uarg; |
| @@ -312,7 +179,7 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) · 38 unmodified lines |
| 312 | 179 | case SG_EMULATED_HOST: |
| 313 | 180 | return put_user(1, intp); |
| 314 | 181 | case SG_IO: |
| 315 | | − return bsg_sg_io(q, file->f_mode, uarg); |
| 182 | + return bsg_sg_io(bd, file->f_mode, uarg); |
| 316 | 183 | case SCSI_IOCTL_SEND_COMMAND: |
| 317 | 184 | pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n", |
| 318 | 185 | current->comm); |
| @@ -331,83 +198,66 @@ static const struct file_operations bsg_fops = { · 12 unmodified lines |
| 331 | 198 | .llseek = default_llseek, |
| 332 | 199 | }; |
| 333 | 200 | |
| 334 | | −void bsg_unregister_queue(struct request_queue *q) |
| 201 | +void bsg_unregister_queue(struct bsg_device *bd) |
| 335 | 202 | { |
| 336 | | − struct bsg_class_device *bcd = &q->bsg_dev; |
| 337 | | − |
| 338 | | − if (!bcd->class_dev) |
| 339 | | − return; |
| 340 | | − |
| 341 | | − mutex_lock(&bsg_mutex); |
| 342 | | − idr_remove(&bsg_minor_idr, bcd->minor); |
| 343 | | − if (q->kobj.sd) |
| 344 | | − sysfs_remove_link(&q->kobj, "bsg"); |
| 345 | | − device_unregister(bcd->class_dev); |
| 346 | | − bcd->class_dev = NULL; |
| 347 | | − mutex_unlock(&bsg_mutex); |
| 203 | + if (bd->queue->kobj.sd) |
| 204 | + sysfs_remove_link(&bd->queue->kobj, "bsg"); |
| 205 | + cdev_device_del(&bd->cdev, &bd->device); |
| 206 | + ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); |
| 207 | + kfree(bd); |
| 348 | 208 | } |
| 349 | 209 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
| 350 | 210 | |
| 351 | | −int bsg_register_queue(struct request_queue *q, struct device *parent, |
| 352 | | − const char *name, const struct bsg_ops *ops) |
| 211 | +struct bsg_device *bsg_register_queue(struct request_queue *q, |
| 212 | + struct device *parent, const char *name, |
| 213 | + const struct bsg_ops *ops) |
| 353 | 214 | { |
| 354 | | − struct bsg_class_device *bcd; |
| 355 | | − dev_t dev; |
| 215 | + struct bsg_device *bd; |
| 356 | 216 | int ret; |
| 357 | | − struct device *class_dev = NULL; |
| 358 | | − |
| 359 | | − /* |
| 360 | | − * we need a proper transport to send commands, not a stacked device |
| 361 | | − */ |
| 362 | | − if (!queue_is_mq(q)) |
| 363 | | − return 0; |
| 364 | 217 | |
| 365 | | − bcd = &q->bsg_dev; |
| 366 | | − memset(bcd, 0, sizeof(*bcd)); |
| 367 | | − |
| 368 | | − mutex_lock(&bsg_mutex); |
| 218 | + bd = kzalloc(sizeof(*bd), GFP_KERNEL); |
| 219 | + if (!bd) |
| 220 | + return ERR_PTR(-ENOMEM); |
| 221 | + bd->max_queue = BSG_DEFAULT_CMDS; |
| 222 | + bd->queue = q; |
| 223 | + bd->ops = ops; |
| 369 | 224 | |
| 370 | | − ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
| 225 | + ret = ida_simple_get(&bsg_minor_ida, 0, BSG_MAX_DEVS, GFP_KERNEL); |
| 371 | 226 | if (ret < 0) { |
| 372 | | − if (ret == -ENOSPC) { |
| 373 | | − printk(KERN_ERR "bsg: too many bsg devices\n"); |
| 374 | | − ret = -EINVAL; |
| 375 | | − } |
| 376 | | − goto unlock; |
| 377 | | − } |
| 378 | | − |
| 379 | | − bcd->minor = ret; |
| 380 | | − bcd->queue = q; |
| 381 | | − bcd->ops = ops; |
| 382 | | − dev = MKDEV(bsg_major, bcd->minor); |
| 383 | | − class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); |
| 384 | | − if (IS_ERR(class_dev)) { |
| 385 | | − ret = PTR_ERR(class_dev); |
| 386 | | − goto idr_remove; |
| 227 | + if (ret == -ENOSPC) |
| 228 | + dev_err(parent, "bsg: too many bsg devices\n"); |
| 229 | + goto out_kfree; |
| 387 | 230 | } |
| 388 | | − bcd->class_dev = class_dev; |
| 231 | + bd->device.devt = MKDEV(bsg_major, ret); |
| 232 | + bd->device.class = bsg_class; |
| 233 | + bd->device.parent = parent; |
| 234 | + dev_set_name(&bd->device, "%s", name); |
| 235 | + device_initialize(&bd->device); |
| 236 | + |
| 237 | + cdev_init(&bd->cdev, &bsg_fops); |
| 238 | + bd->cdev.owner = THIS_MODULE; |
| 239 | + ret = cdev_device_add(&bd->cdev, &bd->device); |
| 240 | + if (ret) |
| 241 | + goto out_ida_remove; |
| 389 | 242 | |
| 390 | 243 | if (q->kobj.sd) { |
| 391 | | − ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
| 244 | + ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg"); |
| 392 | 245 | if (ret) |
| 393 | | − goto unregister_class_dev; |
| 246 | + goto out_device_del; |
| 394 | 247 | } |
| 395 | 248 | |
| 396 | | − mutex_unlock(&bsg_mutex); |
| 397 | | − return 0; |
| 249 | + return bd; |
| 398 | 250 | |
| 399 | | −unregister_class_dev: |
| 400 | | − device_unregister(class_dev); |
| 401 | | −idr_remove: |
| 402 | | − idr_remove(&bsg_minor_idr, bcd->minor); |
| 403 | | −unlock: |
| 404 | | − mutex_unlock(&bsg_mutex); |
| 405 | | − return ret; |
| 251 | +out_device_del: |
| 252 | + cdev_device_del(&bd->cdev, &bd->device); |
| 253 | +out_ida_remove: |
| 254 | + ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); |
| 255 | +out_kfree: |
| 256 | + kfree(bd); |
| 257 | + return ERR_PTR(ret); |
| 406 | 258 | } |
| 407 | 259 | EXPORT_SYMBOL_GPL(bsg_register_queue); |
| 408 | 260 | |
| 409 | | −static struct cdev bsg_cdev; |
| 410 | | − |
| 411 | 261 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
| 412 | 262 | { |
| 413 | 263 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| @@ -415,11 +265,8 @@ static char *bsg_devnode(struct device *dev, umode_t *mode) · 1 unmodified line |
| 415 | 265 | |
| 416 | 266 | static int __init bsg_init(void) |
| 417 | 267 | { |
| 418 | | − int ret, i; |
| 419 | 268 | dev_t devid; |
| 420 | | − |
| 421 | | − for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
| 422 | | − INIT_HLIST_HEAD(&bsg_device_list[i]); |
| 269 | + int ret; |
| 423 | 270 | |
| 424 | 271 | bsg_class = class_create(THIS_MODULE, "bsg"); |
| 425 | 272 | if (IS_ERR(bsg_class)) |
| @@ -429,19 +276,12 @@ static int __init bsg_init(void) · 3 unmodified lines |
| 429 | 276 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
| 430 | 277 | if (ret) |
| 431 | 278 | goto destroy_bsg_class; |
| 432 | | − |
| 433 | 279 | bsg_major = MAJOR(devid); |
| 434 | 280 | |
| 435 | | − cdev_init(&bsg_cdev, &bsg_fops); |
| 436 | | − ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 437 | | − if (ret) |
| 438 | | − goto unregister_chrdev; |
| 439 | | − |
| 440 | 281 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
| 441 | 282 | " loaded (major %d)\n", bsg_major); |
| 442 | 283 | return 0; |
| 443 | | −unregister_chrdev: |
| 444 | | − unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 284 | + |
| 445 | 285 | destroy_bsg_class: |
| 446 | 286 | class_destroy(bsg_class); |
| 447 | 287 | return ret; |