[PATCH 1/2] kill FMODE_NDELAY_NOW
[safe/jmp/linux-2.6] / block / bsg.c
index 0b3b282..e8bd247 100644 (file)
@@ -45,8 +45,6 @@ struct bsg_device {
        char name[BUS_ID_SIZE];
        int max_queue;
        unsigned long flags;
-       struct blk_scsi_cmd_filter *cmd_filter;
-       mode_t *f_mode;
 };
 
 enum {
@@ -174,7 +172,8 @@ unlock:
 }
 
 static int blk_fill_sgv4_hdr_rq(struct request_queue *q, struct request *rq,
-                               struct sg_io_v4 *hdr, struct bsg_device *bd)
+                               struct sg_io_v4 *hdr, struct bsg_device *bd,
+                               fmode_t has_write_perm)
 {
        if (hdr->request_len > BLK_MAX_CDB) {
                rq->cmd = kzalloc(hdr->request_len, GFP_KERNEL);
@@ -187,8 +186,7 @@ static int blk_fill_sgv4_hdr_rq(struct request_queue *q, struct request *rq,
                return -EFAULT;
 
        if (hdr->subprotocol == BSG_SUB_PROTOCOL_SCSI_CMD) {
-               if (blk_cmd_filter_verify_command(bd->cmd_filter, rq->cmd,
-                                                bd->f_mode))
+               if (blk_verify_command(&q->cmd_filter, rq->cmd, has_write_perm))
                        return -EPERM;
        } else if (!capable(CAP_SYS_RAWIO))
                return -EPERM;
@@ -244,7 +242,7 @@ bsg_validate_sgv4_hdr(struct request_queue *q, struct sg_io_v4 *hdr, int *rw)
  * map sg_io_v4 to a request.
  */
 static struct request *
-bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
+bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr, fmode_t has_write_perm)
 {
        struct request_queue *q = bd->queue;
        struct request *rq, *next_rq = NULL;
@@ -266,7 +264,7 @@ bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
        rq = blk_get_request(q, rw, GFP_KERNEL);
        if (!rq)
                return ERR_PTR(-ENOMEM);
-       ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd);
+       ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, bd, has_write_perm);
        if (ret)
                goto out;
 
@@ -285,7 +283,8 @@ bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
                next_rq->cmd_type = rq->cmd_type;
 
                dxferp = (void*)(unsigned long)hdr->din_xferp;
-               ret =  blk_rq_map_user(q, next_rq, dxferp, hdr->din_xfer_len);
+               ret =  blk_rq_map_user(q, next_rq, NULL, dxferp,
+                                      hdr->din_xfer_len, GFP_KERNEL);
                if (ret)
                        goto out;
        }
@@ -300,7 +299,8 @@ bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
                dxfer_len = 0;
 
        if (dxfer_len) {
-               ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
+               ret = blk_rq_map_user(q, rq, NULL, dxferp, dxfer_len,
+                                     GFP_KERNEL);
                if (ret)
                        goto out;
        }
@@ -568,25 +568,6 @@ static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
                set_bit(BSG_F_BLOCK, &bd->flags);
 }
 
-static void bsg_set_cmd_filter(struct bsg_device *bd,
-                          struct file *file)
-{
-       struct inode *inode;
-       struct gendisk *disk;
-
-       if (!file)
-               return;
-
-       inode = file->f_dentry->d_inode;
-       if (!inode)
-               return;
-
-       disk = inode->i_bdev->bd_disk;
-
-       bd->cmd_filter = &disk->cmd_filter;
-       bd->f_mode = &file->f_mode;
-}
-
 /*
  * Check if the error is a "real" error that we should return.
  */
@@ -608,7 +589,6 @@ bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
        dprintk("%s: read %Zd bytes\n", bd->name, count);
 
        bsg_set_block(bd, file);
-       bsg_set_cmd_filter(bd, file);
 
        bytes_read = 0;
        ret = __bsg_read(buf, count, bd, NULL, &bytes_read);
@@ -621,7 +601,8 @@ bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 }
 
 static int __bsg_write(struct bsg_device *bd, const char __user *buf,
-                      size_t count, ssize_t *bytes_written)
+                      size_t count, ssize_t *bytes_written,
+                      fmode_t has_write_perm)
 {
        struct bsg_command *bc;
        struct request *rq;
@@ -652,7 +633,7 @@ static int __bsg_write(struct bsg_device *bd, const char __user *buf,
                /*
                 * get a request, fill in the blanks, and add to request queue
                 */
-               rq = bsg_map_hdr(bd, &bc->hdr);
+               rq = bsg_map_hdr(bd, &bc->hdr, has_write_perm);
                if (IS_ERR(rq)) {
                        ret = PTR_ERR(rq);
                        rq = NULL;
@@ -683,10 +664,11 @@ bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
        dprintk("%s: write %Zd bytes\n", bd->name, count);
 
        bsg_set_block(bd, file);
-       bsg_set_cmd_filter(bd, file);
 
        bytes_written = 0;
-       ret = __bsg_write(bd, buf, count, &bytes_written);
+       ret = __bsg_write(bd, buf, count, &bytes_written,
+                         file->f_mode & FMODE_WRITE);
+
        *ppos = bytes_written;
 
        /*
@@ -740,8 +722,13 @@ static int bsg_put_device(struct bsg_device *bd)
        mutex_lock(&bsg_mutex);
 
        do_free = atomic_dec_and_test(&bd->ref_count);
-       if (!do_free)
+       if (!do_free) {
+               mutex_unlock(&bsg_mutex);
                goto out;
+       }
+
+       hlist_del(&bd->dev_list);
+       mutex_unlock(&bsg_mutex);
 
        dprintk("%s: tearing down\n", bd->name);
 
@@ -757,10 +744,8 @@ static int bsg_put_device(struct bsg_device *bd)
         */
        ret = bsg_complete_all_commands(bd);
 
-       hlist_del(&bd->dev_list);
        kfree(bd);
 out:
-       mutex_unlock(&bsg_mutex);
        kref_put(&q->bsg_dev.ref, bsg_kref_release_function);
        if (do_free)
                blk_put_queue(q);
@@ -789,7 +774,6 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
        bd->queue = rq;
 
        bsg_set_block(bd, file);
-       bsg_set_cmd_filter(bd, file);
 
        atomic_set(&bd->ref_count, 1);
        mutex_lock(&bsg_mutex);
@@ -930,7 +914,7 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        case SG_EMULATED_HOST:
        case SCSI_IOCTL_SEND_COMMAND: {
                void __user *uarg = (void __user *) arg;
-               return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
+               return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
        }
        case SG_IO: {
                struct request *rq;
@@ -940,7 +924,7 @@ static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                if (copy_from_user(&hdr, uarg, sizeof(hdr)))
                        return -EFAULT;
 
-               rq = bsg_map_hdr(bd, &hdr);
+               rq = bsg_map_hdr(bd, &hdr, file->f_mode & FMODE_WRITE);
                if (IS_ERR(rq))
                        return PTR_ERR(rq);
 
@@ -1041,7 +1025,7 @@ int bsg_register_queue(struct request_queue *q, struct device *parent,
        bcd->release = release;
        kref_init(&bcd->ref);
        dev = MKDEV(bsg_major, bcd->minor);
-       class_dev = device_create(bsg_class, parent, dev, "%s", devname);
+       class_dev = device_create(bsg_class, parent, dev, NULL, "%s", devname);
        if (IS_ERR(class_dev)) {
                ret = PTR_ERR(class_dev);
                goto put_dev;