Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / um / drivers / ubd_kern.c
index 2a7f689..0897852 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  * Licensed under the GPL
  */
@@ -35,6 +35,7 @@
 #include "linux/blkpg.h"
 #include "linux/genhd.h"
 #include "linux/spinlock.h"
+#include "linux/platform_device.h"
 #include "asm/segment.h"
 #include "asm/uaccess.h"
 #include "asm/irq.h"
@@ -70,7 +71,7 @@ struct io_thread_req {
        int error;
 };
 
-extern int open_ubd_file(char *file, struct openflags *openflags,
+extern int open_ubd_file(char *file, struct openflags *openflags, int shared,
                         char **backing_file_out, int *bitmap_offset_out,
                         unsigned long *bitmap_len_out, int *data_offset_out,
                         int *create_cow_out);
@@ -116,6 +117,7 @@ static int ubd_open(struct inode * inode, struct file * filp);
 static int ubd_release(struct inode * inode, struct file * file);
 static int ubd_ioctl(struct inode * inode, struct file * file,
                     unsigned int cmd, unsigned long arg);
+static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
 
 #define MAX_DEV (8)
 
@@ -124,6 +126,7 @@ static struct block_device_operations ubd_blops = {
         .open          = ubd_open,
         .release       = ubd_release,
         .ioctl         = ubd_ioctl,
+       .getgeo         = ubd_getgeo,
 };
 
 /* Protected by the queue_lock */
@@ -134,7 +137,7 @@ static int fake_major = MAJOR_NR;
 
 static struct gendisk *ubd_gendisk[MAX_DEV];
 static struct gendisk *fake_gendisk[MAX_DEV];
+
 #ifdef CONFIG_BLK_DEV_UBD_SYNC
 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
                                         .cl = 1 })
@@ -165,6 +168,7 @@ struct ubd {
        __u64 size;
        struct openflags boot_openflags;
        struct openflags openflags;
+       int shared;
        int no_cow;
        struct cow cow;
        struct platform_device pdev;
@@ -186,6 +190,7 @@ struct ubd {
        .boot_openflags =       OPEN_FLAGS, \
        .openflags =            OPEN_FLAGS, \
         .no_cow =               0, \
+       .shared =               0, \
         .cow =                 DEFAULT_COW, \
 }
 
@@ -302,7 +307,7 @@ static int ubd_setup_common(char *str, int *index_out)
                }
                major = simple_strtoul(str, &end, 0);
                if((*end != '\0') || (end == str)){
-                       printk(KERN_ERR 
+                       printk(KERN_ERR
                               "ubd_setup : didn't parse major number\n");
                        return(1);
                }
@@ -313,7 +318,7 @@ static int ubd_setup_common(char *str, int *index_out)
                        printk(KERN_ERR "Can't assign a fake major twice\n");
                        goto out1;
                }
+
                fake_major = major;
 
                printk(KERN_INFO "Setting extra ubd major number to %d\n",
@@ -348,7 +353,7 @@ static int ubd_setup_common(char *str, int *index_out)
        if (index_out)
                *index_out = n;
 
-       for (i = 0; i < 4; i++) {
+       for (i = 0; i < sizeof("rscd="); i++) {
                switch (*str) {
                case 'r':
                        flags.w = 0;
@@ -359,11 +364,14 @@ static int ubd_setup_common(char *str, int *index_out)
                case 'd':
                        dev->no_cow = 1;
                        break;
+               case 'c':
+                       dev->shared = 1;
+                       break;
                case '=':
                        str++;
                        goto break_loop;
                default:
-                       printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r,s or d)\n");
+                       printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
                        goto out;
                }
                str++;
@@ -512,7 +520,7 @@ static void ubd_handler(void)
                spin_unlock(&ubd_io_lock);
                return;
        }
-        
+
        ubd_finish(rq, req.error);
        reactivate_fd(thread_fd, UBD_IRQ);      
        do_ubd_request(ubd_queue);
@@ -529,7 +537,7 @@ static int io_pid = -1;
 
 void kill_io_thread(void)
 {
-       if(io_pid != -1) 
+       if(io_pid != -1)
                os_kill_process(io_pid, 1);
 }
 
@@ -564,14 +572,15 @@ static int ubd_open_dev(struct ubd *dev)
        create_cow = 0;
        create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
        back_ptr = dev->no_cow ? NULL : &dev->cow.file;
-       dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
-                               &dev->cow.bitmap_offset, &dev->cow.bitmap_len, 
-                               &dev->cow.data_offset, create_ptr);
+       dev->fd = open_ubd_file(dev->file, &dev->openflags, dev->shared,
+                               back_ptr, &dev->cow.bitmap_offset,
+                               &dev->cow.bitmap_len, &dev->cow.data_offset,
+                               create_ptr);
 
        if((dev->fd == -ENOENT) && create_cow){
-               dev->fd = create_cow_file(dev->file, dev->cow.file, 
+               dev->fd = create_cow_file(dev->file, dev->cow.file,
                                          dev->openflags, 1 << 9, PAGE_SIZE,
-                                         &dev->cow.bitmap_offset, 
+                                         &dev->cow.bitmap_offset,
                                          &dev->cow.bitmap_len,
                                          &dev->cow.data_offset);
                if(dev->fd >= 0){
@@ -595,16 +604,16 @@ static int ubd_open_dev(struct ubd *dev)
                }
                flush_tlb_kernel_vm();
 
-               err = read_cow_bitmap(dev->fd, dev->cow.bitmap, 
-                                     dev->cow.bitmap_offset, 
+               err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
+                                     dev->cow.bitmap_offset,
                                      dev->cow.bitmap_len);
                if(err < 0)
                        goto error;
 
                flags = dev->openflags;
                flags.w = 0;
-               err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL, 
-                                   NULL, NULL);
+               err = open_ubd_file(dev->cow.file, &flags, dev->shared, NULL,
+                                   NULL, NULL, NULL, NULL);
                if(err < 0) goto error;
                dev->cow.fd = err;
        }
