diff options
-rw-r--r-- | block/Kconfig | 7 | ||||
-rw-r--r-- | block/Makefile | 1 | ||||
-rw-r--r-- | block/bsg.c | 997 | ||||
-rw-r--r-- | block/ll_rw_blk.c | 8 | ||||
-rw-r--r-- | block/scsi_ioctl.c | 163 | ||||
-rw-r--r-- | drivers/ide/ide-floppy.c | 29 | ||||
-rw-r--r-- | drivers/ide/ide.c | 10 | ||||
-rw-r--r-- | include/linux/blkdev.h | 12 | ||||
-rw-r--r-- | include/linux/bsg.h | 21 | ||||
-rw-r--r-- | include/linux/genhd.h | 2 |
10 files changed, 1171 insertions, 79 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 | ||
52 | endif # BLOCK | 52 | endif # BLOCK |
53 | 53 | ||
54 | config 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 | |||
54 | source block/Kconfig.iosched | 61 | source 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 | ||
5 | obj-$(CONFIG_BLOCK) := elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o | 5 | obj-$(CONFIG_BLOCK) := elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o |
6 | 6 | ||
7 | obj-$(CONFIG_BLK_DEV_BSG) += bsg.o | ||
7 | obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o | 8 | obj-$(CONFIG_IOSCHED_NOOP) += noop-iosched.o |
8 | obj-$(CONFIG_IOSCHED_AS) += as-iosched.o | 9 | obj-$(CONFIG_IOSCHED_AS) += as-iosched.o |
9 | obj-$(CONFIG_IOSCHED_DEADLINE) += deadline-iosched.o | 10 | obj-$(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 | |||
35 | static char bsg_version[] = "block layer sg (bsg) 0.4"; | ||
36 | |||
37 | struct bsg_command; | ||
38 | |||
39 | struct 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 | |||
59 | enum { | ||
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 | |||
88 | static DEFINE_MUTEX(bsg_mutex); | ||
89 | static int bsg_device_nr; | ||
90 | |||
91 | #define BSG_LIST_SIZE (8) | ||
92 | #define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1)) | ||
93 | static struct hlist_head bsg_device_list[BSG_LIST_SIZE]; | ||
94 | |||
95 | static struct class *bsg_class; | ||
96 | static LIST_HEAD(bsg_class_list); | ||
97 | |||
98 | /* | ||
99 | * our internal command type | ||
100 | */ | ||
101 | struct 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 | |||
112 | static 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 | |||
128 | static 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; | ||
155 | out: | ||
156 | dprintk("%s: failed (depth %d)\n", bd->name, bd->queued_cmds); | ||
157 | spin_unlock_irq(&bd->lock); | ||
158 | return bc; | ||
159 | } | ||
160 | |||
161 | static inline void | ||
162 | bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc) | ||
163 | { | ||
164 | bd->done_cmds--; | ||
165 | list_del(&bc->list); | ||
166 | } | ||
167 | |||
168 | static inline void | ||
169 | bsg_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 | |||
176 | static 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; | ||
210 | unlock: | ||
211 | spin_unlock_irq(&bd->lock); | ||
212 | return ret; | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | * get a new free command, blocking if needed and specified | ||
217 | */ | ||
218 | static 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 | */ | ||
242 | static int | ||
243 | bsg_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 | */ | ||
277 | static struct request * | ||
278 | bsg_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) { | ||
331 | out: | ||
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 | */ | ||
344 | static 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 | */ | ||
365 | static 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 | |||
387 | static 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 | */ | ||
404 | static 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 | |||
426 | static struct bsg_command * | ||
427 | bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov) | ||
428 | { | ||
429 | return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE); | ||
430 | } | ||
431 | |||
432 | static struct bsg_command * | ||
433 | bsg_get_done_cmd_nosignals(struct bsg_device *bd) | ||
434 | { | ||
435 | return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE); | ||
436 | } | ||
437 | |||
438 | static 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 | |||
490 | typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov); | ||
491 | |||
492 | static 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 | |||
534 | static 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 | |||
542 | static 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 | |||
550 | static 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 | |||
558 | static ssize_t | ||
559 | bsg_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 | |||
579 | static 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 | |||
638 | static ssize_t | ||
639 | bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | ||
640 | { | ||
641 | struct bsg_device *bd = file->private_data; | ||
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 | |||
664 | static 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 | |||
673 | static 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 | |||
708 | out_free_bitmap: | ||
709 | kfree(cmd_bitmap); | ||
710 | out_free_bd: | ||
711 | kfree(bd); | ||
712 | return NULL; | ||
713 | } | ||
714 | |||
715 | static 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); | ||
741 | out: | ||
742 | mutex_unlock(&bsg_mutex); | ||
743 | return ret; | ||
744 | } | ||
745 | |||
746 | static 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 | |||
777 | static 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 | |||
799 | static 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 | |||
826 | static 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 | |||
837 | static 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 | |||
845 | static 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 | |||
863 | static int | ||
864 | bsg_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 | |||
919 | static 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 | |||
929 | void 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 | |||
944 | int 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 | |||
972 | static 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 | |||
993 | MODULE_AUTHOR("Jens Axboe"); | ||
994 | MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver"); | ||
995 | MODULE_LICENSE("GPL"); | ||
996 | |||
997 | subsys_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 | ||
42 | EXPORT_SYMBOL(scsi_command_size); | 42 | EXPORT_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 | ||
48 | static int sg_get_version(int __user *p) | 46 | static 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 | ||
117 | static int verify_command(struct file *file, unsigned char *cmd) | 115 | static 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 | ||
216 | int 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 | } | ||
240 | EXPORT_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 | */ | ||
246 | int 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 | } | ||
262 | EXPORT_SYMBOL_GPL(blk_unmap_sghdr_rq); | ||
263 | |||
264 | int 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 | } | ||
299 | EXPORT_SYMBOL_GPL(blk_complete_sghdr_rq); | ||
300 | |||
225 | static int sg_io(struct file *file, request_queue_t *q, | 301 | static 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). */ | ||
344 | out: | 385 | out: |
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; |
diff --git a/drivers/ide/ide-floppy.c b/drivers/ide/ide-floppy.c index f429be88c4f9..a21f585b1caa 100644 --- a/drivers/ide/ide-floppy.c +++ b/drivers/ide/ide-floppy.c | |||
@@ -1258,19 +1258,25 @@ static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t | |||
1258 | set_bit(PC_DMA_RECOMMENDED, &pc->flags); | 1258 | set_bit(PC_DMA_RECOMMENDED, &pc->flags); |
1259 | } | 1259 | } |
1260 | 1260 | ||
1261 | static int | 1261 | static void |
1262 | idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq) | 1262 | idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq) |
1263 | { | 1263 | { |
1264 | /* | ||
1265 | * just support eject for now, it would not be hard to make the | ||
1266 | * REQ_BLOCK_PC support fully-featured | ||
1267 | */ | ||
1268 | if (rq->cmd[0] != IDEFLOPPY_START_STOP_CMD) | ||
1269 | return 1; | ||
1270 | |||
1271 | idefloppy_init_pc(pc); | 1264 | idefloppy_init_pc(pc); |
1265 | pc->callback = &idefloppy_rw_callback; | ||
1272 | memcpy(pc->c, rq->cmd, sizeof(pc->c)); | 1266 | memcpy(pc->c, rq->cmd, sizeof(pc->c)); |
1273 | return 0; | 1267 | pc->rq = rq; |
1268 | pc->b_count = rq->data_len; | ||
1269 | if (rq->data_len && rq_data_dir(rq) == WRITE) | ||
1270 | set_bit(PC_WRITING, &pc->flags); | ||
1271 | pc->buffer = rq->data; | ||
1272 | if (rq->bio) | ||
1273 | set_bit(PC_DMA_RECOMMENDED, &pc->flags); | ||
1274 | |||
1275 | /* | ||
1276 | * possibly problematic, doesn't look like ide-floppy correctly | ||
1277 | * handled scattered requests if dma fails... | ||
1278 | */ | ||
1279 | pc->request_transfer = pc->buffer_size = rq->data_len; | ||
1274 | } | 1280 | } |
1275 | 1281 | ||
1276 | /* | 1282 | /* |
@@ -1317,10 +1323,7 @@ static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request | |||
1317 | pc = (idefloppy_pc_t *) rq->buffer; | 1323 | pc = (idefloppy_pc_t *) rq->buffer; |
1318 | } else if (blk_pc_request(rq)) { | 1324 | } else if (blk_pc_request(rq)) { |
1319 | pc = idefloppy_next_pc_storage(drive); | 1325 | pc = idefloppy_next_pc_storage(drive); |
1320 | if (idefloppy_blockpc_cmd(floppy, pc, rq)) { | 1326 | idefloppy_blockpc_cmd(floppy, pc, rq); |
1321 | idefloppy_do_end_request(drive, 0, 0); | ||
1322 | return ide_stopped; | ||
1323 | } | ||
1324 | } else { | 1327 | } else { |
1325 | blk_dump_rq_flags(rq, | 1328 | blk_dump_rq_flags(rq, |
1326 | "ide-floppy: unsupported command in queue"); | 1329 | "ide-floppy: unsupported command in queue"); |
diff --git a/drivers/ide/ide.c b/drivers/ide/ide.c index c948a5c17a5d..9ae60a7400a2 100644 --- a/drivers/ide/ide.c +++ b/drivers/ide/ide.c | |||
@@ -1049,9 +1049,13 @@ int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device | |||
1049 | unsigned long flags; | 1049 | unsigned long flags; |
1050 | ide_driver_t *drv; | 1050 | ide_driver_t *drv; |
1051 | void __user *p = (void __user *)arg; | 1051 | void __user *p = (void __user *)arg; |
1052 | int err = 0, (*setfunc)(ide_drive_t *, int); | 1052 | int err, (*setfunc)(ide_drive_t *, int); |
1053 | u8 *val; | 1053 | u8 *val; |
1054 | 1054 | ||
1055 | err = scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p); | ||
1056 | if (err != -ENOTTY) | ||
1057 | return err; | ||
1058 | |||
1055 | switch (cmd) { | 1059 | switch (cmd) { |
1056 | case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val; | 1060 | case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val; |
1057 | case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val; | 1061 | case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val; |
@@ -1171,10 +1175,6 @@ int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device | |||
1171 | return 0; | 1175 | return 0; |
1172 | } | 1176 | } |
1173 | 1177 | ||
1174 | case CDROMEJECT: | ||
1175 | case CDROMCLOSETRAY: | ||
1176 | return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p); | ||
1177 | |||
1178 | case HDIO_GET_BUSSTATE: | 1178 | case HDIO_GET_BUSSTATE: |
1179 | if (!capable(CAP_SYS_ADMIN)) | 1179 | if (!capable(CAP_SYS_ADMIN)) |
1180 | return -EACCES; | 1180 | return -EACCES; |
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index fae138bd2207..53002d40efa2 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h | |||
@@ -41,6 +41,8 @@ struct elevator_queue; | |||
41 | typedef struct elevator_queue elevator_t; | 41 | typedef struct elevator_queue elevator_t; |
42 | struct request_pm_state; | 42 | struct request_pm_state; |
43 | struct blk_trace; | 43 | struct blk_trace; |
44 | struct request; | ||
45 | struct sg_io_hdr; | ||
44 | 46 | ||
45 | #define BLKDEV_MIN_RQ 4 | 47 | #define BLKDEV_MIN_RQ 4 |
46 | #define BLKDEV_MAX_RQ 128 /* Default maximum */ | 48 | #define BLKDEV_MAX_RQ 128 /* Default maximum */ |
@@ -607,6 +609,11 @@ extern unsigned long blk_max_low_pfn, blk_max_pfn; | |||
607 | #define BLK_BOUNCE_ANY ((u64)blk_max_pfn << PAGE_SHIFT) | 609 | #define BLK_BOUNCE_ANY ((u64)blk_max_pfn << PAGE_SHIFT) |
608 | #define BLK_BOUNCE_ISA (ISA_DMA_THRESHOLD) | 610 | #define BLK_BOUNCE_ISA (ISA_DMA_THRESHOLD) |
609 | 611 | ||
612 | /* | ||
613 | * default timeout for SG_IO if none specified | ||
614 | */ | ||
615 | #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ) | ||
616 | |||
610 | #ifdef CONFIG_MMU | 617 | #ifdef CONFIG_MMU |
611 | extern int init_emergency_isa_pool(void); | 618 | extern int init_emergency_isa_pool(void); |
612 | extern void blk_queue_bounce(request_queue_t *q, struct bio **bio); | 619 | extern void blk_queue_bounce(request_queue_t *q, struct bio **bio); |
@@ -680,6 +687,11 @@ extern int blk_execute_rq(request_queue_t *, struct gendisk *, | |||
680 | struct request *, int); | 687 | struct request *, int); |
681 | extern void blk_execute_rq_nowait(request_queue_t *, struct gendisk *, | 688 | extern void blk_execute_rq_nowait(request_queue_t *, struct gendisk *, |
682 | struct request *, int, rq_end_io_fn *); | 689 | struct request *, int, rq_end_io_fn *); |
690 | extern int blk_fill_sghdr_rq(request_queue_t *, struct request *, | ||
691 | struct sg_io_hdr *, int); | ||
692 | extern int blk_unmap_sghdr_rq(struct request *, struct sg_io_hdr *); | ||
693 | extern int blk_complete_sghdr_rq(struct request *, struct sg_io_hdr *, | ||
694 | struct bio *); | ||
683 | 695 | ||
684 | static inline request_queue_t *bdev_get_queue(struct block_device *bdev) | 696 | static inline request_queue_t *bdev_get_queue(struct block_device *bdev) |
685 | { | 697 | { |
diff --git a/include/linux/bsg.h b/include/linux/bsg.h new file mode 100644 index 000000000000..dc0d7282c4cb --- /dev/null +++ b/include/linux/bsg.h | |||
@@ -0,0 +1,21 @@ | |||
1 | #ifndef BSG_H | ||
2 | #define BSG_H | ||
3 | |||
4 | #if defined(CONFIG_BLK_DEV_BSG) | ||
5 | struct bsg_class_device { | ||
6 | struct class_device *class_dev; | ||
7 | struct device *dev; | ||
8 | int minor; | ||
9 | struct gendisk *disk; | ||
10 | struct list_head list; | ||
11 | }; | ||
12 | |||
13 | extern int bsg_register_disk(struct gendisk *); | ||
14 | extern void bsg_unregister_disk(struct gendisk *); | ||
15 | #else | ||
16 | struct bsg_class_device { }; | ||
17 | #define bsg_register_disk(disk) (0) | ||
18 | #define bsg_unregister_disk(disk) do { } while (0) | ||
19 | #endif | ||
20 | |||
21 | #endif | ||
diff --git a/include/linux/genhd.h b/include/linux/genhd.h index 9756fc102a83..8c43d7032612 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h | |||
@@ -67,6 +67,7 @@ struct partition { | |||
67 | #include <linux/string.h> | 67 | #include <linux/string.h> |
68 | #include <linux/fs.h> | 68 | #include <linux/fs.h> |
69 | #include <linux/workqueue.h> | 69 | #include <linux/workqueue.h> |
70 | #include <linux/bsg.h> | ||
70 | 71 | ||
71 | struct partition { | 72 | struct partition { |
72 | unsigned char boot_ind; /* 0x80 - active */ | 73 | unsigned char boot_ind; /* 0x80 - active */ |
@@ -91,6 +92,7 @@ struct hd_struct { | |||
91 | #ifdef CONFIG_FAIL_MAKE_REQUEST | 92 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
92 | int make_it_fail; | 93 | int make_it_fail; |
93 | #endif | 94 | #endif |
95 | struct bsg_class_device bsg_dev; | ||
94 | }; | 96 | }; |
95 | 97 | ||
96 | #define GENHD_FL_REMOVABLE 1 | 98 | #define GENHD_FL_REMOVABLE 1 |