45dde2f7034d4f5a0479f26c49c06cd3dd6b5e57
[powerpc.git] / fs / proc / proc_net.c
1 /*
2  *  linux/fs/proc/net.c
3  *
4  *  Copyright (C) 2007
5  *
6  *  Author: Eric Biederman <ebiederm@xmission.com>
7  *
8  *  proc net directory handling functions
9  */
10
11 #include <asm/uaccess.h>
12
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>
25
26 #include "internal.h"
27
28
29 struct proc_dir_entry *proc_net_create(struct net *net,
30         const char *name, mode_t mode, get_info_t *get_info)
31 {
32         return create_proc_info_entry(name,mode, net->proc_net, get_info);
33 }
34
35 struct proc_dir_entry *proc_net_fops_create(struct net *net,
36         const char *name, mode_t mode, const struct file_operations *fops)
37 {
38         struct proc_dir_entry *res;
39
40         res = create_proc_entry(name, mode, net->proc_net);
41         if (res)
42                 res->proc_fops = fops;
43         return res;
44 }
45
46 void proc_net_remove(struct net *net, const char *name)
47 {
48         remove_proc_entry(name, net->proc_net);
49 }
50
51
52 static struct proc_dir_entry *proc_net_shadow;
53
54 static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
55                                                 struct proc_dir_entry *de)
56 {
57         struct dentry *shadow = NULL;
58         struct inode *inode;
59         if (!de)
60                 goto out;
61         de_get(de);
62         inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
63         if (!inode)
64                 goto out_de_put;
65         shadow = d_alloc_name(parent, de->name);
66         if (!shadow)
67                 goto out_iput;
68         shadow->d_op = parent->d_op; /* proc_dentry_operations */
69         d_instantiate(shadow, inode);
70 out:
71         return shadow;
72 out_iput:
73         iput(inode);
74 out_de_put:
75         de_put(de);
76         goto out;
77 }
78
79 static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
80 {
81         struct net *net = current->nsproxy->net_ns;
82         struct dentry *shadow;
83         shadow = proc_net_shadow_dentry(parent, net->proc_net);
84         if (!shadow)
85                 return ERR_PTR(-ENOENT);
86
87         dput(nd->dentry);
88         /* My dentry count is 1 and that should be enough as the
89          * shadow dentry is thrown away immediately.
90          */
91         nd->dentry = shadow;
92         return NULL;
93 }
94
95 static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
96                                       struct nameidata *nd)
97 {
98         struct net *net = current->nsproxy->net_ns;
99         struct dentry *shadow;
100
101         shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
102         if (!shadow)
103                 return ERR_PTR(-ENOENT);
104
105         dput(nd->dentry);
106         nd->dentry = shadow;
107
108         return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
109 }
110
111 static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
112 {
113         struct net *net = current->nsproxy->net_ns;
114         struct dentry *shadow;
115         int ret;
116
117         shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
118         if (!shadow)
119                 return -ENOENT;
120         ret = shadow->d_inode->i_op->setattr(shadow, iattr);
121         dput(shadow);
122         return ret;
123 }
124
125 static const struct file_operations proc_net_dir_operations = {
126         .read                   = generic_read_dir,
127 };
128
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,
133 };
134
135 static int proc_net_ns_init(struct net *net)
136 {
137         struct proc_dir_entry *root, *netd, *net_statd;
138         int err;
139
140         err = -ENOMEM;
141         root = kzalloc(sizeof(*root), GFP_KERNEL);
142         if (!root)
143                 goto out;
144
145         err = -EEXIST;
146         netd = proc_mkdir("net", root);
147         if (!netd)
148                 goto free_root;
149
150         err = -EEXIST;
151         net_statd = proc_mkdir("stat", netd);
152         if (!net_statd)
153                 goto free_net;
154
155         root->data = net;
156         netd->data = net;
157         net_statd->data = net;
158
159         net->proc_net_root = root;
160         net->proc_net = netd;
161         net->proc_net_stat = net_statd;
162         err = 0;
163
164 out:
165         return err;
166 free_net:
167         remove_proc_entry("net", root);
168 free_root:
169         kfree(root);
170         goto out;
171 }
172
173 static void proc_net_ns_exit(struct net *net)
174 {
175         remove_proc_entry("stat", net->proc_net);
176         remove_proc_entry("net", net->proc_net_root);
177         kfree(net->proc_net_root);
178 }
179
180 struct pernet_operations proc_net_ns_ops = {
181         .init = proc_net_ns_init,
182         .exit = proc_net_ns_exit,
183 };
184
185 int proc_net_init(void)
186 {
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;
190
191         return register_pernet_subsys(&proc_net_ns_ops);
192 }