Skip to contentSkip to conversation

bsg, block layer sg

PATCH In review Jens Axboe 4 participants 9 messages Mar 2, 2006 – Mar 6, 2006

Subsystems (2)
lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

PATCH 1/1 bsg, block layer sg

Hi,

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

What's new in this area is that the bsg character device is closely tied to the block device. This relationsship is depicted in sysfs. bsg devices will show up in /sys/class/bsg/<devname>, and there is a link from /sys/block/<devname>/queue/bsg to that directory. With some udev/hotplug magic, it should create device nodes for you automatically.

TODO:

- Fold block/scsi_ioctl.c and block/bsg.c into one
- Further improve the sysfs relations between a device and bsg
- Test and so on

Probably some bugs still pending, it works for me though.

---

 block/Kconfig            |    7 
 block/Makefile           |    3 
 block/bsg.c              | 1259 ++++++++++++++++++++++++++++++++++++++++++++++
 block/ll_rw_blk.c        |    8 
 block/scsi_ioctl.c       |  165 ++++--
 drivers/ide/ide-floppy.c |   29 +
 drivers/ide/ide.c        |   10 
 include/linux/blkdev.h   |   12 
 include/linux/bsg.h      |   21 +
 include/linux/genhd.h    |    2 
 10 files changed, 1432 insertions(+), 84 deletions(-)
 create mode 100644 block/bsg.c
 create mode 100644 include/linux/bsg.h

42b7b79f6f42e12e584f7b5e44c5c968a8acc304

block/Kconfig +7 -0
index 377f6dd..f124079 100644
@@ -11,4 +11,11 @@ config LBD
 	  your machine, or if you want to have a raid or loopback device
 	  bigger than 2TB.  Otherwise say N.
 
+config BLK_DEV_BSG
+	bool "Block layer SG support"
+	default y
+	---help---
+	Saying Y here will enable generic SG (SCSI generic) v3
+	support for any block device.
+
 source block/Kconfig.iosched
block/Makefile +2 -1
index 7e4f93e..edacc3e 100644
@@ -2,8 +2,9 @@
 # Makefile for the kernel block layer
 #
 
-obj-y	:= elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o
+obj-y	:= elevator.o ll_rw_blk.o ioctl.o genhd.o scsi_ioctl.o bsg.o
 
+obj-$(CONFIG_BLK_DEV_BSG)	+= bsg.o
 obj-$(CONFIG_IOSCHED_NOOP)	+= noop-iosched.o
 obj-$(CONFIG_IOSCHED_AS)	+= as-iosched.o
 obj-$(CONFIG_IOSCHED_DEADLINE)	+= deadline-iosched.o
block/bsg.c new file +1259 -0

1,259 lines not on this page · raw message

block/ll_rw_blk.c +8 -0
index 03d9c82..8ff5bbd 100644
@@ -3812,6 +3812,13 @@ int blk_register_queue(struct gendisk *d
 		return ret;
 	}
 
+	ret = bsg_register_disk(disk);
+	if (ret) {
+		elv_unregister_queue(q);
+		kobject_unregister(&q->kobj);
+		return ret;
+	}
+
 	return 0;
 }
 
