Commit | Line | Data |
---|---|---|
3d6392cf JA |
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); |