[MTD] [NOR] Fixup for incorrect CFI data in Spansion S29GL064/32N flash chips
[linux-2.6/btrfs-unstable.git] / net / core / net_namespace.c
blob72b4c184dd84a4d583a20ef0ed5dbd06618f5757
1 #include <linux/workqueue.h>
2 #include <linux/rtnetlink.h>
3 #include <linux/cache.h>
4 #include <linux/slab.h>
5 #include <linux/list.h>
6 #include <linux/delay.h>
7 #include <linux/sched.h>
8 #include <linux/idr.h>
9 #include <net/net_namespace.h>
10 #include <net/netns/generic.h>
13 * Our network namespace constructor/destructor lists
16 static LIST_HEAD(pernet_list);
17 static struct list_head *first_device = &pernet_list;
18 static DEFINE_MUTEX(net_mutex);
20 LIST_HEAD(net_namespace_list);
22 struct net init_net;
23 EXPORT_SYMBOL(init_net);
25 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
28 * setup_net runs the initializers for the network namespace object.
30 static __net_init int setup_net(struct net *net)
32 /* Must be called with net_mutex held */
33 struct pernet_operations *ops;
34 int error;
35 struct net_generic *ng;
37 atomic_set(&net->count, 1);
38 #ifdef NETNS_REFCNT_DEBUG
39 atomic_set(&net->use_count, 0);
40 #endif
42 error = -ENOMEM;
43 ng = kzalloc(sizeof(struct net_generic) +
44 INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL);
45 if (ng == NULL)
46 goto out;
48 ng->len = INITIAL_NET_GEN_PTRS;
49 INIT_RCU_HEAD(&ng->rcu);
50 rcu_assign_pointer(net->gen, ng);
52 error = 0;
53 list_for_each_entry(ops, &pernet_list, list) {
54 if (ops->init) {
55 error = ops->init(net);
56 if (error < 0)
57 goto out_undo;
60 out:
61 return error;
63 out_undo:
64 /* Walk through the list backwards calling the exit functions
65 * for the pernet modules whose init functions did not fail.
67 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
68 if (ops->exit)
69 ops->exit(net);
72 rcu_barrier();
73 kfree(ng);
74 goto out;
77 #ifdef CONFIG_NET_NS
78 static struct kmem_cache *net_cachep;
79 static struct workqueue_struct *netns_wq;
81 static struct net *net_alloc(void)
83 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
86 static void net_free(struct net *net)
88 if (!net)
89 return;
91 #ifdef NETNS_REFCNT_DEBUG
92 if (unlikely(atomic_read(&net->use_count) != 0)) {
93 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
94 atomic_read(&net->use_count));
95 return;
97 #endif
99 kmem_cache_free(net_cachep, net);
102 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
104 struct net *new_net = NULL;
105 int err;
107 get_net(old_net);
109 if (!(flags & CLONE_NEWNET))
110 return old_net;
112 err = -ENOMEM;
113 new_net = net_alloc();
114 if (!new_net)
115 goto out;
117 mutex_lock(&net_mutex);
118 err = setup_net(new_net);
119 if (err)
120 goto out_unlock;
122 rtnl_lock();
123 list_add_tail(&new_net->list, &net_namespace_list);
124 rtnl_unlock();
127 out_unlock:
128 mutex_unlock(&net_mutex);
129 out:
130 put_net(old_net);
131 if (err) {
132 net_free(new_net);
133 new_net = ERR_PTR(err);
135 return new_net;
138 static void cleanup_net(struct work_struct *work)
140 struct pernet_operations *ops;
141 struct net *net;
143 net = container_of(work, struct net, work);
145 mutex_lock(&net_mutex);
147 /* Don't let anyone else find us. */
148 rtnl_lock();
149 list_del(&net->list);
150 rtnl_unlock();
152 /* Run all of the network namespace exit methods */
153 list_for_each_entry_reverse(ops, &pernet_list, list) {
154 if (ops->exit)
155 ops->exit(net);
158 mutex_unlock(&net_mutex);
160 /* Ensure there are no outstanding rcu callbacks using this
161 * network namespace.
163 rcu_barrier();
165 /* Finally it is safe to free my network namespace structure */
166 net_free(net);
169 void __put_net(struct net *net)
171 /* Cleanup the network namespace in process context */
172 INIT_WORK(&net->work, cleanup_net);
173 queue_work(netns_wq, &net->work);
175 EXPORT_SYMBOL_GPL(__put_net);
177 #else
178 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
180 if (flags & CLONE_NEWNET)
181 return ERR_PTR(-EINVAL);
182 return old_net;
184 #endif
186 static int __init net_ns_init(void)
188 int err;
190 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
191 #ifdef CONFIG_NET_NS
192 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
193 SMP_CACHE_BYTES,
194 SLAB_PANIC, NULL);
196 /* Create workqueue for cleanup */
197 netns_wq = create_singlethread_workqueue("netns");
198 if (!netns_wq)
199 panic("Could not create netns workq");
200 #endif
202 mutex_lock(&net_mutex);
203 err = setup_net(&init_net);
205 rtnl_lock();
206 list_add_tail(&init_net.list, &net_namespace_list);
207 rtnl_unlock();
209 mutex_unlock(&net_mutex);
210 if (err)
211 panic("Could not setup the initial network namespace");
213 return 0;
216 pure_initcall(net_ns_init);
218 #ifdef CONFIG_NET_NS
219 static int register_pernet_operations(struct list_head *list,
220 struct pernet_operations *ops)
222 struct net *net, *undo_net;
223 int error;
225 list_add_tail(&ops->list, list);
226 if (ops->init) {
227 for_each_net(net) {
228 error = ops->init(net);
229 if (error)
230 goto out_undo;
233 return 0;
235 out_undo:
236 /* If I have an error cleanup all namespaces I initialized */
237 list_del(&ops->list);
238 if (ops->exit) {
239 for_each_net(undo_net) {
240 if (undo_net == net)
241 goto undone;
242 ops->exit(undo_net);
245 undone:
246 return error;
249 static void unregister_pernet_operations(struct pernet_operations *ops)
251 struct net *net;
253 list_del(&ops->list);
254 if (ops->exit)
255 for_each_net(net)
256 ops->exit(net);
259 #else
261 static int register_pernet_operations(struct list_head *list,
262 struct pernet_operations *ops)
264 if (ops->init == NULL)
265 return 0;
266 return ops->init(&init_net);
269 static void unregister_pernet_operations(struct pernet_operations *ops)
271 if (ops->exit)
272 ops->exit(&init_net);
274 #endif
276 static DEFINE_IDA(net_generic_ids);
279 * register_pernet_subsys - register a network namespace subsystem
280 * @ops: pernet operations structure for the subsystem
282 * Register a subsystem which has init and exit functions
283 * that are called when network namespaces are created and
284 * destroyed respectively.
286 * When registered all network namespace init functions are
287 * called for every existing network namespace. Allowing kernel
288 * modules to have a race free view of the set of network namespaces.
290 * When a new network namespace is created all of the init
291 * methods are called in the order in which they were registered.
293 * When a network namespace is destroyed all of the exit methods
294 * are called in the reverse of the order with which they were
295 * registered.
297 int register_pernet_subsys(struct pernet_operations *ops)
299 int error;
300 mutex_lock(&net_mutex);
301 error = register_pernet_operations(first_device, ops);
302 mutex_unlock(&net_mutex);
303 return error;
305 EXPORT_SYMBOL_GPL(register_pernet_subsys);
308 * unregister_pernet_subsys - unregister a network namespace subsystem
309 * @ops: pernet operations structure to manipulate
311 * Remove the pernet operations structure from the list to be
312 * used when network namespaces are created or destroyed. In
313 * addition run the exit method for all existing network
314 * namespaces.
316 void unregister_pernet_subsys(struct pernet_operations *module)
318 mutex_lock(&net_mutex);
319 unregister_pernet_operations(module);
320 mutex_unlock(&net_mutex);
322 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
325 * register_pernet_device - register a network namespace device
326 * @ops: pernet operations structure for the subsystem
328 * Register a device which has init and exit functions
329 * that are called when network namespaces are created and
330 * destroyed respectively.
332 * When registered all network namespace init functions are
333 * called for every existing network namespace. Allowing kernel
334 * modules to have a race free view of the set of network namespaces.
336 * When a new network namespace is created all of the init
337 * methods are called in the order in which they were registered.
339 * When a network namespace is destroyed all of the exit methods
340 * are called in the reverse of the order with which they were
341 * registered.
343 int register_pernet_device(struct pernet_operations *ops)
345 int error;
346 mutex_lock(&net_mutex);
347 error = register_pernet_operations(&pernet_list, ops);
348 if (!error && (first_device == &pernet_list))
349 first_device = &ops->list;
350 mutex_unlock(&net_mutex);
351 return error;
353 EXPORT_SYMBOL_GPL(register_pernet_device);
355 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
357 int error;
358 mutex_lock(&net_mutex);
359 again:
360 error = ida_get_new_above(&net_generic_ids, 1, id);
361 if (error) {
362 if (error == -EAGAIN) {
363 ida_pre_get(&net_generic_ids, GFP_KERNEL);
364 goto again;
366 goto out;
368 error = register_pernet_operations(&pernet_list, ops);
369 if (error)
370 ida_remove(&net_generic_ids, *id);
371 else if (first_device == &pernet_list)
372 first_device = &ops->list;
373 out:
374 mutex_unlock(&net_mutex);
375 return error;
377 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
380 * unregister_pernet_device - unregister a network namespace netdevice
381 * @ops: pernet operations structure to manipulate
383 * Remove the pernet operations structure from the list to be
384 * used when network namespaces are created or destroyed. In
385 * addition run the exit method for all existing network
386 * namespaces.
388 void unregister_pernet_device(struct pernet_operations *ops)
390 mutex_lock(&net_mutex);
391 if (&ops->list == first_device)
392 first_device = first_device->next;
393 unregister_pernet_operations(ops);
394 mutex_unlock(&net_mutex);
396 EXPORT_SYMBOL_GPL(unregister_pernet_device);
398 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
400 mutex_lock(&net_mutex);
401 if (&ops->list == first_device)
402 first_device = first_device->next;
403 unregister_pernet_operations(ops);
404 ida_remove(&net_generic_ids, id);
405 mutex_unlock(&net_mutex);
407 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
409 static void net_generic_release(struct rcu_head *rcu)
411 struct net_generic *ng;
413 ng = container_of(rcu, struct net_generic, rcu);
414 kfree(ng);
417 int net_assign_generic(struct net *net, int id, void *data)
419 struct net_generic *ng, *old_ng;
421 BUG_ON(!mutex_is_locked(&net_mutex));
422 BUG_ON(id == 0);
424 ng = old_ng = net->gen;
425 if (old_ng->len >= id)
426 goto assign;
428 ng = kzalloc(sizeof(struct net_generic) +
429 id * sizeof(void *), GFP_KERNEL);
430 if (ng == NULL)
431 return -ENOMEM;
434 * Some synchronisation notes:
436 * The net_generic explores the net->gen array inside rcu
437 * read section. Besides once set the net->gen->ptr[x]
438 * pointer never changes (see rules in netns/generic.h).
440 * That said, we simply duplicate this array and schedule
441 * the old copy for kfree after a grace period.
444 ng->len = id;
445 INIT_RCU_HEAD(&ng->rcu);
446 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
448 rcu_assign_pointer(net->gen, ng);
449 call_rcu(&old_ng->rcu, net_generic_release);
450 assign:
451 ng->ptr[id - 1] = data;
452 return 0;
454 EXPORT_SYMBOL_GPL(net_assign_generic);