]> Pileus Git - ~andy/linux/blobdiff - fs/proc/proc_sysctl.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/sysctl
[~andy/linux] / fs / proc / proc_sysctl.c
index 722ec116208dcbdfb9e750b83c4bd83cddcf0d90..21d836f40292dba85a47b61aed4b0b30028ec41f 100644 (file)
@@ -6,7 +6,9 @@
 #include <linux/poll.h>
 #include <linux/proc_fs.h>
 #include <linux/security.h>
+#include <linux/sched.h>
 #include <linux/namei.h>
+#include <linux/mm.h>
 #include <linux/module.h>
 #include "internal.h"
 
@@ -73,6 +75,7 @@ static int namecmp(const char *name1, int len1, const char *name2, int len2)
        return cmp;
 }
 
+/* Called under sysctl_lock */
 static struct ctl_table *find_entry(struct ctl_table_header **phead,
        struct ctl_dir *dir, const char *name, int namelen)
 {
@@ -450,10 +453,12 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
        if (!p)
                goto out;
 
-       ret = sysctl_follow_link(&h, &p, current->nsproxy);
-       err = ERR_PTR(ret);
-       if (ret)
-               goto out;
+       if (S_ISLNK(p->mode)) {
+               ret = sysctl_follow_link(&h, &p, current->nsproxy);
+               err = ERR_PTR(ret);
+               if (ret)
+                       goto out;
+       }
 
        err = ERR_PTR(-ENOMEM);
        inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
@@ -522,20 +527,32 @@ static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
 
 static int proc_sys_open(struct inode *inode, struct file *filp)
 {
+       struct ctl_table_header *head = grab_header(inode);
        struct ctl_table *table = PROC_I(inode)->sysctl_entry;
 
+       /* sysctl was unregistered */
+       if (IS_ERR(head))
+               return PTR_ERR(head);
+
        if (table->poll)
                filp->private_data = proc_sys_poll_event(table->poll);
 
+       sysctl_head_finish(head);
+
        return 0;
 }
 
 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
 {
        struct inode *inode = filp->f_path.dentry->d_inode;
+       struct ctl_table_header *head = grab_header(inode);
        struct ctl_table *table = PROC_I(inode)->sysctl_entry;
-       unsigned long event = (unsigned long)filp->private_data;
        unsigned int ret = DEFAULT_POLLMASK;
+       unsigned long event;
+
+       /* sysctl was unregistered */
+       if (IS_ERR(head))
+               return POLLERR | POLLHUP;
 
        if (!table->proc_handler)
                goto out;
@@ -543,6 +560,7 @@ static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
        if (!table->poll)
                goto out;
 
+       event = (unsigned long)filp->private_data;
        poll_wait(filp, &table->poll->wait, wait);
 
        if (event != atomic_read(&table->poll->event)) {
@@ -551,6 +569,8 @@ static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
        }
 
 out:
+       sysctl_head_finish(head);
+
        return ret;
 }
 
@@ -600,10 +620,12 @@ static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
        int err, ret = 0;
        head = sysctl_head_grab(head);
 
-       /* It is not an error if we can not follow the link ignore it */
-       err = sysctl_follow_link(&head, &table, current->nsproxy);
-       if (err)
-               goto out;
+       if (S_ISLNK(table->mode)) {
+               /* It is not an error if we can not follow the link ignore it */
+               err = sysctl_follow_link(&head, &table, current->nsproxy);
+               if (err)
+                       goto out;
+       }
 
        ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
 out:
@@ -864,6 +886,18 @@ static struct ctl_dir *new_dir(struct ctl_table_set *set,
        return new;
 }
 
+/**
+ * get_subdir - find or create a subdir with the specified name.
+ * @dir:  Directory to create the subdirectory in
+ * @name: The name of the subdirectory to find or create
+ * @namelen: The length of name
+ *
+ * Takes a directory with an elevated reference count so we know that
+ * if we drop the lock the directory will not go away.  Upon success
+ * the reference is moved from @dir to the returned subdirectory.
+ * Upon error an error code is returned and the reference on @dir is
+ * simply dropped.
+ */
 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
                                  const char *name, int namelen)
 {
@@ -885,12 +919,14 @@ static struct ctl_dir *get_subdir(struct ctl_dir *dir,
        if (!new)
                goto failed;
 
+       /* Was the subdir added while we dropped the lock? */
        subdir = find_subdir(dir, name, namelen);
        if (!IS_ERR(subdir))
                goto found;
        if (PTR_ERR(subdir) != -ENOENT)
                goto failed;
 
+       /* Nope.  Use the our freshly made directory entry. */
        err = insert_header(dir, &new->header);
        subdir = ERR_PTR(err);
        if (err)
@@ -935,10 +971,6 @@ static int sysctl_follow_link(struct ctl_table_header **phead,
        struct ctl_dir *dir;
        int ret;
 
-       /* Get out quickly if not a link */
-       if (!S_ISLNK((*pentry)->mode))
-               return 0;
-
        ret = 0;
        spin_lock(&sysctl_lock);
        root = (*pentry)->data;
@@ -1190,6 +1222,7 @@ struct ctl_table_header *__register_sysctl_table(
 
        spin_lock(&sysctl_lock);
        dir = &set->dir;
+       /* Reference moved down the diretory tree get_subdir */
        dir->header.nreg++;
        spin_unlock(&sysctl_lock);