Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[powerpc.git] / arch / um / drivers / ubd_kern.c
index 761e3f9..49c047b 100644 (file)
@@ -106,10 +106,15 @@ static inline void ubd_set_bit(__u64 bit, unsigned char *data)
 
 #define DRIVER_NAME "uml-blkdev"
 
+/* Can be taken in interrupt context, and is passed to the block layer to lock
+ * the request queue. Kernel side code knows that. */
 static DEFINE_SPINLOCK(ubd_io_lock);
-static DEFINE_SPINLOCK(ubd_lock);
 
-static void (*do_ubd)(void);
+static DEFINE_MUTEX(ubd_lock);
+
+/* XXX - this made sense in 2.4 days, now it's only used as a boolean, and
+ * probably it doesn't make sense even for that. */
+static int do_ubd;
 
 static int ubd_open(struct inode * inode, struct file * filp);
 static int ubd_release(struct inode * inode, struct file * file);
@@ -169,8 +174,8 @@ struct ubd {
        __u64 size;
        struct openflags boot_openflags;
        struct openflags openflags;
-       int shared;
-       int no_cow;
+       unsigned shared:1;
+       unsigned no_cow:1;
        struct cow cow;
        struct platform_device pdev;
 };
@@ -197,17 +202,6 @@ struct ubd {
 
 struct ubd ubd_devs[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
 
-static int ubd0_init(void)
-{
-       struct ubd *ubd_dev = &ubd_devs[0];
-
-       if(ubd_dev->file == NULL)
-               ubd_dev->file = "root_fs";
-       return(0);
-}
-
-__initcall(ubd0_init);
-
 /* Only changed by fake_ide_setup which is a setup */
 static int fake_ide = 0;
 static struct proc_dir_entry *proc_ide_root = NULL;
@@ -288,6 +282,10 @@ static int parse_unit(char **ptr)
        return(n);
 }
 
+/* If *index_out == -1 at exit, the passed option was a general one;
+ * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
+ * should not be freed on exit.
+ */
 static int ubd_setup_common(char *str, int *index_out)
 {
        struct ubd *ubd_dev;
@@ -314,7 +312,7 @@ static int ubd_setup_common(char *str, int *index_out)
                }
 
                err = 1;
-               spin_lock(&ubd_lock);
+               mutex_lock(&ubd_lock);
                if(fake_major != MAJOR_NR){
                        printk(KERN_ERR "Can't assign a fake major twice\n");
                        goto out1;
@@ -326,7 +324,7 @@ static int ubd_setup_common(char *str, int *index_out)
                       major);
                err = 0;
        out1:
-               spin_unlock(&ubd_lock);
+               mutex_unlock(&ubd_lock);
                return(err);
        }
 
@@ -343,7 +341,7 @@ static int ubd_setup_common(char *str, int *index_out)
        }
 
        err = 1;
-       spin_lock(&ubd_lock);
+       mutex_lock(&ubd_lock);
 
        ubd_dev = &ubd_devs[n];
        if(ubd_dev->file != NULL){
@@ -405,7 +403,7 @@ break_loop:
        ubd_dev->cow.file = backing_file;
        ubd_dev->boot_openflags = flags;
 out:
-       spin_unlock(&ubd_lock);
+       mutex_unlock(&ubd_lock);
        return(err);
 }
 
@@ -475,8 +473,9 @@ int thread_fd = -1;
 
 /* Changed by ubd_handler, which is serialized because interrupts only
  * happen on CPU 0.
+ * XXX: currently unused.
  */
-int intr_count = 0;
+static int intr_count = 0;
 
 /* call ubd_finish if you need to serialize */
 static void __ubd_finish(struct request *req, int error)
@@ -496,6 +495,8 @@ static void __ubd_finish(struct request *req, int error)
        end_request(req, 1);
 }
 
+/* Callable only from interrupt context - otherwise you need to do
+ * spin_lock_irq()/spin_lock_irqsave() */
 static inline void ubd_finish(struct request *req, int error)
 {
        spin_lock(&ubd_io_lock);
@@ -503,14 +504,15 @@ static inline void ubd_finish(struct request *req, int error)
        spin_unlock(&ubd_io_lock);
 }
 
