net: move and export get_net_ns_by_pid
[safe/jmp/linux-2.6] / net / core / net_namespace.c
index b0767ab..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>
 
@@ -63,10 +65,6 @@ out_undo:
        goto out;
 }
 
-#ifdef CONFIG_NET_NS
-static struct kmem_cache *net_cachep;
-static struct workqueue_struct *netns_wq;
-
 static struct net_generic *net_alloc_generic(void)
 {
        struct net_generic *ng;
@@ -80,6 +78,10 @@ static struct net_generic *net_alloc_generic(void)
        return ng;
 }
 
+#ifdef CONFIG_NET_NS
+static struct kmem_cache *net_cachep;
+static struct workqueue_struct *netns_wq;
+
 static struct net *net_alloc(void)
 {
        struct net *net = NULL;
@@ -115,41 +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;
-
-       get_net(old_net);
-
-       if (!(flags & CLONE_NEWNET))
-               return 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:
-       put_net(old_net);
-       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)
@@ -157,18 +152,22 @@ static void cleanup_net(struct work_struct *work)
        struct pernet_operations *ops;
        struct net *net;
 
-       /* Be very certain incoming network packets will not find us */
-       rcu_barrier();
-
        net = container_of(work, struct net, work);
 
        mutex_lock(&net_mutex);
 
        /* 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)
@@ -203,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,
@@ -227,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;
 }