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