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