net: move and export get_net_ns_by_pid
[safe/jmp/linux-2.6] / net / core / net_namespace.c
index 4488010..ddd2cd2 100644 (file)
@@ -6,6 +6,8 @@
 #include <linux/delay.h>
 #include <linux/sched.h>
 #include <linux/idr.h>
+#include <linux/rculist.h>
+#include <linux/nsproxy.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
 
@@ -115,38 +117,34 @@ static void net_free(struct net *net)
        kmem_cache_free(net_cachep, net);
 }
 
-struct net *copy_net_ns(unsigned long flags, struct net *old_net)
+static struct net *net_create(void)
 {
-       struct net *new_net = NULL;
-       int err;
-
-       if (!(flags & CLONE_NEWNET))
-               return get_net(old_net);
-
-       err = -ENOMEM;
-       new_net = net_alloc();
-       if (!new_net)
-               goto out_err;
+       struct net *net;
+       int rv;
 
+       net = net_alloc();
+       if (!net)
+               return ERR_PTR(-ENOMEM);
        mutex_lock(&net_mutex);
-       err = setup_net(new_net);
-       if (!err) {
+       rv = setup_net(net);
+       if (rv == 0) {
                rtnl_lock();
-               list_add_tail(&new_net->list, &net_namespace_list);
+               list_add_tail_rcu(&net->list, &net_namespace_list);
                rtnl_unlock();
        }
        mutex_unlock(&net_mutex);
+       if (rv < 0) {
+               net_free(net);
+               return ERR_PTR(rv);
+       }
+       return net;
+}
 
-       if (err)
-               goto out_free;
-out:
-       return new_net;
-
-out_free:
-       net_free(new_net);
-out_err:
-       new_net = ERR_PTR(err);
-       goto out;
+struct net *copy_net_ns(unsigned long flags, struct net *old_net)
+{
+       if (!(flags & CLONE_NEWNET))
+               return get_net(old_net);
+       return net_create();
 }
 
 static void cleanup_net(struct work_struct *work)
@@ -160,9 +158,16 @@ static void cleanup_net(struct work_struct *work)
 
        /* Don't let anyone else find us. */
        rtnl_lock();
-       list_del(&net->list);
+       list_del_rcu(&net->list);
        rtnl_unlock();
 
+       /*
+        * Another CPU might be rcu-iterating the list, wait for it.
+        * This needs to be before calling the exit() notifiers, so
+        * the rcu_barrier() below isn't sufficient alone.
+        */
+       synchronize_rcu();
+
        /* Run all of the network namespace exit methods */
        list_for_each_entry_reverse(ops, &pernet_list, list) {
                if (ops->exit)
@@ -197,12 +202,30 @@ struct net *copy_net_ns(unsigned long flags, struct net *old_net)
 }
 #endif
 
+struct net *get_net_ns_by_pid(pid_t pid)
+{
+       struct task_struct *tsk;
+       struct net *net;
+
+       /* Lookup the network namespace */
+       net = ERR_PTR(-ESRCH);
+       rcu_read_lock();
+       tsk = find_task_by_vpid(pid);
+       if (tsk) {
+               struct nsproxy *nsproxy;
+               nsproxy = task_nsproxy(tsk);
+               if (nsproxy)
+                       net = get_net(nsproxy->net_ns);
+       }
+       rcu_read_unlock();
+       return net;
+}
+EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
+
 static int __init net_ns_init(void)
 {
        struct net_generic *ng;
-       int err;
 
-       printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
 #ifdef CONFIG_NET_NS
        net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
                                        SMP_CACHE_BYTES,
@@ -221,15 +244,14 @@ static int __init net_ns_init(void)
        rcu_assign_pointer(init_net.gen, ng);
 
        mutex_lock(&net_mutex);
-       err = setup_net(&init_net);
+       if (setup_net(&init_net))
+               panic("Could not setup the initial network namespace");
 
        rtnl_lock();
-       list_add_tail(&init_net.list, &net_namespace_list);
+       list_add_tail_rcu(&init_net.list, &net_namespace_list);
        rtnl_unlock();
 
        mutex_unlock(&net_mutex);
-       if (err)
-               panic("Could not setup the initial network namespace");
 
        return 0;
 }