aboutsummaryrefslogtreecommitdiffstats
path: root/block
diff options
context:
space:
mode:
authorJens Axboe <jens.axboe@oracle.com>2007-07-09 06:38:05 -0400
committerJens Axboe <jens.axboe@oracle.com>2007-07-16 02:52:44 -0400
commit3d6392cfbd7dc11f23058e3493683afab4ac13a3 (patch)
tree70c2b65c479f5feb7a5214a4a4930d489a069b1f /block
parent8f41958bdd577731f7411c9605cfaa9db6766809 (diff)
bsg: support for full generic block layer SG v3
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Diffstat (limited to 'block')
-rw-r--r--block/Kconfig7
-rw-r--r--block/Makefile1
-rw-r--r--block/bsg.c997
-rw-r--r--block/ll_rw_blk.c8
-rw-r--r--block/scsi_ioctl.c163
5 files changed, 1115 insertions, 61 deletions
diff --git a/block/Kconfig b/block/Kconfig
index 285935134bcd..da12f2649cce 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -51,4 +51,11 @@ config LSF
51 51
52endif # BLOCK 52endif # BLOCK
53 53
54config BLK_DEV_BSG
55 bool "Block layer SG support"
56 default y
57 ---help---
58 Saying Y here will enable generic SG (SCSI generic) v3
59 support for any block device.
60
54source block/Kconfig.iosched 61source block/Kconfig.iosched
diff --git a/block/Makefile b/block/Makefile
index 4b84d0d5947b..959feeb253be 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -4,6 +4,7 @@
4 4
5obj-$(CONFIG_BLOCK) := elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o 5obj-$(CONFIG_BLOCK) := elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o
6 6
7obj-$(CONFIG_BLK_DEV_BSG) += bsg.o
7obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o 8obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o
8obj-$(CONFIG_IOSCHED_AS) += as-iosched.o 9obj-$(CONFIG_IOSCHED_AS) += as-iosched.o
9obj-$(CONFIG_IOSCHED_DEADLINE) += deadline-iosched.o 10obj-$(CONFIG_IOSCHED_DEADLINE) += deadline-iosched.o
diff --git a/block/bsg.c b/block/bsg.c
new file mode 100644
index 000000000000..724b69391cdc
--- /dev/null
+++ b/block/bsg.c
@@ -0,0 +1,997 @@
1/*
2 * bsg.c - block layer implementation of the sg v3 interface
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 */
12/*
13 * TODO
14 * - Should this get merged, block/scsi_ioctl.c will be migrated into
15 * this file. To keep maintenance down, it's easier to have them
16 * seperated right now.
17 *
18 */
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/file.h>
23#include <linux/blkdev.h>
24#include <linux/poll.h>
25#include <linux/cdev.h>
26#include <linux/percpu.h>
27#include <linux/uio.h>
28#include <linux/bsg.h>
29
30#include <scsi/scsi.h>
31#include <scsi/scsi_ioctl.h>
32#include <scsi/scsi_cmnd.h>
33#include <scsi/sg.h>
34
35static char bsg_version[] = "block layer sg (bsg) 0.4";
36
37struct bsg_command;
38
39struct bsg_device {
40 struct gendisk *disk;
41 request_queue_t *queue;
42 spinlock_t lock;
43 struct list_head busy_list;
44 struct list_head done_list;
45 struct hlist_node dev_list;
46 atomic_t ref_count;
47 int minor;
48 int queued_cmds;
49 int done_cmds;
50 unsigned long *cmd_bitmap;
51 struct bsg_command *cmd_map;
52 wait_queue_head_t wq_done;
53 wait_queue_head_t wq_free;
54 char name[BDEVNAME_SIZE];
55 int max_queue;
56 unsigned long flags;
57};
58
59enum {
60 BSG_F_BLOCK = 1,
61 BSG_F_WRITE_PERM = 2,
62};
63
64/*
65 * command allocation bitmap defines
66 */
67#define BSG_CMDS_PAGE_ORDER (1)
68#define BSG_CMDS_PER_LONG (sizeof(unsigned long) * 8)
69#define BSG_CMDS_MASK (BSG_CMDS_PER_LONG - 1)
70#define BSG_CMDS_BYTES (PAGE_SIZE * (1 << BSG_CMDS_PAGE_ORDER))
71#define BSG_CMDS (BSG_CMDS_BYTES / sizeof(struct bsg_command))
72
73#undef BSG_DEBUG
74
75#ifdef BSG_DEBUG
76#define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
77#else
78#define dprintk(fmt, args...)
79#endif
80
81#define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
82
83/*
84 * just for testing
85 */
86#define BSG_MAJOR (240)
87
88static DEFINE_MUTEX(bsg_mutex);
89static int bsg_device_nr;
90
91#define BSG_LIST_SIZE (8)
92#define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
93static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
94
95static struct class *bsg_class;
96static LIST_HEAD(bsg_class_list);
97
98/*
99 * our internal command type
100 */
101struct bsg_command {
102 struct bsg_device *bd;
103 struct list_head list;
104 struct request *rq;
105 struct bio *bio;
106 int err;
107 struct sg_io_hdr hdr;
108 struct sg_io_hdr __user *uhdr;
109 char sense[SCSI_SENSE_BUFFERSIZE];
110};
111
112static void bsg_free_command(struct bsg_command *bc)
113{
114 struct bsg_device *bd = bc->bd;
115 unsigned long bitnr = bc - bd->cmd_map;
116 unsigned long flags;
117
118 dprintk("%s: command bit offset %lu\n", bd->name, bitnr);
119
120 spin_lock_irqsave(&bd->lock, flags);
121 bd->queued_cmds--;
122 __clear_bit(bitnr, bd->cmd_bitmap);
123 spin_unlock_irqrestore(&bd->lock, flags);
124
125 wake_up(&bd->wq_free);
126}
127
128static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
129{
130 struct bsg_command *bc = NULL;
131 unsigned long *map;
132 int free_nr;
133
134 spin_lock_irq(&bd->lock);
135
136 if (bd->queued_cmds >= bd->max_queue)
137 goto out;
138
139 for (free_nr = 0, map = bd->cmd_bitmap; *map == ~0UL; map++)
140 free_nr += BSG_CMDS_PER_LONG;
141
142 BUG_ON(*map == ~0UL);
143
144 bd->queued_cmds++;
145 free_nr += ffz(*map);
146 __set_bit(free_nr, bd->cmd_bitmap);
147 spin_unlock_irq(&bd->lock);
148
149 bc = bd->cmd_map + free_nr;
150 memset(bc, 0, sizeof(*bc));
151 bc->bd = bd;
152 INIT_LIST_HEAD(&bc->list);
153 dprintk("%s: returning free cmd %p (bit %d)\n", bd->name, bc, free_nr);
154 return bc;
155out:
156 dprintk("%s: failed (depth %d)\n", bd->name, bd->queued_cmds);
157 spin_unlock_irq(&bd->lock);
158 return bc;
159}
160
161static inline void
162bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
163{
164 bd->done_cmds--;
165 list_del(&bc->list);
166}
167
168static inline void
169bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
170{
171 bd->done_cmds++;
172 list_add_tail(&bc->list, &bd->done_list);
173 wake_up(&bd->wq_done);
174}
175
176static inline int bsg_io_schedule(struct bsg_device *bd, int state)
177{
178 DEFINE_WAIT(wait);
179 int ret = 0;
180
181 spin_lock_irq(&bd->lock);
182
183 BUG_ON(bd->done_cmds > bd->queued_cmds);
184
185 /*
186 * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
187 * work to do", even though we return -ENOSPC after this same test
188 * during bsg_write() -- there, it means our buffer can't have more
189 * bsg_commands added to it, thus has no space left.
190 */
191 if (bd->done_cmds == bd->queued_cmds) {
192 ret = -ENODATA;
193 goto unlock;
194 }
195
196 if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
197 ret = -EAGAIN;
198 goto unlock;
199 }
200
201 prepare_to_wait(&bd->wq_done, &wait, state);
202 spin_unlock_irq(&bd->lock);
203 io_schedule();
204 finish_wait(&bd->wq_done, &wait);
205
206 if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
207 ret = -ERESTARTSYS;
208
209 return ret;
210unlock:
211 spin_unlock_irq(&bd->lock);
212 return ret;
213}
214
215/*
216 * get a new free command, blocking if needed and specified
217 */
218static struct bsg_command *bsg_get_command(struct bsg_device *bd)
219{
220 struct bsg_command *bc;
221 int ret;
222
223 do {
224 bc = __bsg_alloc_command(bd);
225 if (bc)
226 break;
227
228 ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);
229 if (ret) {
230 bc = ERR_PTR(ret);
231 break;
232 }
233
234 } while (1);
235
236 return bc;
237}
238
239/*
240 * Check if sg_io_hdr from user is allowed and valid
241 */
242static int
243bsg_validate_sghdr(request_queue_t *q, struct sg_io_hdr *hdr, int *rw)
244{
245 if (hdr->interface_id != 'S')
246 return -EINVAL;
247 if (hdr->cmd_len > BLK_MAX_CDB)
248 return -EINVAL;
249 if (hdr->dxfer_len > (q->max_sectors << 9))
250 return -EIO;
251
252 /*
253 * looks sane, if no data then it should be fine from our POV
254 */
255 if (!hdr->dxfer_len)
256 return 0;
257
258 switch (hdr->dxfer_direction) {
259 case SG_DXFER_TO_FROM_DEV:
260 case SG_DXFER_FROM_DEV:
261 *rw = READ;
262 break;
263 case SG_DXFER_TO_DEV:
264 *rw = WRITE;
265 break;
266 default:
267 return -EINVAL;
268 }
269
270 return 0;
271}
272
273/*
274 * map sg_io_hdr to a request. for scatter-gather sg_io_hdr, we map
275 * each segment to a bio and string multiple bio's to the request
276 */
277static struct request *
278bsg_map_hdr(struct bsg_device *bd, int rw, struct sg_io_hdr *hdr)
279{
280 request_queue_t *q = bd->queue;
281 struct sg_iovec iov;
282 struct sg_iovec __user *u_iov;
283 struct request *rq;
284 int ret, i = 0;
285
286 dprintk("map hdr %p/%d/%d\n", hdr->dxferp, hdr->dxfer_len,
287 hdr->iovec_count);
288
289 ret = bsg_validate_sghdr(q, hdr, &rw);
290 if (ret)
291 return ERR_PTR(ret);
292
293 /*
294 * map scatter-gather elements seperately and string them to request
295 */
296 rq = blk_get_request(q, rw, GFP_KERNEL);
297 ret = blk_fill_sghdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
298 &bd->flags));
299 if (ret) {
300 blk_put_request(rq);
301 return ERR_PTR(ret);
302 }
303
304 if (!hdr->iovec_count) {
305 ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
306 if (ret)
307 goto out;
308 }
309
310 u_iov = hdr->dxferp;
311 for (ret = 0, i = 0; i < hdr->iovec_count; i++, u_iov++) {
312 if (copy_from_user(&iov, u_iov, sizeof(iov))) {
313 ret = -EFAULT;
314 break;
315 }
316
317 if (!iov.iov_len || !iov.iov_base) {
318 ret = -EINVAL;
319 break;
320 }
321
322 ret = blk_rq_map_user(q, rq, iov.iov_base, iov.iov_len);
323 if (ret)
324 break;
325 }
326
327 /*
328 * bugger, cleanup
329 */
330 if (ret) {
331out:
332 dprintk("failed map at %d: %d\n", i, ret);
333 blk_unmap_sghdr_rq(rq, hdr);
334 rq = ERR_PTR(ret);
335 }
336
337 return rq;
338}
339
340/*
341 * async completion call-back from the block layer, when scsi/ide/whatever
342 * calls end_that_request_last() on a request
343 */
344static void bsg_rq_end_io(struct request *rq, int uptodate)
345{
346 struct bsg_command *bc = rq->end_io_data;
347 struct bsg_device *bd = bc->bd;
348 unsigned long flags;
349
350 dprintk("%s: finished rq %p bio %p, bc %p offset %ld stat %d\n",
351 bd->name, rq, bc, bc->bio, bc - bd->cmd_map, uptodate);
352
353 bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
354
355 spin_lock_irqsave(&bd->lock, flags);
356 list_del(&bc->list);
357 bsg_add_done_cmd(bd, bc);
358 spin_unlock_irqrestore(&bd->lock, flags);
359}
360
361/*
362 * do final setup of a 'bc' and submit the matching 'rq' to the block
363 * layer for io
364 */
365static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
366 struct bsg_command *bc, struct request *rq)
367{
368 rq->sense = bc->sense;
369 rq->sense_len = 0;
370
371 /*
372 * add bc command to busy queue and submit rq for io
373 */
374 bc->rq = rq;
375 bc->bio = rq->bio;
376 bc->hdr.duration = jiffies;
377 spin_lock_irq(&bd->lock);
378 list_add_tail(&bc->list, &bd->busy_list);
379 spin_unlock_irq(&bd->lock);
380
381 dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
382
383 rq->end_io_data = bc;
384 blk_execute_rq_nowait(q, bd->disk, rq, 1, bsg_rq_end_io);
385}
386
387static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
388{
389 struct bsg_command *bc = NULL;
390
391 spin_lock_irq(&bd->lock);
392 if (bd->done_cmds) {
393 bc = list_entry_bc(bd->done_list.next);
394 bsg_del_done_cmd(bd, bc);
395 }
396 spin_unlock_irq(&bd->lock);
397
398 return bc;
399}
400
401/*
402 * Get a finished command from the done list
403 */
404static struct bsg_command *__bsg_get_done_cmd(struct bsg_device *bd, int state)
405{
406 struct bsg_command *bc;
407 int ret;
408
409 do {
410 bc = bsg_next_done_cmd(bd);
411 if (bc)
412 break;
413
414 ret = bsg_io_schedule(bd, state);
415 if (ret) {
416 bc = ERR_PTR(ret);
417 break;
418 }
419 } while (1);
420
421 dprintk("%s: returning done %p\n", bd->name, bc);
422
423 return bc;
424}
425
426static struct bsg_command *
427bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov)
428{
429 return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE);
430}
431
432static struct bsg_command *
433bsg_get_done_cmd_nosignals(struct bsg_device *bd)
434{
435 return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE);
436}
437
438static int bsg_complete_all_commands(struct bsg_device *bd)
439{
440 struct bsg_command *bc;
441 int ret, tret;
442
443 dprintk("%s: entered\n", bd->name);
444
445 set_bit(BSG_F_BLOCK, &bd->flags);
446
447 /*
448 * wait for all commands to complete
449 */
450 ret = 0;
451 do {
452 ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
453 /*
454 * look for -ENODATA specifically -- we'll sometimes get
455 * -ERESTARTSYS when we've taken a signal, but we can't
456 * return until we're done freeing the queue, so ignore
457 * it. The signal will get handled when we're done freeing
458 * the bsg_device.
459 */
460 } while (ret != -ENODATA);
461
462 /*
463 * discard done commands
464 */
465 ret = 0;
466 do {
467 bc = bsg_get_done_cmd_nosignals(bd);
468
469 /*
470 * we _must_ complete before restarting, because
471 * bsg_release can't handle this failing.
472 */
473 if (PTR_ERR(bc) == -ERESTARTSYS)
474 continue;
475 if (IS_ERR(bc)) {
476 ret = PTR_ERR(bc);
477 break;
478 }
479
480 tret = blk_complete_sghdr_rq(bc->rq, &bc->hdr, bc->bio);
481 if (!ret)
482 ret = tret;
483
484 bsg_free_command(bc);
485 } while (1);
486
487 return ret;
488}
489
490typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov);
491
492static ssize_t
493__bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
494 struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
495{
496 struct bsg_command *bc;
497 int nr_commands, ret;
498
499 if (count % sizeof(struct sg_io_hdr))
500 return -EINVAL;
501
502 ret = 0;
503 nr_commands = count / sizeof(struct sg_io_hdr);
504 while (nr_commands) {
505 bc = get_bc(bd, iov);
506 if (IS_ERR(bc)) {
507 ret = PTR_ERR(bc);
508 break;
509 }
510
511 /*
512 * this is the only case where we need to copy data back
513 * after completing the request. so do that here,
514 * bsg_complete_work() cannot do that for us
515 */
516 ret = blk_complete_sghdr_rq(bc->rq, &bc->hdr, bc->bio);
517
518 if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
519 ret = -EFAULT;
520
521 bsg_free_command(bc);
522
523 if (ret)
524 break;
525
526 buf += sizeof(struct sg_io_hdr);
527 *bytes_read += sizeof(struct sg_io_hdr);
528 nr_commands--;
529 }
530
531 return ret;
532}
533
534static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
535{
536 if (file->f_flags & O_NONBLOCK)
537 clear_bit(BSG_F_BLOCK, &bd->flags);
538 else
539 set_bit(BSG_F_BLOCK, &bd->flags);
540}
541
542static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
543{
544 if (file->f_mode & FMODE_WRITE)
545 set_bit(BSG_F_WRITE_PERM, &bd->flags);
546 else
547 clear_bit(BSG_F_WRITE_PERM, &bd->flags);
548}
549
550static inline int err_block_err(int ret)
551{
552 if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
553 return 1;
554
555 return 0;
556}
557
558static ssize_t
559bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
560{
561 struct bsg_device *bd = file->private_data;
562 int ret;
563 ssize_t bytes_read;
564
565 dprintk("%s: read %lu bytes\n", bd->name, count);
566
567 bsg_set_block(bd, file);
568 bytes_read = 0;
569 ret = __bsg_read(buf, count, bsg_get_done_cmd,
570 bd, NULL, &bytes_read);
571 *ppos = bytes_read;
572
573 if (!bytes_read || (bytes_read && err_block_err(ret)))
574 bytes_read = ret;
575
576 return bytes_read;
577}
578
579static ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
580 size_t count, ssize_t *bytes_read)
581{
582 struct bsg_command *bc;
583 struct request *rq;
584 int ret, nr_commands;
585
586 if (count % sizeof(struct sg_io_hdr))
587 return -EINVAL;
588
589 nr_commands = count / sizeof(struct sg_io_hdr);
590 rq = NULL;
591 bc = NULL;
592 ret = 0;
593 while (nr_commands) {
594 request_queue_t *q = bd->queue;
595 int rw = READ;
596
597 bc = bsg_get_command(bd);
598 if (!bc)
599 break;
600 if (IS_ERR(bc)) {
601 ret = PTR_ERR(bc);
602 bc = NULL;
603 break;
604 }
605
606 bc->uhdr = (struct sg_io_hdr __user *) buf;
607 if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
608 ret = -EFAULT;
609 break;
610 }
611
612 /*
613 * get a request, fill in the blanks, and add to request queue
614 */
615 rq = bsg_map_hdr(bd, rw, &bc->hdr);
616 if (IS_ERR(rq)) {
617 ret = PTR_ERR(rq);
618 rq = NULL;
619 break;
620 }
621
622 bsg_add_command(bd, q, bc, rq);
623 bc = NULL;
624 rq = NULL;
625 nr_commands--;
626 buf += sizeof(struct sg_io_hdr);
627 *bytes_read += sizeof(struct sg_io_hdr);
628 }
629
630 if (rq)
631 blk_unmap_sghdr_rq(rq, &bc->hdr);
632 if (bc)
633 bsg_free_command(bc);
634
635 return ret;
636}
637
638static ssize_t
639bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
640{
641 struct bsg_device *bd = file->private_data;
642 ssize_t bytes_read;
643 int ret;
644
645 dprintk("%s: write %lu bytes\n", bd->name, count);
646
647 bsg_set_block(bd, file);
648 bsg_set_write_perm(bd, file);
649
650 bytes_read = 0;
651 ret = __bsg_write(bd, buf, count, &bytes_read);
652 *ppos = bytes_read;
653
654 /*
655 * return bytes written on non-fatal errors
656 */
657 if (!bytes_read || (bytes_read && err_block_err(ret)))
658 bytes_read = ret;
659
660 dprintk("%s: returning %lu\n", bd->name, bytes_read);
661 return bytes_read;
662}
663
664static void bsg_free_device(struct bsg_device *bd)
665{
666 if (bd->cmd_map)
667 free_pages((unsigned long) bd->cmd_map, BSG_CMDS_PAGE_ORDER);
668
669 kfree(bd->cmd_bitmap);
670 kfree(bd);
671}
672
673static struct bsg_device *bsg_alloc_device(void)
674{
675 struct bsg_command *cmd_map;
676 unsigned long *cmd_bitmap;
677 struct bsg_device *bd;
678 int bits;
679
680 bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
681 if (unlikely(!bd))
682 return NULL;
683
684 spin_lock_init(&bd->lock);
685
686 bd->max_queue = BSG_CMDS;
687
688 bits = (BSG_CMDS / BSG_CMDS_PER_LONG) + 1;
689 cmd_bitmap = kzalloc(bits * sizeof(unsigned long), GFP_KERNEL);
690 if (!cmd_bitmap)
691 goto out_free_bd;
692 bd->cmd_bitmap = cmd_bitmap;
693
694 cmd_map = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
695 BSG_CMDS_PAGE_ORDER);
696 if (!cmd_map)
697 goto out_free_bitmap;
698 bd->cmd_map = cmd_map;
699
700 INIT_LIST_HEAD(&bd->busy_list);
701 INIT_LIST_HEAD(&bd->done_list);
702 INIT_HLIST_NODE(&bd->dev_list);
703
704 init_waitqueue_head(&bd->wq_free);
705 init_waitqueue_head(&bd->wq_done);
706 return bd;
707
708out_free_bitmap:
709 kfree(cmd_bitmap);
710out_free_bd:
711 kfree(bd);
712 return NULL;
713}
714
715static int bsg_put_device(struct bsg_device *bd)
716{
717 int ret = 0;
718
719 mutex_lock(&bsg_mutex);
720
721 if (!atomic_dec_and_test(&bd->ref_count))
722 goto out;
723
724 dprintk("%s: tearing down\n", bd->name);
725
726 /*
727 * close can always block
728 */
729 set_bit(BSG_F_BLOCK, &bd->flags);
730
731 /*
732 * correct error detection baddies here again. it's the responsibility
733 * of the app to properly reap commands before close() if it wants
734 * fool-proof error detection
735 */
736 ret = bsg_complete_all_commands(bd);
737
738 blk_put_queue(bd->queue);
739 hlist_del(&bd->dev_list);
740 bsg_free_device(bd);
741out:
742 mutex_unlock(&bsg_mutex);
743 return ret;
744}
745
746static struct bsg_device *bsg_add_device(struct inode *inode,
747 struct gendisk *disk,
748 struct file *file)
749{
750 struct bsg_device *bd = NULL;
751#ifdef BSG_DEBUG
752 unsigned char buf[32];
753#endif
754
755 bd = bsg_alloc_device();
756 if (!bd)
757 return ERR_PTR(-ENOMEM);
758
759 bd->disk = disk;
760 bd->queue = disk->queue;
761 kobject_get(&disk->queue->kobj);
762 bsg_set_block(bd, file);
763
764 atomic_set(&bd->ref_count, 1);
765 bd->minor = iminor(inode);
766 mutex_lock(&bsg_mutex);
767 hlist_add_head(&bd->dev_list,&bsg_device_list[bsg_list_idx(bd->minor)]);
768
769 strncpy(bd->name, disk->disk_name, sizeof(bd->name) - 1);
770 dprintk("bound to <%s>, max queue %d\n",
771 format_dev_t(buf, i->i_rdev), bd->max_queue);
772
773 mutex_unlock(&bsg_mutex);
774 return bd;
775}
776
777static struct bsg_device *__bsg_get_device(int minor)
778{
779 struct hlist_head *list = &bsg_device_list[bsg_list_idx(minor)];
780 struct bsg_device *bd = NULL;
781 struct hlist_node *entry;
782
783 mutex_lock(&bsg_mutex);
784
785 hlist_for_each(entry, list) {
786 bd = hlist_entry(entry, struct bsg_device, dev_list);
787 if (bd->minor == minor) {
788 atomic_inc(&bd->ref_count);
789 break;
790 }
791
792 bd = NULL;
793 }
794
795 mutex_unlock(&bsg_mutex);
796 return bd;
797}
798
799static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
800{
801 struct bsg_device *bd = __bsg_get_device(iminor(inode));
802 struct bsg_class_device *bcd, *__bcd;
803
804 if (bd)
805 return bd;
806
807 /*
808 * find the class device
809 */
810 bcd = NULL;
811 mutex_lock(&bsg_mutex);
812 list_for_each_entry(__bcd, &bsg_class_list, list) {
813 if (__bcd->minor == iminor(inode)) {
814 bcd = __bcd;
815 break;
816 }
817 }
818 mutex_unlock(&bsg_mutex);
819
820 if (!bcd)
821 return ERR_PTR(-ENODEV);
822
823 return bsg_add_device(inode, bcd->disk, file);
824}
825
826static int bsg_open(struct inode *inode, struct file *file)
827{
828 struct bsg_device *bd = bsg_get_device(inode, file);
829
830 if (IS_ERR(bd))
831 return PTR_ERR(bd);
832
833 file->private_data = bd;
834 return 0;
835}
836
837static int bsg_release(struct inode *inode, struct file *file)
838{
839 struct bsg_device *bd = file->private_data;
840
841 file->private_data = NULL;
842 return bsg_put_device(bd);
843}
844
845static unsigned int bsg_poll(struct file *file, poll_table *wait)
846{
847 struct bsg_device *bd = file->private_data;
848 unsigned int mask = 0;
849
850 poll_wait(file, &bd->wq_done, wait);
851 poll_wait(file, &bd->wq_free, wait);
852
853 spin_lock_irq(&bd->lock);
854 if (!list_empty(&bd->done_list))
855 mask |= POLLIN | POLLRDNORM;
856 if (bd->queued_cmds >= bd->max_queue)
857 mask |= POLLOUT;
858 spin_unlock_irq(&bd->lock);
859
860 return mask;
861}
862
863static int
864bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
865 unsigned long arg)
866{
867 struct bsg_device *bd = file->private_data;
868 int __user *uarg = (int __user *) arg;
869
870 if (!bd)
871 return -ENXIO;
872
873 switch (cmd) {
874 /*
875 * our own ioctls
876 */
877 case SG_GET_COMMAND_Q:
878 return put_user(bd->max_queue, uarg);
879 case SG_SET_COMMAND_Q: {
880 int queue;
881
882 if (get_user(queue, uarg))
883 return -EFAULT;
884 if (queue > BSG_CMDS || queue < 1)
885 return -EINVAL;
886
887 bd->max_queue = queue;
888 return 0;
889 }
890
891 /*
892 * SCSI/sg ioctls
893 */
894 case SG_GET_VERSION_NUM:
895 case SCSI_IOCTL_GET_IDLUN:
896 case SCSI_IOCTL_GET_BUS_NUMBER:
897 case SG_SET_TIMEOUT:
898 case SG_GET_TIMEOUT:
899 case SG_GET_RESERVED_SIZE:
900 case SG_SET_RESERVED_SIZE:
901 case SG_EMULATED_HOST:
902 case SG_IO:
903 case SCSI_IOCTL_SEND_COMMAND: {
904 void __user *uarg = (void __user *) arg;
905 return scsi_cmd_ioctl(file, bd->disk, cmd, uarg);
906 }
907 /*
908 * block device ioctls
909 */
910 default:
911#if 0
912 return ioctl_by_bdev(bd->bdev, cmd, arg);
913#else
914 return -ENOTTY;
915#endif
916 }
917}
918
919static struct file_operations bsg_fops = {
920 .read = bsg_read,
921 .write = bsg_write,
922 .poll = bsg_poll,
923 .open = bsg_open,
924 .release = bsg_release,
925 .ioctl = bsg_ioctl,
926 .owner = THIS_MODULE,
927};
928
929void bsg_unregister_disk(struct gendisk *disk)
930{
931 struct bsg_class_device *bcd = &disk->bsg_dev;
932
933 if (!bcd->class_dev)
934 return;
935
936 mutex_lock(&bsg_mutex);
937 sysfs_remove_link(&bcd->disk->queue->kobj, "bsg");
938 class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
939 bcd->class_dev = NULL;
940 list_del_init(&bcd->list);
941 mutex_unlock(&bsg_mutex);
942}
943
944int bsg_register_disk(struct gendisk *disk)
945{
946 request_queue_t *q = disk->queue;
947 struct bsg_class_device *bcd;
948 dev_t dev;
949
950 /*
951 * we need a proper transport to send commands, not a stacked device
952 */
953 if (!q->request_fn)
954 return 0;
955
956 bcd = &disk->bsg_dev;
957 memset(bcd, 0, sizeof(*bcd));
958 INIT_LIST_HEAD(&bcd->list);
959
960 mutex_lock(&bsg_mutex);
961 dev = MKDEV(BSG_MAJOR, bsg_device_nr);
962 bcd->minor = bsg_device_nr;
963 bsg_device_nr++;
964 bcd->disk = disk;
965 bcd->class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", disk->disk_name);
966 list_add_tail(&bcd->list, &bsg_class_list);
967 sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
968 mutex_unlock(&bsg_mutex);
969 return 0;
970}
971
972static int __init bsg_init(void)
973{
974 int ret, i;
975
976 for (i = 0; i < BSG_LIST_SIZE; i++)
977 INIT_HLIST_HEAD(&bsg_device_list[i]);
978
979 bsg_class = class_create(THIS_MODULE, "bsg");
980 if (IS_ERR(bsg_class))
981 return PTR_ERR(bsg_class);
982
983 ret = register_chrdev(BSG_MAJOR, "bsg", &bsg_fops);
984 if (ret) {
985 class_destroy(bsg_class);
986 return ret;
987 }
988
989 printk(KERN_INFO "%s loaded\n", bsg_version);
990 return 0;
991}
992
993MODULE_AUTHOR("Jens Axboe");
994MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
995MODULE_LICENSE("GPL");
996
997subsys_initcall(bsg_init);
diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
index ef42bb2b12b6..3795e0708a22 100644
--- a/block/ll_rw_blk.c
+++ b/block/ll_rw_blk.c
@@ -4091,6 +4091,13 @@ int blk_register_queue(struct gendisk *disk)
4091 return ret; 4091 return ret;
4092 } 4092 }
4093 4093
4094 ret = bsg_register_disk(disk);
4095 if (ret) {
4096 elv_unregister_queue(q);
4097 kobject_unregister(&q->kobj);
4098 return ret;
4099 }
4100
4094 return 0; 4101 return 0;
4095} 4102}
4096 4103
@@ -4099,6 +4106,7 @@ void blk_unregister_queue(struct gendisk *disk)
4099 request_queue_t *q = disk->queue; 4106 request_queue_t *q = disk->queue;
4100 4107
4101 if (q && q->request_fn) { 4108 if (q && q->request_fn) {
4109 bsg_unregister_disk(disk);
4102 elv_unregister_queue(q); 4110 elv_unregister_queue(q);
4103 4111
4104 kobject_uevent(&q->kobj, KOBJ_REMOVE); 4112 kobject_uevent(&q->kobj, KOBJ_REMOVE);
diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c
index e83f1dbf7c29..88fd008d38bd 100644
--- a/block/scsi_ioctl.c
+++ b/block/scsi_ioctl.c
@@ -41,8 +41,6 @@ const unsigned char scsi_command_size[8] =
41 41
42EXPORT_SYMBOL(scsi_command_size); 42EXPORT_SYMBOL(scsi_command_size);
43 43
44#define BLK_DEFAULT_TIMEOUT (60 * HZ)
45
46#include <scsi/sg.h> 44#include <scsi/sg.h>
47 45
48static int sg_get_version(int __user *p) 46static int sg_get_version(int __user *p)
@@ -114,7 +112,7 @@ static int sg_emulated_host(request_queue_t *q, int __user *p)
114#define safe_for_read(cmd) [cmd] = CMD_READ_SAFE 112#define safe_for_read(cmd) [cmd] = CMD_READ_SAFE
115#define safe_for_write(cmd) [cmd] = CMD_WRITE_SAFE 113#define safe_for_write(cmd) [cmd] = CMD_WRITE_SAFE
116 114
117static int verify_command(struct file *file, unsigned char *cmd) 115static int verify_command(unsigned char *cmd, int has_write_perm)
118{ 116{
119 static unsigned char cmd_type[256] = { 117 static unsigned char cmd_type[256] = {
120 118
@@ -193,18 +191,11 @@ static int verify_command(struct file *file, unsigned char *cmd)
193 safe_for_write(GPCMD_SET_STREAMING), 191 safe_for_write(GPCMD_SET_STREAMING),
194 }; 192 };
195 unsigned char type = cmd_type[cmd[0]]; 193 unsigned char type = cmd_type[cmd[0]];
196 int has_write_perm = 0;
197 194
198 /* Anybody who can open the device can do a read-safe command */ 195 /* Anybody who can open the device can do a read-safe command */
199 if (type & CMD_READ_SAFE) 196 if (type & CMD_READ_SAFE)
200 return 0; 197 return 0;
201 198
202 /*
203 * file can be NULL from ioctl_by_bdev()...
204 */
205 if (file)
206 has_write_perm = file->f_mode & FMODE_WRITE;
207
208 /* Write-safe commands just require a writable open.. */ 199 /* Write-safe commands just require a writable open.. */
209 if ((type & CMD_WRITE_SAFE) && has_write_perm) 200 if ((type & CMD_WRITE_SAFE) && has_write_perm)
210 return 0; 201 return 0;
@@ -222,24 +213,104 @@ static int verify_command(struct file *file, unsigned char *cmd)
222 return -EPERM; 213 return -EPERM;
223} 214}
224 215
216int blk_fill_sghdr_rq(request_queue_t *q, struct request *rq,
217 struct sg_io_hdr *hdr, int has_write_perm)
218{
219 memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
220
221 if (copy_from_user(rq->cmd, hdr->cmdp, hdr->cmd_len))
222 return -EFAULT;
223 if (verify_command(rq->cmd, has_write_perm))
224 return -EPERM;
225
226 /*
227 * fill in request structure
228 */
229 rq->cmd_len = hdr->cmd_len;
230 rq->cmd_type = REQ_TYPE_BLOCK_PC;
231
232 rq->timeout = (hdr->timeout * HZ) / 1000;
233 if (!rq->timeout)
234 rq->timeout = q->sg_timeout;
235 if (!rq->timeout)
236 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
237
238 return 0;
239}
240EXPORT_SYMBOL_GPL(blk_fill_sghdr_rq);
241
242/*
243 * unmap a request that was previously mapped to this sg_io_hdr. handles
244 * both sg and non-sg sg_io_hdr.
245 */
246int blk_unmap_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr)
247{
248 struct bio *bio = rq->bio;
249
250 /*
251 * also releases request
252 */
253 if (!hdr->iovec_count)
254 return blk_rq_unmap_user(bio, hdr->dxfer_len);
255
256 rq_for_each_bio(bio, rq)
257 bio_unmap_user(bio);
258
259 blk_put_request(rq);
260 return 0;
261}
262EXPORT_SYMBOL_GPL(blk_unmap_sghdr_rq);
263
264int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
265 struct bio *bio)
266{
267 int r, ret = 0;
268
269 /*
270 * fill in all the output members
271 */
272 hdr->status = rq->errors & 0xff;
273 hdr->masked_status = status_byte(rq->errors);
274 hdr->msg_status = msg_byte(rq->errors);
275 hdr->host_status = host_byte(rq->errors);
276 hdr->driver_status = driver_byte(rq->errors);
277 hdr->info = 0;
278 if (hdr->masked_status || hdr->host_status || hdr->driver_status)
279 hdr->info |= SG_INFO_CHECK;
280 hdr->resid = rq->data_len;
281 hdr->sb_len_wr = 0;
282
283 if (rq->sense_len && hdr->sbp) {
284 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
285
286 if (!copy_to_user(hdr->sbp, rq->sense, len))
287 hdr->sb_len_wr = len;
288 else
289 ret = -EFAULT;
290 }
291
292 rq->bio = bio;
293 r = blk_unmap_sghdr_rq(rq, hdr);
294 if (ret)
295 r = ret;
296
297 return r;
298}
299EXPORT_SYMBOL_GPL(blk_complete_sghdr_rq);
300
225static int sg_io(struct file *file, request_queue_t *q, 301static int sg_io(struct file *file, request_queue_t *q,
226 struct gendisk *bd_disk, struct sg_io_hdr *hdr) 302 struct gendisk *bd_disk, struct sg_io_hdr *hdr)
227{ 303{
228 unsigned long start_time, timeout; 304 unsigned long start_time;
229 int writing = 0, ret = 0; 305 int writing = 0, ret = 0, has_write_perm = 0;
230 struct request *rq; 306 struct request *rq;
231 char sense[SCSI_SENSE_BUFFERSIZE]; 307 char sense[SCSI_SENSE_BUFFERSIZE];
232 unsigned char cmd[BLK_MAX_CDB];
233 struct bio *bio; 308 struct bio *bio;
234 309
235 if (hdr->interface_id != 'S') 310 if (hdr->interface_id != 'S')
236 return -EINVAL; 311 return -EINVAL;
237 if (hdr->cmd_len > BLK_MAX_CDB) 312 if (hdr->cmd_len > BLK_MAX_CDB)
238 return -EINVAL; 313 return -EINVAL;
239 if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
240 return -EFAULT;
241 if (verify_command(file, cmd))
242 return -EPERM;
243 314
244 if (hdr->dxfer_len > (q->max_hw_sectors << 9)) 315 if (hdr->dxfer_len > (q->max_hw_sectors << 9))
245 return -EIO; 316 return -EIO;
@@ -260,25 +331,14 @@ static int sg_io(struct file *file, request_queue_t *q,
260 if (!rq) 331 if (!rq)
261 return -ENOMEM; 332 return -ENOMEM;
262 333
263 /* 334 if (file)
264 * fill in request structure 335 has_write_perm = file->f_mode & FMODE_WRITE;
265 */
266 rq->cmd_len = hdr->cmd_len;
267 memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
268 memcpy(rq->cmd, cmd, hdr->cmd_len);
269
270 memset(sense, 0, sizeof(sense));
271 rq->sense = sense;
272 rq->sense_len = 0;
273
274 rq->cmd_type = REQ_TYPE_BLOCK_PC;
275 336
276 timeout = msecs_to_jiffies(hdr->timeout); 337 if (blk_fill_sghdr_rq(q, rq, hdr, has_write_perm)) {
277 rq->timeout = (timeout < INT_MAX) ? timeout : INT_MAX; 338 blk_rq_unmap_user(bio, hdr->dxfer_len);
278 if (!rq->timeout) 339 blk_put_request(rq);
279 rq->timeout = q->sg_timeout; 340 return -EFAULT;
280 if (!rq->timeout) 341 }
281 rq->timeout = BLK_DEFAULT_TIMEOUT;
282 342
283 if (hdr->iovec_count) { 343 if (hdr->iovec_count) {
284 const int size = sizeof(struct sg_iovec) * hdr->iovec_count; 344 const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
@@ -306,6 +366,9 @@ static int sg_io(struct file *file, request_queue_t *q,
306 goto out; 366 goto out;
307 367
308 bio = rq->bio; 368 bio = rq->bio;
369 memset(sense, 0, sizeof(sense));
370 rq->sense = sense;
371 rq->sense_len = 0;
309 rq->retries = 0; 372 rq->retries = 0;
310 373
311 start_time = jiffies; 374 start_time = jiffies;
@@ -316,31 +379,9 @@ static int sg_io(struct file *file, request_queue_t *q,
316 */ 379 */
317 blk_execute_rq(q, bd_disk, rq, 0); 380 blk_execute_rq(q, bd_disk, rq, 0);
318 381
319 /* write to all output members */
320 hdr->status = 0xff & rq->errors;
321 hdr->masked_status = status_byte(rq->errors);
322 hdr->msg_status = msg_byte(rq->errors);
323 hdr->host_status = host_byte(rq->errors);
324 hdr->driver_status = driver_byte(rq->errors);
325 hdr->info = 0;
326 if (hdr->masked_status || hdr->host_status || hdr->driver_status)
327 hdr->info |= SG_INFO_CHECK;
328 hdr->resid = rq->data_len;
329 hdr->duration = ((jiffies - start_time) * 1000) / HZ; 382 hdr->duration = ((jiffies - start_time) * 1000) / HZ;
330 hdr->sb_len_wr = 0;
331
332 if (rq->sense_len && hdr->sbp) {
333 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
334
335 if (!copy_to_user(hdr->sbp, rq->sense, len))
336 hdr->sb_len_wr = len;
337 }
338
339 if (blk_rq_unmap_user(bio))
340 ret = -EFAULT;
341 383
342 /* may not have succeeded, but output values written to control 384 return blk_complete_sghdr_rq(rq, hdr, bio);
343 * structure (struct sg_io_hdr). */
344out: 385out:
345 blk_put_request(rq); 386 blk_put_request(rq);
346 return ret; 387 return ret;
@@ -427,7 +468,7 @@ int sg_scsi_ioctl(struct file *file, struct request_queue *q,
427 if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len)) 468 if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
428 goto error; 469 goto error;
429 470
430 err = verify_command(file, rq->cmd); 471 err = verify_command(rq->cmd, file->f_mode & FMODE_WRITE);
431 if (err) 472 if (err)
432 goto error; 473 goto error;
433 474
@@ -454,7 +495,7 @@ int sg_scsi_ioctl(struct file *file, struct request_queue *q,
454 rq->retries = 1; 495 rq->retries = 1;
455 break; 496 break;
456 default: 497 default:
457 rq->timeout = BLK_DEFAULT_TIMEOUT; 498 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
458 break; 499 break;
459 } 500 }
460 501
@@ -501,7 +542,7 @@ static int __blk_send_generic(request_queue_t *q, struct gendisk *bd_disk, int c
501 rq->cmd_type = REQ_TYPE_BLOCK_PC; 542 rq->cmd_type = REQ_TYPE_BLOCK_PC;
502 rq->data = NULL; 543 rq->data = NULL;
503 rq->data_len = 0; 544 rq->data_len = 0;
504 rq->timeout = BLK_DEFAULT_TIMEOUT; 545 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
505 memset(rq->cmd, 0, sizeof(rq->cmd)); 546 memset(rq->cmd, 0, sizeof(rq->cmd));
506 rq->cmd[0] = cmd; 547 rq->cmd[0] = cmd;
507 rq->cmd[4] = data; 548 rq->cmd[4] = data;