@@ -668,24 +677,25 @@ static int ubd_add(int n)
        struct ubd *dev = &ubd_dev[n];
        int err;
 
+       err = -ENODEV;
        if(dev->file == NULL)
-               return(-ENODEV);
+               goto out;
 
        if (ubd_open_dev(dev))
-               return(-ENODEV);
+               goto out;
 
        err = ubd_file_size(dev, &dev->size);
        if(err < 0)
-               return(err);
+               goto out_close;
 
        dev->size = ROUND_BLOCK(dev->size);
 
        err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
-       if(err) 
-               return(err);
+       if(err)
+               goto out_close;
+
        if(fake_major != MAJOR_NR)
-               ubd_new_disk(fake_major, dev->size, n, 
+               ubd_new_disk(fake_major, dev->size, n,
                             &fake_gendisk[n]);
 
        /* perhaps this should also be under the "if (fake_major)" above */
@@ -693,15 +703,18 @@ static int ubd_add(int n)
        if (fake_ide)
                make_ide_entries(ubd_gendisk[n]->disk_name);
 
+       err = 0;
+out_close:
        ubd_close(dev);
-       return 0;
+out:
+       return err;
 }
 
 static int ubd_config(char *str)
 {
        int n, err;
 
-       str = uml_strdup(str);
+       str = kstrdup(str, GFP_KERNEL);
        if(str == NULL){
                printk(KERN_ERR "ubd_config failed to strdup string\n");
                return(1);
@@ -754,24 +767,34 @@ static int ubd_get_config(char *name, char *str, int size, char **error_out)
        return(len);
 }
 
-static int ubd_remove(char *str)
+static int ubd_id(char **str, int *start_out, int *end_out)
+{
+        int n;
+
+       n = parse_unit(str);
+        *start_out = 0;
+        *end_out = MAX_DEV - 1;
+        return n;
+}
+
+static int ubd_remove(int n)
 {
        struct ubd *dev;
-       int n, err = -ENODEV;
+       int err = -ENODEV;
 
-       n = parse_unit(&str);
+       spin_lock(&ubd_lock);
 
-       if((n < 0) || (n >= MAX_DEV))
-               return(err);
+       if(ubd_gendisk[n] == NULL)
+               goto out;
 
        dev = &ubd_dev[n];
-       if(dev->count > 0)
-               return(-EBUSY); /* you cannot remove a open disk */
 
-       err = 0;
-       spin_lock(&ubd_lock);
+       if(dev->file == NULL)
+               goto out;
 
-       if(ubd_gendisk[n] == NULL)
+       /* you cannot remove a open disk */
+       err = -EBUSY;
+       if(dev->count > 0)
                goto out;
 
        del_gendisk(ubd_gendisk[n]);
@@ -787,15 +810,16 @@ static int ubd_remove(char *str)
        platform_device_unregister(&dev->pdev);
        *dev = ((struct ubd) DEFAULT_UBD);
        err = 0;
- out:
-       spin_unlock(&ubd_lock);
-       return(err);
+out:
+       spin_unlock(&ubd_lock);
+       return err;
 }
 
 static struct mc_device ubd_mc = {
        .name           = "ubd",
        .config         = ubd_config,
        .get_config     = ubd_get_config,
+       .id             = ubd_id,
        .remove         = ubd_remove,
 };
 
@@ -807,9 +831,10 @@ static int ubd_mc_init(void)
 
 __initcall(ubd_mc_init);
 
-static struct device_driver ubd_driver = {
-       .name  = DRIVER_NAME,
-       .bus   = &platform_bus_type,
+static struct platform_driver ubd_driver = {
+       .driver = {
+               .name  = DRIVER_NAME,
+       },
 };
 
 int ubd_init(void)
@@ -834,8 +859,8 @@ int ubd_init(void)
                if (register_blkdev(fake_major, "ubd"))
                        return -1;
        }
-       driver_register(&ubd_driver);
-       for (i = 0; i < MAX_DEV; i++) 
+       platform_driver_register(&ubd_driver);
+       for (i = 0; i < MAX_DEV; i++)
                ubd_add(i);
        return 0;
 }
