drivers: Push down BKL into various drivers
[safe/jmp/linux-2.6] / drivers / char / raw.c
index f13e5de..b38942f 100644 (file)
@@ -10,7 +10,6 @@
 
 #include <linux/init.h>
 #include <linux/fs.h>
-#include <linux/devfs_fs_kernel.h>
 #include <linux/major.h>
 #include <linux/blkdev.h>
 #include <linux/module.h>
@@ -19,6 +18,9 @@
 #include <linux/uio.h>
 #include <linux/cdev.h>
 #include <linux/device.h>
+#include <linux/mutex.h>
+#include <linux/smp_lock.h>
+#include <linux/gfp.h>
 
 #include <asm/uaccess.h>
 
@@ -29,8 +31,8 @@ struct raw_device_data {
 
 static struct class *raw_class;
 static struct raw_device_data raw_devices[MAX_RAW_MINORS];
-static DECLARE_MUTEX(raw_mutex);
-static struct file_operations raw_ctl_fops;         /* forward declaration */
+static DEFINE_MUTEX(raw_mutex);
+static const struct file_operations raw_ctl_fops; /* forward declaration */
 
 /*
  * Open/close code for raw IO.
@@ -53,7 +55,8 @@ static int raw_open(struct inode *inode, struct file *filp)
                return 0;
        }
 
-       down(&raw_mutex);
+       lock_kernel();
+       mutex_lock(&raw_mutex);
 
        /*
         * All we need to do on open is check that the device is bound.
@@ -63,30 +66,32 @@ static int raw_open(struct inode *inode, struct file *filp)
        if (!bdev)
                goto out;
        igrab(bdev->bd_inode);
-       err = blkdev_get(bdev, filp->f_mode, 0);
+       err = blkdev_get(bdev, filp->f_mode);
        if (err)
                goto out;
        err = bd_claim(bdev, raw_open);
        if (err)
                goto out1;
-       err = set_blocksize(bdev, bdev_hardsect_size(bdev));
+       err = set_blocksize(bdev, bdev_logical_block_size(bdev));
        if (err)
                goto out2;
        filp->f_flags |= O_DIRECT;
        filp->f_mapping = bdev->bd_inode->i_mapping;
        if (++raw_devices[minor].inuse == 1)
-               filp->f_dentry->d_inode->i_mapping =
+               filp->f_path.dentry->d_inode->i_mapping =
                        bdev->bd_inode->i_mapping;
        filp->private_data = bdev;
-       up(&raw_mutex);
+       mutex_unlock(&raw_mutex);
+       unlock_kernel();
        return 0;
 
 out2:
        bd_release(bdev);
 out1:
-       blkdev_put(bdev);
+       blkdev_put(bdev, filp->f_mode);
 out:
-       up(&raw_mutex);
+       mutex_unlock(&raw_mutex);
+       unlock_kernel();
        return err;
 }
 
@@ -99,50 +104,55 @@ static int raw_release(struct inode *inode, struct file *filp)
        const int minor= iminor(inode);
        struct block_device *bdev;
 
-       down(&raw_mutex);
+       mutex_lock(&raw_mutex);
        bdev = raw_devices[minor].binding;
        if (--raw_devices[minor].inuse == 0) {
                /* Here  inode->i_mapping == bdev->bd_inode->i_mapping  */
                inode->i_mapping = &inode->i_data;
                inode->i_mapping->backing_dev_info = &default_backing_dev_info;
        }
-       up(&raw_mutex);
+       mutex_unlock(&raw_mutex);
 
        bd_release(bdev);
-       blkdev_put(bdev);
+       blkdev_put(bdev, filp->f_mode);
        return 0;
 }
 
 /*
  * Forward ioctls to the underlying block device.
  */
-static int
-raw_ioctl(struct inode *inode, struct file *filp,
-                 unsigned int command, unsigned long arg)
+static long
+raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
 {
        struct block_device *bdev = filp->private_data;
+       int ret;
 
-       return blkdev_ioctl(bdev->bd_inode, NULL, command, arg);
+       lock_kernel();
+       ret = blkdev_ioctl(bdev, 0, command, arg);
+       unlock_kernel();
+
+       return ret;
 }
 
 static void bind_device(struct raw_config_request *rq)
 {
-       class_device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
-       class_device_create(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor),
-                                     NULL, "raw%d", rq->raw_minor);
+       device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
+       device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL,
+                     "raw%d", rq->raw_minor);
 }
 
 /*
  * Deal with ioctls against the raw-device control interface, to bind
  * and unbind other raw devices.
  */
-static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
-                       unsigned int command, unsigned long arg)
+static long raw_ctl_ioctl(struct file *filp, unsigned int command,
+                         unsigned long arg)
 {
        struct raw_config_request rq;
        struct raw_device_data *rawdev;
        int err = 0;
 
+       lock_kernel();
        switch (command) {
        case RAW_SETBIND:
        case RAW_GETBIND:
@@ -154,7 +164,7 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                        goto out;
                }
 
-               if (rq.raw_minor < 0 || rq.raw_minor >= MAX_RAW_MINORS) {
+               if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) {
                        err = -EINVAL;
                        goto out;
                }
@@ -187,9 +197,9 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                                goto out;
                        }
 
-                       down(&raw_mutex);
+                       mutex_lock(&raw_mutex);
                        if (rawdev->inuse) {
-                               up(&raw_mutex);
+                               mutex_unlock(&raw_mutex);
                                err = -EBUSY;
                                goto out;
                        }
