]> err.no Git - linux-2.6/blobdiff - fs/proc/generic.c
Merge git://git.infradead.org/battery-2.6
[linux-2.6] / fs / proc / generic.c
index b638fb500743406a55d3e24c0671882ddec68261..1bdb624357587ae23f31ec3b6f84ef42827cc6f4 100644 (file)
 #include <linux/idr.h>
 #include <linux/namei.h>
 #include <linux/bitops.h>
+#include <linux/spinlock.h>
+#include <linux/completion.h>
 #include <asm/uaccess.h>
 
+#include "internal.h"
+
 static ssize_t proc_file_read(struct file *file, char __user *buf,
                              size_t nbytes, loff_t *ppos);
 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
                               size_t count, loff_t *ppos);
 static loff_t proc_file_lseek(struct file *, loff_t, int);
 
-int proc_match(int len, const char *name, struct proc_dir_entry *de)
+DEFINE_SPINLOCK(proc_subdir_lock);
+
+static int proc_match(int len, const char *name, struct proc_dir_entry *de)
 {
        if (de->namelen != len)
                return 0;
        return !memcmp(name, de->name, len);
 }
 
-static struct file_operations proc_file_operations = {
+static const struct file_operations proc_file_operations = {
        .llseek         = proc_file_lseek,
        .read           = proc_file_read,
        .write          = proc_file_write,
@@ -47,16 +53,28 @@ static ssize_t
 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
               loff_t *ppos)
 {
-       struct inode * inode = file->f_dentry->d_inode;
+       struct inode * inode = file->f_path.dentry->d_inode;
        char    *page;
        ssize_t retval=0;
        int     eof=0;
        ssize_t n, count;
        char    *start;
        struct proc_dir_entry * dp;
+       unsigned long long pos;
+
+       /*
+        * Gaah, please just use "seq_file" instead. The legacy /proc
+        * interfaces cut loff_t down to off_t for reads, and ignore
+        * the offset entirely for writes..
+        */
+       pos = *ppos;
+       if (pos > MAX_NON_LFS)
+               return 0;
+       if (nbytes > MAX_NON_LFS - pos)
+               nbytes = MAX_NON_LFS - pos;
 
        dp = PDE(inode);
-       if (!(page = (char*) __get_free_page(GFP_KERNEL)))
+       if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
                return -ENOMEM;
 
        while ((nbytes > 0) && !eof) {
@@ -186,7 +204,7 @@ static ssize_t
 proc_file_write(struct file *file, const char __user *buffer,
                size_t count, loff_t *ppos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file->f_path.dentry->d_inode;
        struct proc_dir_entry * dp;
        
        dp = PDE(inode);
@@ -202,30 +220,17 @@ proc_file_write(struct file *file, const char __user *buffer,
 static loff_t
 proc_file_lseek(struct file *file, loff_t offset, int orig)
 {
-    lock_kernel();
-
-    switch (orig) {
-    case 0:
-       if (offset < 0)
-           goto out;
-       file->f_pos = offset;
-       unlock_kernel();
-       return(file->f_pos);
-    case 1:
-       if (offset + file->f_pos < 0)
-           goto out;
-       file->f_pos += offset;
-       unlock_kernel();
-       return(file->f_pos);
-    case 2:
-       goto out;
-    default:
-       goto out;
-    }
-
-out:
-    unlock_kernel();
-    return -EINVAL;
+       loff_t retval = -EINVAL;
+       switch (orig) {
+       case 1:
+               offset += file->f_pos;
+       /* fallthrough */
+       case 0:
+               if (offset < 0 || offset > MAX_NON_LFS)
+                       break;
+               file->f_pos = retval = offset;
+       }
+       return retval;
 }
 
 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
@@ -261,7 +266,7 @@ static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
        return 0;
 }
 
-static struct inode_operations proc_file_inode_operations = {
+static const struct inode_operations proc_file_inode_operations = {
        .setattr        = proc_notify_change,
 };
 
@@ -276,7 +281,9 @@ static int xlate_proc_name(const char *name,
        const char              *cp = name, *next;
        struct proc_dir_entry   *de;
        int                     len;
+       int                     rtn = 0;
 
+       spin_lock(&proc_subdir_lock);
        de = &proc_root;
        while (1) {
                next = strchr(cp, '/');
@@ -288,13 +295,17 @@ static int xlate_proc_name(const char *name,
                        if (proc_match(len, cp, de))
                                break;
                }
-               if (!de)
-                       return -ENOENT;
+               if (!de) {
+                       rtn = -ENOENT;
+                       goto out;
+               }
                cp += len + 1;
        }
        *residual = cp;
        *ret = de;
-       return 0;
+out:
+       spin_unlock(&proc_subdir_lock);
+       return rtn;
 }
 
 static DEFINE_IDR(proc_inum_idr);
@@ -347,7 +358,7 @@ static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
        return NULL;
 }
 
-static struct inode_operations proc_link_inode_operations = {
+static const struct inode_operations proc_link_inode_operations = {
        .readlink       = generic_readlink,
        .follow_link    = proc_follow_link,
 };
@@ -379,6 +390,7 @@ struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nam
        int error = -ENOENT;
 
        lock_kernel();
+       spin_lock(&proc_subdir_lock);
        de = PDE(dir);
        if (de) {
                for (de = de->subdir; de ; de = de->next) {
@@ -387,12 +399,16 @@ struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nam
                        if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
                                unsigned int ino = de->low_ino;
 
+                               de_get(de);
+                               spin_unlock(&proc_subdir_lock);
                                error = -EINVAL;
                                inode = proc_get_inode(dir->i_sb, ino, de);
+                               spin_lock(&proc_subdir_lock);
                                break;
                        }
                }
        }
+       spin_unlock(&proc_subdir_lock);
        unlock_kernel();
 
        if (inode) {
@@ -400,6 +416,7 @@ struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nam
                d_add(dentry, inode);
                return NULL;
        }
+       de_put(de);
        return ERR_PTR(error);
 }
 
@@ -418,7 +435,7 @@ int proc_readdir(struct file * filp,
        struct proc_dir_entry * de;
        unsigned int ino;
        int i;
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        int ret = 0;
 
        lock_kernel();
@@ -439,18 +456,20 @@ int proc_readdir(struct file * filp,
                        /* fall through */
                case 1:
                        if (filldir(dirent, "..", 2, i,
-                                   parent_ino(filp->f_dentry),
+                                   parent_ino(filp->f_path.dentry),
                                    DT_DIR) < 0)
                                goto out;
                        i++;
                        filp->f_pos++;
                        /* fall through */
                default:
+                       spin_lock(&proc_subdir_lock);
                        de = de->subdir;
                        i -= 2;
                        for (;;) {
                                if (!de) {
                                        ret = 1;
+                                       spin_unlock(&proc_subdir_lock);
                                        goto out;
                                }
                                if (!i)
@@ -460,12 +479,23 @@ int proc_readdir(struct file * filp,
                        }
 
                        do {
+                               struct proc_dir_entry *next;
+
+                               /* filldir passes info to user space */
+                               de_get(de);
+                               spin_unlock(&proc_subdir_lock);
                                if (filldir(dirent, de->name, de->namelen, filp->f_pos,
-                                           de->low_ino, de->mode >> 12) < 0)
+                                           de->low_ino, de->mode >> 12) < 0) {
+                                       de_put(de);
                                        goto out;
+                               }
+                               spin_lock(&proc_subdir_lock);
                                filp->f_pos++;
-                               de = de->next;
+                               next = de->next;
+                               de_put(de);
+                               de = next;
                        } while (de);
+                       spin_unlock(&proc_subdir_lock);
        }
        ret = 1;
 out:   unlock_kernel();
@@ -477,7 +507,7 @@ out:        unlock_kernel();
  * use the in-memory "struct proc_dir_entry" tree to parse
  * the /proc directory.
  */
-static struct file_operations proc_dir_operations = {
+static const struct file_operations proc_dir_operations = {
        .read                   = generic_read_dir,
        .readdir                = proc_readdir,
 };
@@ -485,7 +515,7 @@ static struct file_operations proc_dir_operations = {
 /*
  * proc directories can do almost nothing..
  */
-static struct inode_operations proc_dir_inode_operations = {
+static const struct inode_operations proc_dir_inode_operations = {
        .lookup         = proc_lookup,
        .getattr        = proc_getattr,
        .setattr        = proc_notify_change,
@@ -499,9 +529,7 @@ static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp
        if (i == 0)
                return -EAGAIN;
        dp->low_ino = i;
-       dp->next = dir->subdir;
-       dp->parent = dir;
-       dir->subdir = dp;
+
        if (S_ISDIR(dp->mode)) {
                if (dp->proc_iops == NULL) {
                        dp->proc_fops = &proc_dir_operations;
@@ -517,6 +545,13 @@ static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp
                if (dp->proc_iops == NULL)
                        dp->proc_iops = &proc_file_inode_operations;
        }
+
+       spin_lock(&proc_subdir_lock);
+       dp->next = dir->subdir;
+       dp->parent = dir;
+       dir->subdir = dp;
+       spin_unlock(&proc_subdir_lock);
+
        return 0;
 }
 
@@ -534,9 +569,9 @@ static void proc_kill_inodes(struct proc_dir_entry *de)
        file_list_lock();
        list_for_each(p, &sb->s_files) {
                struct file * filp = list_entry(p, struct file, f_u.fu_list);
-               struct dentry * dentry = filp->f_dentry;
+               struct dentry * dentry = filp->f_path.dentry;
                struct inode * inode;
-               struct file_operations *fops;
+               const struct file_operations *fops;
 
                if (dentry->d_op != &proc_dentry_operations)
                        continue;
@@ -580,6 +615,9 @@ static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
        ent->namelen = len;
        ent->mode = mode;
        ent->nlink = nlink;
+       ent->pde_users = 0;
+       spin_lock_init(&ent->pde_unload_lock);
+       ent->pde_unload_completion = NULL;
  out:
        return ent;
 }
@@ -616,9 +654,6 @@ struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
 
        ent = proc_create(&parent, name, S_IFDIR | mode, 2);
        if (ent) {
-               ent->proc_fops = &proc_dir_operations;
-               ent->proc_iops = &proc_dir_inode_operations;
-
                if (proc_register(parent, ent) < 0) {
                        kfree(ent);
                        ent = NULL;
@@ -653,10 +688,6 @@ struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
 
        ent = proc_create(&parent,name,mode,nlink);
        if (ent) {
-               if (S_ISDIR(mode)) {
-                       ent->proc_fops = &proc_dir_operations;
-                       ent->proc_iops = &proc_dir_inode_operations;
-               }
                if (proc_register(parent, ent) < 0) {
                        kfree(ent);
                        ent = NULL;
@@ -693,15 +724,43 @@ void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
        if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
                goto out;
        len = strlen(fn);
+
+       spin_lock(&proc_subdir_lock);
        for (p = &parent->subdir; *p; p=&(*p)->next ) {
                if (!proc_match(len, fn, *p))
                        continue;
                de = *p;
                *p = de->next;
                de->next = NULL;
+
+               spin_lock(&de->pde_unload_lock);
+               /*
+                * Stop accepting new callers into module. If you're
+                * dynamically allocating ->proc_fops, save a pointer somewhere.
+                */
+               de->proc_fops = NULL;
+               /* Wait until all existing callers into module are done. */
+               if (de->pde_users > 0) {
+                       DECLARE_COMPLETION_ONSTACK(c);
+
+                       if (!de->pde_unload_completion)
+                               de->pde_unload_completion = &c;
+
+                       spin_unlock(&de->pde_unload_lock);
+                       spin_unlock(&proc_subdir_lock);
+
+                       wait_for_completion(de->pde_unload_completion);
+
+                       spin_lock(&proc_subdir_lock);
+                       goto continue_removing;
+               }
+               spin_unlock(&de->pde_unload_lock);
+
+continue_removing:
                if (S_ISDIR(de->mode))
                        parent->nlink--;
-               proc_kill_inodes(de);
+               if (!S_ISREG(de->mode))
+                       proc_kill_inodes(de);
                de->nlink = 0;
                WARN_ON(de->subdir);
                if (!atomic_read(&de->count))
@@ -713,6 +772,7 @@ void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
                }
                break;
        }
+       spin_unlock(&proc_subdir_lock);
 out:
        return;
 }