CRISv10 memset library add lineendings to asm
[wrt350n-kernel.git] / fs / proc / proc_net.c
blob131f9c68be5f1d3b2fa036189f7864ca371c63ae
1 /*
2 * linux/fs/proc/net.c
4 * Copyright (C) 2007
6 * Author: Eric Biederman <ebiederm@xmission.com>
8 * proc net directory handling functions
9 */
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>
26 #include "internal.h"
29 struct proc_dir_entry *proc_net_fops_create(struct net *net,
30 const char *name, mode_t mode, const struct file_operations *fops)
32 struct proc_dir_entry *res;
34 res = create_proc_entry(name, mode, net->proc_net);
35 if (res)
36 res->proc_fops = fops;
37 return res;
39 EXPORT_SYMBOL_GPL(proc_net_fops_create);
41 void proc_net_remove(struct net *net, const char *name)
43 remove_proc_entry(name, net->proc_net);
45 EXPORT_SYMBOL_GPL(proc_net_remove);
47 struct net *get_proc_net(const struct inode *inode)
49 return maybe_get_net(PDE_NET(PDE(inode)));
51 EXPORT_SYMBOL_GPL(get_proc_net);
53 static struct proc_dir_entry *proc_net_shadow;
55 static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
56 struct proc_dir_entry *de)
58 struct dentry *shadow = NULL;
59 struct inode *inode;
60 if (!de)
61 goto out;
62 de_get(de);
63 inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
64 if (!inode)
65 goto out_de_put;
66 shadow = d_alloc_name(parent, de->name);
67 if (!shadow)
68 goto out_iput;
69 shadow->d_op = parent->d_op; /* proc_dentry_operations */
70 d_instantiate(shadow, inode);
71 out:
72 return shadow;
73 out_iput:
74 iput(inode);
75 out_de_put:
76 de_put(de);
77 goto out;
80 static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
82 struct net *net = current->nsproxy->net_ns;
83 struct dentry *shadow;
84 shadow = proc_net_shadow_dentry(parent, net->proc_net);
85 if (!shadow)
86 return ERR_PTR(-ENOENT);
88 dput(nd->dentry);
89 /* My dentry count is 1 and that should be enough as the
90 * shadow dentry is thrown away immediately.
92 nd->dentry = shadow;
93 return NULL;
96 static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
97 struct nameidata *nd)
99 struct net *net = current->nsproxy->net_ns;
100 struct dentry *shadow;
102 shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
103 if (!shadow)
104 return ERR_PTR(-ENOENT);
106 dput(nd->dentry);
107 nd->dentry = shadow;
109 return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
112 static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
114 struct net *net = current->nsproxy->net_ns;
115 struct dentry *shadow;
116 int ret;
118 shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
119 if (!shadow)
120 return -ENOENT;
121 ret = shadow->d_inode->i_op->setattr(shadow, iattr);
122 dput(shadow);
123 return ret;
126 static const struct file_operations proc_net_dir_operations = {
127 .read = generic_read_dir,
130 static struct inode_operations proc_net_dir_inode_operations = {
131 .follow_link = proc_net_follow_link,
132 .lookup = proc_net_lookup,
133 .setattr = proc_net_setattr,
136 static __net_init int proc_net_ns_init(struct net *net)
138 struct proc_dir_entry *root, *netd, *net_statd;
139 int err;
141 err = -ENOMEM;
142 root = kzalloc(sizeof(*root), GFP_KERNEL);
143 if (!root)
144 goto out;
146 err = -EEXIST;
147 netd = proc_mkdir("net", root);
148 if (!netd)
149 goto free_root;
151 err = -EEXIST;
152 net_statd = proc_mkdir("stat", netd);
153 if (!net_statd)
154 goto free_net;
156 root->data = net;
157 netd->data = net;
158 net_statd->data = net;
160 net->proc_net_root = root;
161 net->proc_net = netd;
162 net->proc_net_stat = net_statd;
163 err = 0;
165 out:
166 return err;
167 free_net:
168 remove_proc_entry("net", root);
169 free_root:
170 kfree(root);
171 goto out;
174 static __net_exit void proc_net_ns_exit(struct net *net)
176 remove_proc_entry("stat", net->proc_net);
177 remove_proc_entry("net", net->proc_net_root);
178 kfree(net->proc_net_root);
181 static struct pernet_operations __net_initdata proc_net_ns_ops = {
182 .init = proc_net_ns_init,
183 .exit = proc_net_ns_exit,
186 int __init proc_net_init(void)
188 proc_net_shadow = proc_mkdir("net", NULL);
189 proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
190 proc_net_shadow->proc_fops = &proc_net_dir_operations;
192 return register_pernet_subsys(&proc_net_ns_ops);