@@ -200,7 +210,7 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                        if (rq.block_major == 0 && rq.block_minor == 0) {
                                /* unbind */
                                rawdev->binding = NULL;
-                               class_device_destroy(raw_class,
+                               device_destroy(raw_class,
                                                MKDEV(RAW_MAJOR, rq.raw_minor));
                        } else {
                                rawdev->binding = bdget(dev);
@@ -211,11 +221,11 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                                        bind_device(&rq);
                                }
                        }
-                       up(&raw_mutex);
+                       mutex_unlock(&raw_mutex);
                } else {
                        struct block_device *bdev;
 
-                       down(&raw_mutex);
+                       mutex_lock(&raw_mutex);
                        bdev = rawdev->binding;
                        if (bdev) {
                                rq.block_major = MAJOR(bdev->bd_dev);
@@ -223,7 +233,7 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                        } else {
                                rq.block_major = rq.block_minor = 0;
                        }
-                       up(&raw_mutex);
+                       mutex_unlock(&raw_mutex);
                        if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
                                err = -EFAULT;
                                goto out;
@@ -235,103 +245,72 @@ static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
                break;
        }
 out:
+       unlock_kernel();
        return err;
 }
 
-static ssize_t raw_file_write(struct file *file, const char __user *buf,
-                                  size_t count, loff_t *ppos)
-{
-       struct iovec local_iov = {
-               .iov_base = (char __user *)buf,
-               .iov_len = count
-       };
-
-       return generic_file_write_nolock(file, &local_iov, 1, ppos);
-}
-
-static ssize_t raw_file_aio_write(struct kiocb *iocb, const char __user *buf,
-                                       size_t count, loff_t pos)
-{
-       struct iovec local_iov = {
-               .iov_base = (char __user *)buf,
-               .iov_len = count
-       };
-
-       return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
-}
-
-
-static struct file_operations raw_fops = {
-       .read   =       generic_file_read,
-       .aio_read =     generic_file_aio_read,
-       .write  =       raw_file_write,
-       .aio_write =    raw_file_aio_write,
-       .open   =       raw_open,
-       .release=       raw_release,
-       .ioctl  =       raw_ioctl,
-       .readv  =       generic_file_readv,
-       .writev =       generic_file_writev,
-       .owner  =       THIS_MODULE,
+static const struct file_operations raw_fops = {
+       .read           = do_sync_read,
+       .aio_read       = generic_file_aio_read,
+       .write          = do_sync_write,
+       .aio_write      = blkdev_aio_write,
+       .fsync          = blkdev_fsync,
+       .open           = raw_open,
+       .release        = raw_release,
+       .unlocked_ioctl = raw_ioctl,
+       .owner          = THIS_MODULE,
 };
 
-static struct file_operations raw_ctl_fops = {
-       .ioctl  =       raw_ctl_ioctl,
-       .open   =       raw_open,
-       .owner  =       THIS_MODULE,
+static const struct file_operations raw_ctl_fops = {
+       .unlocked_ioctl = raw_ctl_ioctl,
+       .open           = raw_open,
+       .owner          = THIS_MODULE,
 };
 
-static struct cdev raw_cdev = {
-       .kobj   =       {.name = "raw", },
-       .owner  =       THIS_MODULE,
-};
+static struct cdev raw_cdev;
+
+static char *raw_devnode(struct device *dev, mode_t *mode)
+{
+       return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
+}
 
 static int __init raw_init(void)
 {
-       int i;
        dev_t dev = MKDEV(RAW_MAJOR, 0);
+       int ret;
 
-       if (register_chrdev_region(dev, MAX_RAW_MINORS, "raw"))
+       ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
+       if (ret)
                goto error;
 
        cdev_init(&raw_cdev, &raw_fops);
-       if (cdev_add(&raw_cdev, dev, MAX_RAW_MINORS)) {
+       ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
+       if (ret) {
                kobject_put(&raw_cdev.kobj);
-               unregister_chrdev_region(dev, MAX_RAW_MINORS);
-               goto error;
+               goto error_region;
        }
 
        raw_class = class_create(THIS_MODULE, "raw");
        if (IS_ERR(raw_class)) {
                printk(KERN_ERR "Error creating raw class.\n");
                cdev_del(&raw_cdev);
-               unregister_chrdev_region(dev, MAX_RAW_MINORS);
-               goto error;
+               ret = PTR_ERR(raw_class);
+               goto error_region;
        }
-       class_device_create(raw_class, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
-
-       devfs_mk_cdev(MKDEV(RAW_MAJOR, 0),
-                     S_IFCHR | S_IRUGO | S_IWUGO,
-                     "raw/rawctl");
-       for (i = 1; i < MAX_RAW_MINORS; i++)
-               devfs_mk_cdev(MKDEV(RAW_MAJOR, i),
-                             S_IFCHR | S_IRUGO | S_IWUGO,
-                             "raw/raw%d", i);
+       raw_class->devnode = raw_devnode;
+       device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
+
        return 0;
 
+error_region:
+       unregister_chrdev_region(dev, MAX_RAW_MINORS);
 error:
-       printk(KERN_ERR "error register raw device\n");
-       return 1;
+       return ret;
 }
 
 static void __exit raw_exit(void)
 {
-       int i;
-
-       for (i = 1; i < MAX_RAW_MINORS; i++)
-               devfs_remove("raw/raw%d", i);
-       devfs_remove("raw/rawctl");
-       devfs_remove("raw");
-       class_device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
+       device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
        class_destroy(raw_class);
        cdev_del(&raw_cdev);
        unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);