-/* Called without ubd_io_lock held */
+/* XXX - move this inside ubd_intr. */
+/* Called without ubd_io_lock held, and only in interrupt context. */
 static void ubd_handler(void)
 {
        struct io_thread_req req;
        struct request *rq = elv_next_request(ubd_queue);
        int n;
 
-       do_ubd = NULL;
+       do_ubd = 0;
        intr_count++;
        n = os_read_file(thread_fd, &req, sizeof(req));
        if(n != sizeof(req)){
@@ -524,7 +526,9 @@ static void ubd_handler(void)
 
        ubd_finish(rq, req.error);
        reactivate_fd(thread_fd, UBD_IRQ);      
+       spin_lock(&ubd_io_lock);
        do_ubd_request(ubd_queue);
+       spin_unlock(&ubd_io_lock);
 }
 
 static irqreturn_t ubd_intr(int irq, void *dev)
@@ -544,7 +548,7 @@ void kill_io_thread(void)
 
 __uml_exitcall(kill_io_thread);
 
-static int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
+static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
 {
        char *file;
 
@@ -552,7 +556,7 @@ static int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
        return(os_file_size(file, size_out));
 }
 
-static void ubd_close(struct ubd *ubd_dev)
+static void ubd_close_dev(struct ubd *ubd_dev)
 {
        os_close_file(ubd_dev->fd);
        if(ubd_dev->cow.file == NULL)
@@ -568,33 +572,36 @@ static int ubd_open_dev(struct ubd *ubd_dev)
        struct openflags flags;
        char **back_ptr;
        int err, create_cow, *create_ptr;
+       int fd;
 
        ubd_dev->openflags = ubd_dev->boot_openflags;
        create_cow = 0;
        create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
        back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
-       ubd_dev->fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
+
+       fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
                                back_ptr, &ubd_dev->cow.bitmap_offset,
                                &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
                                create_ptr);
 
-       if((ubd_dev->fd == -ENOENT) && create_cow){
-               ubd_dev->fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
+       if((fd == -ENOENT) && create_cow){
+               fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
                                          ubd_dev->openflags, 1 << 9, PAGE_SIZE,
                                          &ubd_dev->cow.bitmap_offset,
                                          &ubd_dev->cow.bitmap_len,
                                          &ubd_dev->cow.data_offset);
-               if(ubd_dev->fd >= 0){
+               if(fd >= 0){
                        printk(KERN_INFO "Creating \"%s\" as COW file for "
                               "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
                }
        }
 
-       if(ubd_dev->fd < 0){
+       if(fd < 0){
                printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
-                      -ubd_dev->fd);
-               return(ubd_dev->fd);
+                      -fd);
+               return fd;
        }
+       ubd_dev->fd = fd;
 
        if(ubd_dev->cow.file != NULL){
                err = -ENOMEM;
@@ -624,7 +631,7 @@ static int ubd_open_dev(struct ubd *ubd_dev)
        return(err);
 }
 
-static int ubd_new_disk(int major, u64 size, int unit,
+static int ubd_disk_register(int major, u64 size, int unit,
                        struct gendisk **disk_out)
                        
 {
@@ -676,12 +683,12 @@ static int ubd_add(int n)
 
        ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
 
-       err = ubd_new_disk(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
+       err = ubd_disk_register(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
        if(err)
                goto out;
 
        if(fake_major != MAJOR_NR)
-               ubd_new_disk(fake_major, ubd_dev->size, n,
+               ubd_disk_register(fake_major, ubd_dev->size, n,
                             &fake_gendisk[n]);
 
        /* perhaps this should also be under the "if (fake_major)" above */
@@ -696,27 +703,36 @@ out:
 
 static int ubd_config(char *str)
 {
-       int n, err;
+       int n, ret;
 
        str = kstrdup(str, GFP_KERNEL);
-       if(str == NULL){
+       if (str == NULL) {
                printk(KERN_ERR "ubd_config failed to strdup string\n");
-               return(1);
+               ret = 1;
+               goto out;
        }
-       err = ubd_setup_common(str, &n);
-       if(err){
-               kfree(str);
-               return(-1);
+       ret = ubd_setup_common(str, &n);
+       if (ret) {
+               ret = -1;
+               goto err_free;
+       }
+       if (n == -1) {
+               ret = 0;
+               goto err_free;
        }
-       if(n == -1) return(0);
 
-       spin_lock(&ubd_lock);
-       err = ubd_add(n);
-       if(err)
+       mutex_lock(&ubd_lock);
+       ret = ubd_add(n);
+       if (ret)
                ubd_devs[n].file = NULL;
-       spin_unlock(&ubd_lock);
+       mutex_unlock(&ubd_lock);
 
-       return(err);
+out:
+       return ret;
+
+err_free:
+       kfree(str);
+       goto out;
 }
 
 static int ubd_get_config(char *name, char *str, int size, char **error_out)
@@ -731,7 +747,7 @@ static int ubd_get_config(char *name, char *str, int size, char **error_out)
        }
 
        ubd_dev = &ubd_devs[n];
-       spin_lock(&ubd_lock);
+       mutex_lock(&ubd_lock);
 
        if(ubd_dev->file == NULL){
                CONFIG_CHUNK(str, size, len, "", 1);
@@ -747,7 +763,7 @@ static int ubd_get_config(char *name, char *str, int size, char **error_out)
        else CONFIG_CHUNK(str, size, len, "", 1);
 
  out:
-       spin_unlock(&ubd_lock);
+       mutex_unlock(&ubd_lock);
        return(len);
 }
 
@@ -766,7 +782,7 @@ static int ubd_remove(int n)
        struct ubd *ubd_dev;
        int err = -ENODEV;
 
-       spin_lock(&ubd_lock);
+       mutex_lock(&ubd_lock);
 
        if(ubd_gendisk[n] == NULL)
                goto out;
@@ -795,10 +811,11 @@ static int ubd_remove(int n)
        *ubd_dev = ((struct ubd) DEFAULT_UBD);
        err = 0;
 out:
-       spin_unlock(&ubd_lock);
+       mutex_unlock(&ubd_lock);
        return err;
 }
 
+/* All these are called by mconsole in process context and without ubd-specific locks. */
 static struct mc_device ubd_mc = {
        .name           = "ubd",
        .config         = ubd_config,
@@ -807,7 +824,7 @@ static struct mc_device ubd_mc = {
        .remove         = ubd_remove,
 };
 
-static int ubd_mc_init(void)
+static int __init ubd_mc_init(void)
 {
        mconsole_register_dev(&ubd_mc);
        return 0;
@@ -815,13 +832,24 @@ static int ubd_mc_init(void)
 
 __initcall(ubd_mc_init);
 
+static int __init ubd0_init(void)
+{
+       struct ubd *ubd_dev = &ubd_devs[0];
+
+       if(ubd_dev->file == NULL)
+               ubd_dev->file = "root_fs";
+       return(0);
+}
+
+__initcall(ubd0_init);
+
 static struct platform_driver ubd_driver = {
        .driver = {
                .name  = DRIVER_NAME,
        },
 };
 
-int ubd_init(void)
+static int __init ubd_init(void)
 {
         int i;
 
@@ -849,7 +877,7 @@ int ubd_init(void)
 
 late_initcall(ubd_init);
 
-int ubd_driver_init(void){
+static int __init ubd_driver_init(void){
        unsigned long stack;
        int err;
 
@@ -898,7 +926,7 @@ static int ubd_open(struct inode *inode, struct file *filp)
        /* This should no more be needed. And it didn't work anyway to exclude
         * read-write remounting of filesystems.*/
        /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
-               if(--ubd_dev->count == 0) ubd_close(ubd_dev);
+               if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
                err = -EROFS;
        }*/
  out:
@@ -911,7 +939,7 @@ static int ubd_release(struct inode * inode, struct file * file)
        struct ubd *ubd_dev = disk->private_data;
 
        if(--ubd_dev->count == 0)
-               ubd_close(ubd_dev);
+               ubd_close_dev(ubd_dev);
        return(0);
 }
 
@@ -1036,7 +1064,7 @@ static void do_ubd_request(request_queue_t *q)
                        return;
                err = prepare_request(req, &io_req);
                if(!err){
-                       do_ubd = ubd_handler;
+                       do_ubd = 1;
                        n = os_write_file(thread_fd, (char *) &io_req,
                                         sizeof(io_req));
                        if(n != sizeof(io_req))
@@ -1356,8 +1384,8 @@ void do_io(struct io_thread_req *req)
  */
 int kernel_fd = -1;
 
-/* Only changed by the io thread */
-int io_count = 0;
+/* Only changed by the io thread. XXX: currently unused. */
+static int io_count = 0;
 
 int io_thread(void *arg)
 {