kernel: explicitly include required header files under kernel/
[safe/jmp/linux-2.6] / kernel / ns_cgroup.c
1 /*
2  * ns_cgroup.c - namespace cgroup subsystem
3  *
4  * Copyright 2006, 2007 IBM Corp
5  */
6
7 #include <linux/module.h>
8 #include <linux/cgroup.h>
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11
12 struct ns_cgroup {
13         struct cgroup_subsys_state css;
14         spinlock_t lock;
15 };
16
17 struct cgroup_subsys ns_subsys;
18
19 static inline struct ns_cgroup *cgroup_to_ns(
20                 struct cgroup *cgroup)
21 {
22         return container_of(cgroup_subsys_state(cgroup, ns_subsys_id),
23                             struct ns_cgroup, css);
24 }
25
26 int ns_cgroup_clone(struct task_struct *task)
27 {
28         return cgroup_clone(task, &ns_subsys);
29 }
30
31 /*
32  * Rules:
33  *   1. you can only enter a cgroup which is a child of your current
34  *     cgroup
35  *   2. you can only place another process into a cgroup if
36  *     a. you have CAP_SYS_ADMIN
37  *     b. your cgroup is an ancestor of task's destination cgroup
38  *       (hence either you are in the same cgroup as task, or in an
39  *        ancestor cgroup thereof)
40  */
41 static int ns_can_attach(struct cgroup_subsys *ss,
42                 struct cgroup *new_cgroup, struct task_struct *task)
43 {
44         struct cgroup *orig;
45
46         if (current != task) {
47                 if (!capable(CAP_SYS_ADMIN))
48                         return -EPERM;
49
50                 if (!cgroup_is_descendant(new_cgroup))
51                         return -EPERM;
52         }
53
54         if (atomic_read(&new_cgroup->count) != 0)
55                 return -EPERM;
56
57         orig = task_cgroup(task, ns_subsys_id);
58         if (orig && orig != new_cgroup->parent)
59                 return -EPERM;
60
61         return 0;
62 }
63
64 /*
65  * Rules: you can only create a cgroup if
66  *     1. you are capable(CAP_SYS_ADMIN)
67  *     2. the target cgroup is a descendant of your own cgroup
68  */
69 static struct cgroup_subsys_state *ns_create(struct cgroup_subsys *ss,
70                                                 struct cgroup *cgroup)
71 {
72         struct ns_cgroup *ns_cgroup;
73
74         if (!capable(CAP_SYS_ADMIN))
75                 return ERR_PTR(-EPERM);
76         if (!cgroup_is_descendant(cgroup))
77                 return ERR_PTR(-EPERM);
78
79         ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);
80         if (!ns_cgroup)
81                 return ERR_PTR(-ENOMEM);
82         spin_lock_init(&ns_cgroup->lock);
83         return &ns_cgroup->css;
84 }
85
86 static void ns_destroy(struct cgroup_subsys *ss,
87                         struct cgroup *cgroup)
88 {
89         struct ns_cgroup *ns_cgroup;
90
91         ns_cgroup = cgroup_to_ns(cgroup);
92         kfree(ns_cgroup);
93 }
94
95 struct cgroup_subsys ns_subsys = {
96         .name = "ns",
97         .can_attach = ns_can_attach,
98         .create = ns_create,
99         .destroy  = ns_destroy,
100         .subsys_id = ns_subsys_id,
101 };