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);
35 struct proc_dir_entry *proc_net_fops_create(struct net *net,
36 const char *name, mode_t mode, const struct file_operations *fops)
38 struct proc_dir_entry *res;
40 res = create_proc_entry(name, mode, net->proc_net);
42 res->proc_fops = fops;
46 void proc_net_remove(struct net *net, const char *name)
48 remove_proc_entry(name, net->proc_net);
52 static struct proc_dir_entry *proc_net_shadow;
54 static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
55 struct proc_dir_entry *de)
57 struct dentry *shadow = NULL;
62 inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
65 shadow = d_alloc_name(parent, de->name);
68 shadow->d_op = parent->d_op; /* proc_dentry_operations */
69 d_instantiate(shadow, inode);
79 static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
81 struct net *net = current->nsproxy->net_ns;
82 struct dentry *shadow;
83 shadow = proc_net_shadow_dentry(parent, net->proc_net);
85 return ERR_PTR(-ENOENT);
88 /* My dentry count is 1 and that should be enough as the
89 * shadow dentry is thrown away immediately.
95 static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
98 struct net *net = current->nsproxy->net_ns;
99 struct dentry *shadow;
101 shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
103 return ERR_PTR(-ENOENT);
108 return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
111 static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
113 struct net *net = current->nsproxy->net_ns;
114 struct dentry *shadow;
117 shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
120 ret = shadow->d_inode->i_op->setattr(shadow, iattr);
125 static const struct file_operations proc_net_dir_operations = {
126 .read = generic_read_dir,
129 static struct inode_operations proc_net_dir_inode_operations = {
130 .follow_link = proc_net_follow_link,
131 .lookup = proc_net_lookup,
132 .setattr = proc_net_setattr,
135 static int proc_net_ns_init(struct net *net)
137 struct proc_dir_entry *root, *netd, *net_statd;
141 root = kzalloc(sizeof(*root), GFP_KERNEL);
146 netd = proc_mkdir("net", root);
151 net_statd = proc_mkdir("stat", netd);
157 net_statd->data = net;
159 net->proc_net_root = root;
160 net->proc_net = netd;
161 net->proc_net_stat = net_statd;
167 remove_proc_entry("net", root);
173 static void proc_net_ns_exit(struct net *net)
175 remove_proc_entry("stat", net->proc_net);
176 remove_proc_entry("net", net->proc_net_root);
177 kfree(net->proc_net_root);
180 struct pernet_operations proc_net_ns_ops = {
181 .init = proc_net_ns_init,
182 .exit = proc_net_ns_exit,
185 int proc_net_init(void)
187 proc_net_shadow = proc_mkdir("net", NULL);
188 proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
189 proc_net_shadow->proc_fops = &proc_net_dir_operations;
191 return register_pernet_subsys(&proc_net_ns_ops);