~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to net/core/net_namespace.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include <linux/idr.h>
9
9
#include <linux/rculist.h>
10
10
#include <linux/nsproxy.h>
 
11
#include <linux/proc_fs.h>
 
12
#include <linux/file.h>
11
13
#include <net/net_namespace.h>
12
14
#include <net/netns/generic.h>
13
15
 
27
29
 
28
30
#define INITIAL_NET_GEN_PTRS    13 /* +1 for len +2 for rcu_head */
29
31
 
30
 
static void net_generic_release(struct rcu_head *rcu)
31
 
{
32
 
        struct net_generic *ng;
33
 
 
34
 
        ng = container_of(rcu, struct net_generic, rcu);
35
 
        kfree(ng);
36
 
}
37
 
 
38
32
static int net_assign_generic(struct net *net, int id, void *data)
39
33
{
40
34
        struct net_generic *ng, *old_ng;
68
62
        memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
69
63
 
70
64
        rcu_assign_pointer(net->gen, ng);
71
 
        call_rcu(&old_ng->rcu, net_generic_release);
 
65
        kfree_rcu(old_ng, rcu);
72
66
assign:
73
67
        ng->ptr[id - 1] = data;
74
68
        return 0;
134
128
        LIST_HEAD(net_exit_list);
135
129
 
136
130
        atomic_set(&net->count, 1);
 
131
        atomic_set(&net->passive, 1);
137
132
 
138
133
#ifdef NETNS_REFCNT_DEBUG
139
134
        atomic_set(&net->use_count, 0);
216
211
        kmem_cache_free(net_cachep, net);
217
212
}
218
213
 
219
 
static struct net *net_create(void)
 
214
void net_drop_ns(void *p)
 
215
{
 
216
        struct net *ns = p;
 
217
        if (ns && atomic_dec_and_test(&ns->passive))
 
218
                net_free(ns);
 
219
}
 
220
 
 
221
struct net *copy_net_ns(unsigned long flags, struct net *old_net)
220
222
{
221
223
        struct net *net;
222
224
        int rv;
223
225
 
 
226
        if (!(flags & CLONE_NEWNET))
 
227
                return get_net(old_net);
 
228
 
224
229
        net = net_alloc();
225
230
        if (!net)
226
231
                return ERR_PTR(-ENOMEM);
233
238
        }
234
239
        mutex_unlock(&net_mutex);
235
240
        if (rv < 0) {
236
 
                net_free(net);
 
241
                net_drop_ns(net);
237
242
                return ERR_PTR(rv);
238
243
        }
239
244
        return net;
240
245
}
241
246
 
242
 
struct net *copy_net_ns(unsigned long flags, struct net *old_net)
243
 
{
244
 
        if (!(flags & CLONE_NEWNET))
245
 
                return get_net(old_net);
246
 
        return net_create();
247
 
}
248
 
 
249
247
static DEFINE_SPINLOCK(cleanup_list_lock);
250
248
static LIST_HEAD(cleanup_list);  /* Must hold cleanup_list_lock to touch */
251
249
 
296
294
        /* Finally it is safe to free my network namespace structure */
297
295
        list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
298
296
                list_del_init(&net->exit_list);
299
 
                net_free(net);
 
297
                net_drop_ns(net);
300
298
        }
301
299
}
302
300
static DECLARE_WORK(net_cleanup_work, cleanup_net);
314
312
}
315
313
EXPORT_SYMBOL_GPL(__put_net);
316
314
 
 
315
struct net *get_net_ns_by_fd(int fd)
 
316
{
 
317
        struct proc_inode *ei;
 
318
        struct file *file;
 
319
        struct net *net;
 
320
 
 
321
        file = proc_ns_fget(fd);
 
322
        if (IS_ERR(file))
 
323
                return ERR_CAST(file);
 
324
 
 
325
        ei = PROC_I(file->f_dentry->d_inode);
 
326
        if (ei->ns_ops == &netns_operations)
 
327
                net = get_net(ei->ns);
 
328
        else
 
329
                net = ERR_PTR(-EINVAL);
 
330
 
 
331
        fput(file);
 
332
        return net;
 
333
}
 
334
 
317
335
#else
318
336
struct net *copy_net_ns(unsigned long flags, struct net *old_net)
319
337
{
321
339
                return ERR_PTR(-EINVAL);
322
340
        return old_net;
323
341
}
 
342
 
 
343
struct net *get_net_ns_by_fd(int fd)
 
344
{
 
345
        return ERR_PTR(-EINVAL);
 
346
}
324
347
#endif
325
348
 
326
349
struct net *get_net_ns_by_pid(pid_t pid)
573
596
        mutex_unlock(&net_mutex);
574
597
}
575
598
EXPORT_SYMBOL_GPL(unregister_pernet_device);
 
599
 
 
600
#ifdef CONFIG_NET_NS
 
601
static void *netns_get(struct task_struct *task)
 
602
{
 
603
        struct net *net = NULL;
 
604
        struct nsproxy *nsproxy;
 
605
 
 
606
        rcu_read_lock();
 
607
        nsproxy = task_nsproxy(task);
 
608
        if (nsproxy)
 
609
                net = get_net(nsproxy->net_ns);
 
610
        rcu_read_unlock();
 
611
 
 
612
        return net;
 
613
}
 
614
 
 
615
static void netns_put(void *ns)
 
616
{
 
617
        put_net(ns);
 
618
}
 
619
 
 
620
static int netns_install(struct nsproxy *nsproxy, void *ns)
 
621
{
 
622
        put_net(nsproxy->net_ns);
 
623
        nsproxy->net_ns = get_net(ns);
 
624
        return 0;
 
625
}
 
626
 
 
627
const struct proc_ns_operations netns_operations = {
 
628
        .name           = "net",
 
629
        .type           = CLONE_NEWNET,
 
630
        .get            = netns_get,
 
631
        .put            = netns_put,
 
632
        .install        = netns_install,
 
633
};
 
634
#endif