netlink: fix for too early rmmod
[safe/jmp/linux-2.6] / drivers / char / mem.c
index bed3503..be832b6 100644 (file)
@@ -5,7 +5,7 @@
  *
  *  Added devfs support. 
  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
- *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
+ *  Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
  */
 
 #include <linux/mm.h>
@@ -26,7 +26,6 @@
 #include <linux/bootmem.h>
 #include <linux/splice.h>
 #include <linux/pfn.h>
-#include <linux/smp_lock.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
 # include <linux/efi.h>
 #endif
 
+static inline unsigned long size_inside_page(unsigned long start,
+                                            unsigned long size)
+{
+       unsigned long sz;
+
+       sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
+
+       return min(sz, size);
+}
+
 /*
  * Architectures vary in how they handle caching for addresses
  * outside of main memory.
@@ -44,7 +53,7 @@ static inline int uncached_access(struct file *file, unsigned long addr)
 {
 #if defined(CONFIG_IA64)
        /*
-        * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
+        * On ia64, we ignore O_DSYNC because we cannot tolerate memory attribute aliases.
         */
        return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
 #elif defined(CONFIG_MIPS)
@@ -57,9 +66,9 @@ static inline int uncached_access(struct file *file, unsigned long addr)
 #else
        /*
         * Accessing memory above the top the kernel knows about or through a file pointer
-        * that was marked O_SYNC will be done non-cached.
+        * that was marked O_DSYNC will be done non-cached.
         */
-       if (file->f_flags & O_SYNC)
+       if (file->f_flags & O_DSYNC)
                return 1;
        return addr >= __pa(high_memory);
 #endif
