6 * Author: Eric Biederman <ebiederm@xmission.com>
8 * proc net directory handling functions
11 #include <asm/uaccess.h>
13 #include <linux/errno.h>
14 #include <linux/time.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/bitops.h>
21 #include <linux/smp_lock.h>
22 #include <linux/mount.h>
23 #include <linux/nsproxy.h>
24 #include <net/net_namespace.h>
29 struct proc_dir_entry *proc_net_create(struct net *net,
30 const char *name, mode_t mode, get_info_t *get_info)
32 return create_proc_info_entry(name,mode, net->proc_net, get_info);
34 EXPORT_SYMBOL_GPL(proc_net_create);
36 struct proc_dir_entry *proc_net_fops_create(struct net *net,
37 const char *name, mode_t mode, const struct file_operations *fops)
39 struct proc_dir_entry *res;
41 res = create_proc_entry(name, mode, net->proc_net);
43 res->proc_fops = fops;
46 EXPORT_SYMBOL_GPL(proc_net_fops_create);
48 void proc_net_remove(struct net *net, const char *name)
50 remove_proc_entry(name, net->proc_net);
52 EXPORT_SYMBOL_GPL(proc_net_remove);
54 static struct proc_dir_entry *proc_net_shadow;
56 static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
57 struct proc_dir_entry *de)
59 struct dentry *shadow = NULL;
64 inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
67 shadow = d_alloc_name(parent, de->name);
70 shadow->d_op = parent->d_op; /* proc_dentry_operations */
71 d_instantiate(shadow, inode);
81 static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
83 struct net *net = current->nsproxy->net_ns;
84 struct dentry *shadow;
85 shadow = proc_net_shadow_dentry(parent, net->proc_net);
87 return ERR_PTR(-ENOENT);
90 /* My dentry count is 1 and that should be enough as the
91 * shadow dentry is thrown away immediately.
97 static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
100 struct net *net = current->nsproxy->net_ns;
101 struct dentry *shadow;
103 shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
105 return ERR_PTR(-ENOENT);
110 return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
113 static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
115 struct net *net = current->nsproxy->net_ns;
116 struct dentry *shadow;
119 shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
122 ret = shadow->d_inode->i_op->setattr(shadow, iattr);
127 static const struct file_operations proc_net_dir_operations = {
128 .read = generic_read_dir,
131 static struct inode_operations proc_net_dir_inode_operations = {
132 .follow_link = proc_net_follow_link,
133 .lookup = proc_net_lookup,
134 .setattr = proc_net_setattr,
137 static int proc_net_ns_init(struct net *net)
139 struct proc_dir_entry *root, *netd, *net_statd;
143 root = kzalloc(sizeof(*root), GFP_KERNEL);
148 netd = proc_mkdir("net", root);
153 net_statd = proc_mkdir("stat", netd);
159 net_statd->data = net;
161 net->proc_net_root = root;
162 net->proc_net = netd;
163 net->proc_net_stat = net_statd;
169 remove_proc_entry("net", root);
175 static void proc_net_ns_exit(struct net *net)
177 remove_proc_entry("stat", net->proc_net);
178 remove_proc_entry("net", net->proc_net_root);
179 kfree(net->proc_net_root);
182 struct pernet_operations proc_net_ns_ops = {
183 .init = proc_net_ns_init,
184 .exit = proc_net_ns_exit,
187 int proc_net_init(void)
189 proc_net_shadow = proc_mkdir("net", NULL);
190 proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
191 proc_net_shadow->proc_fops = &proc_net_dir_operations;
193 return register_pernet_subsys(&proc_net_ns_ops);