@@ -853,20 +878,20 @@ int ubd_driver_init(void){
                 * enough. So use anyway the io thread. */
        }
        stack = alloc_stack(0, 0);
-       io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), 
+       io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
                                 &thread_fd);
        if(io_pid < 0){
-               printk(KERN_ERR 
+               printk(KERN_ERR
                       "ubd : Failed to start I/O thread (errno = %d) - "
                       "falling back to synchronous I/O\n", -io_pid);
                io_pid = -1;
                return(0);
        }
-       err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, 
+       err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
                             SA_INTERRUPT, "ubd", ubd_dev);
        if(err != 0)
                printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
-       return(err);
+       return 0;
 }
 
 device_initcall(ubd_driver_init);
@@ -959,7 +984,7 @@ static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
        if(req->op == UBD_READ) {
                for(i = 0; i < req->length >> 9; i++){
                        if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
-                               ubd_set_bit(i, (unsigned char *) 
+                               ubd_set_bit(i, (unsigned char *)
                                            &req->sector_mask);
                 }
        }
@@ -980,7 +1005,7 @@ static int prepare_request(struct request *req, struct io_thread_req *io_req)
 
        /* This should be impossible now */
        if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
-               printk("Write attempted on readonly ubd device %s\n", 
+               printk("Write attempted on readonly ubd device %s\n",
                       disk->disk_name);
                end_request(req, 0);
                return(1);
@@ -1041,10 +1066,19 @@ static void do_ubd_request(request_queue_t *q)
        }
 }
 
