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