X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=block%2Fbsg.c;h=69b0a9d333064c782676a4f84fd79e4006c2c879;hb=444ad82bc3eaa554be40d22dc248e58aeefd54d9;hp=3b2f05258a9278d1b51b4b842e4da5784aa4eaed;hpb=d941cf5e373c356723fa648b9f0302a11c9b1770;p=linux-2.6 diff --git a/block/bsg.c b/block/bsg.c index 3b2f05258a..69b0a9d333 100644 --- a/block/bsg.c +++ b/block/bsg.c @@ -1,5 +1,5 @@ /* - * bsg.c - block layer implementation of the sg v3 interface + * bsg.c - block layer implementation of the sg v4 interface * * Copyright (C) 2004 Jens Axboe SUSE Labs * Copyright (C) 2004 Peter M. Jones @@ -9,13 +9,6 @@ * archive for more details. * */ -/* - * TODO - * - Should this get merged, block/scsi_ioctl.c will be migrated into - * this file. To keep maintenance down, it's easier to have them - * seperated right now. - * - */ #include #include #include @@ -24,6 +17,7 @@ #include #include #include +#include #include #include @@ -70,13 +64,12 @@ enum { #endif static DEFINE_MUTEX(bsg_mutex); -static int bsg_device_nr, bsg_minor_idx; +static DEFINE_IDR(bsg_minor_idr); #define BSG_LIST_ARRAY_SIZE 8 static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE]; static struct class *bsg_class; -static LIST_HEAD(bsg_class_list); static int bsg_major; static struct kmem_cache *bsg_cmd_cachep; @@ -92,7 +85,6 @@ struct bsg_command { struct bio *bidi_bio; int err; struct sg_io_v4 hdr; - struct sg_io_v4 __user *uhdr; char sense[SCSI_SENSE_BUFFERSIZE]; }; @@ -429,7 +421,6 @@ static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr, hdr->info = 0; if (hdr->device_status || hdr->transport_status || hdr->driver_status) hdr->info |= SG_INFO_CHECK; - hdr->din_resid = rq->data_len; hdr->response_len = 0; if (rq->sense_len && hdr->response) { @@ -445,9 +436,23 @@ static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr, } if (rq->next_rq) { + hdr->dout_resid = rq->data_len; + hdr->din_resid = rq->next_rq->data_len; blk_rq_unmap_user(bidi_bio); blk_put_request(rq->next_rq); - } + } else if (rq_data_dir(rq) == READ) + hdr->din_resid = rq->data_len; + else + hdr->dout_resid = rq->data_len; + + /* + * If the request generated a negative error number, return it + * (providing we aren't already returning an error); if it's + * just a protocol response (i.e. non negative), that gets + * processed above. + */ + if (!ret && rq->errors < 0) + ret = rq->errors; blk_rq_unmap_user(bio); blk_put_request(rq); @@ -620,7 +625,6 @@ static int __bsg_write(struct bsg_device *bd, const char __user *buf, break; } - bc->uhdr = (struct sg_io_v4 __user *) buf; if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) { ret = -EFAULT; break; @@ -781,23 +785,18 @@ static struct bsg_device *__bsg_get_device(int minor) static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) { - struct bsg_device *bd = __bsg_get_device(iminor(inode)); - struct bsg_class_device *bcd, *__bcd; + struct bsg_device *bd; + struct bsg_class_device *bcd; + bd = __bsg_get_device(iminor(inode)); if (bd) return bd; /* * find the class device */ - bcd = NULL; mutex_lock(&bsg_mutex); - list_for_each_entry(__bcd, &bsg_class_list, list) { - if (__bcd->minor == iminor(inode)) { - bcd = __bcd; - break; - } - } + bcd = idr_find(&bsg_minor_idr, iminor(inode)); mutex_unlock(&bsg_mutex); if (!bcd) @@ -847,6 +846,7 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct bsg_device *bd = file->private_data; int __user *uarg = (int __user *) arg; + int ret; switch (cmd) { /* @@ -899,12 +899,12 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (rq->next_rq) bidi_bio = rq->next_rq->bio; blk_execute_rq(bd->queue, NULL, rq, 0); - blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); + ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio); if (copy_to_user(uarg, &hdr, sizeof(hdr))) return -EFAULT; - return 0; + return ret; } /* * block device ioctls @@ -918,7 +918,7 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } } -static struct file_operations bsg_fops = { +static const struct file_operations bsg_fops = { .read = bsg_read, .write = bsg_write, .poll = bsg_poll, @@ -936,13 +936,12 @@ void bsg_unregister_queue(struct request_queue *q) return; mutex_lock(&bsg_mutex); + idr_remove(&bsg_minor_idr, bcd->minor); sysfs_remove_link(&q->kobj, "bsg"); class_device_unregister(bcd->class_dev); put_device(bcd->dev); bcd->class_dev = NULL; bcd->dev = NULL; - list_del_init(&bcd->list); - bsg_device_nr--; mutex_unlock(&bsg_mutex); } EXPORT_SYMBOL_GPL(bsg_unregister_queue); @@ -950,9 +949,9 @@ EXPORT_SYMBOL_GPL(bsg_unregister_queue); int bsg_register_queue(struct request_queue *q, struct device *gdev, const char *name) { - struct bsg_class_device *bcd, *__bcd; + struct bsg_class_device *bcd; dev_t dev; - int ret = -EMFILE; + int ret, minor; struct class_device *class_dev = NULL; const char *devname; @@ -969,28 +968,26 @@ int bsg_register_queue(struct request_queue *q, struct device *gdev, bcd = &q->bsg_dev; memset(bcd, 0, sizeof(*bcd)); - INIT_LIST_HEAD(&bcd->list); mutex_lock(&bsg_mutex); - if (bsg_device_nr == BSG_MAX_DEVS) { - printk(KERN_ERR "bsg: too many bsg devices\n"); - goto err; - } -retry: - list_for_each_entry(__bcd, &bsg_class_list, list) { - if (__bcd->minor == bsg_minor_idx) { - bsg_minor_idx++; - if (bsg_minor_idx == BSG_MAX_DEVS) - bsg_minor_idx = 0; - goto retry; - } + ret = idr_pre_get(&bsg_minor_idr, GFP_KERNEL); + if (!ret) { + ret = -ENOMEM; + goto unlock; } - bcd->minor = bsg_minor_idx++; - if (bsg_minor_idx == BSG_MAX_DEVS) - bsg_minor_idx = 0; + ret = idr_get_new(&bsg_minor_idr, bcd, &minor); + if (ret < 0) + goto unlock; + if (minor >= BSG_MAX_DEVS) { + printk(KERN_ERR "bsg: too many bsg devices\n"); + ret = -EINVAL; + goto remove_idr; + } + + bcd->minor = minor; bcd->queue = q; bcd->dev = get_device(gdev); dev = MKDEV(bsg_major, bcd->minor); @@ -998,36 +995,32 @@ retry: devname); if (IS_ERR(class_dev)) { ret = PTR_ERR(class_dev); - goto err_put; + goto put_dev; } bcd->class_dev = class_dev; if (q->kobj.sd) { ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg"); if (ret) - goto err_unregister; + goto unregister_class_dev; } - list_add_tail(&bcd->list, &bsg_class_list); - bsg_device_nr++; - mutex_unlock(&bsg_mutex); return 0; -err_unregister: +unregister_class_dev: class_device_unregister(class_dev); -err_put: +put_dev: put_device(gdev); -err: +remove_idr: + idr_remove(&bsg_minor_idr, minor); +unlock: mutex_unlock(&bsg_mutex); return ret; } EXPORT_SYMBOL_GPL(bsg_register_queue); -static struct cdev bsg_cdev = { - .kobj = {.name = "bsg", }, - .owner = THIS_MODULE, -}; +static struct cdev bsg_cdev; static int __init bsg_init(void) {