1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * bsg.c - block layer implementation of the s 3 * bsg.c - block layer implementation of the sg v4 interface 4 */ 4 */ 5 #include <linux/module.h> 5 #include <linux/module.h> 6 #include <linux/init.h> 6 #include <linux/init.h> 7 #include <linux/file.h> 7 #include <linux/file.h> 8 #include <linux/blkdev.h> 8 #include <linux/blkdev.h> 9 #include <linux/cdev.h> 9 #include <linux/cdev.h> 10 #include <linux/jiffies.h> 10 #include <linux/jiffies.h> 11 #include <linux/percpu.h> 11 #include <linux/percpu.h> 12 #include <linux/idr.h> 12 #include <linux/idr.h> 13 #include <linux/bsg.h> 13 #include <linux/bsg.h> 14 #include <linux/slab.h> 14 #include <linux/slab.h> 15 15 16 #include <scsi/scsi.h> 16 #include <scsi/scsi.h> 17 #include <scsi/scsi_ioctl.h> 17 #include <scsi/scsi_ioctl.h> >> 18 #include <scsi/scsi_cmnd.h> >> 19 #include <scsi/scsi_device.h> >> 20 #include <scsi/scsi_driver.h> 18 #include <scsi/sg.h> 21 #include <scsi/sg.h> 19 22 20 #define BSG_DESCRIPTION "Block layer SCSI gene 23 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" 21 #define BSG_VERSION "0.4" 24 #define BSG_VERSION "0.4" 22 25 >> 26 #define bsg_dbg(bd, fmt, ...) \ >> 27 pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) >> 28 23 struct bsg_device { 29 struct bsg_device { 24 struct request_queue *queue; 30 struct request_queue *queue; 25 struct device device; !! 31 spinlock_t lock; 26 struct cdev cdev; !! 32 struct hlist_node dev_list; >> 33 refcount_t ref_count; >> 34 char name[20]; 27 int max_queue; 35 int max_queue; 28 unsigned int timeout; << 29 unsigned int reserved_size; << 30 bsg_sg_io_fn *sg_io_fn; << 31 }; 36 }; 32 37 33 static inline struct bsg_device *to_bsg_device !! 38 #define BSG_DEFAULT_CMDS 64 >> 39 #define BSG_MAX_DEVS 32768 >> 40 >> 41 static DEFINE_MUTEX(bsg_mutex); >> 42 static DEFINE_IDR(bsg_minor_idr); >> 43 >> 44 #define BSG_LIST_ARRAY_SIZE 8 >> 45 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; >> 46 >> 47 static struct class *bsg_class; >> 48 static int bsg_major; >> 49 >> 50 static inline struct hlist_head *bsg_dev_idx_hash(int index) 34 { 51 { 35 return container_of(inode->i_cdev, str !! 52 return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; 36 } 53 } 37 54 38 #define BSG_DEFAULT_CMDS 64 !! 55 #define uptr64(val) ((void __user *)(uintptr_t)(val)) 39 #define BSG_MAX_DEVS (1 << MINORBIT << 40 56 41 static DEFINE_IDA(bsg_minor_ida); !! 57 static int bsg_scsi_check_proto(struct sg_io_v4 *hdr) 42 static const struct class bsg_class; !! 58 { 43 static int bsg_major; !! 59 if (hdr->protocol != BSG_PROTOCOL_SCSI || >> 60 hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD) >> 61 return -EINVAL; >> 62 return 0; >> 63 } >> 64 >> 65 static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr, >> 66 fmode_t mode) >> 67 { >> 68 struct scsi_request *sreq = scsi_req(rq); >> 69 >> 70 if (hdr->dout_xfer_len && hdr->din_xfer_len) { >> 71 pr_warn_once("BIDI support in bsg has been removed.\n"); >> 72 return -EOPNOTSUPP; >> 73 } >> 74 >> 75 sreq->cmd_len = hdr->request_len; >> 76 if (sreq->cmd_len > BLK_MAX_CDB) { >> 77 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL); >> 78 if (!sreq->cmd) >> 79 return -ENOMEM; >> 80 } >> 81 >> 82 if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len)) >> 83 return -EFAULT; >> 84 if (blk_verify_command(sreq->cmd, mode)) >> 85 return -EPERM; >> 86 return 0; >> 87 } 44 88 45 static unsigned int bsg_timeout(struct bsg_dev !! 89 static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr) 46 { 90 { 47 unsigned int timeout = BLK_DEFAULT_SG_ !! 91 struct scsi_request *sreq = scsi_req(rq); >> 92 int ret = 0; >> 93 >> 94 /* >> 95 * fill in all the output members >> 96 */ >> 97 hdr->device_status = sreq->result & 0xff; >> 98 hdr->transport_status = host_byte(sreq->result); >> 99 hdr->driver_status = driver_byte(sreq->result); >> 100 hdr->info = 0; >> 101 if (hdr->device_status || hdr->transport_status || hdr->driver_status) >> 102 hdr->info |= SG_INFO_CHECK; >> 103 hdr->response_len = 0; >> 104 >> 105 if (sreq->sense_len && hdr->response) { >> 106 int len = min_t(unsigned int, hdr->max_response_len, >> 107 sreq->sense_len); >> 108 >> 109 if (copy_to_user(uptr64(hdr->response), sreq->sense, len)) >> 110 ret = -EFAULT; >> 111 else >> 112 hdr->response_len = len; >> 113 } >> 114 >> 115 if (rq_data_dir(rq) == READ) >> 116 hdr->din_resid = sreq->resid_len; >> 117 else >> 118 hdr->dout_resid = sreq->resid_len; 48 119 49 if (hdr->timeout) !! 120 return ret; 50 timeout = msecs_to_jiffies(hdr !! 121 } 51 else if (bd->timeout) << 52 timeout = bd->timeout; << 53 122 54 return max_t(unsigned int, timeout, BL !! 123 static void bsg_scsi_free_rq(struct request *rq) >> 124 { >> 125 scsi_req_free_cmd(scsi_req(rq)); 55 } 126 } 56 127 57 static int bsg_sg_io(struct bsg_device *bd, bo !! 128 static const struct bsg_ops bsg_scsi_ops = { 58 void __user *uarg) !! 129 .check_proto = bsg_scsi_check_proto, >> 130 .fill_hdr = bsg_scsi_fill_hdr, >> 131 .complete_rq = bsg_scsi_complete_rq, >> 132 .free_rq = bsg_scsi_free_rq, >> 133 }; >> 134 >> 135 static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg) 59 { 136 { >> 137 struct request *rq; >> 138 struct bio *bio; 60 struct sg_io_v4 hdr; 139 struct sg_io_v4 hdr; 61 int ret; 140 int ret; 62 141 63 if (copy_from_user(&hdr, uarg, sizeof( 142 if (copy_from_user(&hdr, uarg, sizeof(hdr))) 64 return -EFAULT; 143 return -EFAULT; >> 144 >> 145 if (!q->bsg_dev.class_dev) >> 146 return -ENXIO; >> 147 65 if (hdr.guard != 'Q') 148 if (hdr.guard != 'Q') 66 return -EINVAL; 149 return -EINVAL; 67 ret = bd->sg_io_fn(bd->queue, &hdr, op !! 150 ret = q->bsg_dev.ops->check_proto(&hdr); 68 bsg_timeout(bd, &hd !! 151 if (ret) >> 152 return ret; >> 153 >> 154 rq = blk_get_request(q, hdr.dout_xfer_len ? >> 155 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0); >> 156 if (IS_ERR(rq)) >> 157 return PTR_ERR(rq); >> 158 >> 159 ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode); >> 160 if (ret) >> 161 return ret; >> 162 >> 163 rq->timeout = msecs_to_jiffies(hdr.timeout); >> 164 if (!rq->timeout) >> 165 rq->timeout = q->sg_timeout; >> 166 if (!rq->timeout) >> 167 rq->timeout = BLK_DEFAULT_SG_TIMEOUT; >> 168 if (rq->timeout < BLK_MIN_SG_TIMEOUT) >> 169 rq->timeout = BLK_MIN_SG_TIMEOUT; >> 170 >> 171 if (hdr.dout_xfer_len) { >> 172 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp), >> 173 hdr.dout_xfer_len, GFP_KERNEL); >> 174 } else if (hdr.din_xfer_len) { >> 175 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp), >> 176 hdr.din_xfer_len, GFP_KERNEL); >> 177 } >> 178 >> 179 if (ret) >> 180 goto out_free_rq; >> 181 >> 182 bio = rq->bio; >> 183 >> 184 blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL)); >> 185 ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr); >> 186 blk_rq_unmap_user(bio); >> 187 >> 188 out_free_rq: >> 189 rq->q->bsg_dev.ops->free_rq(rq); >> 190 blk_put_request(rq); 69 if (!ret && copy_to_user(uarg, &hdr, s 191 if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr))) 70 return -EFAULT; 192 return -EFAULT; 71 return ret; 193 return ret; 72 } 194 } 73 195 >> 196 static struct bsg_device *bsg_alloc_device(void) >> 197 { >> 198 struct bsg_device *bd; >> 199 >> 200 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); >> 201 if (unlikely(!bd)) >> 202 return NULL; >> 203 >> 204 spin_lock_init(&bd->lock); >> 205 bd->max_queue = BSG_DEFAULT_CMDS; >> 206 INIT_HLIST_NODE(&bd->dev_list); >> 207 return bd; >> 208 } >> 209 >> 210 static int bsg_put_device(struct bsg_device *bd) >> 211 { >> 212 struct request_queue *q = bd->queue; >> 213 >> 214 mutex_lock(&bsg_mutex); >> 215 >> 216 if (!refcount_dec_and_test(&bd->ref_count)) { >> 217 mutex_unlock(&bsg_mutex); >> 218 return 0; >> 219 } >> 220 >> 221 hlist_del(&bd->dev_list); >> 222 mutex_unlock(&bsg_mutex); >> 223 >> 224 bsg_dbg(bd, "tearing down\n"); >> 225 >> 226 /* >> 227 * close can always block >> 228 */ >> 229 kfree(bd); >> 230 blk_put_queue(q); >> 231 return 0; >> 232 } >> 233 >> 234 static struct bsg_device *bsg_add_device(struct inode *inode, >> 235 struct request_queue *rq, >> 236 struct file *file) >> 237 { >> 238 struct bsg_device *bd; >> 239 unsigned char buf[32]; >> 240 >> 241 lockdep_assert_held(&bsg_mutex); >> 242 >> 243 if (!blk_get_queue(rq)) >> 244 return ERR_PTR(-ENXIO); >> 245 >> 246 bd = bsg_alloc_device(); >> 247 if (!bd) { >> 248 blk_put_queue(rq); >> 249 return ERR_PTR(-ENOMEM); >> 250 } >> 251 >> 252 bd->queue = rq; >> 253 >> 254 refcount_set(&bd->ref_count, 1); >> 255 hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); >> 256 >> 257 strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); >> 258 bsg_dbg(bd, "bound to <%s>, max queue %d\n", >> 259 format_dev_t(buf, inode->i_rdev), bd->max_queue); >> 260 >> 261 return bd; >> 262 } >> 263 >> 264 static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) >> 265 { >> 266 struct bsg_device *bd; >> 267 >> 268 lockdep_assert_held(&bsg_mutex); >> 269 >> 270 hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { >> 271 if (bd->queue == q) { >> 272 refcount_inc(&bd->ref_count); >> 273 goto found; >> 274 } >> 275 } >> 276 bd = NULL; >> 277 found: >> 278 return bd; >> 279 } >> 280 >> 281 static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) >> 282 { >> 283 struct bsg_device *bd; >> 284 struct bsg_class_device *bcd; >> 285 >> 286 /* >> 287 * find the class device >> 288 */ >> 289 mutex_lock(&bsg_mutex); >> 290 bcd = idr_find(&bsg_minor_idr, iminor(inode)); >> 291 >> 292 if (!bcd) { >> 293 bd = ERR_PTR(-ENODEV); >> 294 goto out_unlock; >> 295 } >> 296 >> 297 bd = __bsg_get_device(iminor(inode), bcd->queue); >> 298 if (!bd) >> 299 bd = bsg_add_device(inode, bcd->queue, file); >> 300 >> 301 out_unlock: >> 302 mutex_unlock(&bsg_mutex); >> 303 return bd; >> 304 } >> 305 74 static int bsg_open(struct inode *inode, struc 306 static int bsg_open(struct inode *inode, struct file *file) 75 { 307 { 76 if (!blk_get_queue(to_bsg_device(inode !! 308 struct bsg_device *bd; 77 return -ENXIO; !! 309 >> 310 bd = bsg_get_device(inode, file); >> 311 >> 312 if (IS_ERR(bd)) >> 313 return PTR_ERR(bd); >> 314 >> 315 file->private_data = bd; 78 return 0; 316 return 0; 79 } 317 } 80 318 81 static int bsg_release(struct inode *inode, st 319 static int bsg_release(struct inode *inode, struct file *file) 82 { 320 { 83 blk_put_queue(to_bsg_device(inode)->qu !! 321 struct bsg_device *bd = file->private_data; 84 return 0; !! 322 >> 323 file->private_data = NULL; >> 324 return bsg_put_device(bd); 85 } 325 } 86 326 87 static int bsg_get_command_q(struct bsg_device 327 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg) 88 { 328 { 89 return put_user(READ_ONCE(bd->max_queu !! 329 return put_user(bd->max_queue, uarg); 90 } 330 } 91 331 92 static int bsg_set_command_q(struct bsg_device 332 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg) 93 { 333 { 94 int max_queue; !! 334 int queue; 95 335 96 if (get_user(max_queue, uarg)) !! 336 if (get_user(queue, uarg)) 97 return -EFAULT; 337 return -EFAULT; 98 if (max_queue < 1) !! 338 if (queue < 1) 99 return -EINVAL; 339 return -EINVAL; 100 WRITE_ONCE(bd->max_queue, max_queue); !! 340 >> 341 spin_lock_irq(&bd->lock); >> 342 bd->max_queue = queue; >> 343 spin_unlock_irq(&bd->lock); 101 return 0; 344 return 0; 102 } 345 } 103 346 104 static long bsg_ioctl(struct file *file, unsig 347 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 105 { 348 { 106 struct bsg_device *bd = to_bsg_device( !! 349 struct bsg_device *bd = file->private_data; 107 struct request_queue *q = bd->queue; << 108 void __user *uarg = (void __user *) ar 350 void __user *uarg = (void __user *) arg; 109 int __user *intp = uarg; << 110 int val; << 111 351 112 switch (cmd) { 352 switch (cmd) { 113 /* 353 /* 114 * Our own ioctls 354 * Our own ioctls 115 */ 355 */ 116 case SG_GET_COMMAND_Q: 356 case SG_GET_COMMAND_Q: 117 return bsg_get_command_q(bd, u 357 return bsg_get_command_q(bd, uarg); 118 case SG_SET_COMMAND_Q: 358 case SG_SET_COMMAND_Q: 119 return bsg_set_command_q(bd, u 359 return bsg_set_command_q(bd, uarg); 120 360 121 /* 361 /* 122 * SCSI/sg ioctls 362 * SCSI/sg ioctls 123 */ 363 */ 124 case SG_GET_VERSION_NUM: 364 case SG_GET_VERSION_NUM: 125 return put_user(30527, intp); << 126 case SCSI_IOCTL_GET_IDLUN: 365 case SCSI_IOCTL_GET_IDLUN: 127 return put_user(0, intp); << 128 case SCSI_IOCTL_GET_BUS_NUMBER: 366 case SCSI_IOCTL_GET_BUS_NUMBER: 129 return put_user(0, intp); << 130 case SG_SET_TIMEOUT: 367 case SG_SET_TIMEOUT: 131 if (get_user(val, intp)) << 132 return -EFAULT; << 133 bd->timeout = clock_t_to_jiffi << 134 return 0; << 135 case SG_GET_TIMEOUT: 368 case SG_GET_TIMEOUT: 136 return jiffies_to_clock_t(bd-> << 137 case SG_GET_RESERVED_SIZE: 369 case SG_GET_RESERVED_SIZE: 138 return put_user(min(bd->reserv << 139 intp); << 140 case SG_SET_RESERVED_SIZE: 370 case SG_SET_RESERVED_SIZE: 141 if (get_user(val, intp)) << 142 return -EFAULT; << 143 if (val < 0) << 144 return -EINVAL; << 145 bd->reserved_size = << 146 min_t(unsigned int, va << 147 return 0; << 148 case SG_EMULATED_HOST: 371 case SG_EMULATED_HOST: 149 return put_user(1, intp); << 150 case SG_IO: << 151 return bsg_sg_io(bd, file->f_m << 152 case SCSI_IOCTL_SEND_COMMAND: 372 case SCSI_IOCTL_SEND_COMMAND: 153 pr_warn_ratelimited("%s: calli !! 373 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); 154 current->comm) !! 374 case SG_IO: 155 return -EINVAL; !! 375 return bsg_sg_io(bd->queue, file->f_mode, uarg); 156 default: 376 default: 157 return -ENOTTY; 377 return -ENOTTY; 158 } 378 } 159 } 379 } 160 380 161 static const struct file_operations bsg_fops = 381 static const struct file_operations bsg_fops = { 162 .open = bsg_open, 382 .open = bsg_open, 163 .release = bsg_release, 383 .release = bsg_release, 164 .unlocked_ioctl = bsg_ioctl, 384 .unlocked_ioctl = bsg_ioctl, 165 .compat_ioctl = compat_ptr_ioc << 166 .owner = THIS_MODULE, 385 .owner = THIS_MODULE, 167 .llseek = default_llseek 386 .llseek = default_llseek, 168 }; 387 }; 169 388 170 static void bsg_device_release(struct device * !! 389 void bsg_unregister_queue(struct request_queue *q) 171 { 390 { 172 struct bsg_device *bd = container_of(d !! 391 struct bsg_class_device *bcd = &q->bsg_dev; 173 << 174 ida_free(&bsg_minor_ida, MINOR(bd->dev << 175 kfree(bd); << 176 } << 177 392 178 void bsg_unregister_queue(struct bsg_device *b !! 393 if (!bcd->class_dev) 179 { !! 394 return; 180 struct gendisk *disk = bd->queue->disk << 181 395 182 if (disk && disk->queue_kobj.sd) !! 396 mutex_lock(&bsg_mutex); 183 sysfs_remove_link(&disk->queue !! 397 idr_remove(&bsg_minor_idr, bcd->minor); 184 cdev_device_del(&bd->cdev, &bd->device !! 398 if (q->kobj.sd) 185 put_device(&bd->device); !! 399 sysfs_remove_link(&q->kobj, "bsg"); >> 400 device_unregister(bcd->class_dev); >> 401 bcd->class_dev = NULL; >> 402 mutex_unlock(&bsg_mutex); 186 } 403 } 187 EXPORT_SYMBOL_GPL(bsg_unregister_queue); 404 EXPORT_SYMBOL_GPL(bsg_unregister_queue); 188 405 189 struct bsg_device *bsg_register_queue(struct r !! 406 int bsg_register_queue(struct request_queue *q, struct device *parent, 190 struct device *parent, const c !! 407 const char *name, const struct bsg_ops *ops) 191 { 408 { 192 struct bsg_device *bd; !! 409 struct bsg_class_device *bcd; >> 410 dev_t dev; 193 int ret; 411 int ret; >> 412 struct device *class_dev = NULL; 194 413 195 bd = kzalloc(sizeof(*bd), GFP_KERNEL); !! 414 /* 196 if (!bd) !! 415 * we need a proper transport to send commands, not a stacked device 197 return ERR_PTR(-ENOMEM); !! 416 */ 198 bd->max_queue = BSG_DEFAULT_CMDS; !! 417 if (!queue_is_mq(q)) 199 bd->reserved_size = INT_MAX; !! 418 return 0; 200 bd->queue = q; !! 419 201 bd->sg_io_fn = sg_io_fn; !! 420 bcd = &q->bsg_dev; >> 421 memset(bcd, 0, sizeof(*bcd)); >> 422 >> 423 mutex_lock(&bsg_mutex); 202 424 203 ret = ida_alloc_max(&bsg_minor_ida, BS !! 425 ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); 204 if (ret < 0) { 426 if (ret < 0) { 205 if (ret == -ENOSPC) !! 427 if (ret == -ENOSPC) { 206 dev_err(parent, "bsg: !! 428 printk(KERN_ERR "bsg: too many bsg devices\n"); 207 kfree(bd); !! 429 ret = -EINVAL; 208 return ERR_PTR(ret); !! 430 } 209 } !! 431 goto unlock; 210 bd->device.devt = MKDEV(bsg_major, ret !! 432 } 211 bd->device.class = &bsg_class; << 212 bd->device.parent = parent; << 213 bd->device.release = bsg_device_releas << 214 dev_set_name(&bd->device, "%s", name); << 215 device_initialize(&bd->device); << 216 << 217 cdev_init(&bd->cdev, &bsg_fops); << 218 bd->cdev.owner = THIS_MODULE; << 219 ret = cdev_device_add(&bd->cdev, &bd-> << 220 if (ret) << 221 goto out_put_device; << 222 433 223 if (q->disk && q->disk->queue_kobj.sd) !! 434 bcd->minor = ret; 224 ret = sysfs_create_link(&q->di !! 435 bcd->queue = q; 225 "bsg") !! 436 bcd->ops = ops; >> 437 dev = MKDEV(bsg_major, bcd->minor); >> 438 class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name); >> 439 if (IS_ERR(class_dev)) { >> 440 ret = PTR_ERR(class_dev); >> 441 goto idr_remove; >> 442 } >> 443 bcd->class_dev = class_dev; >> 444 >> 445 if (q->kobj.sd) { >> 446 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); 226 if (ret) 447 if (ret) 227 goto out_device_del; !! 448 goto unregister_class_dev; 228 } 449 } 229 450 230 return bd; !! 451 mutex_unlock(&bsg_mutex); >> 452 return 0; 231 453 232 out_device_del: !! 454 unregister_class_dev: 233 cdev_device_del(&bd->cdev, &bd->device !! 455 device_unregister(class_dev); 234 out_put_device: !! 456 idr_remove: 235 put_device(&bd->device); !! 457 idr_remove(&bsg_minor_idr, bcd->minor); 236 return ERR_PTR(ret); !! 458 unlock: >> 459 mutex_unlock(&bsg_mutex); >> 460 return ret; 237 } 461 } 238 EXPORT_SYMBOL_GPL(bsg_register_queue); << 239 462 240 static char *bsg_devnode(const struct device * !! 463 int bsg_scsi_register_queue(struct request_queue *q, struct device *parent) 241 { 464 { 242 return kasprintf(GFP_KERNEL, "bsg/%s", !! 465 if (!blk_queue_scsi_passthrough(q)) { >> 466 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); >> 467 return -EINVAL; >> 468 } >> 469 >> 470 return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops); 243 } 471 } >> 472 EXPORT_SYMBOL_GPL(bsg_scsi_register_queue); 244 473 245 static const struct class bsg_class = { !! 474 static struct cdev bsg_cdev; 246 .name = "bsg", !! 475 247 .devnode = bsg_devnode, !! 476 static char *bsg_devnode(struct device *dev, umode_t *mode) 248 }; !! 477 { >> 478 return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); >> 479 } 249 480 250 static int __init bsg_init(void) 481 static int __init bsg_init(void) 251 { 482 { >> 483 int ret, i; 252 dev_t devid; 484 dev_t devid; 253 int ret; << 254 485 255 ret = class_register(&bsg_class); !! 486 for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) 256 if (ret) !! 487 INIT_HLIST_HEAD(&bsg_device_list[i]); 257 return ret; !! 488 >> 489 bsg_class = class_create(THIS_MODULE, "bsg"); >> 490 if (IS_ERR(bsg_class)) >> 491 return PTR_ERR(bsg_class); >> 492 bsg_class->devnode = bsg_devnode; 258 493 259 ret = alloc_chrdev_region(&devid, 0, B 494 ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); 260 if (ret) 495 if (ret) 261 goto destroy_bsg_class; 496 goto destroy_bsg_class; >> 497 262 bsg_major = MAJOR(devid); 498 bsg_major = MAJOR(devid); 263 499 >> 500 cdev_init(&bsg_cdev, &bsg_fops); >> 501 ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); >> 502 if (ret) >> 503 goto unregister_chrdev; >> 504 264 printk(KERN_INFO BSG_DESCRIPTION " ver 505 printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION 265 " loaded (major %d)\n", bsg_maj 506 " loaded (major %d)\n", bsg_major); 266 return 0; 507 return 0; 267 !! 508 unregister_chrdev: >> 509 unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); 268 destroy_bsg_class: 510 destroy_bsg_class: 269 class_unregister(&bsg_class); !! 511 class_destroy(bsg_class); 270 return ret; 512 return ret; 271 } 513 } 272 514 273 MODULE_AUTHOR("Jens Axboe"); 515 MODULE_AUTHOR("Jens Axboe"); 274 MODULE_DESCRIPTION(BSG_DESCRIPTION); 516 MODULE_DESCRIPTION(BSG_DESCRIPTION); 275 MODULE_LICENSE("GPL"); 517 MODULE_LICENSE("GPL"); 276 518 277 device_initcall(bsg_init); 519 device_initcall(bsg_init); 278 520
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.