@@ -3820,6 +3827,7 @@ void blk_unregister_queue(struct gendisk
 	request_queue_t *q = disk->queue;
 
 	if (q && q->request_fn) {
+		bsg_unregister_disk(disk);
 		elv_unregister_queue(q);
 
 		kobject_unregister(&q->kobj);
block/scsi_ioctl.c +100 -65

242 lines not on this page · raw message

drivers/ide/ide-floppy.c +16 -13
index 1f8db9a..e398cfd 100644
@@ -1261,19 +1261,25 @@ static void idefloppy_create_rw_cmd (ide
 	set_bit(PC_DMA_RECOMMENDED, &pc->flags);
 }
 
-static int
+static void
 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
 {
-	/*
-	 * just support eject for now, it would not be hard to make the
-	 * REQ_BLOCK_PC support fully-featured
-	 */
-	if (rq->cmd[0] != IDEFLOPPY_START_STOP_CMD)
-		return 1;
-
 	idefloppy_init_pc(pc);
+	pc->callback = &idefloppy_rw_callback;
 	memcpy(pc->c, rq->cmd, sizeof(pc->c));
-	return 0;
+	pc->rq = rq;
+	pc->b_count = rq->data_len;
+	if (rq->data_len && rq_data_dir(rq) == WRITE)
+		set_bit(PC_WRITING, &pc->flags);
+	pc->buffer = rq->data;
+	if (rq->bio)
+		set_bit(PC_DMA_RECOMMENDED, &pc->flags);
+		
+	/*
+	 * possibly problematic, doesn't look like ide-floppy correctly
+	 * handled scattered requests if dma fails...
+	 */
+	pc->request_transfer = pc->buffer_size = rq->data_len;
 }
 
 /*
@@ -1321,10 +1327,7 @@ static ide_startstop_t idefloppy_do_requ
 		pc = (idefloppy_pc_t *) rq->buffer;
 	} else if (rq->flags & REQ_BLOCK_PC) {
 		pc = idefloppy_next_pc_storage(drive);
-		if (idefloppy_blockpc_cmd(floppy, pc, rq)) {
-			idefloppy_do_end_request(drive, 0, 0);
-			return ide_stopped;
-		}
+		idefloppy_blockpc_cmd(floppy, pc, rq);
 	} else {
 		blk_dump_rq_flags(rq,
 			"ide-floppy: unsupported command in queue");
drivers/ide/ide.c +5 -5
index b2cc437..0b9575b 100644
@@ -1256,8 +1256,12 @@ int generic_ide_ioctl(ide_drive_t *drive
 {
 	ide_settings_t *setting;
 	ide_driver_t *drv;
-	int err = 0;
 	void __user *p = (void __user *)arg;
+	int err;
+
+	err = scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
+	if (err != -ENOTTY)
+		return err;
 
 	down(&ide_setting_sem);
 	if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
@@ -1379,10 +1383,6 @@ int generic_ide_ioctl(ide_drive_t *drive
 			return 0;
 		}
 
-		case CDROMEJECT:
-		case CDROMCLOSETRAY:
-			return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
-
 		case HDIO_GET_BUSSTATE:
 			if (!capable(CAP_SYS_ADMIN))
 				return -EACCES;
include/linux/blkdev.h +12 -0
index 860e7a4..ad47cc4 100644
@@ -22,6 +22,8 @@ typedef struct request_queue request_que
 struct elevator_queue;
 typedef struct elevator_queue elevator_t;
 struct request_pm_state;
+struct request;
+struct sg_io_hdr;
 
 #define BLKDEV_MIN_RQ	4
 #define BLKDEV_MAX_RQ	128	/* Default maximum */
@@ -566,6 +568,11 @@ extern unsigned long blk_max_low_pfn, bl
 #define BLK_BOUNCE_ANY		((u64)blk_max_pfn << PAGE_SHIFT)
 #define BLK_BOUNCE_ISA		(ISA_DMA_THRESHOLD)
 
+/*
+ * default timeout for SG_IO if none specified
+ */
+#define BLK_DEFAULT_SG_TIMEOUT	(60 * HZ)
+
 #ifdef CONFIG_MMU
 extern int init_emergency_isa_pool(void);
 extern void blk_queue_bounce(request_queue_t *q, struct bio **bio);
@@ -616,6 +623,11 @@ extern int blk_execute_rq(request_queue_
 			  struct request *, int);
 extern void blk_execute_rq_nowait(request_queue_t *, struct gendisk *,
 				  struct request *, int, rq_end_io_fn *);
+extern int blk_fill_sghdr_rq(request_queue_t *, struct request *,
+		      struct sg_io_hdr *, int);
+extern int blk_unmap_sghdr_rq(struct request *, struct sg_io_hdr *);
+extern int blk_complete_sghdr_rq(struct request *, struct sg_io_hdr *,
+			  struct bio *);
 
 static inline request_queue_t *bdev_get_queue(struct block_device *bdev)
 {
include/linux/bsg.h new file +21 -0
new file mode 100644
index 0000000..dc0d728
@@ -0,0 +1,21 @@
+#ifndef BSG_H
+#define BSG_H
+
+#if defined(CONFIG_BLK_DEV_BSG)
+struct bsg_class_device {
+	struct class_device *class_dev;
+	struct device *dev;
+	int minor;
+	struct gendisk *disk;
+	struct list_head list;
+};
+
+extern int bsg_register_disk(struct gendisk *);
+extern void bsg_unregister_disk(struct gendisk *);
+#else
+struct bsg_class_device { };
+#define bsg_register_disk(disk)		(0)
+#define bsg_unregister_disk(disk)	do { } while (0)
+#endif
+
+#endif
include/linux/genhd.h +2 -0
index eef5ccd..ea9eda9 100644
@@ -16,6 +16,7 @@
 #include <linux/smp.h>
 #include <linux/string.h>
 #include <linux/fs.h>
+#include <linux/bsg.h>
 
 enum {
 /* These three have identical behaviour; use the second one if DOS FDISK gets
@@ -126,6 +127,7 @@ struct gendisk {
 #else
 	struct disk_stats dkstats;
 #endif
+	struct bsg_class_device bsg_dev;
 };
 
 /* Structure for sysfs attributes on block devices */
Signature · 5 lines
1.2.3.g2cf3


-- 
Jens Axboe
#1 lore raw
Quoted 14 lines · Jens Axboe

Hi,

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

What's new in this area is that the bsg character device is closely tied to the block device. This relationsship is depicted in sysfs. bsg devices will show up in /sys/class/bsg/<devname>, and there is a link from /sys/block/<devname>/queue/bsg to that directory. With some udev/hotplug magic, it should create device nodes for you automatically.

Quoted 7 lines

TODO:

- Fold block/scsi_ioctl.c and block/bsg.c into one
- Further improve the sysfs relations between a device and bsg
- Test and so on

Probably some bugs still pending, it works for me though.

For my part, this gets a strong ACK. I've been waiting for something like this since an OLS discussion years ago.

Thanks Jens!

Jeff

#2 lore raw
In reply to Jens Axboe
Quoted 18 lines · Jens Axboe

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

What's new in this area is that the bsg character device is closely tied to the block device. This relationsship is depicted in sysfs. bsg devices will show up in /sys/class/bsg/<devname>, and there is a link from /sys/block/<devname>/queue/bsg to that directory. With some udev/hotplug magic, it should create device nodes for you automatically.

block/bsg.c new file +5 -0
+static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
+{
+	struct bsg_command *bc = NULL;
+	unsigned long *map;
+	int free_nr;

block/bsg.c line 147

Quoted 10 lines
block/bsg.c new file +10 -0
+
+	spin_lock_irq(&bd->lock);
+
+	if (bd->queued_cmds >= bd->max_queue)
+		goto out;
+
+	for (free_nr = 0, map = bd->cmd_bitmap; *map == ~0UL; map++)
+		free_nr += BSG_CMDS_PER_LONG;
+
+	BUG_ON(*map == ~0UL);

It would be strange for this assertion to trigger.

Review

Quoted 1 line
(quoted diff) +1 -0
+			

Three free tabs!

block/bsg.c line 150

Quoted 2 lines
block/bsg.c new file +2 -0
+	bd->queued_cmds++;
+	free_nr += ffz(*map);

Can't find_first_bit() be used here?

block/bsg.c line 151

Quoted 1 line
block/bsg.c new file +1 -0
+	__set_bit(free_nr, bd->cmd_bitmap);

I'm suspecting that the whole cmd_bitmap thing could use the bitmap API?

Quoted 1 line

+static inline int bsg_io_schedule(struct bsg_device *bd, int state)

This large function has four callers...

Needs a comment block, please.

Quoted 20 lines
block/bsg.c new file +20 -0
+	DEFINE_WAIT(wait);
+	int ret = 0;
+
+	spin_lock_irq(&bd->lock);
+
+	BUG_ON(bd->done_cmds > bd->queued_cmds);
+
+	/*
+	 * -ENOSPC or -ENODATA?  I'm going for -ENODATA, meaning "I have no
+	 * work to do", even though we return -ENOSPC after this same test
+	 * during bsg_write() -- there, it means our buffer can't have more
+	 * bsg_commands added to it, thus has no space left.
+	 */
+	if (bd->done_cmds == bd->queued_cmds) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+
+	if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
+		ret = -EAGAIN;

block/bsg.c line 212

Quoted 10 lines
block/bsg.c new file +10 -0
+		goto unlock;
+	}
+
+	spin_unlock_irq(&bd->lock);
+	prepare_to_wait(&bd->wq_done, &wait, state);
+	io_schedule();
+	finish_wait(&bd->wq_done, &wait);
+
+	if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
+		ret = -ERESTARTSYS;

Racy? Should the the prepare_to_wait() happen before the lock is dropped?

Quoted 4 lines
block/bsg.c new file +4 -0
+/*
+ * get a new free command, blocking if needed and specified
+ */
+static struct bsg_command *bsg_get_command(struct bsg_device *bd)

block/bsg.c line 233

Quoted 10 lines
block/bsg.c new file +10 -0
+{
+	struct bsg_command *bc;
+	int ret;
+
+	do {
+		bc = __bsg_alloc_command(bd);
+		if (bc)
+			break;
+			
+		ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);

OK. I trust you've tested that this all does the right thing when it's sent a signal?

block/bsg.c line 251

Quoted 8 lines
block/bsg.c new file +8 -0
+/*
+ * Check if sg_io_hdr from user is allowed and valid
+ */
+static int
+bsg_validate_sghdr(request_queue_t *q, struct sg_io_hdr *hdr, int *rw)
+{
+	if (hdr->interface_id != 'S')
+		return -EINVAL;

'S'? What does that mean?

Quoted 14 lines
block/bsg.c new file +14 -0
+/*
+ * map sg_io_hdr to a request. for scatter-gather sg_io_hdr, we map
+ * each segment to a bio and string multiple bio's to the request
+ */
+static struct request *
+bsg_map_hdr(request_queue_t *q, int rw, struct sg_io_hdr *hdr)
+{
+	struct sg_iovec iov;
+	struct sg_iovec __user *u_iov;
+	struct request *rq;
+	struct bio *bio;
+	int ret, i = 0;
+
+	dprintk("map hdr %p/%d/%d\n", hdr->dxferp, hdr->dxfer_len,

block/bsg.c line 303

Quoted 10 lines
block/bsg.c new file +10 -0
+					hdr->iovec_count);
+
+	ret = bsg_validate_sghdr(q, hdr, &rw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	/*
+	 * map scatter-gather elements seperately and string them to request
+	 */
+	rq = blk_get_request(q, rw, __GFP_WAIT);

GFP_NOIO would be more meaningful.

block/bsg.c line 316

Quoted 12 lines
block/bsg.c new file +12 -0
+	if (!hdr->iovec_count) {
+		ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
+		if (ret)
+			goto out;
+	}
+
+	u_iov = hdr->dxferp;
+	for (ret = 0, i = 0; i < hdr->iovec_count; i++, u_iov++) {
+		int to_vm = rw == READ;
+		unsigned long uaddr;
+
+		if (copy_from_user(&iov, u_iov, sizeof(iov))) {

If we can do copy_from_user() here then that blk_get_request() could have used GFP_KERNEL.

block/bsg.c line 328

Quoted 12 lines
block/bsg.c new file +12 -0
+			ret = -EFAULT;
+			break;
+		}
+
+		if (!iov.iov_len || !iov.iov_base) {
+			ret = -EINVAL;
+			break;
+		}
+
+		uaddr = (unsigned long) iov.iov_base;
+		if (!(uaddr & queue_dma_alignment(q))
+		    && !(iov.iov_len & queue_dma_alignment(q)))

hm, queue_dma_alignment() is an ugly thing. queue_dma_aligned(q, addr) would be nicer.

Quoted 18 lines
block/bsg.c new file +18 -0
+/*
+ * do final setup of a 'bc' and submit the matching 'rq' to the block
+ * layer for io
+ */
+static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
+			    struct bsg_command *bc, struct request *rq)
+{
+	rq->sense = bc->sense;
+	rq->sense_len = 0;
+
+	rq->rq_disk = bd->disk;
+	rq->end_io_data = bc;
+	rq->end_io = bsg_rq_end_io;
+
+	/*
+	 * add bc command to busy queue and submit rq for io
+	 */
+	bc->rq = rq;

block/bsg.c line 440

Quoted 10 lines
block/bsg.c new file +10 -0
+	bc->bio = rq->bio;
+	bc->hdr.duration = jiffies;
+	spin_lock_irq(&bd->lock);
+	list_add_tail(&bc->list, &bd->busy_list);
+	spin_unlock_irq(&bd->lock);
+
+	dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
+
+	elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
+	generic_unplug_device(q);

If you expand the two above statements you get:

	spin_lock_irqsave(q->queue_lock, flags);
	__elv_add_request(q, rq, where, plug);
	spin_unlock_irqrestore(q->queue_lock, flags);
	spin_lock_irq(q->queue_lock);
	__generic_unplug_device(q);
	spin_unlock_irq(q->queue_lock);

which is a bit sad.

Quoted 17 lines
block/bsg.c new file +17 -0
+static int bsg_complete_all_commands(struct bsg_device *bd)
+{
+	struct bsg_command *bc;
+	int ret, tret;
+
+	dprintk("%s: entered\n", bd->name);
+
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * wait for all commands to complete
+	 */
+	ret = 0;
+	do {
+		ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
+		/*
+		 * look for -ENODATA specifically -- we'll sometimes get

block/bsg.c line 520

Quoted 10 lines
block/bsg.c new file +10 -0
+		 * -ERESTARTSYS when we've taken a signal, but we can't
+		 * return until we're done freeing the queue, so ignore
+		 * it.  The signal will get handled when we're done freeing
+		 * the bsg_device.
+		 */
+	} while (ret != -ENODATA);
+
+	/*
+	 * discard done commands
+	 */

Would it be useful to reap the completed commands earlier? While their predecessors are still in flight?

block/bsg.c line 531

Quoted 11 lines
block/bsg.c new file +11 -0
+	ret = 0;
+	do {
+		bc = bsg_get_done_cmd_nosignals(bd);
+
+		/*
+		 * we _must_ complete before restarting, because
+		 * bsg_release can't handle this failing.
+		 */
+		if (PTR_ERR(bc) == -ERESTARTSYS)
+			continue;
+		if (PTR_ERR(bc)) {

You wanted IS_ERR(), I think.

block/bsg.c line 539

Quoted 8 lines
block/bsg.c new file +8 -0
+			ret = PTR_ERR(bc);
+			break;
+		}
+
+		/*
+		 * If we get any other error, bd->queued_cmds is wrong.
+		 */
+		BUG_ON(IS_ERR(bc));

If so, that can't trigger.

Quoted 26 lines
block/bsg.c new file +26 -0
+
+static ssize_t
+__bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
+	   struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
+{
+	struct bsg_command *bc;
+	int nr_commands, ret;
+
+	if (count % sizeof(struct sg_io_hdr))
+		return -EINVAL;
+
+	ret = 0;
+	nr_commands = count / sizeof(struct sg_io_hdr);
+	while (nr_commands) {
+		bc = get_bc(bd, iov);
+		if (IS_ERR(bc)) {
+			ret = PTR_ERR(bc);
+			break;
+		}
+
+		/*
+		 * this is the only case where we need to copy data back
+		 * after completing the request. so do that here,
+		 * bsg_complete_work() cannot do that for us
+		 */
+		ret = blk_complete_sghdr_rq(bc->rq, &bc->hdr, bc->bio);

block/bsg.c line 587

Quoted 10 lines
block/bsg.c new file +10 -0
+	
+		if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
+			ret = -EFAULT;
+
+		bsg_free_command(bc);
+
+		if (ret)
+			break;
+
+		buf += sizeof(struct sg_io_hdr);

yowch, this sg_io_hdr thing is cast in stone, isn't it?

block/bsg.c line 612

Quoted 3 lines
block/bsg.c new file +3 -0
+
+#define err_block_err(ret)	\
+	((ret) && (ret) != -ENOSPC && (ret) != -ENODATA && (ret) != -EAGAIN)

Make this a function?

block/bsg.c line 622

Quoted 9 lines
block/bsg.c new file +9 -0
+static ssize_t
+bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+	struct bsg_device *bd = file->private_data;
+	int ret;
+	ssize_t bytes_read;
+
+	if (unlikely(!bd))
+		return -ENXIO;

Is that possible?

block/bsg.c line 725

Quoted 10 lines
block/bsg.c new file +10 -0
+static ssize_t
+bsg_readv(struct file *file, const struct iovec *iov, unsigned long nr_segs,
+	  loff_t *ppos)
+{
+	struct bsg_device *bd = file->private_data;
+	int ret = 0;
+	ssize_t bytes_read = 0;
+
+	if (unlikely(!bd))
+		return -ENXIO;

No, looking at bsg_open() I don't think it is.

Quoted 5 lines
block/bsg.c new file +5 -0
+static int bsg_writev_validate_iovec(const struct iovec *iov)
+{
+	struct sg_io_hdr hdr;
+
+	dprintk("iov[0] = {%p, %Zu}, sizeof(struct sg_io_hdr) = %Zu\n",

block/bsg.c line 850

Quoted 10 lines
block/bsg.c new file +10 -0
+		iov[0].iov_base, iov[0].iov_len, sizeof(struct sg_io_hdr));
+	if (iov[0].iov_len != sizeof(struct sg_io_hdr))
+		return -EINVAL;
+
+	/*
+	 * I really don't like doing this copy twice, but I don't see a good
+	 * way around it...
+	 */
+	if (copy_from_user(&hdr, iov[0].iov_base, sizeof(struct sg_io_hdr)))
+		return -EFAULT;

Is this function temporary? If not, you might want to optimise out the double copy_from_user().

block/bsg.c line 916

Quoted 12 lines
block/bsg.c new file +12 -0
+static void bsg_free_device(struct bsg_device *bd)
+{
+	if (bd->cmd_map)
+		free_pages((unsigned long) bd->cmd_map, BSG_CMDS_PAGE_ORDER);
+
+	kfree(bd->cmd_bitmap);
+	bd->cmd_bitmap = NULL;
+	bd->disk = NULL;
+	bd->queue = NULL;
+	bd->cmd_map = NULL;
+	kfree(bd);
+}

Those assignments-to-NULL are a bit unnecessary - CONFIG_DEBUG_SLAB will catch use-after-frees (and setting them to NULL might actively hide bugs).

block/bsg.c line 928

Quoted 11 lines
block/bsg.c new file +11 -0
+static struct bsg_device *bsg_alloc_device(void)
+{
+	struct bsg_device *bd = kmalloc(sizeof(struct bsg_device), GFP_KERNEL);
+	struct bsg_command *cmd_map;
+	unsigned long *cmd_bitmap;
+	int bits;
+
+	if (unlikely(!bd))
+		return NULL;
+
+	memset(bd, 0, sizeof(struct bsg_device));

kzalloc().

block/bsg.c line 934

Quoted 6 lines
block/bsg.c new file +6 -0
+	spin_lock_init(&bd->lock);
+
+	bd->max_queue = BSG_CMDS;
+
+	bits = (BSG_CMDS / BSG_CMDS_PER_LONG) + 1;
+	cmd_bitmap = kmalloc(bits * sizeof(unsigned long), GFP_KERNEL);

kzalloc().

block/bsg.c line 940

Quoted 6 lines
block/bsg.c new file +6 -0
+	if (!cmd_bitmap)
+		goto out_free_bd;
+	bd->cmd_bitmap = cmd_bitmap;
+
+	cmd_map = (struct bsg_command *) __get_free_pages(GFP_KERNEL,
+							  BSG_CMDS_PAGE_ORDER);

__GFP_ZERO, perhaps.

block/bsg.c line 969

Quoted 8 lines
block/bsg.c new file +8 -0
+static int bsg_put_device(struct bsg_device *bd)
+{
+	int ret;
+
+	if (!atomic_dec_and_test(&bd->ref_count))
+		return 0;
+
+	mutex_lock(&bsg_mutex);

Isn't this racy? Someone can still find this device on bsg_device_list[]

Quoted 25 lines
block/bsg.c new file +25 -0
+	dprintk("%s: tearing down\n", bd->name);
+
+	/*
+	 * close can always block
+	 */
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * correct error detection baddies here again. it's the responsibility
+	 * of the app to properly reap commands before close() if it wants
+	 * fool-proof error detection
+	 */
+	ret = bsg_complete_all_commands(bd);
+
+	blk_cleanup_queue(bd->queue);
+	hlist_del(&bd->dev_list);
+	bsg_free_device(bd);
+	mutex_unlock(&bsg_mutex);
+	return ret;
+}
+
+static struct bsg_device *bsg_add_device(struct inode *inode,
+					 struct gendisk *disk,
+					 struct file *file)
+{

block/bsg.c line 1005

Quoted 10 lines
block/bsg.c new file +10 -0
+	struct bsg_device *bd = NULL;
+#ifdef BSG_DEBUG
+	unsigned char buf[32];
+#endif
+	int ret;
+
+	mutex_lock(&bsg_mutex);
+
+	ret = -ENOMEM;
+	bd = bsg_alloc_device();

This can be called outside the lock.

Can this driver be used for pagecache or swap I/O? If so, doing GFP_KERNEL allocations while holding the lock might be a problem. (OK, probably it's not deadlocky, but still - do it outside the lock).

block/bsg.c line 1018

Quoted 13 lines
block/bsg.c new file +13 -0
+	if (!bd)
+		goto out;
+
+	bd->disk = disk;
+	bd->queue = disk->queue;
+	atomic_inc(&disk->queue->refcnt);
+	bsg_set_block(bd, file);
+
+	atomic_set(&bd->ref_count, 1);
+	bd->minor = iminor(inode);
+	hlist_add_head(&bd->dev_list,&bsg_device_list[bsg_list_idx(bd->minor)]);
+
+	strncpy(bd->name, disk->disk_name, sizeof(bd->name));

This might not null-terminate bd->name.

Quoted 5 lines
block/bsg.c new file +5 -0
+static int
+bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
+	  unsigned long arg)
+{
+	struct bsg_device *bd = file->private_data;

block/bsg.c line 1137

Quoted 10 lines
block/bsg.c new file +10 -0
+	int __user *uarg = (int __user *) arg;
+
+	if (!bd)
+		return -ENXIO;
+
+	switch (cmd) {
+		/*
+		 * our own ioctls
+		 */
+		case SG_GET_COMMAND_Q:

The switch's body could be moved a tabstop to the left.

Quoted 15 lines
block/bsg.c new file +15 -0
+int bsg_register_disk(struct gendisk *disk)
+{
+	request_queue_t *q = disk->queue;
+	struct bsg_class_device *bcd;
+	dev_t dev;
+
+	/*
+	 * we need a proper transport to send commands, not a stacked device
+	 */
+	if (!q->request_fn)
+		return 0;
+
+	bcd = &disk->bsg_dev;
+	memset(bcd, 0, sizeof(*bcd));
+	INIT_LIST_HEAD(&bcd->list);

block/bsg.c line 1230

Quoted 10 lines
block/bsg.c new file +10 -0
+
+	mutex_lock(&bsg_mutex);
+	dev = MKDEV(BSG_MAJOR, bsg_device_nr);
+	bcd->minor = bsg_device_nr;
+	bsg_device_nr++;
+	bcd->disk = disk;
+	bcd->class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", disk->disk_name);
+	list_add_tail(&bcd->list, &bsg_class_list);
+	sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
+	mutex_unlock(&bsg_mutex);

Again, we're probably doing GFP_KERNEL allocations with bsg_mutex held. Please check.

Quoted 1 line

+EXPORT_SYMBOL(blk_fill_sghdr_rq);

GPL?

Quoted 11 lines
block/scsi_ioctl.c +11 -0
+
+/*
+ * unmap a request that was previously mapped to this sg_io_hdr. handles
+ * both sg and non-sg sg_io_hdr.
+ */
+int blk_unmap_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr)
+{
+	struct bio *bio = rq->bio;
+
+	/*
+	 * also releases request

block/scsi_ioctl.c static int verify_command(struct file *f · line 262

Quoted 10 lines
block/scsi_ioctl.c +10 -0
+	 */
+	if (!hdr->iovec_count)
+		return blk_rq_unmap_user(bio, hdr->dxfer_len);
+
+	while ((bio = rq->bio)) {
+		rq->bio = bio->bi_next;
+		bio->bi_next = NULL;
+
+		bio_unmap_user(bio);
+	}

rq_for_each_bio()?

Quoted 13 lines
block/scsi_ioctl.c +13 -0
+
+int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
+			  struct bio *bio)
+{
+	/*
+	 * fill in all the output members
+	 */
+	hdr->status = rq->errors & 0xff;
+	hdr->masked_status = status_byte(rq->errors);
+	hdr->msg_status = msg_byte(rq->errors);
+	hdr->host_status = host_byte(rq->errors);
+	hdr->driver_status = driver_byte(rq->errors);
+	hdr->info = 0;

block/scsi_ioctl.c static int verify_command(struct file *f · line 290

Quoted 10 lines
block/scsi_ioctl.c +10 -0
+	if (hdr->masked_status || hdr->host_status || hdr->driver_status)
+		hdr->info |= SG_INFO_CHECK;
+	hdr->resid = rq->data_len;
+	hdr->sb_len_wr = 0;
+
+	if (rq->sense_len && hdr->sbp) {
+		int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
+
+		if (!copy_to_user(hdr->sbp, rq->sense, len))
+			hdr->sb_len_wr = len;

No -EFAULT?

author
#3 lore raw
Quoted 23 lines · Andrew Morton
Quoted 22 lines · Jens Axboe

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

What's new in this area is that the bsg character device is closely tied to the block device. This relationsship is depicted in sysfs. bsg devices will show up in /sys/class/bsg/<devname>, and there is a link from /sys/block/<devname>/queue/bsg to that directory. With some udev/hotplug magic, it should create device nodes for you automatically.

block/bsg.c new file +9 -0
+static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
+{
+	struct bsg_command *bc = NULL;
+	unsigned long *map;
+	int free_nr;
+
+	spin_lock_irq(&bd->lock);
+
+	if (bd->queued_cmds >= bd->max_queue)
Quoted 8 lines

block/bsg.c line 147

Quoted 6 lines
block/bsg.c new file +6 -0
+		goto out;
+
+	for (free_nr = 0, map = bd->cmd_bitmap; *map == ~0UL; map++)
+		free_nr += BSG_CMDS_PER_LONG;
+
+	BUG_ON(*map == ~0UL);

It would be strange for this assertion to trigger.

Yeah it should not, of course, unless there's a bug elsewhere.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			

Three free tabs!

Killed, apparently my editor settings only show trailing spaces and not trailing tabs.

Quoted 8 lines

block/bsg.c line 150

Quoted 2 lines
block/bsg.c new file +2 -0
+	bd->queued_cmds++;
+	free_nr += ffz(*map);

Can't find_first_bit() be used here?

block/bsg.c line 151

Quoted 1 line
block/bsg.c new file +1 -0
+	__set_bit(free_nr, bd->cmd_bitmap);

I'm suspecting that the whole cmd_bitmap thing could use the bitmap API?

It probably can. The code predates that being introduced in the kernel, I'll update it.

Quoted 5 lines
Quoted 1 line

+static inline int bsg_io_schedule(struct bsg_device *bd, int state)

This large function has four callers...

Needs a comment block, please.

Yeah, and it's ugly. I'll fix it up.

Quoted 24 lines
Quoted 24 lines
block/bsg.c new file +24 -0
+	DEFINE_WAIT(wait);
+	int ret = 0;
+
+	spin_lock_irq(&bd->lock);
+
+	BUG_ON(bd->done_cmds > bd->queued_cmds);
+
+	/*
+	 * -ENOSPC or -ENODATA?  I'm going for -ENODATA, meaning "I have no
+	 * work to do", even though we return -ENOSPC after this same test
+	 * during bsg_write() -- there, it means our buffer can't have more
+	 * bsg_commands added to it, thus has no space left.
+	 */
+	if (bd->done_cmds == bd->queued_cmds) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+
+	if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
+		ret = -EAGAIN;
+		goto unlock;
+	}
+
+	spin_unlock_irq(&bd->lock);
Quoted 8 lines

block/bsg.c line 212

Quoted 6 lines
block/bsg.c new file +6 -0
+	prepare_to_wait(&bd->wq_done, &wait, state);
+	io_schedule();
+	finish_wait(&bd->wq_done, &wait);
+
+	if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
+		ret = -ERESTARTSYS;

Racy? Should the the prepare_to_wait() happen before the lock is dropped?

It should, fixed.

Quoted 9 lines
Quoted 9 lines
block/bsg.c new file +9 -0
+/*
+ * get a new free command, blocking if needed and specified
+ */
+static struct bsg_command *bsg_get_command(struct bsg_device *bd)
+{
+	struct bsg_command *bc;
+	int ret;
+
+	do {
Quoted 8 lines

block/bsg.c line 233

Quoted 5 lines
block/bsg.c new file +5 -0
+		bc = __bsg_alloc_command(bd);
+		if (bc)
+			break;
+			
+		ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);

OK. I trust you've tested that this all does the right thing when it's sent a signal?

It should, I'll double check again (long time ago).

Quoted 10 lines

block/bsg.c line 251

Quoted 8 lines
block/bsg.c new file +8 -0
+/*
+ * Check if sg_io_hdr from user is allowed and valid
+ */
+static int
+bsg_validate_sghdr(request_queue_t *q, struct sg_io_hdr *hdr, int *rw)
+{
+	if (hdr->interface_id != 'S')
+		return -EINVAL;

'S'? What does that mean?

It's the SG v3 magic, basically tells us that sg_io_hdr is what we expect it to be.

Quoted 18 lines
Quoted 18 lines
block/bsg.c new file +18 -0
+/*
+ * map sg_io_hdr to a request. for scatter-gather sg_io_hdr, we map
+ * each segment to a bio and string multiple bio's to the request
+ */
+static struct request *
+bsg_map_hdr(request_queue_t *q, int rw, struct sg_io_hdr *hdr)
+{
+	struct sg_iovec iov;
+	struct sg_iovec __user *u_iov;
+	struct request *rq;
+	struct bio *bio;
+	int ret, i = 0;
+
+	dprintk("map hdr %p/%d/%d\n", hdr->dxferp, hdr->dxfer_len,
+					hdr->iovec_count);
+
+	ret = bsg_validate_sghdr(q, hdr, &rw);
+	if (ret)
Quoted 8 lines

block/bsg.c line 303

Quoted 6 lines
block/bsg.c new file +6 -0
+		return ERR_PTR(ret);
+
+	/*
+	 * map scatter-gather elements seperately and string them to request
+	 */
+	rq = blk_get_request(q, rw, __GFP_WAIT);

GFP_NOIO would be more meaningful.

Really? __GFP_WAIT to me means that we may wait. But as you note further down, we should just make these GFP_KERNEL as they are all called directly from the user process.

Quoted 7 lines
Quoted 7 lines
block/bsg.c new file +7 -0
+	if (!hdr->iovec_count) {
+		ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
+		if (ret)
+			goto out;
+	}
+
+	u_iov = hdr->dxferp;
Quoted 8 lines

block/bsg.c line 316

Quoted 5 lines
block/bsg.c new file +5 -0
+	for (ret = 0, i = 0; i < hdr->iovec_count; i++, u_iov++) {
+		int to_vm = rw == READ;
+		unsigned long uaddr;
+
+		if (copy_from_user(&iov, u_iov, sizeof(iov))) {

If we can do copy_from_user() here then that blk_get_request() could have used GFP_KERNEL.

Indeed, fixed.

Quoted 16 lines

block/bsg.c line 328

Quoted 13 lines
block/bsg.c new file +12 -0
+			ret = -EFAULT;
+			break;
+		}
+
+		if (!iov.iov_len || !iov.iov_base) {
+			ret = -EINVAL;
+			break;
+		}
+
+		uaddr = (unsigned long) iov.iov_base;
+		if (!(uaddr & queue_dma_alignment(q))
+		    && !(iov.iov_len & queue_dma_alignment(q)))

hm, queue_dma_alignment() is an ugly thing. queue_dma_aligned(q, addr) would be nicer.

Agree, I'll add that separately to the post-2.6.16 stuff.

Quoted 31 lines
Quoted 18 lines
block/bsg.c new file +18 -0
+/*
+ * do final setup of a 'bc' and submit the matching 'rq' to the block
+ * layer for io
+ */
+static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
+			    struct bsg_command *bc, struct request *rq)
+{
+	rq->sense = bc->sense;
+	rq->sense_len = 0;
+
+	rq->rq_disk = bd->disk;
+	rq->end_io_data = bc;
+	rq->end_io = bsg_rq_end_io;
+
+	/*
+	 * add bc command to busy queue and submit rq for io
+	 */
+	bc->rq = rq;

block/bsg.c line 440

Quoted 10 lines
block/bsg.c new file +10 -0
+	bc->bio = rq->bio;
+	bc->hdr.duration = jiffies;
+	spin_lock_irq(&bd->lock);
+	list_add_tail(&bc->list, &bd->busy_list);
+	spin_unlock_irq(&bd->lock);
+
+	dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
+
+	elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
+	generic_unplug_device(q);

If you expand the two above statements you get:

Quoted 8 lines
	spin_lock_irqsave(q->queue_lock, flags);
	__elv_add_request(q, rq, where, plug);
	spin_unlock_irqrestore(q->queue_lock, flags);
	spin_lock_irq(q->queue_lock);
	__generic_unplug_device(q);
	spin_unlock_irq(q->queue_lock);

which is a bit sad.

Indeed, I'll do the locking manually and use the __ functions.

Quoted 22 lines
Quoted 22 lines
block/bsg.c new file +22 -0
+static int bsg_complete_all_commands(struct bsg_device *bd)
+{
+	struct bsg_command *bc;
+	int ret, tret;
+
+	dprintk("%s: entered\n", bd->name);
+
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * wait for all commands to complete
+	 */
+	ret = 0;
+	do {
+		ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
+		/*
+		 * look for -ENODATA specifically -- we'll sometimes get
+		 * -ERESTARTSYS when we've taken a signal, but we can't
+		 * return until we're done freeing the queue, so ignore
+		 * it.  The signal will get handled when we're done freeing
+		 * the bsg_device.
+		 */
Quoted 8 lines

block/bsg.c line 520

Quoted 5 lines
block/bsg.c new file +5 -0
+	} while (ret != -ENODATA);
+
+	/*
+	 * discard done commands
+	 */

Would it be useful to reap the completed commands earlier? While their predecessors are still in flight?

Not sure I follow... You mean if someone attempts to queue and fails because we are out of commands, then try and reap some done commands?

Quoted 5 lines
Quoted 5 lines
block/bsg.c new file +5 -0
+	ret = 0;
+	do {
+		bc = bsg_get_done_cmd_nosignals(bd);
+
+		/*
Quoted 8 lines

block/bsg.c line 531

Quoted 6 lines
block/bsg.c new file +6 -0
+		 * we _must_ complete before restarting, because
+		 * bsg_release can't handle this failing.
+		 */
+		if (PTR_ERR(bc) == -ERESTARTSYS)
+			continue;
+		if (PTR_ERR(bc)) {

You wanted IS_ERR(), I think.

Yes, recently introduced in fact...

Quoted 10 lines

block/bsg.c line 539

Quoted 8 lines
block/bsg.c new file +8 -0
+			ret = PTR_ERR(bc);
+			break;
+		}
+
+		/*
+		 * If we get any other error, bd->queued_cmds is wrong.
+		 */
+		BUG_ON(IS_ERR(bc));

If so, that can't trigger.

Gone

Quoted 30 lines
Quoted 30 lines
block/bsg.c new file +30 -0
+
+static ssize_t
+__bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
+	   struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
+{
+	struct bsg_command *bc;
+	int nr_commands, ret;
+
+	if (count % sizeof(struct sg_io_hdr))
+		return -EINVAL;
+
+	ret = 0;
+	nr_commands = count / sizeof(struct sg_io_hdr);
+	while (nr_commands) {
+		bc = get_bc(bd, iov);
+		if (IS_ERR(bc)) {
+			ret = PTR_ERR(bc);
+			break;
+		}
+
+		/*
+		 * this is the only case where we need to copy data back
+		 * after completing the request. so do that here,
+		 * bsg_complete_work() cannot do that for us
+		 */
+		ret = blk_complete_sghdr_rq(bc->rq, &bc->hdr, bc->bio);
+	
+		if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
+			ret = -EFAULT;
+
Quoted 8 lines

block/bsg.c line 587

Quoted 6 lines
block/bsg.c new file +6 -0
+		bsg_free_command(bc);
+
+		if (ret)
+			break;
+
+		buf += sizeof(struct sg_io_hdr);

yowch, this sg_io_hdr thing is cast in stone, isn't it?

It is, we cannot do anything about that. reads and writes must be in full units of that request size.

Quoted 5 lines

block/bsg.c line 612

Quoted 3 lines
block/bsg.c new file +3 -0
+
+#define err_block_err(ret)	\
+	((ret) && (ret) != -ENOSPC && (ret) != -ENODATA && (ret) != -EAGAIN)

Make this a function?

Sure.

Quoted 11 lines

block/bsg.c line 622

Quoted 9 lines
block/bsg.c new file +9 -0
+static ssize_t
+bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+	struct bsg_device *bd = file->private_data;
+	int ret;
+	ssize_t bytes_read;
+
+	if (unlikely(!bd))
+		return -ENXIO;

Is that possible?

Not really, not. It used to be with the older code, because we had to do some ugly ioctl setup magic. But now it can go.

Quoted 10 lines
Quoted 10 lines
block/bsg.c new file +10 -0
+static int bsg_writev_validate_iovec(const struct iovec *iov)
+{
+	struct sg_io_hdr hdr;
+
+	dprintk("iov[0] = {%p, %Zu}, sizeof(struct sg_io_hdr) = %Zu\n",
+		iov[0].iov_base, iov[0].iov_len, sizeof(struct sg_io_hdr));
+	if (iov[0].iov_len != sizeof(struct sg_io_hdr))
+		return -EINVAL;
+
+	/*
Quoted 8 lines

block/bsg.c line 850

Quoted 5 lines
block/bsg.c new file +5 -0
+	 * I really don't like doing this copy twice, but I don't see a good
+	 * way around it...
+	 */
+	if (copy_from_user(&hdr, iov[0].iov_base, sizeof(struct sg_io_hdr)))
+		return -EFAULT;

Is this function temporary? If not, you might want to optimise out the double copy_from_user().

Hmm not sure, I'll make a note of it.

Quoted 8 lines
Quoted 8 lines
block/bsg.c new file +8 -0
+static void bsg_free_device(struct bsg_device *bd)
+{
+	if (bd->cmd_map)
+		free_pages((unsigned long) bd->cmd_map, BSG_CMDS_PAGE_ORDER);
+
+	kfree(bd->cmd_bitmap);
+	bd->cmd_bitmap = NULL;
+	bd->disk = NULL;
Quoted 8 lines

block/bsg.c line 916

Quoted 4 lines
block/bsg.c new file +4 -0
+	bd->queue = NULL;
+	bd->cmd_map = NULL;
+	kfree(bd);
+}

Those assignments-to-NULL are a bit unnecessary - CONFIG_DEBUG_SLAB will catch use-after-frees (and setting them to NULL might actively hide bugs).

Must be my tidy nature :-). I'll kill it.

Quoted 5 lines
Quoted 5 lines
block/bsg.c new file +5 -0
+static struct bsg_device *bsg_alloc_device(void)
+{
+	struct bsg_device *bd = kmalloc(sizeof(struct bsg_device), GFP_KERNEL);
+	struct bsg_command *cmd_map;
+	unsigned long *cmd_bitmap;
Quoted 8 lines

block/bsg.c line 928

Quoted 6 lines
block/bsg.c new file +6 -0
+	int bits;
+
+	if (unlikely(!bd))
+		return NULL;
+
+	memset(bd, 0, sizeof(struct bsg_device));

kzalloc().

Fixed.

Quoted 8 lines

block/bsg.c line 934

Quoted 6 lines
block/bsg.c new file +6 -0
+	spin_lock_init(&bd->lock);
+
+	bd->max_queue = BSG_CMDS;
+
+	bits = (BSG_CMDS / BSG_CMDS_PER_LONG) + 1;
+	cmd_bitmap = kmalloc(bits * sizeof(unsigned long), GFP_KERNEL);

kzalloc().

Fixed.

Quoted 8 lines

block/bsg.c line 940

Quoted 6 lines
block/bsg.c new file +6 -0
+	if (!cmd_bitmap)
+		goto out_free_bd;
+	bd->cmd_bitmap = cmd_bitmap;
+
+	cmd_map = (struct bsg_command *) __get_free_pages(GFP_KERNEL,
+							  BSG_CMDS_PAGE_ORDER);

__GFP_ZERO, perhaps.

Certainly, fixed.

Quoted 10 lines

block/bsg.c line 969

Quoted 8 lines
block/bsg.c new file +8 -0
+static int bsg_put_device(struct bsg_device *bd)
+{
+	int ret;
+
+	if (!atomic_dec_and_test(&bd->ref_count))
+		return 0;
+
+	mutex_lock(&bsg_mutex);

Isn't this racy? Someone can still find this device on bsg_device_list[]

Ok, grabbed the mutex before the test now.

Quoted 29 lines
Quoted 29 lines
block/bsg.c new file +29 -0
+	dprintk("%s: tearing down\n", bd->name);
+
+	/*
+	 * close can always block
+	 */
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * correct error detection baddies here again. it's the responsibility
+	 * of the app to properly reap commands before close() if it wants
+	 * fool-proof error detection
+	 */
+	ret = bsg_complete_all_commands(bd);
+
+	blk_cleanup_queue(bd->queue);
+	hlist_del(&bd->dev_list);
+	bsg_free_device(bd);
+	mutex_unlock(&bsg_mutex);
+	return ret;
+}
+
+static struct bsg_device *bsg_add_device(struct inode *inode,
+					 struct gendisk *disk,
+					 struct file *file)
+{
+	struct bsg_device *bd = NULL;
+#ifdef BSG_DEBUG
+	unsigned char buf[32];
+#endif
Quoted 8 lines

block/bsg.c line 1005

Quoted 6 lines
block/bsg.c new file +6 -0
+	int ret;
+
+	mutex_lock(&bsg_mutex);
+
+	ret = -ENOMEM;
+	bd = bsg_alloc_device();

This can be called outside the lock.

Indeed, fixed.

Quoted 3 lines

Can this driver be used for pagecache or swap I/O? If so, doing GFP_KERNEL allocations while holding the lock might be a problem. (OK, probably it's not deadlocky, but still - do it outside the lock).

It can't, it only initiates io on behalf of the a user process.

Quoted 7 lines
Quoted 7 lines
block/bsg.c new file +7 -0
+	if (!bd)
+		goto out;
+
+	bd->disk = disk;
+	bd->queue = disk->queue;
+	atomic_inc(&disk->queue->refcnt);
+	bsg_set_block(bd, file);
Quoted 8 lines

block/bsg.c line 1018

Quoted 6 lines
block/bsg.c new file +6 -0
+
+	atomic_set(&bd->ref_count, 1);
+	bd->minor = iminor(inode);
+	hlist_add_head(&bd->dev_list,&bsg_device_list[bsg_list_idx(bd->minor)]);
+
+	strncpy(bd->name, disk->disk_name, sizeof(bd->name));

This might not null-terminate bd->name.

sizeof(bd->name) - 1 now.

Quoted 9 lines
Quoted 9 lines
block/bsg.c new file +9 -0
+static int
+bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
+	  unsigned long arg)
+{
+	struct bsg_device *bd = file->private_data;
+	int __user *uarg = (int __user *) arg;
+
+	if (!bd)
+		return -ENXIO;
Quoted 8 lines

block/bsg.c line 1137

Quoted 6 lines
block/bsg.c new file +6 -0
+
+	switch (cmd) {
+		/*
+		 * our own ioctls
+		 */
+		case SG_GET_COMMAND_Q:

The switch's body could be moved a tabstop to the left.

Never liked that style very much myself, unless you nest really deep... If it matters to you, I can change it.

Quoted 20 lines
Quoted 20 lines
block/bsg.c new file +20 -0
+int bsg_register_disk(struct gendisk *disk)
+{
+	request_queue_t *q = disk->queue;
+	struct bsg_class_device *bcd;
+	dev_t dev;
+
+	/*
+	 * we need a proper transport to send commands, not a stacked device
+	 */
+	if (!q->request_fn)
+		return 0;
+
+	bcd = &disk->bsg_dev;
+	memset(bcd, 0, sizeof(*bcd));
+	INIT_LIST_HEAD(&bcd->list);
+
+	mutex_lock(&bsg_mutex);
+	dev = MKDEV(BSG_MAJOR, bsg_device_nr);
+	bcd->minor = bsg_device_nr;
+	bsg_device_nr++;
Quoted 8 lines

block/bsg.c line 1230

Quoted 5 lines
block/bsg.c new file +5 -0
+	bcd->disk = disk;
+	bcd->class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", disk->disk_name);
+	list_add_tail(&bcd->list, &bsg_class_list);
+	sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
+	mutex_unlock(&bsg_mutex);

Again, we're probably doing GFP_KERNEL allocations with bsg_mutex held. Please check.

Should be ok.

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(blk_fill_sghdr_rq);

GPL?

Done, for all three added exports.

Quoted 15 lines
Quoted 15 lines
block/scsi_ioctl.c +15 -0
+
+/*
+ * unmap a request that was previously mapped to this sg_io_hdr. handles
+ * both sg and non-sg sg_io_hdr.
+ */
+int blk_unmap_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr)
+{
+	struct bio *bio = rq->bio;
+
+	/*
+	 * also releases request
+	 */
+	if (!hdr->iovec_count)
+		return blk_rq_unmap_user(bio, hdr->dxfer_len);
+
Quoted 8 lines

block/scsi_ioctl.c static int verify_command(struct file *f · line 262

Quoted 6 lines
block/scsi_ioctl.c +6 -0
+	while ((bio = rq->bio)) {
+		rq->bio = bio->bi_next;
+		bio->bi_next = NULL;
+
+		bio_unmap_user(bio);
+	}

rq_for_each_bio()?

Yeah should be enough, we don't need to detach the bio.

Quoted 17 lines
Quoted 17 lines
block/scsi_ioctl.c +17 -0
+
+int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
+			  struct bio *bio)
+{
+	/*
+	 * fill in all the output members
+	 */
+	hdr->status = rq->errors & 0xff;
+	hdr->masked_status = status_byte(rq->errors);
+	hdr->msg_status = msg_byte(rq->errors);
+	hdr->host_status = host_byte(rq->errors);
+	hdr->driver_status = driver_byte(rq->errors);
+	hdr->info = 0;
+	if (hdr->masked_status || hdr->host_status || hdr->driver_status)
+		hdr->info |= SG_INFO_CHECK;
+	hdr->resid = rq->data_len;
+	hdr->sb_len_wr = 0;
Quoted 8 lines

block/scsi_ioctl.c static int verify_command(struct file *f · line 290

Quoted 6 lines
block/scsi_ioctl.c +6 -0
+
+	if (rq->sense_len && hdr->sbp) {
+		int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
+
+		if (!copy_to_user(hdr->sbp, rq->sense, len))
+			hdr->sb_len_wr = len;

No -EFAULT?

Don't remember exactly why, but we've always done it like that for the sense copy. I'll just make it return -EFAULT on copy_to_user() error, seems the most sensible. I don't see a valid reason for it not to do it.

Thanks for you comments, Andrew!

Signature · 1 line
Jens Axboe
#4 lore raw
Quoted 12 lines · Jens Axboe

...

Quoted 10 lines

If you expand the two above statements you get:

	spin_lock_irqsave(q->queue_lock, flags);
	__elv_add_request(q, rq, where, plug);
	spin_unlock_irqrestore(q->queue_lock, flags);
	spin_lock_irq(q->queue_lock);
	__generic_unplug_device(q);
	spin_unlock_irq(q->queue_lock);
Quoted 3 lines
Quoted 1 line

which is a bit sad.

Indeed, I'll do the locking manually and use the __ functions.

blk_execute_rq_nowait() and pkt_generic_packet() also do the above two
calls. It might be worth creating a new library function.

Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
block/bsg.c new file +22 -0
+static int bsg_complete_all_commands(struct bsg_device *bd)
+{
+	struct bsg_command *bc;
+	int ret, tret;
+
+	dprintk("%s: entered\n", bd->name);
+
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * wait for all commands to complete
+	 */
+	ret = 0;
+	do {
+		ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
+		/*
+		 * look for -ENODATA specifically -- we'll sometimes get
+		 * -ERESTARTSYS when we've taken a signal, but we can't
+		 * return until we're done freeing the queue, so ignore
+		 * it.  The signal will get handled when we're done freeing
+		 * the bsg_device.
+		 */
Quoted 8 lines

block/bsg.c line 520

Quoted 5 lines
block/bsg.c new file +5 -0
+	} while (ret != -ENODATA);
+
+	/*
+	 * discard done commands
+	 */

Would it be useful to reap the completed commands earlier? While their predecessors are still in flight?

Quoted 2 lines

Not sure I follow... You mean if someone attempts to queue and fails because we are out of commands, then try and reap some done commands?

Rather than waiting for all commands to complete then freeing them all up, it might be possible to free some of them earlier, while the rest are still in flight. Pipeline the reaping with the I/O completion. A minor saving in
cycles and memory, perhaps. Probably it's not worth the complexity - I was
just asking ;)

author
#5 lore raw
Quoted 13 lines · Andrew Morton
Quoted 12 lines · Jens Axboe

...

Quoted 10 lines

If you expand the two above statements you get:

	spin_lock_irqsave(q->queue_lock, flags);
	__elv_add_request(q, rq, where, plug);
	spin_unlock_irqrestore(q->queue_lock, flags);
	spin_lock_irq(q->queue_lock);
	__generic_unplug_device(q);
	spin_unlock_irq(q->queue_lock);
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

which is a bit sad.

Indeed, I'll do the locking manually and use the __ functions.

blk_execute_rq_nowait() and pkt_generic_packet() also do the above two
calls. It might be worth creating a new library function.

Yes it might, there are other call sites like this in the kernel. But it's basically blk_execute_rq_nowait(). I'll make that change.

Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 17 lines
block/bsg.c new file +17 -0
+static int bsg_complete_all_commands(struct bsg_device *bd)
+{
+	struct bsg_command *bc;
+	int ret, tret;
+
+	dprintk("%s: entered\n", bd->name);
+
+	set_bit(BSG_F_BLOCK, &bd->flags);
+
+	/*
+	 * wait for all commands to complete
+	 */
+	ret = 0;
+	do {
+		ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
+		/*
+		 * look for -ENODATA specifically -- we'll sometimes get

block/bsg.c line 520

Quoted 10 lines
block/bsg.c new file +10 -0
+		 * -ERESTARTSYS when we've taken a signal, but we can't
+		 * return until we're done freeing the queue, so ignore
+		 * it.  The signal will get handled when we're done freeing
+		 * the bsg_device.
+		 */
+	} while (ret != -ENODATA);
+
+	/*
+	 * discard done commands
+	 */

Would it be useful to reap the completed commands earlier? While their predecessors are still in flight?

Quoted 8 lines
Quoted 2 lines

Not sure I follow... You mean if someone attempts to queue and fails because we are out of commands, then try and reap some done commands?

Rather than waiting for all commands to complete then freeing them all up, it might be possible to free some of them earlier, while the rest are still in flight. Pipeline the reaping with the I/O completion. A
minor saving in cycles and memory, perhaps. Probably it's not worth
the complexity - I was just asking ;)

For the example above, it's the final shutdown case just waiting for all commands to complete. So definitely not worth extra complexity :-)

Signature · 1 line
Jens Axboe
author
#6 lore raw
Quoted 16 lines · Jens Axboe
Quoted 15 lines · Andrew Morton
Quoted 14 lines · Jens Axboe

...

Quoted 11 lines

If you expand the two above statements you get:

	spin_lock_irqsave(q->queue_lock, flags);
	__elv_add_request(q, rq, where, plug);
	spin_unlock_irqrestore(q->queue_lock, flags);
	spin_lock_irq(q->queue_lock);
	__generic_unplug_device(q);
	spin_unlock_irq(q->queue_lock);

which is a bit sad.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Indeed, I'll do the locking manually and use the __ functions.

blk_execute_rq_nowait() and pkt_generic_packet() also do the above two
calls. It might be worth creating a new library function.

Yes it might, there are other call sites like this in the kernel. But it's basically blk_execute_rq_nowait(). I'll make that change.

First step:

http://brick.kernel.dk/git/?p=linux-2.6-block.git;a=commitdiff;h=6a09cbe527fe051c919c5d9526ba4a2d2689fb61

Second step:

http://brick.kernel.dk/git/?p=linux-2.6-block.git;a=commitdiff;h=0f62c8deddf27b15f56edcf6414c3905e93fd0ef

Signature · 1 line
Jens Axboe
#7 lore raw
In reply to Jens Axboe
Quoted 7 lines · Jens Axboe

Hi,

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

After this is merged I suppose I could then, i.e. run an SG_IO ioctl doing i.e. INQUIRY_CMD with some random block device, such as an /dev/nbd0, or /dev/loop0, or some such. Which in general does not seem to make any sense at all unless the block device has some physical device level support for SCSI/ATAPI/MMC. So while it addresses the needs of cdrecord and friends for CD burning, does it make sense to implement this as a general capability for all block devices? I'm not objecting or arguing, I'm simply puzzled why a generic SG_IO layer for _all_ block devices (whether SCSI/ATAPI/MMC capable or not) is useful?

-Erik

--
Erik B. Andersen http://codepoet-consulting.com/
--This message was written using 73% post-consumer electrons--

author
#8 lore raw
Quoted 9 lines · Erik Andersen
Quoted 7 lines · Jens Axboe

Hi,

After all that SG_IO and cdrecord talk, I decided to brush off the bsg driver I wrote some time ago. Basically this is a full (aims to be at least, probably still some minor bits missing) SG v3 interface. It supports both SG_IO (which we just pass through for now), as well as read/write and readv/writev of sg_io_hdr structures.

Quoted 10 lines

After this is merged I suppose I could then, i.e. run an SG_IO ioctl doing i.e. INQUIRY_CMD with some random block device, such as an /dev/nbd0, or /dev/loop0, or some such. Which in general does not seem to make any sense at all unless the block device has some physical device level support for SCSI/ATAPI/MMC. So while it addresses the needs of cdrecord and friends for CD burning, does it make sense to implement this as a general capability for all block devices? I'm not objecting or arguing, I'm simply puzzled why a generic SG_IO layer for _all_ block devices (whether SCSI/ATAPI/MMC capable or not) is useful?

No of course that doesn't make sense. The generic part of this applies to the transport method, it's only meant to be used for transporting ATA (through the SCSI opcode pass through method standardized) and SCSI commands. As such it applies to ATA drives, ATAPI, and SCSI. Or any other type of device that uses the same command protocol, like usb or firewire storage. And so on.

You could have the driver flag this in the queue settings, so you would only register a bsg node for suitable devices. It could also be used as a transport for other commands - say cciss wanted to use this instead of some ioctl pass through that most drivers of that type define, they could just fill whatever they need into the cmdp part as long as it fits in the 16-byte cdb. Then it would be truly generic.

Signature · 1 line
Jens Axboe