Task Control Groups: add procfs interface
[safe/jmp/linux-2.6] / include / linux / cgroup.h
1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
3 /*
4  *  cgroup interface
5  *
6  *  Copyright (C) 2003 BULL SA
7  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
8  *
9  */
10
11 #include <linux/sched.h>
12 #include <linux/kref.h>
13 #include <linux/cpumask.h>
14 #include <linux/nodemask.h>
15 #include <linux/rcupdate.h>
16
17 #ifdef CONFIG_CGROUPS
18
19 struct cgroupfs_root;
20 struct cgroup_subsys;
21 struct inode;
22
23 extern int cgroup_init_early(void);
24 extern int cgroup_init(void);
25 extern void cgroup_init_smp(void);
26 extern void cgroup_lock(void);
27 extern void cgroup_unlock(void);
28 extern void cgroup_fork(struct task_struct *p);
29 extern void cgroup_fork_callbacks(struct task_struct *p);
30 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
31
32 extern struct file_operations proc_cgroup_operations;
33
34 /* Per-subsystem/per-cgroup state maintained by the system. */
35 struct cgroup_subsys_state {
36         /* The cgroup that this subsystem is attached to. Useful
37          * for subsystems that want to know about the cgroup
38          * hierarchy structure */
39         struct cgroup *cgroup;
40
41         /* State maintained by the cgroup system to allow
42          * subsystems to be "busy". Should be accessed via css_get()
43          * and css_put() */
44
45         atomic_t refcnt;
46
47         unsigned long flags;
48 };
49
50 /* bits in struct cgroup_subsys_state flags field */
51 enum {
52         CSS_ROOT, /* This CSS is the root of the subsystem */
53 };
54
55 /*
56  * Call css_get() to hold a reference on the cgroup;
57  *
58  */
59
60 static inline void css_get(struct cgroup_subsys_state *css)
61 {
62         /* We don't need to reference count the root state */
63         if (!test_bit(CSS_ROOT, &css->flags))
64                 atomic_inc(&css->refcnt);
65 }
66 /*
67  * css_put() should be called to release a reference taken by
68  * css_get()
69  */
70
71 static inline void css_put(struct cgroup_subsys_state *css)
72 {
73         if (!test_bit(CSS_ROOT, &css->flags))
74                 atomic_dec(&css->refcnt);
75 }
76
77 struct cgroup {
78         unsigned long flags;            /* "unsigned long" so bitops work */
79
80         /* count users of this cgroup. >0 means busy, but doesn't
81          * necessarily indicate the number of tasks in the
82          * cgroup */
83         atomic_t count;
84
85         /*
86          * We link our 'sibling' struct into our parent's 'children'.
87          * Our children link their 'sibling' into our 'children'.
88          */
89         struct list_head sibling;       /* my parent's children */
90         struct list_head children;      /* my children */
91
92         struct cgroup *parent;  /* my parent */
93         struct dentry *dentry;          /* cgroup fs entry */
94
95         /* Private pointers for each registered subsystem */
96         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
97
98         struct cgroupfs_root *root;
99         struct cgroup *top_cgroup;
100 };
101
102 /* struct cftype:
103  *
104  * The files in the cgroup filesystem mostly have a very simple read/write
105  * handling, some common function will take care of it. Nevertheless some cases
106  * (read tasks) are special and therefore I define this structure for every
107  * kind of file.
108  *
109  *
110  * When reading/writing to a file:
111  *      - the cgroup to use in file->f_dentry->d_parent->d_fsdata
112  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
113  */
114
115 #define MAX_CFTYPE_NAME 64
116 struct cftype {
117         /* By convention, the name should begin with the name of the
118          * subsystem, followed by a period */
119         char name[MAX_CFTYPE_NAME];
120         int private;
121         int (*open) (struct inode *inode, struct file *file);
122         ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
123                          struct file *file,
124                          char __user *buf, size_t nbytes, loff_t *ppos);
125         /*
126          * read_uint() is a shortcut for the common case of returning a
127          * single integer. Use it in place of read()
128          */
129         u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
130         ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
131                           struct file *file,
132                           const char __user *buf, size_t nbytes, loff_t *ppos);
133
134         /*
135          * write_uint() is a shortcut for the common case of accepting
136          * a single integer (as parsed by simple_strtoull) from
137          * userspace. Use in place of write(); return 0 or error.
138          */
139         int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
140
141         int (*release) (struct inode *inode, struct file *file);
142 };
143
144 /* Add a new file to the given cgroup directory. Should only be
145  * called by subsystems from within a populate() method */
146 int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
147                        const struct cftype *cft);
148
149 /* Add a set of new files to the given cgroup directory. Should
150  * only be called by subsystems from within a populate() method */
151 int cgroup_add_files(struct cgroup *cont,
152                         struct cgroup_subsys *subsys,
153                         const struct cftype cft[],
154                         int count);
155
156 int cgroup_is_removed(const struct cgroup *cont);
157
158 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
159
160 int __cgroup_task_count(const struct cgroup *cont);
161 static inline int cgroup_task_count(const struct cgroup *cont)
162 {
163         int task_count;
164         rcu_read_lock();
165         task_count = __cgroup_task_count(cont);
166         rcu_read_unlock();
167         return task_count;
168 }
169
170 /* Return true if the cgroup is a descendant of the current cgroup */
171 int cgroup_is_descendant(const struct cgroup *cont);
172
173 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
174
175 struct cgroup_subsys {
176         struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
177                                                   struct cgroup *cont);
178         void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
179         int (*can_attach)(struct cgroup_subsys *ss,
180                           struct cgroup *cont, struct task_struct *tsk);
181         void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
182                         struct cgroup *old_cont, struct task_struct *tsk);
183         void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
184         void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
185         int (*populate)(struct cgroup_subsys *ss,
186                         struct cgroup *cont);
187         void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
188         void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
189         int subsys_id;
190         int active;
191         int early_init;
192 #define MAX_CGROUP_TYPE_NAMELEN 32
193         const char *name;
194
195         /* Protected by RCU */
196         struct cgroupfs_root *root;
197
198         struct list_head sibling;
199
200         void *private;
201 };
202
203 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
204 #include <linux/cgroup_subsys.h>
205 #undef SUBSYS
206
207 static inline struct cgroup_subsys_state *cgroup_subsys_state(
208         struct cgroup *cont, int subsys_id)
209 {
210         return cont->subsys[subsys_id];
211 }
212
213 static inline struct cgroup_subsys_state *task_subsys_state(
214         struct task_struct *task, int subsys_id)
215 {
216         return rcu_dereference(task->cgroups.subsys[subsys_id]);
217 }
218
219 static inline struct cgroup* task_cgroup(struct task_struct *task,
220                                                int subsys_id)
221 {
222         return task_subsys_state(task, subsys_id)->cgroup;
223 }
224
225 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
226
227 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
228
229 #else /* !CONFIG_CGROUPS */
230
231 static inline int cgroup_init_early(void) { return 0; }
232 static inline int cgroup_init(void) { return 0; }
233 static inline void cgroup_init_smp(void) {}
234 static inline void cgroup_fork(struct task_struct *p) {}
235 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
236 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
237
238 static inline void cgroup_lock(void) {}
239 static inline void cgroup_unlock(void) {}
240
241 #endif /* !CONFIG_CGROUPS */
242
243 #endif /* _LINUX_CGROUP_H */