Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1 | /* |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 2 | * bsg.c - block layer implementation of the sg v4 interface |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs |
| 5 | * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com> |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License version 2. See the file "COPYING" in the main directory of this |
| 9 | * archive for more details. |
| 10 | * |
| 11 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/poll.h> |
| 17 | #include <linux/cdev.h> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 19 | #include <linux/percpu.h> |
| 20 | #include <linux/uio.h> |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 21 | #include <linux/idr.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 22 | #include <linux/bsg.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 24 | |
| 25 | #include <scsi/scsi.h> |
| 26 | #include <scsi/scsi_ioctl.h> |
| 27 | #include <scsi/scsi_cmnd.h> |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 28 | #include <scsi/scsi_device.h> |
| 29 | #include <scsi/scsi_driver.h> |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 30 | #include <scsi/sg.h> |
| 31 | |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 32 | #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver" |
| 33 | #define BSG_VERSION "0.4" |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 34 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 35 | #define bsg_dbg(bd, fmt, ...) \ |
| 36 | pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__) |
| 37 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 38 | struct bsg_device { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 39 | struct request_queue *queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 40 | spinlock_t lock; |
| 41 | struct list_head busy_list; |
| 42 | struct list_head done_list; |
| 43 | struct hlist_node dev_list; |
| 44 | atomic_t ref_count; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 45 | int queued_cmds; |
| 46 | int done_cmds; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 47 | wait_queue_head_t wq_done; |
| 48 | wait_queue_head_t wq_free; |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 49 | char name[20]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 50 | int max_queue; |
| 51 | unsigned long flags; |
| 52 | }; |
| 53 | |
| 54 | enum { |
| 55 | BSG_F_BLOCK = 1, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 58 | #define BSG_DEFAULT_CMDS 64 |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 59 | #define BSG_MAX_DEVS 32768 |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 60 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 61 | static DEFINE_MUTEX(bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 62 | static DEFINE_IDR(bsg_minor_idr); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 63 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 64 | #define BSG_LIST_ARRAY_SIZE 8 |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 65 | static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 66 | |
| 67 | static struct class *bsg_class; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 68 | static int bsg_major; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 69 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 70 | static struct kmem_cache *bsg_cmd_cachep; |
| 71 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 72 | /* |
| 73 | * our internal command type |
| 74 | */ |
| 75 | struct bsg_command { |
| 76 | struct bsg_device *bd; |
| 77 | struct list_head list; |
| 78 | struct request *rq; |
| 79 | struct bio *bio; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 80 | struct bio *bidi_bio; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 81 | int err; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 82 | struct sg_io_v4 hdr; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static void bsg_free_command(struct bsg_command *bc) |
| 86 | { |
| 87 | struct bsg_device *bd = bc->bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 88 | unsigned long flags; |
| 89 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 90 | kmem_cache_free(bsg_cmd_cachep, bc); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 91 | |
| 92 | spin_lock_irqsave(&bd->lock, flags); |
| 93 | bd->queued_cmds--; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 94 | spin_unlock_irqrestore(&bd->lock, flags); |
| 95 | |
| 96 | wake_up(&bd->wq_free); |
| 97 | } |
| 98 | |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 99 | static struct bsg_command *bsg_alloc_command(struct bsg_device *bd) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 100 | { |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 101 | struct bsg_command *bc = ERR_PTR(-EINVAL); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 102 | |
| 103 | spin_lock_irq(&bd->lock); |
| 104 | |
| 105 | if (bd->queued_cmds >= bd->max_queue) |
| 106 | goto out; |
| 107 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 108 | bd->queued_cmds++; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 109 | spin_unlock_irq(&bd->lock); |
| 110 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 111 | bc = kmem_cache_zalloc(bsg_cmd_cachep, GFP_KERNEL); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 112 | if (unlikely(!bc)) { |
| 113 | spin_lock_irq(&bd->lock); |
FUJITA Tomonori | 7e75d73 | 2007-01-24 09:05:54 +0100 | [diff] [blame] | 114 | bd->queued_cmds--; |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 115 | bc = ERR_PTR(-ENOMEM); |
FUJITA Tomonori | 7e75d73 | 2007-01-24 09:05:54 +0100 | [diff] [blame] | 116 | goto out; |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 119 | bc->bd = bd; |
| 120 | INIT_LIST_HEAD(&bc->list); |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 121 | bsg_dbg(bd, "returning free cmd %p\n", bc); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 122 | return bc; |
| 123 | out: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 124 | spin_unlock_irq(&bd->lock); |
| 125 | return bc; |
| 126 | } |
| 127 | |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 128 | static inline struct hlist_head *bsg_dev_idx_hash(int index) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 129 | { |
FUJITA Tomonori | 1c1133e | 2007-07-17 12:21:15 +0200 | [diff] [blame] | 130 | return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)]; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 133 | static int blk_fill_sgv4_hdr_rq(struct request_queue *q, struct request *rq, |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 134 | struct sg_io_v4 *hdr, struct bsg_device *bd, |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 135 | fmode_t mode) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 136 | { |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 137 | struct scsi_request *req = scsi_req(rq); |
| 138 | |
FUJITA Tomonori | 9f5de6b | 2008-04-30 13:16:21 +0900 | [diff] [blame] | 139 | if (hdr->request_len > BLK_MAX_CDB) { |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 140 | req->cmd = kzalloc(hdr->request_len, GFP_KERNEL); |
| 141 | if (!req->cmd) |
FUJITA Tomonori | 9f5de6b | 2008-04-30 13:16:21 +0900 | [diff] [blame] | 142 | return -ENOMEM; |
| 143 | } |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 144 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 145 | if (copy_from_user(req->cmd, (void __user *)(unsigned long)hdr->request, |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 146 | hdr->request_len)) |
| 147 | return -EFAULT; |
FUJITA Tomonori | 15d10b6 | 2007-07-16 08:52:16 +0200 | [diff] [blame] | 148 | |
| 149 | if (hdr->subprotocol == BSG_SUB_PROTOCOL_SCSI_CMD) { |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 150 | if (blk_verify_command(req->cmd, mode)) |
FUJITA Tomonori | 15d10b6 | 2007-07-16 08:52:16 +0200 | [diff] [blame] | 151 | return -EPERM; |
| 152 | } else if (!capable(CAP_SYS_RAWIO)) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 153 | return -EPERM; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 154 | |
| 155 | /* |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 156 | * fill in request structure |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 157 | */ |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 158 | req->cmd_len = hdr->request_len; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 159 | |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 160 | rq->timeout = msecs_to_jiffies(hdr->timeout); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 161 | if (!rq->timeout) |
| 162 | rq->timeout = q->sg_timeout; |
| 163 | if (!rq->timeout) |
| 164 | rq->timeout = BLK_DEFAULT_SG_TIMEOUT; |
Linus Torvalds | f2f1fa7 | 2008-12-05 14:49:18 -0800 | [diff] [blame] | 165 | if (rq->timeout < BLK_MIN_SG_TIMEOUT) |
| 166 | rq->timeout = BLK_MIN_SG_TIMEOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 172 | * Check if sg_io_v4 from user is allowed and valid |
| 173 | */ |
| 174 | static int |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 175 | bsg_validate_sgv4_hdr(struct sg_io_v4 *hdr, int *op) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 176 | { |
FUJITA Tomonori | 15d10b6 | 2007-07-16 08:52:16 +0200 | [diff] [blame] | 177 | int ret = 0; |
| 178 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 179 | if (hdr->guard != 'Q') |
| 180 | return -EINVAL; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 181 | |
FUJITA Tomonori | 15d10b6 | 2007-07-16 08:52:16 +0200 | [diff] [blame] | 182 | switch (hdr->protocol) { |
| 183 | case BSG_PROTOCOL_SCSI: |
| 184 | switch (hdr->subprotocol) { |
| 185 | case BSG_SUB_PROTOCOL_SCSI_CMD: |
| 186 | case BSG_SUB_PROTOCOL_SCSI_TRANSPORT: |
| 187 | break; |
| 188 | default: |
| 189 | ret = -EINVAL; |
| 190 | } |
| 191 | break; |
| 192 | default: |
| 193 | ret = -EINVAL; |
| 194 | } |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 195 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 196 | *op = hdr->dout_xfer_len ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN; |
FUJITA Tomonori | 15d10b6 | 2007-07-16 08:52:16 +0200 | [diff] [blame] | 197 | return ret; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
| 201 | * map sg_io_v4 to a request. |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 202 | */ |
| 203 | static struct request * |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 204 | bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr, fmode_t mode) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 205 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 206 | struct request_queue *q = bd->queue; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 207 | struct request *rq, *next_rq = NULL; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 208 | int ret; |
| 209 | unsigned int op, dxfer_len; |
Namhyung Kim | 2b727c6 | 2011-06-20 13:27:45 +0200 | [diff] [blame] | 210 | void __user *dxferp = NULL; |
James Smart | c7a841f | 2010-11-14 11:12:04 -0500 | [diff] [blame] | 211 | struct bsg_class_device *bcd = &q->bsg_dev; |
| 212 | |
| 213 | /* if the LLD has been removed then the bsg_unregister_queue will |
| 214 | * eventually be called and the class_dev was freed, so we can no |
| 215 | * longer use this request_queue. Return no such address. |
| 216 | */ |
| 217 | if (!bcd->class_dev) |
| 218 | return ERR_PTR(-ENXIO); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 219 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 220 | bsg_dbg(bd, "map hdr %llx/%u %llx/%u\n", |
| 221 | (unsigned long long) hdr->dout_xferp, |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 222 | hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp, |
| 223 | hdr->din_xfer_len); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 224 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 225 | ret = bsg_validate_sgv4_hdr(hdr, &op); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 226 | if (ret) |
| 227 | return ERR_PTR(ret); |
| 228 | |
| 229 | /* |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 230 | * map scatter-gather elements separately and string them to request |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 231 | */ |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 232 | rq = blk_get_request(q, op, GFP_KERNEL); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 233 | if (IS_ERR(rq)) |
| 234 | return rq; |
Jens Axboe | f27b087 | 2014-06-06 07:57:37 -0600 | [diff] [blame] | 235 | |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 236 | ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd, mode); |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 237 | if (ret) |
| 238 | goto out; |
| 239 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 240 | if (op == REQ_OP_SCSI_OUT && hdr->din_xfer_len) { |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 241 | if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) { |
| 242 | ret = -EOPNOTSUPP; |
| 243 | goto out; |
| 244 | } |
| 245 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 246 | next_rq = blk_get_request(q, REQ_OP_SCSI_IN, GFP_KERNEL); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 247 | if (IS_ERR(next_rq)) { |
| 248 | ret = PTR_ERR(next_rq); |
Jens Axboe | 55872c5 | 2014-08-28 15:05:40 -0600 | [diff] [blame] | 249 | next_rq = NULL; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 250 | goto out; |
| 251 | } |
| 252 | rq->next_rq = next_rq; |
| 253 | |
Namhyung Kim | 2b727c6 | 2011-06-20 13:27:45 +0200 | [diff] [blame] | 254 | dxferp = (void __user *)(unsigned long)hdr->din_xferp; |
FUJITA Tomonori | 152e283 | 2008-08-28 16:17:06 +0900 | [diff] [blame] | 255 | ret = blk_rq_map_user(q, next_rq, NULL, dxferp, |
| 256 | hdr->din_xfer_len, GFP_KERNEL); |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 257 | if (ret) |
| 258 | goto out; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 259 | } |
| 260 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 261 | if (hdr->dout_xfer_len) { |
| 262 | dxfer_len = hdr->dout_xfer_len; |
Namhyung Kim | 2b727c6 | 2011-06-20 13:27:45 +0200 | [diff] [blame] | 263 | dxferp = (void __user *)(unsigned long)hdr->dout_xferp; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 264 | } else if (hdr->din_xfer_len) { |
| 265 | dxfer_len = hdr->din_xfer_len; |
Namhyung Kim | 2b727c6 | 2011-06-20 13:27:45 +0200 | [diff] [blame] | 266 | dxferp = (void __user *)(unsigned long)hdr->din_xferp; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 267 | } else |
| 268 | dxfer_len = 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 269 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 270 | if (dxfer_len) { |
FUJITA Tomonori | 152e283 | 2008-08-28 16:17:06 +0900 | [diff] [blame] | 271 | ret = blk_rq_map_user(q, rq, NULL, dxferp, dxfer_len, |
| 272 | GFP_KERNEL); |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 273 | if (ret) |
| 274 | goto out; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 275 | } |
Boaz Harrosh | c1c2012 | 2009-02-03 07:47:29 +0100 | [diff] [blame] | 276 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 277 | return rq; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 278 | out: |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 279 | scsi_req_free_cmd(scsi_req(rq)); |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 280 | blk_put_request(rq); |
| 281 | if (next_rq) { |
| 282 | blk_rq_unmap_user(next_rq->bio); |
| 283 | blk_put_request(next_rq); |
| 284 | } |
| 285 | return ERR_PTR(ret); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* |
| 289 | * async completion call-back from the block layer, when scsi/ide/whatever |
| 290 | * calls end_that_request_last() on a request |
| 291 | */ |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 292 | static void bsg_rq_end_io(struct request *rq, blk_status_t status) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 293 | { |
| 294 | struct bsg_command *bc = rq->end_io_data; |
| 295 | struct bsg_device *bd = bc->bd; |
| 296 | unsigned long flags; |
| 297 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 298 | bsg_dbg(bd, "finished rq %p bc %p, bio %p\n", |
| 299 | rq, bc, bc->bio); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 300 | |
| 301 | bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration); |
| 302 | |
| 303 | spin_lock_irqsave(&bd->lock, flags); |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 304 | list_move_tail(&bc->list, &bd->done_list); |
| 305 | bd->done_cmds++; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 306 | spin_unlock_irqrestore(&bd->lock, flags); |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 307 | |
| 308 | wake_up(&bd->wq_done); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
| 312 | * do final setup of a 'bc' and submit the matching 'rq' to the block |
| 313 | * layer for io |
| 314 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 315 | static void bsg_add_command(struct bsg_device *bd, struct request_queue *q, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 316 | struct bsg_command *bc, struct request *rq) |
| 317 | { |
Boaz Harrosh | 0537894 | 2009-03-24 12:23:40 +0100 | [diff] [blame] | 318 | int at_head = (0 == (bc->hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 319 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 320 | /* |
| 321 | * add bc command to busy queue and submit rq for io |
| 322 | */ |
| 323 | bc->rq = rq; |
| 324 | bc->bio = rq->bio; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 325 | if (rq->next_rq) |
| 326 | bc->bidi_bio = rq->next_rq->bio; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 327 | bc->hdr.duration = jiffies; |
| 328 | spin_lock_irq(&bd->lock); |
| 329 | list_add_tail(&bc->list, &bd->busy_list); |
| 330 | spin_unlock_irq(&bd->lock); |
| 331 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 332 | bsg_dbg(bd, "queueing rq %p, bc %p\n", rq, bc); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 333 | |
| 334 | rq->end_io_data = bc; |
Boaz Harrosh | 0537894 | 2009-03-24 12:23:40 +0100 | [diff] [blame] | 335 | blk_execute_rq_nowait(q, NULL, rq, at_head, bsg_rq_end_io); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 338 | static struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 339 | { |
| 340 | struct bsg_command *bc = NULL; |
| 341 | |
| 342 | spin_lock_irq(&bd->lock); |
| 343 | if (bd->done_cmds) { |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 344 | bc = list_first_entry(&bd->done_list, struct bsg_command, list); |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 345 | list_del(&bc->list); |
| 346 | bd->done_cmds--; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 347 | } |
| 348 | spin_unlock_irq(&bd->lock); |
| 349 | |
| 350 | return bc; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Get a finished command from the done list |
| 355 | */ |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 356 | static struct bsg_command *bsg_get_done_cmd(struct bsg_device *bd) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 357 | { |
| 358 | struct bsg_command *bc; |
| 359 | int ret; |
| 360 | |
| 361 | do { |
| 362 | bc = bsg_next_done_cmd(bd); |
| 363 | if (bc) |
| 364 | break; |
| 365 | |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 366 | if (!test_bit(BSG_F_BLOCK, &bd->flags)) { |
| 367 | bc = ERR_PTR(-EAGAIN); |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | ret = wait_event_interruptible(bd->wq_done, bd->done_cmds); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 372 | if (ret) { |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 373 | bc = ERR_PTR(-ERESTARTSYS); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | } while (1); |
| 377 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 378 | bsg_dbg(bd, "returning done %p\n", bc); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 379 | |
| 380 | return bc; |
| 381 | } |
| 382 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 383 | static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr, |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 384 | struct bio *bio, struct bio *bidi_bio) |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 385 | { |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 386 | struct scsi_request *req = scsi_req(rq); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 387 | int ret = 0; |
| 388 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 389 | pr_debug("rq %p bio %p 0x%x\n", rq, bio, req->result); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 390 | /* |
| 391 | * fill in all the output members |
| 392 | */ |
Christoph Hellwig | 17d5363 | 2017-04-20 16:03:01 +0200 | [diff] [blame] | 393 | hdr->device_status = req->result & 0xff; |
| 394 | hdr->transport_status = host_byte(req->result); |
| 395 | hdr->driver_status = driver_byte(req->result); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 396 | hdr->info = 0; |
| 397 | if (hdr->device_status || hdr->transport_status || hdr->driver_status) |
| 398 | hdr->info |= SG_INFO_CHECK; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 399 | hdr->response_len = 0; |
| 400 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 401 | if (req->sense_len && hdr->response) { |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 402 | int len = min_t(unsigned int, hdr->max_response_len, |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 403 | req->sense_len); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 404 | |
Namhyung Kim | 2b727c6 | 2011-06-20 13:27:45 +0200 | [diff] [blame] | 405 | ret = copy_to_user((void __user *)(unsigned long)hdr->response, |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 406 | req->sense, len); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 407 | if (!ret) |
| 408 | hdr->response_len = len; |
| 409 | else |
| 410 | ret = -EFAULT; |
| 411 | } |
| 412 | |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 413 | if (rq->next_rq) { |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 414 | hdr->dout_resid = req->resid_len; |
| 415 | hdr->din_resid = scsi_req(rq->next_rq)->resid_len; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 416 | blk_rq_unmap_user(bidi_bio); |
| 417 | blk_put_request(rq->next_rq); |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 418 | } else if (rq_data_dir(rq) == READ) |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 419 | hdr->din_resid = req->resid_len; |
FUJITA Tomonori | 0c6a89b | 2007-07-29 23:00:46 +0900 | [diff] [blame] | 420 | else |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 421 | hdr->dout_resid = req->resid_len; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 422 | |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 423 | /* |
| 424 | * If the request generated a negative error number, return it |
| 425 | * (providing we aren't already returning an error); if it's |
| 426 | * just a protocol response (i.e. non negative), that gets |
| 427 | * processed above. |
| 428 | */ |
Christoph Hellwig | 17d5363 | 2017-04-20 16:03:01 +0200 | [diff] [blame] | 429 | if (!ret && req->result < 0) |
| 430 | ret = req->result; |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 431 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 432 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 433 | scsi_req_free_cmd(req); |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 434 | blk_put_request(rq); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 439 | static bool bsg_complete(struct bsg_device *bd) |
| 440 | { |
| 441 | bool ret = false; |
| 442 | bool spin; |
| 443 | |
| 444 | do { |
| 445 | spin_lock_irq(&bd->lock); |
| 446 | |
| 447 | BUG_ON(bd->done_cmds > bd->queued_cmds); |
| 448 | |
| 449 | /* |
| 450 | * All commands consumed. |
| 451 | */ |
| 452 | if (bd->done_cmds == bd->queued_cmds) |
| 453 | ret = true; |
| 454 | |
| 455 | spin = !test_bit(BSG_F_BLOCK, &bd->flags); |
| 456 | |
| 457 | spin_unlock_irq(&bd->lock); |
| 458 | } while (!ret && spin); |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 463 | static int bsg_complete_all_commands(struct bsg_device *bd) |
| 464 | { |
| 465 | struct bsg_command *bc; |
| 466 | int ret, tret; |
| 467 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 468 | bsg_dbg(bd, "entered\n"); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 469 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 470 | /* |
| 471 | * wait for all commands to complete |
| 472 | */ |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 473 | io_wait_event(bd->wq_done, bsg_complete(bd)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * discard done commands |
| 477 | */ |
| 478 | ret = 0; |
| 479 | do { |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 480 | spin_lock_irq(&bd->lock); |
| 481 | if (!bd->queued_cmds) { |
| 482 | spin_unlock_irq(&bd->lock); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 483 | break; |
| 484 | } |
FUJITA Tomonori | efba1a3 | 2007-06-07 13:24:06 +0200 | [diff] [blame] | 485 | spin_unlock_irq(&bd->lock); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 486 | |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 487 | bc = bsg_get_done_cmd(bd); |
| 488 | if (IS_ERR(bc)) |
| 489 | break; |
| 490 | |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 491 | tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio, |
| 492 | bc->bidi_bio); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 493 | if (!ret) |
| 494 | ret = tret; |
| 495 | |
| 496 | bsg_free_command(bc); |
| 497 | } while (1); |
| 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 502 | static int |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 503 | __bsg_read(char __user *buf, size_t count, struct bsg_device *bd, |
| 504 | const struct iovec *iov, ssize_t *bytes_read) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 505 | { |
| 506 | struct bsg_command *bc; |
| 507 | int nr_commands, ret; |
| 508 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 509 | if (count % sizeof(struct sg_io_v4)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 510 | return -EINVAL; |
| 511 | |
| 512 | ret = 0; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 513 | nr_commands = count / sizeof(struct sg_io_v4); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 514 | while (nr_commands) { |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 515 | bc = bsg_get_done_cmd(bd); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 516 | if (IS_ERR(bc)) { |
| 517 | ret = PTR_ERR(bc); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * this is the only case where we need to copy data back |
| 523 | * after completing the request. so do that here, |
| 524 | * bsg_complete_work() cannot do that for us |
| 525 | */ |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 526 | ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio, |
| 527 | bc->bidi_bio); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 528 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 529 | if (copy_to_user(buf, &bc->hdr, sizeof(bc->hdr))) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 530 | ret = -EFAULT; |
| 531 | |
| 532 | bsg_free_command(bc); |
| 533 | |
| 534 | if (ret) |
| 535 | break; |
| 536 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 537 | buf += sizeof(struct sg_io_v4); |
| 538 | *bytes_read += sizeof(struct sg_io_v4); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 539 | nr_commands--; |
| 540 | } |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static inline void bsg_set_block(struct bsg_device *bd, struct file *file) |
| 546 | { |
| 547 | if (file->f_flags & O_NONBLOCK) |
| 548 | clear_bit(BSG_F_BLOCK, &bd->flags); |
| 549 | else |
| 550 | set_bit(BSG_F_BLOCK, &bd->flags); |
| 551 | } |
| 552 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 553 | /* |
| 554 | * Check if the error is a "real" error that we should return. |
| 555 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 556 | static inline int err_block_err(int ret) |
| 557 | { |
| 558 | if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN) |
| 559 | return 1; |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static ssize_t |
| 565 | bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 566 | { |
| 567 | struct bsg_device *bd = file->private_data; |
| 568 | int ret; |
| 569 | ssize_t bytes_read; |
| 570 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 571 | bsg_dbg(bd, "read %zd bytes\n", count); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 572 | |
| 573 | bsg_set_block(bd, file); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 574 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 575 | bytes_read = 0; |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 576 | ret = __bsg_read(buf, count, bd, NULL, &bytes_read); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 577 | *ppos = bytes_read; |
| 578 | |
Namhyung Kim | 44194e3 | 2011-06-20 13:27:44 +0200 | [diff] [blame] | 579 | if (!bytes_read || err_block_err(ret)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 580 | bytes_read = ret; |
| 581 | |
| 582 | return bytes_read; |
| 583 | } |
| 584 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 585 | static int __bsg_write(struct bsg_device *bd, const char __user *buf, |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 586 | size_t count, ssize_t *bytes_written, fmode_t mode) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 587 | { |
| 588 | struct bsg_command *bc; |
| 589 | struct request *rq; |
| 590 | int ret, nr_commands; |
| 591 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 592 | if (count % sizeof(struct sg_io_v4)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 593 | return -EINVAL; |
| 594 | |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 595 | nr_commands = count / sizeof(struct sg_io_v4); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 596 | rq = NULL; |
| 597 | bc = NULL; |
| 598 | ret = 0; |
| 599 | while (nr_commands) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 600 | struct request_queue *q = bd->queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 601 | |
FUJITA Tomonori | e7d7217 | 2007-05-08 15:32:03 +0200 | [diff] [blame] | 602 | bc = bsg_alloc_command(bd); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 603 | if (IS_ERR(bc)) { |
| 604 | ret = PTR_ERR(bc); |
| 605 | bc = NULL; |
| 606 | break; |
| 607 | } |
| 608 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 609 | if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) { |
| 610 | ret = -EFAULT; |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * get a request, fill in the blanks, and add to request queue |
| 616 | */ |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 617 | rq = bsg_map_hdr(bd, &bc->hdr, mode); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 618 | if (IS_ERR(rq)) { |
| 619 | ret = PTR_ERR(rq); |
| 620 | rq = NULL; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | bsg_add_command(bd, q, bc, rq); |
| 625 | bc = NULL; |
| 626 | rq = NULL; |
| 627 | nr_commands--; |
FUJITA Tomonori | 70e36ec | 2006-12-20 11:20:15 +0100 | [diff] [blame] | 628 | buf += sizeof(struct sg_io_v4); |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 629 | *bytes_written += sizeof(struct sg_io_v4); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 632 | if (bc) |
| 633 | bsg_free_command(bc); |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static ssize_t |
| 639 | bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 640 | { |
| 641 | struct bsg_device *bd = file->private_data; |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 642 | ssize_t bytes_written; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 643 | int ret; |
| 644 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 645 | bsg_dbg(bd, "write %zd bytes\n", count); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 646 | |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 647 | if (unlikely(uaccess_kernel())) |
Al Viro | 128394e | 2016-12-16 13:42:06 -0500 | [diff] [blame] | 648 | return -EINVAL; |
| 649 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 650 | bsg_set_block(bd, file); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 651 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 652 | bytes_written = 0; |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 653 | ret = __bsg_write(bd, buf, count, &bytes_written, file->f_mode); |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 654 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 655 | *ppos = bytes_written; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 656 | |
| 657 | /* |
| 658 | * return bytes written on non-fatal errors |
| 659 | */ |
Namhyung Kim | 44194e3 | 2011-06-20 13:27:44 +0200 | [diff] [blame] | 660 | if (!bytes_written || err_block_err(ret)) |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 661 | bytes_written = ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 662 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 663 | bsg_dbg(bd, "returning %zd\n", bytes_written); |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 664 | return bytes_written; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 667 | static struct bsg_device *bsg_alloc_device(void) |
| 668 | { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 669 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 670 | |
| 671 | bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL); |
| 672 | if (unlikely(!bd)) |
| 673 | return NULL; |
| 674 | |
| 675 | spin_lock_init(&bd->lock); |
| 676 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 677 | bd->max_queue = BSG_DEFAULT_CMDS; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 678 | |
| 679 | INIT_LIST_HEAD(&bd->busy_list); |
| 680 | INIT_LIST_HEAD(&bd->done_list); |
| 681 | INIT_HLIST_NODE(&bd->dev_list); |
| 682 | |
| 683 | init_waitqueue_head(&bd->wq_free); |
| 684 | init_waitqueue_head(&bd->wq_done); |
| 685 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 686 | } |
| 687 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 688 | static void bsg_kref_release_function(struct kref *kref) |
| 689 | { |
| 690 | struct bsg_class_device *bcd = |
| 691 | container_of(kref, struct bsg_class_device, ref); |
James Bottomley | 8df5fc0 | 2008-07-07 15:50:01 -0500 | [diff] [blame] | 692 | struct device *parent = bcd->parent; |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 693 | |
| 694 | if (bcd->release) |
| 695 | bcd->release(bcd->parent); |
| 696 | |
James Bottomley | 8df5fc0 | 2008-07-07 15:50:01 -0500 | [diff] [blame] | 697 | put_device(parent); |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 698 | } |
| 699 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 700 | static int bsg_put_device(struct bsg_device *bd) |
| 701 | { |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 702 | int ret = 0, do_free; |
| 703 | struct request_queue *q = bd->queue; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 704 | |
| 705 | mutex_lock(&bsg_mutex); |
| 706 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 707 | do_free = atomic_dec_and_test(&bd->ref_count); |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 708 | if (!do_free) { |
| 709 | mutex_unlock(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 710 | goto out; |
FUJITA Tomonori | 3f27e3e | 2008-05-29 07:56:55 +0900 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | hlist_del(&bd->dev_list); |
| 714 | mutex_unlock(&bsg_mutex); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 715 | |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 716 | bsg_dbg(bd, "tearing down\n"); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 717 | |
| 718 | /* |
| 719 | * close can always block |
| 720 | */ |
| 721 | set_bit(BSG_F_BLOCK, &bd->flags); |
| 722 | |
| 723 | /* |
| 724 | * correct error detection baddies here again. it's the responsibility |
| 725 | * of the app to properly reap commands before close() if it wants |
| 726 | * fool-proof error detection |
| 727 | */ |
| 728 | ret = bsg_complete_all_commands(bd); |
| 729 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 730 | kfree(bd); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 731 | out: |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 732 | kref_put(&q->bsg_dev.ref, bsg_kref_release_function); |
| 733 | if (do_free) |
| 734 | blk_put_queue(q); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | static struct bsg_device *bsg_add_device(struct inode *inode, |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 739 | struct request_queue *rq, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 740 | struct file *file) |
| 741 | { |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 742 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 743 | unsigned char buf[32]; |
Bart Van Assche | d9f9726 | 2017-05-31 14:43:47 -0700 | [diff] [blame] | 744 | |
| 745 | if (!blk_queue_scsi_passthrough(rq)) { |
| 746 | WARN_ONCE(true, "Attempt to register a non-SCSI queue\n"); |
| 747 | return ERR_PTR(-EINVAL); |
| 748 | } |
| 749 | |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 750 | if (!blk_get_queue(rq)) |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 751 | return ERR_PTR(-ENXIO); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 752 | |
| 753 | bd = bsg_alloc_device(); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 754 | if (!bd) { |
| 755 | blk_put_queue(rq); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 756 | return ERR_PTR(-ENOMEM); |
FUJITA Tomonori | c3ff1b9 | 2008-03-31 10:03:39 +0900 | [diff] [blame] | 757 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 758 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 759 | bd->queue = rq; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 760 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 761 | bsg_set_block(bd, file); |
| 762 | |
| 763 | atomic_set(&bd->ref_count, 1); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 764 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 765 | hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode))); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 766 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 767 | strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1); |
Johannes Thumshirn | 3124b65 | 2018-01-24 09:50:06 -0700 | [diff] [blame] | 768 | bsg_dbg(bd, "bound to <%s>, max queue %d\n", |
FUJITA Tomonori | 9e69fbb | 2006-12-20 11:18:22 +0100 | [diff] [blame] | 769 | format_dev_t(buf, inode->i_rdev), bd->max_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 770 | |
| 771 | mutex_unlock(&bsg_mutex); |
| 772 | return bd; |
| 773 | } |
| 774 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 775 | static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 776 | { |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 777 | struct bsg_device *bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 778 | |
| 779 | mutex_lock(&bsg_mutex); |
| 780 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 781 | hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) { |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 782 | if (bd->queue == q) { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 783 | atomic_inc(&bd->ref_count); |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 784 | goto found; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 785 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 786 | } |
FUJITA Tomonori | 43ac9e6 | 2008-03-31 10:03:40 +0900 | [diff] [blame] | 787 | bd = NULL; |
| 788 | found: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 789 | mutex_unlock(&bsg_mutex); |
| 790 | return bd; |
| 791 | } |
| 792 | |
| 793 | static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) |
| 794 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 795 | struct bsg_device *bd; |
| 796 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 797 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 798 | /* |
| 799 | * find the class device |
| 800 | */ |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 801 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 802 | bcd = idr_find(&bsg_minor_idr, iminor(inode)); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 803 | if (bcd) |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 804 | kref_get(&bcd->ref); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 805 | mutex_unlock(&bsg_mutex); |
| 806 | |
| 807 | if (!bcd) |
| 808 | return ERR_PTR(-ENODEV); |
| 809 | |
FUJITA Tomonori | 842ea77 | 2008-03-31 10:03:41 +0900 | [diff] [blame] | 810 | bd = __bsg_get_device(iminor(inode), bcd->queue); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 811 | if (bd) |
| 812 | return bd; |
| 813 | |
| 814 | bd = bsg_add_device(inode, bcd->queue, file); |
| 815 | if (IS_ERR(bd)) |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 816 | kref_put(&bcd->ref, bsg_kref_release_function); |
FUJITA Tomonori | d45ac4f | 2008-03-31 10:03:38 +0900 | [diff] [blame] | 817 | |
| 818 | return bd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static int bsg_open(struct inode *inode, struct file *file) |
| 822 | { |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 823 | struct bsg_device *bd; |
| 824 | |
Jonathan Corbet | 75bd2ef | 2008-05-15 09:09:23 -0600 | [diff] [blame] | 825 | bd = bsg_get_device(inode, file); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 826 | |
| 827 | if (IS_ERR(bd)) |
| 828 | return PTR_ERR(bd); |
| 829 | |
| 830 | file->private_data = bd; |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int bsg_release(struct inode *inode, struct file *file) |
| 835 | { |
| 836 | struct bsg_device *bd = file->private_data; |
| 837 | |
| 838 | file->private_data = NULL; |
| 839 | return bsg_put_device(bd); |
| 840 | } |
| 841 | |
Al Viro | 1771e70 | 2017-07-02 23:03:13 -0400 | [diff] [blame] | 842 | static __poll_t bsg_poll(struct file *file, poll_table *wait) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 843 | { |
| 844 | struct bsg_device *bd = file->private_data; |
Al Viro | 1771e70 | 2017-07-02 23:03:13 -0400 | [diff] [blame] | 845 | __poll_t mask = 0; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 846 | |
| 847 | poll_wait(file, &bd->wq_done, wait); |
| 848 | poll_wait(file, &bd->wq_free, wait); |
| 849 | |
| 850 | spin_lock_irq(&bd->lock); |
| 851 | if (!list_empty(&bd->done_list)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 852 | mask |= EPOLLIN | EPOLLRDNORM; |
Namhyung Kim | 80ceb05 | 2011-06-20 13:27:44 +0200 | [diff] [blame] | 853 | if (bd->queued_cmds < bd->max_queue) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 854 | mask |= EPOLLOUT; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 855 | spin_unlock_irq(&bd->lock); |
| 856 | |
| 857 | return mask; |
| 858 | } |
| 859 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 860 | 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] | 861 | { |
| 862 | struct bsg_device *bd = file->private_data; |
| 863 | int __user *uarg = (int __user *) arg; |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 864 | int ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 865 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 866 | switch (cmd) { |
| 867 | /* |
| 868 | * our own ioctls |
| 869 | */ |
| 870 | case SG_GET_COMMAND_Q: |
| 871 | return put_user(bd->max_queue, uarg); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 872 | case SG_SET_COMMAND_Q: { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 873 | int queue; |
| 874 | |
| 875 | if (get_user(queue, uarg)) |
| 876 | return -EFAULT; |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 877 | if (queue < 1) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 878 | return -EINVAL; |
| 879 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 880 | spin_lock_irq(&bd->lock); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 881 | bd->max_queue = queue; |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 882 | spin_unlock_irq(&bd->lock); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * SCSI/sg ioctls |
| 888 | */ |
| 889 | case SG_GET_VERSION_NUM: |
| 890 | case SCSI_IOCTL_GET_IDLUN: |
| 891 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 892 | case SG_SET_TIMEOUT: |
| 893 | case SG_GET_TIMEOUT: |
| 894 | case SG_GET_RESERVED_SIZE: |
| 895 | case SG_SET_RESERVED_SIZE: |
| 896 | case SG_EMULATED_HOST: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 897 | case SCSI_IOCTL_SEND_COMMAND: { |
| 898 | void __user *uarg = (void __user *) arg; |
Al Viro | 74f3c8a | 2007-08-27 15:38:10 -0400 | [diff] [blame] | 899 | return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 900 | } |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 901 | case SG_IO: { |
| 902 | struct request *rq; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 903 | struct bio *bio, *bidi_bio = NULL; |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 904 | struct sg_io_v4 hdr; |
Boaz Harrosh | 0537894 | 2009-03-24 12:23:40 +0100 | [diff] [blame] | 905 | int at_head; |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 906 | |
| 907 | if (copy_from_user(&hdr, uarg, sizeof(hdr))) |
| 908 | return -EFAULT; |
| 909 | |
Christoph Hellwig | f00c4d8 | 2017-11-05 10:36:31 +0300 | [diff] [blame] | 910 | rq = bsg_map_hdr(bd, &hdr, file->f_mode); |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 911 | if (IS_ERR(rq)) |
| 912 | return PTR_ERR(rq); |
| 913 | |
| 914 | bio = rq->bio; |
FUJITA Tomonori | 2c9ecdf | 2007-07-16 08:52:15 +0200 | [diff] [blame] | 915 | if (rq->next_rq) |
| 916 | bidi_bio = rq->next_rq->bio; |
Boaz Harrosh | 0537894 | 2009-03-24 12:23:40 +0100 | [diff] [blame] | 917 | |
| 918 | at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL)); |
| 919 | blk_execute_rq(bd->queue, NULL, rq, at_head); |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 920 | ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 921 | |
| 922 | if (copy_to_user(uarg, &hdr, sizeof(hdr))) |
| 923 | return -EFAULT; |
Jens Axboe | b711afa | 2006-12-20 11:25:23 +0100 | [diff] [blame] | 924 | |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 925 | return ret; |
FUJITA Tomonori | 10e8855 | 2006-12-20 11:20:57 +0100 | [diff] [blame] | 926 | } |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 927 | default: |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 928 | return -ENOTTY; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Arjan van de Ven | 7344be0 | 2007-10-15 11:01:53 +0200 | [diff] [blame] | 932 | static const struct file_operations bsg_fops = { |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 933 | .read = bsg_read, |
| 934 | .write = bsg_write, |
| 935 | .poll = bsg_poll, |
| 936 | .open = bsg_open, |
| 937 | .release = bsg_release, |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 938 | .unlocked_ioctl = bsg_ioctl, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 939 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 940 | .llseek = default_llseek, |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 941 | }; |
| 942 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 943 | void bsg_unregister_queue(struct request_queue *q) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 944 | { |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 945 | struct bsg_class_device *bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 946 | |
FUJITA Tomonori | df46882 | 2007-07-21 13:23:25 +0900 | [diff] [blame] | 947 | if (!bcd->class_dev) |
| 948 | return; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 949 | |
| 950 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 951 | idr_remove(&bsg_minor_idr, bcd->minor); |
Stanislaw Gruszka | 37b40ad | 2012-02-08 20:02:03 +0100 | [diff] [blame] | 952 | if (q->kobj.sd) |
| 953 | sysfs_remove_link(&q->kobj, "bsg"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 954 | device_unregister(bcd->class_dev); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 955 | bcd->class_dev = NULL; |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 956 | kref_put(&bcd->ref, bsg_kref_release_function); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 957 | mutex_unlock(&bsg_mutex); |
| 958 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 959 | EXPORT_SYMBOL_GPL(bsg_unregister_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 960 | |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 961 | int bsg_register_queue(struct request_queue *q, struct device *parent, |
| 962 | const char *name, void (*release)(struct device *)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 963 | { |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 964 | struct bsg_class_device *bcd; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 965 | dev_t dev; |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 966 | int ret; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 967 | struct device *class_dev = NULL; |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 968 | const char *devname; |
| 969 | |
| 970 | if (name) |
| 971 | devname = name; |
| 972 | else |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 973 | devname = dev_name(parent); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * we need a proper transport to send commands, not a stacked device |
| 977 | */ |
Jens Axboe | 49fd524 | 2014-04-16 10:57:18 -0600 | [diff] [blame] | 978 | if (!queue_is_rq_based(q)) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 979 | return 0; |
| 980 | |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 981 | bcd = &q->bsg_dev; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 982 | memset(bcd, 0, sizeof(*bcd)); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 983 | |
| 984 | mutex_lock(&bsg_mutex); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 985 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 986 | ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL); |
| 987 | if (ret < 0) { |
| 988 | if (ret == -ENOSPC) { |
| 989 | printk(KERN_ERR "bsg: too many bsg devices\n"); |
| 990 | ret = -EINVAL; |
| 991 | } |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 992 | goto unlock; |
| 993 | } |
| 994 | |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 995 | bcd->minor = ret; |
FUJITA Tomonori | d351af0 | 2007-07-09 12:40:35 +0200 | [diff] [blame] | 996 | bcd->queue = q; |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 997 | bcd->parent = get_device(parent); |
| 998 | bcd->release = release; |
| 999 | kref_init(&bcd->ref); |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 1000 | dev = MKDEV(bsg_major, bcd->minor); |
Greg Kroah-Hartman | 1ff9f54 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1001 | class_dev = device_create(bsg_class, parent, dev, NULL, "%s", devname); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1002 | if (IS_ERR(class_dev)) { |
| 1003 | ret = PTR_ERR(class_dev); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 1004 | goto put_dev; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1005 | } |
| 1006 | bcd->class_dev = class_dev; |
| 1007 | |
Linus Torvalds | abce891 | 2007-07-16 11:18:23 -0700 | [diff] [blame] | 1008 | if (q->kobj.sd) { |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1009 | ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); |
| 1010 | if (ret) |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 1011 | goto unregister_class_dev; |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1014 | mutex_unlock(&bsg_mutex); |
| 1015 | return 0; |
James Bottomley | 6826ee4 | 2007-07-20 16:50:10 -0500 | [diff] [blame] | 1016 | |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 1017 | unregister_class_dev: |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1018 | device_unregister(class_dev); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 1019 | put_dev: |
FUJITA Tomonori | 97f46ae | 2008-04-19 00:43:14 +0900 | [diff] [blame] | 1020 | put_device(parent); |
Tejun Heo | bab998d | 2013-02-27 17:03:57 -0800 | [diff] [blame] | 1021 | idr_remove(&bsg_minor_idr, bcd->minor); |
FUJITA Tomonori | 598443a | 2007-07-23 09:33:26 +0900 | [diff] [blame] | 1022 | unlock: |
Jens Axboe | 264a047 | 2007-01-23 16:30:17 +0100 | [diff] [blame] | 1023 | mutex_unlock(&bsg_mutex); |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1024 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1025 | } |
FUJITA Tomonori | 4cf0723 | 2007-03-30 11:19:39 +0200 | [diff] [blame] | 1026 | EXPORT_SYMBOL_GPL(bsg_register_queue); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1027 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 1028 | static struct cdev bsg_cdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 1029 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 1030 | static char *bsg_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 2bdf914 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1031 | { |
| 1032 | return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev)); |
| 1033 | } |
| 1034 | |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1035 | static int __init bsg_init(void) |
| 1036 | { |
| 1037 | int ret, i; |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 1038 | dev_t devid; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1039 | |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 1040 | bsg_cmd_cachep = kmem_cache_create("bsg_cmd", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1041 | sizeof(struct bsg_command), 0, 0, NULL); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 1042 | if (!bsg_cmd_cachep) { |
| 1043 | printk(KERN_ERR "bsg: failed creating slab cache\n"); |
| 1044 | return -ENOMEM; |
| 1045 | } |
| 1046 | |
Jens Axboe | 25fd164 | 2007-07-17 08:52:29 +0200 | [diff] [blame] | 1047 | for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++) |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1048 | INIT_HLIST_HEAD(&bsg_device_list[i]); |
| 1049 | |
| 1050 | bsg_class = class_create(THIS_MODULE, "bsg"); |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 1051 | if (IS_ERR(bsg_class)) { |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 1052 | ret = PTR_ERR(bsg_class); |
| 1053 | goto destroy_kmemcache; |
Jens Axboe | 5309cb3 | 2007-01-23 16:24:41 +0100 | [diff] [blame] | 1054 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1055 | bsg_class->devnode = bsg_devnode; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1056 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 1057 | ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg"); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 1058 | if (ret) |
| 1059 | goto destroy_bsg_class; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1060 | |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 1061 | bsg_major = MAJOR(devid); |
| 1062 | |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 1063 | cdev_init(&bsg_cdev, &bsg_fops); |
Jens Axboe | 46f6ef4 | 2007-07-17 08:56:10 +0200 | [diff] [blame] | 1064 | ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 1065 | if (ret) |
| 1066 | goto unregister_chrdev; |
FUJITA Tomonori | 292b7f2 | 2007-03-28 13:29:58 +0200 | [diff] [blame] | 1067 | |
Jens Axboe | 5d3a8cd | 2007-07-17 15:10:09 +0200 | [diff] [blame] | 1068 | printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 1069 | " loaded (major %d)\n", bsg_major); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1070 | return 0; |
FUJITA Tomonori | 9b9f770 | 2007-07-17 12:20:46 +0200 | [diff] [blame] | 1071 | unregister_chrdev: |
| 1072 | unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS); |
| 1073 | destroy_bsg_class: |
| 1074 | class_destroy(bsg_class); |
| 1075 | destroy_kmemcache: |
| 1076 | kmem_cache_destroy(bsg_cmd_cachep); |
| 1077 | return ret; |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | MODULE_AUTHOR("Jens Axboe"); |
FUJITA Tomonori | 0ed081c | 2007-07-17 12:21:35 +0200 | [diff] [blame] | 1081 | MODULE_DESCRIPTION(BSG_DESCRIPTION); |
Jens Axboe | 3d6392c | 2007-07-09 12:38:05 +0200 | [diff] [blame] | 1082 | MODULE_LICENSE("GPL"); |
| 1083 | |
FUJITA Tomonori | 4e2872d | 2007-03-28 13:29:24 +0200 | [diff] [blame] | 1084 | device_initcall(bsg_init); |