+static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
+{
+       struct ubd *dev = bdev->bd_disk->private_data;
+
+       geo->heads = 128;
+       geo->sectors = 32;
+       geo->cylinders = dev->size / (128 * 32 * 512);
+       return 0;
+}
+
 static int ubd_ioctl(struct inode * inode, struct file * file,
                     unsigned int cmd, unsigned long arg)
 {
-       struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;
        struct ubd *dev = inode->i_bdev->bd_disk->private_data;
        struct hd_driveid ubd_id = {
                .cyls           = 0,
@@ -1053,16 +1087,7 @@ static int ubd_ioctl(struct inode * inode, struct file * file,
        };
 
        switch (cmd) {
-               struct hd_geometry g;
                struct cdrom_volctrl volume;
-       case HDIO_GETGEO:
-               if(!loc) return(-EINVAL);
-               g.heads = 128;
-               g.sectors = 32;
-               g.cylinders = dev->size / (128 * 32 * 512);
-               g.start = get_start_sect(inode->i_bdev);
-               return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
-
        case HDIO_GET_IDENTITY:
                ubd_id.cyls = dev->size / (128 * 32 * 512);
                if(copy_to_user((char __user *) arg, (char *) &ubd_id,
@@ -1084,37 +1109,39 @@ static int ubd_ioctl(struct inode * inode, struct file * file,
        return(-EINVAL);
 }
 
-static int same_backing_files(char *from_cmdline, char *from_cow, char *cow)
+static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
 {
        struct uml_stat buf1, buf2;
        int err;
 
-       if(from_cmdline == NULL) return(1);
-       if(!strcmp(from_cmdline, from_cow)) return(1);
+       if(from_cmdline == NULL)
+               return 0;
+       if(!strcmp(from_cmdline, from_cow))
+               return 0;
 
        err = os_stat_file(from_cmdline, &buf1);
        if(err < 0){
                printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
-               return(1);
+               return 0;
        }
        err = os_stat_file(from_cow, &buf2);
        if(err < 0){
                printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
-               return(1);
+               return 1;
        }
        if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
-               return(1);
+               return 0;
 
        printk("Backing file mismatch - \"%s\" requested,\n"
               "\"%s\" specified in COW header of \"%s\"\n",
               from_cmdline, from_cow, cow);
-       return(0);
+       return 1;
 }
 
 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
 {
        unsigned long modtime;
-       long long actual;
+       unsigned long long actual;
        int err;
 
        err = os_file_modtime(file, &modtime);
@@ -1161,7 +1188,7 @@ int read_cow_bitmap(int fd, void *buf, int offset, int len)
        return(0);
 }
 
-int open_ubd_file(char *file, struct openflags *openflags,
+int open_ubd_file(char *file, struct openflags *openflags, int shared,
                  char **backing_file_out, int *bitmap_offset_out,
                  unsigned long *bitmap_len_out, int *data_offset_out,
                  int *create_cow_out)
@@ -1170,27 +1197,34 @@ int open_ubd_file(char *file, struct openflags *openflags,
        unsigned long long size;
        __u32 version, align;
        char *backing_file;
-       int fd, err, sectorsize, same, mode = 0644;
+       int fd, err, sectorsize, asked_switch, mode = 0644;
 
        fd = os_open_file(file, *openflags, mode);
-       if(fd < 0){
-               if((fd == -ENOENT) && (create_cow_out != NULL))
+       if (fd < 0) {
+               if ((fd == -ENOENT) && (create_cow_out != NULL))
                        *create_cow_out = 1;
-                if(!openflags->w ||
-                   ((fd != -EROFS) && (fd != -EACCES))) return(fd);
+                if (!openflags->w ||
+                   ((fd != -EROFS) && (fd != -EACCES)))
+                       return fd;
                openflags->w = 0;
                fd = os_open_file(file, *openflags, mode);
-               if(fd < 0)
-                       return(fd);
+               if (fd < 0)
+                       return fd;
         }
 
-       err = os_lock_file(fd, openflags->w);
-       if(err < 0){
-               printk("Failed to lock '%s', err = %d\n", file, -err);
-               goto out_close;
+       if(shared)
+               printk("Not locking \"%s\" on the host\n", file);
+       else {
+               err = os_lock_file(fd, openflags->w);
+               if(err < 0){
+                       printk("Failed to lock '%s', err = %d\n", file, -err);
+                       goto out_close;
+               }
        }
 
-       if(backing_file_out == NULL) return(fd);
+       /* Succesful return case! */
+       if(backing_file_out == NULL)
+               return(fd);
 
        err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
                              &size, &sectorsize, &align, bitmap_offset_out);
@@ -1199,34 +1233,34 @@ int open_ubd_file(char *file, struct openflags *openflags,
                       "errno = %d\n", file, -err);
                goto out_close;
        }
-       if(err) return(fd);
-
-       if(backing_file_out == NULL) return(fd);
+       if(err)
+               return(fd);
 
-       same = same_backing_files(*backing_file_out, backing_file, file);
+       asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
 
-       if(!same && !backing_file_mismatch(*backing_file_out, size, mtime)){
+       /* Allow switching only if no mismatch. */
+       if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
                printk("Switching backing file to '%s'\n", *backing_file_out);
                err = write_cow_header(file, fd, *backing_file_out,
                                       sectorsize, align, &size);
-               if(err){
+               if (err) {
                        printk("Switch failed, errno = %d\n", -err);
-                       return(err);
+                       goto out_close;
                }
-       }
-       else {
+       } else {
                *backing_file_out = backing_file;
                err = backing_file_mismatch(*backing_file_out, size, mtime);
-               if(err) goto out_close;
+               if (err)
+                       goto out_close;
        }
 
        cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
                  bitmap_len_out, data_offset_out);
 
-        return(fd);
+        return fd;
  out_close:
        os_close_file(fd);
-       return(err);
+       return err;
 }
 
 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
@@ -1236,7 +1270,7 @@ int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
        int err, fd;
 
        flags.c = 1;
-       fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
+       fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
        if(fd < 0){
                err = fd;
                printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
@@ -1370,15 +1404,6 @@ int io_thread(void *arg)
                        printk("io_thread - write failed, fd = %d, err = %d\n",
                               kernel_fd, -n);
        }
-}
 
-/*
- * Overrides for Emacs so that we follow Linus's tabbing style.
- * Emacs will notice this stuff at the end of the file and automatically
- * adjust the settings for this buffer only.  This must remain at the end
- * of the file.
- * ---------------------------------------------------------------------------
- * Local variables:
- * c-file-style: "linux"
- * End:
- */
+       return 0;
+}