@@ -127,9 +136,7 @@ static ssize_t read_mem(struct file * file, char __user * buf,
 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
        /* we don't have page 0 mapped on sparc and m68k.. */
        if (p < PAGE_SIZE) {
-               sz = PAGE_SIZE - p;
-               if (sz > count) 
-                       sz = count; 
+               sz = size_inside_page(p, count);
                if (sz > 0) {
                        if (clear_user(buf, sz))
                                return -EFAULT;
@@ -142,15 +149,9 @@ static ssize_t read_mem(struct file * file, char __user * buf,
 #endif
 
        while (count > 0) {
-               /*
-                * Handle first page in case it's not aligned
-                */
-               if (-p & (PAGE_SIZE - 1))
-                       sz = -p & (PAGE_SIZE - 1);
-               else
-                       sz = PAGE_SIZE;
+               unsigned long remaining;
 
-               sz = min_t(unsigned long, sz, count);
+               sz = size_inside_page(p, count);
 
                if (!range_is_allowed(p >> PAGE_SHIFT, count))
                        return -EPERM;
@@ -164,12 +165,10 @@ static ssize_t read_mem(struct file * file, char __user * buf,
                if (!ptr)
                        return -EFAULT;
 
-               if (copy_to_user(buf, ptr, sz)) {
-                       unxlate_dev_mem_ptr(p, ptr);
-                       return -EFAULT;
-               }
-
+               remaining = copy_to_user(buf, ptr, sz);
                unxlate_dev_mem_ptr(p, ptr);
+               if (remaining)
+                       return -EFAULT;
 
                buf += sz;
                p += sz;
@@ -197,9 +196,7 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
        /* we don't have page 0 mapped on sparc and m68k.. */
        if (p < PAGE_SIZE) {
-               unsigned long sz = PAGE_SIZE - p;
-               if (sz > count)
-                       sz = count;
+               sz = size_inside_page(p, count);
                /* Hmm. Do something? */
                buf += sz;
                p += sz;
@@ -209,15 +206,7 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
 #endif
 
        while (count > 0) {
-               /*
-                * Handle first page in case it's not aligned
-                */
-               if (-p & (PAGE_SIZE - 1))
-                       sz = -p & (PAGE_SIZE - 1);
-               else
-                       sz = PAGE_SIZE;
-
-               sz = min_t(unsigned long, sz, count);
+               sz = size_inside_page(p, count);
 
                if (!range_is_allowed(p >> PAGE_SHIFT, sz))
                        return -EPERM;
@@ -235,16 +224,14 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
                }
 
                copied = copy_from_user(ptr, buf, sz);
+               unxlate_dev_mem_ptr(p, ptr);
                if (copied) {
                        written += sz - copied;
-                       unxlate_dev_mem_ptr(p, ptr);
                        if (written)
                                break;
                        return -EFAULT;
                }
 
-               unxlate_dev_mem_ptr(p, ptr);
-
                buf += sz;
                p += sz;
                count -= sz;
@@ -301,7 +288,7 @@ static inline int private_mapping_ok(struct vm_area_struct *vma)
 }
 #endif
 
-static struct vm_operations_struct mmap_mem_ops = {
+static const struct vm_operations_struct mmap_mem_ops = {
 #ifdef CONFIG_HAVE_IOREMAP_PROT
        .access = generic_access_phys
 #endif
@@ -418,27 +405,18 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
                /* we don't have page 0 mapped on sparc and m68k.. */
                if (p < PAGE_SIZE && low_count > 0) {
-                       size_t tmp = PAGE_SIZE - p;
-                       if (tmp > low_count) tmp = low_count;
-                       if (clear_user(buf, tmp))
+                       sz = size_inside_page(p, low_count);
+                       if (clear_user(buf, sz))
                                return -EFAULT;
-                       buf += tmp;
-                       p += tmp;
-                       read += tmp;
-                       low_count -= tmp;
-                       count -= tmp;
+                       buf += sz;
+                       p += sz;
+                       read += sz;
+                       low_count -= sz;
+                       count -= sz;
                }
 #endif
                while (low_count > 0) {
-                       /*
-                        * Handle first page in case it's not aligned
-                        */
-                       if (-p & (PAGE_SIZE - 1))
-                               sz = -p & (PAGE_SIZE - 1);
-                       else
-                               sz = PAGE_SIZE;
-
-                       sz = min_t(unsigned long, sz, low_count);
+                       sz = size_inside_page(p, low_count);
 
                        /*
                         * On ia64 if a page has been mapped somewhere as
@@ -462,21 +440,18 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
                if (!kbuf)
                        return -ENOMEM;
                while (count > 0) {
-                       int len = count;
-
-                       if (len > PAGE_SIZE)
-                               len = PAGE_SIZE;
-                       len = vread(kbuf, (char *)p, len);
-                       if (!len)
+                       sz = size_inside_page(p, count);
+                       sz = vread(kbuf, (char *)p, sz);
+                       if (!sz)
                                break;
-                       if (copy_to_user(buf, kbuf, len)) {
+                       if (copy_to_user(buf, kbuf, sz)) {
                                free_page((unsigned long)kbuf);
                                return -EFAULT;
                        }
-                       count -= len;
-                       buf += len;
-                       read += len;
-                       p += len;
+                       count -= sz;
+                       buf += sz;
+                       read += sz;
+                       p += sz;
                }
                free_page((unsigned long)kbuf);
        }
@@ -486,7 +461,7 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
 
 
 static inline ssize_t
-do_write_kmem(void *p, unsigned long realp, const char __user * buf,
+do_write_kmem(unsigned long p, const char __user *buf,
              size_t count, loff_t *ppos)
 {
        ssize_t written, sz;
@@ -495,14 +470,11 @@ do_write_kmem(void *p, unsigned long realp, const char __user * buf,
        written = 0;
 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
        /* we don't have page 0 mapped on sparc and m68k.. */
-       if (realp < PAGE_SIZE) {
-               unsigned long sz = PAGE_SIZE - realp;
-               if (sz > count)
-                       sz = count;
+       if (p < PAGE_SIZE) {
+               sz = size_inside_page(p, count);
                /* Hmm. Do something? */
                buf += sz;
                p += sz;
-               realp += sz;
                count -= sz;
                written += sz;
        }
@@ -510,22 +482,15 @@ do_write_kmem(void *p, unsigned long realp, const char __user * buf,
 
        while (count > 0) {
                char *ptr;
-               /*
-                * Handle first page in case it's not aligned
-                */
-               if (-realp & (PAGE_SIZE - 1))
-                       sz = -realp & (PAGE_SIZE - 1);
-               else
-                       sz = PAGE_SIZE;
 
-               sz = min_t(unsigned long, sz, count);
+               sz = size_inside_page(p, count);
 
                /*
                 * On ia64 if a page has been mapped somewhere as
                 * uncached, then it must also be accessed uncached
                 * by the kernel or data corruption may occur
                 */
-               ptr = xlate_dev_kmem_ptr(p);
+               ptr = xlate_dev_kmem_ptr((char *)p);
 
                copied = copy_from_user(ptr, buf, sz);
                if (copied) {
@@ -536,7 +501,6 @@ do_write_kmem(void *p, unsigned long realp, const char __user * buf,
                }
                buf += sz;
                p += sz;
-               realp += sz;
                count -= sz;
                written += sz;
        }
@@ -555,19 +519,14 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
        unsigned long p = *ppos;
        ssize_t wrote = 0;
        ssize_t virtr = 0;
-       ssize_t written;
        char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
 
        if (p < (unsigned long) high_memory) {
-
-               wrote = count;
-               if (count > (unsigned long) high_memory - p)
-                       wrote = (unsigned long) high_memory - p;
-
-               written = do_write_kmem((void*)p, p, buf, wrote, ppos);
-               if (written != wrote)
-                       return written;
-               wrote = written;
+               unsigned long to_write = min_t(unsigned long, count,
+                                              (unsigned long)high_memory - p);
+               wrote = do_write_kmem(p, buf, to_write, ppos);
+               if (wrote != to_write)
+                       return wrote;
                p += wrote;
                buf += wrote;
                count -= wrote;
@@ -578,24 +537,21 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
                if (!kbuf)
                        return wrote ? wrote : -ENOMEM;
                while (count > 0) {
-                       int len = count;
-
-                       if (len > PAGE_SIZE)
-                               len = PAGE_SIZE;
-                       if (len) {
-                               written = copy_from_user(kbuf, buf, len);
-                               if (written) {
-                                       if (wrote + virtr)
-                                               break;
-                                       free_page((unsigned long)kbuf);
-                                       return -EFAULT;
-                               }
+                       unsigned long sz = size_inside_page(p, count);
+                       unsigned long n;
+
+                       n = copy_from_user(kbuf, buf, sz);
+                       if (n) {
+                               if (wrote + virtr)
+                                       break;
+                               free_page((unsigned long)kbuf);
+                               return -EFAULT;
                        }
-                       len = vwrite(kbuf, (char *)p, len);
-                       count -= len;
-                       buf += len;
-                       virtr += len;
-                       p += len;
+                       sz = vwrite(kbuf, (char *)p, sz);
+                       count -= sz;
+                       buf += sz;
+                       virtr += sz;
+                       p += sz;
                }
                free_page((unsigned long)kbuf);
        }
@@ -690,7 +646,7 @@ static ssize_t read_zero(struct file * file, char __user * buf,
 
                if (chunk > PAGE_SIZE)
                        chunk = PAGE_SIZE;      /* Just for latency reasons */
-               unwritten = clear_user(buf, chunk);
+               unwritten = __clear_user(buf, chunk);
                written += chunk - unwritten;
                if (unwritten)
                        break;
@@ -866,24 +822,25 @@ static const struct file_operations kmsg_fops = {
 
 static const struct memdev {
        const char *name;
+       mode_t mode;
        const struct file_operations *fops;
        struct backing_dev_info *dev_info;
 } devlist[] = {
-       [ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+        [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
 #ifdef CONFIG_DEVKMEM
-       [ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+        [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
 #endif
-       [ 3] = {"null", &null_fops, NULL },
+        [3] = { "null", 0666, &null_fops, NULL },
 #ifdef CONFIG_DEVPORT
-       [ 4] = { "port", &port_fops, NULL },
+        [4] = { "port", 0, &port_fops, NULL },
 #endif
-       [ 5] = { "zero", &zero_fops, &zero_bdi },
-       [ 6] = { "full", &full_fops, NULL },
-       [ 7] = { "random", &random_fops, NULL },
-       [ 9] = { "urandom", &urandom_fops, NULL },
-       [11] = { "kmsg", &kmsg_fops, NULL },
+        [5] = { "zero", 0666, &zero_fops, &zero_bdi },
+        [7] = { "full", 0666, &full_fops, NULL },
+        [8] = { "random", 0666, &random_fops, NULL },
+        [9] = { "urandom", 0666, &urandom_fops, NULL },
+       [11] = { "kmsg", 0, &kmsg_fops, NULL },
 #ifdef CONFIG_CRASH_DUMP
-       [12] = { "oldmem", &oldmem_fops, NULL },
+       [12] = { "oldmem", 0, &oldmem_fops, NULL },
 #endif
 };
 
@@ -891,35 +848,36 @@ static int memory_open(struct inode *inode, struct file *filp)
 {
        int minor;
        const struct memdev *dev;
-       int ret = -ENXIO;
-
-       lock_kernel();
 
        minor = iminor(inode);
        if (minor >= ARRAY_SIZE(devlist))
-               goto out;
+               return -ENXIO;
 
        dev = &devlist[minor];
        if (!dev->fops)
-               goto out;
+               return -ENXIO;
 
        filp->f_op = dev->fops;
        if (dev->dev_info)
                filp->f_mapping->backing_dev_info = dev->dev_info;
 
        if (dev->fops->open)
-               ret = dev->fops->open(inode, filp);
-       else
-               ret = 0;
-out:
-       unlock_kernel();
-       return ret;
+               return dev->fops->open(inode, filp);
+
+       return 0;
 }
 
 static const struct file_operations memory_fops = {
        .open           = memory_open,
 };
 
+static char *mem_devnode(struct device *dev, mode_t *mode)
+{
+       if (mode && devlist[MINOR(dev->devt)].mode)
+               *mode = devlist[MINOR(dev->devt)].mode;
+       return NULL;
+}
+
 static struct class *mem_class;
 
 static int __init chr_dev_init(void)
@@ -935,6 +893,7 @@ static int __init chr_dev_init(void)
                printk("unable to get major %d for memory devs\n", MEM_MAJOR);
 
        mem_class = class_create(THIS_MODULE, "mem");
+       mem_class->devnode = mem_devnode;
        for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
                if (!devlist[minor].name)
                        continue;