Commit 77b14db5 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Linus Torvalds

[PATCH] sysctl: reimplement the sysctl proc support

With this change the sysctl inodes can be cached and nothing needs to be done
when removing a sysctl table.

For a cost of 2K code we will save about 4K of static tables (when we remove
de from ctl_table) and 70K in proc_dir_entries that we will not allocate, or
about half that on a 32bit arch.

The speed feels about the same, even though we can now cache the sysctl
dentries :(

We get the core advantage that we don't need to have a 1 to 1 mapping between
ctl table entries and proc files.  Making it possible to have /proc/sys vary
depending on the namespace you are in.  The currently merged namespaces don't
have an issue here but the network namespace under /proc/sys/net needs to have
different directories depending on which network adapters are visible.  By
simply being a cache different directories being visible depending on who you
are is trivial to implement.

[akpm@osdl.org: fix uninitialised var]
[akpm@osdl.org: fix ARM build]
[bunk@stusta.de: make things static]
Signed-off-by: default avatarEric W. Biederman <ebiederm@xmission.com>
Cc: Russell King <rmk@arm.linux.org.uk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 1ff007eb
...@@ -8,7 +8,7 @@ proc-y := nommu.o task_nommu.o ...@@ -8,7 +8,7 @@ proc-y := nommu.o task_nommu.o
proc-$(CONFIG_MMU) := mmu.o task_mmu.o proc-$(CONFIG_MMU) := mmu.o task_mmu.o
proc-y += inode.o root.o base.o generic.o array.o \ proc-y += inode.o root.o base.o generic.o array.o \
proc_tty.o proc_misc.o proc_tty.o proc_misc.o proc_sysctl.o
proc-$(CONFIG_PROC_KCORE) += kcore.o proc-$(CONFIG_PROC_KCORE) += kcore.o
proc-$(CONFIG_PROC_VMCORE) += vmcore.o proc-$(CONFIG_PROC_VMCORE) += vmcore.o
......
...@@ -32,7 +32,7 @@ static loff_t proc_file_lseek(struct file *, loff_t, int); ...@@ -32,7 +32,7 @@ static loff_t proc_file_lseek(struct file *, loff_t, int);
DEFINE_SPINLOCK(proc_subdir_lock); DEFINE_SPINLOCK(proc_subdir_lock);
int proc_match(int len, const char *name, struct proc_dir_entry *de) static int proc_match(int len, const char *name, struct proc_dir_entry *de)
{ {
if (de->namelen != len) if (de->namelen != len)
return 0; return 0;
......
...@@ -161,6 +161,7 @@ struct inode *proc_get_inode(struct super_block *sb, unsigned int ino, ...@@ -161,6 +161,7 @@ struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
if (!inode) if (!inode)
goto out_ino; goto out_ino;
PROC_I(inode)->fd = 0;
PROC_I(inode)->pde = de; PROC_I(inode)->pde = de;
if (de) { if (de) {
if (de->mode) { if (de->mode) {
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
extern int proc_sys_init(void);
struct vmalloc_info { struct vmalloc_info {
unsigned long used; unsigned long used;
unsigned long largest_chunk; unsigned long largest_chunk;
......
This diff is collapsed.
...@@ -23,10 +23,6 @@ ...@@ -23,10 +23,6 @@
struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver; struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver;
#ifdef CONFIG_SYSCTL
struct proc_dir_entry *proc_sys_root;
#endif
static int proc_get_sb(struct file_system_type *fs_type, static int proc_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt) int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{ {
...@@ -70,9 +66,6 @@ void __init proc_root_init(void) ...@@ -70,9 +66,6 @@ void __init proc_root_init(void)
#ifdef CONFIG_SYSVIPC #ifdef CONFIG_SYSVIPC
proc_mkdir("sysvipc", NULL); proc_mkdir("sysvipc", NULL);
#endif
#ifdef CONFIG_SYSCTL
proc_sys_root = proc_mkdir("sys", NULL);
#endif #endif
proc_root_fs = proc_mkdir("fs", NULL); proc_root_fs = proc_mkdir("fs", NULL);
proc_root_driver = proc_mkdir("driver", NULL); proc_root_driver = proc_mkdir("driver", NULL);
...@@ -86,6 +79,9 @@ void __init proc_root_init(void) ...@@ -86,6 +79,9 @@ void __init proc_root_init(void)
proc_device_tree_init(); proc_device_tree_init();
#endif #endif
proc_bus = proc_mkdir("bus", NULL); proc_bus = proc_mkdir("bus", NULL);
#ifdef CONFIG_SYSCTL
proc_sys_init();
#endif
} }
static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
......
...@@ -113,8 +113,6 @@ extern struct vfsmount *proc_mnt; ...@@ -113,8 +113,6 @@ extern struct vfsmount *proc_mnt;
extern int proc_fill_super(struct super_block *,void *,int); extern int proc_fill_super(struct super_block *,void *,int);
extern struct inode *proc_get_inode(struct super_block *, unsigned int, struct proc_dir_entry *); extern struct inode *proc_get_inode(struct super_block *, unsigned int, struct proc_dir_entry *);
extern int proc_match(int, const char *,struct proc_dir_entry *);
/* /*
* These are generic /proc routines that use the internal * These are generic /proc routines that use the internal
* "struct proc_dir_entry" tree to traverse the filesystem. * "struct proc_dir_entry" tree to traverse the filesystem.
......
...@@ -930,8 +930,6 @@ extern struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev); ...@@ -930,8 +930,6 @@ extern struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev);
extern void sysctl_head_finish(struct ctl_table_header *prev); extern void sysctl_head_finish(struct ctl_table_header *prev);
extern int sysctl_perm(struct ctl_table *table, int op); extern int sysctl_perm(struct ctl_table *table, int op);
extern void sysctl_init(void);
typedef struct ctl_table ctl_table; typedef struct ctl_table ctl_table;
typedef int ctl_handler (ctl_table *table, int __user *name, int nlen, typedef int ctl_handler (ctl_table *table, int __user *name, int nlen,
......
...@@ -86,7 +86,6 @@ extern void init_IRQ(void); ...@@ -86,7 +86,6 @@ extern void init_IRQ(void);
extern void fork_init(unsigned long); extern void fork_init(unsigned long);
extern void mca_init(void); extern void mca_init(void);
extern void sbus_init(void); extern void sbus_init(void);
extern void sysctl_init(void);
extern void signals_init(void); extern void signals_init(void);
extern void pidhash_init(void); extern void pidhash_init(void);
extern void pidmap_init(void); extern void pidmap_init(void);
...@@ -702,9 +701,6 @@ static void __init do_basic_setup(void) ...@@ -702,9 +701,6 @@ static void __init do_basic_setup(void)
usermodehelper_init(); usermodehelper_init();
driver_init(); driver_init();
#ifdef CONFIG_SYSCTL
sysctl_init();
#endif
#ifdef CONFIG_PROC_FS #ifdef CONFIG_PROC_FS
init_irq_proc(); init_irq_proc();
#endif #endif
......
...@@ -159,26 +159,6 @@ int sysctl_legacy_va_layout; ...@@ -159,26 +159,6 @@ int sysctl_legacy_va_layout;
#endif #endif
/* /proc declarations: */
#ifdef CONFIG_PROC_SYSCTL
static ssize_t proc_readsys(struct file *, char __user *, size_t, loff_t *);
static ssize_t proc_writesys(struct file *, const char __user *, size_t, loff_t *);
static int proc_opensys(struct inode *, struct file *);
const struct file_operations proc_sys_file_operations = {
.open = proc_opensys,
.read = proc_readsys,
.write = proc_writesys,
};
extern struct proc_dir_entry *proc_sys_root;
static void register_proc_table(ctl_table *, struct proc_dir_entry *, void *);
static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
#endif
/* The default sysctl tables: */ /* The default sysctl tables: */
static ctl_table root_table[] = { static ctl_table root_table[] = {
...@@ -1106,13 +1086,6 @@ struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev) ...@@ -1106,13 +1086,6 @@ struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
return NULL; return NULL;
} }
void __init sysctl_init(void)
{
#ifdef CONFIG_PROC_SYSCTL
register_proc_table(root_table, proc_sys_root, &root_table_header);
#endif
}
#ifdef CONFIG_SYSCTL_SYSCALL #ifdef CONFIG_SYSCTL_SYSCALL
int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp, int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
void __user *newval, size_t newlen) void __user *newval, size_t newlen)
...@@ -1348,9 +1321,6 @@ struct ctl_table_header *register_sysctl_table(ctl_table * table) ...@@ -1348,9 +1321,6 @@ struct ctl_table_header *register_sysctl_table(ctl_table * table)
spin_lock(&sysctl_lock); spin_lock(&sysctl_lock);
list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry); list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
spin_unlock(&sysctl_lock); spin_unlock(&sysctl_lock);
#ifdef CONFIG_PROC_SYSCTL
register_proc_table(table, proc_sys_root, tmp);
#endif
return tmp; return tmp;
} }
...@@ -1366,9 +1336,6 @@ void unregister_sysctl_table(struct ctl_table_header * header) ...@@ -1366,9 +1336,6 @@ void unregister_sysctl_table(struct ctl_table_header * header)
might_sleep(); might_sleep();
spin_lock(&sysctl_lock); spin_lock(&sysctl_lock);
start_unregistering(header); start_unregistering(header);
#ifdef CONFIG_PROC_SYSCTL
unregister_proc_table(header->ctl_table, proc_sys_root);
#endif
spin_unlock(&sysctl_lock); spin_unlock(&sysctl_lock);
kfree(header); kfree(header);
} }
...@@ -1392,155 +1359,6 @@ void unregister_sysctl_table(struct ctl_table_header * table) ...@@ -1392,155 +1359,6 @@ void unregister_sysctl_table(struct ctl_table_header * table)
#ifdef CONFIG_PROC_SYSCTL #ifdef CONFIG_PROC_SYSCTL
/* Scan the sysctl entries in table and add them all into /proc */
static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
{
struct proc_dir_entry *de;
int len;
mode_t mode;
for (; table->ctl_name || table->procname; table++) {
/* Can't do anything without a proc name. */
if (!table->procname)
continue;
/* Maybe we can't do anything with it... */
if (!table->proc_handler && !table->child) {
printk(KERN_WARNING "SYSCTL: Can't register %s\n",
table->procname);
continue;
}
len = strlen(table->procname);
mode = table->mode;
de = NULL;
if (table->proc_handler)
mode |= S_IFREG;
else {
mode |= S_IFDIR;
for (de = root->subdir; de; de = de->next) {
if (proc_match(len, table->procname, de))
break;
}
/* If the subdir exists already, de is non-NULL */
}
if (!de) {
de = create_proc_entry(table->procname, mode, root);
if (!de)
continue;
de->set = set;
de->data = (void *) table;
if (table->proc_handler)
de->proc_fops = &proc_sys_file_operations;
}
table->de = de;
if (de->mode & S_IFDIR)
register_proc_table(table->child, de, set);
}
}
/*
* Unregister a /proc sysctl table and any subdirectories.
*/
static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
{
struct proc_dir_entry *de;
for (; table->ctl_name || table->procname; table++) {
if (!(de = table->de))
continue;
if (de->mode & S_IFDIR) {
if (!table->child) {
printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
continue;
}
unregister_proc_table(table->child, de);
/* Don't unregister directories which still have entries.. */
if (de->subdir)
continue;
}
/*
* In any case, mark the entry as goner; we'll keep it
* around if it's busy, but we'll know to do nothing with
* its fields. We are under sysctl_lock here.
*/
de->data = NULL;
/* Don't unregister proc entries that are still being used.. */
if (atomic_read(&de->count))
continue;
table->de = NULL;
remove_proc_entry(table->procname, root);
}
}
static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
int op;
struct proc_dir_entry *de = PDE(file->f_path.dentry->d_inode);
struct ctl_table *table;
size_t res;
ssize_t error = -ENOTDIR;
spin_lock(&sysctl_lock);
if (de && de->data && use_table(de->set)) {
/*
* at that point we know that sysctl was not unregistered
* and won't be until we finish
*/
spin_unlock(&sysctl_lock);
table = (struct ctl_table *) de->data;
if (!table || !table->proc_handler)
goto out;
error = -EPERM;
op = (write ? 002 : 004);
if (sysctl_perm(table, op))
goto out;
/* careful: calling conventions are nasty here */
res = count;
error = (*table->proc_handler)(table, write, file,
buf, &res, ppos);
if (!error)
error = res;
out:
spin_lock(&sysctl_lock);
unuse_table(de->set);
}
spin_unlock(&sysctl_lock);
return error;
}
static int proc_opensys(struct inode *inode, struct file *file)
{
if (file->f_mode & FMODE_WRITE) {
/*
* sysctl entries that are not writable,
* are _NOT_ writable, capabilities or not.
*/
if (!(inode->i_mode & S_IWUSR))
return -EPERM;
}
return 0;
}
static ssize_t proc_readsys(struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
return do_rw_proc(0, file, buf, count, ppos);
}
static ssize_t proc_writesys(struct file * file, const char __user * buf,
size_t count, loff_t *ppos)
{
return do_rw_proc(1, file, (char __user *) buf, count, ppos);
}
static int _proc_do_string(void* data, int maxlen, int write, static int _proc_do_string(void* data, int maxlen, int write,
struct file *filp, void __user *buffer, struct file *filp, void __user *buffer,
size_t *lenp, loff_t *ppos) size_t *lenp, loff_t *ppos)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment