7 #include <linux/kernel.h>
8 #include <linux/rcupdate.h>
9 #include <linux/sched.h>
11 #include "vserver/context.h"
12 #include "vserver/limit.h"
13 #include "vserver/cvirt.h"
16 #define vxdprintk(x...) printk("vxd: " x)
18 #define vxdprintk(x...)
23 extern int proc_pid_vx_info(struct task_struct *, char *);
26 #define get_vx_info(i) __get_vx_info(i,__FILE__,__LINE__)
28 static inline struct vx_info *__get_vx_info(struct vx_info *vxi,
29 const char *_file, int _line)
33 vxdprintk("get_vx_info(%p[#%d.%d])\t%s:%d\n",
34 vxi, vxi?vxi->vx_id:0, vxi?atomic_read(&vxi->vx_usecnt):0,
36 atomic_inc(&vxi->vx_usecnt);
41 #define free_vx_info(vxi) \
42 call_rcu(&vxi->vx_rcu, rcu_free_vx_info, vxi);
44 #define put_vx_info(i) __put_vx_info(i,__FILE__,__LINE__)
46 static inline void __put_vx_info(struct vx_info *vxi, const char *_file, int _line)
50 vxdprintk("put_vx_info(%p[#%d.%d])\t%s:%d\n",
51 vxi, vxi?vxi->vx_id:0, vxi?atomic_read(&vxi->vx_usecnt):0,
53 if (atomic_dec_and_test(&vxi->vx_usecnt))
57 #define set_vx_info(p,i) __set_vx_info(p,i,__FILE__,__LINE__)
59 static inline void __set_vx_info(struct vx_info **vxp, struct vx_info *vxi,
60 const char *_file, int _line)
65 vxdprintk("set_vx_info(%p[#%d.%d.%d])\t%s:%d\n",
66 vxi, vxi?vxi->vx_id:0,
67 vxi?atomic_read(&vxi->vx_usecnt):0,
68 vxi?atomic_read(&vxi->vx_refcnt):0,
70 atomic_inc(&vxi->vx_refcnt);
71 *vxp = __get_vx_info(vxi, _file, _line);
74 #define clr_vx_info(p) __clr_vx_info(p,__FILE__,__LINE__)
76 static inline void __clr_vx_info(struct vx_info **vxp,
77 const char *_file, int _line)
79 struct vx_info *vxo = *vxp;
83 vxdprintk("clr_vx_info(%p[#%d.%d.%d])\t%s:%d\n",
84 vxo, vxo?vxo->vx_id:0,
85 vxo?atomic_read(&vxo->vx_usecnt):0,
86 vxo?atomic_read(&vxo->vx_refcnt):0,
90 if (vxo && atomic_dec_and_test(&vxo->vx_refcnt))
92 __put_vx_info(vxo, _file, _line);
96 #define task_get_vx_info(i) __task_get_vx_info(i,__FILE__,__LINE__)
98 static __inline__ struct vx_info *__task_get_vx_info(struct task_struct *p,
99 const char *_file, int _line)
104 vxi = __get_vx_info(p->vx_info, _file, _line);
110 #define vx_verify_info(p,i) \
111 __vx_verify_info((p)->vx_info,i,__FILE__,__LINE__)
113 static __inline__ void __vx_verify_info(
114 struct vx_info *vxa, struct vx_info *vxb,
115 const char *_file, int _line)
119 printk(KERN_ERR "vx bad assumption (%p==%p) at %s:%d\n",
120 vxa, vxb, _file, _line);
124 #define vx_task_xid(t) ((t)->xid)
126 #define vx_current_xid() vx_task_xid(current)
128 #define vx_check(c,m) __vx_check(vx_current_xid(),c,m)
130 #define vx_weak_check(c,m) ((m) ? vx_check(c,m) : 1)
134 * check current context for ADMIN/WATCH and
135 * optionally agains supplied argument
137 static __inline__ int __vx_check(xid_t cid, xid_t id, unsigned int mode)
139 if (mode & VX_ARG_MASK) {
140 if ((mode & VX_IDENT) &&
144 if (mode & VX_ATR_MASK) {
145 if ((mode & VX_DYNAMIC) &&
146 (id >= MIN_D_CONTEXT) &&
147 (id <= MAX_S_CONTEXT))
149 if ((mode & VX_STATIC) &&
150 (id > 1) && (id < MIN_D_CONTEXT))
153 return (((mode & VX_ADMIN) && (cid == 0)) ||
154 ((mode & VX_WATCH) && (cid == 1)));
158 #define __vx_flags(v,m,f) (((v) & (m)) ^ (f))
160 #define __vx_task_flags(t,m,f) \
161 (((t) && ((t)->vx_info)) ? \
162 __vx_flags((t)->vx_info->vx_flags,(m),(f)) : 0)
164 #define vx_current_flags() \
165 ((current->vx_info) ? current->vx_info->vx_flags : 0)
167 #define vx_flags(m,f) __vx_flags(vx_current_flags(),(m),(f))
170 #define vx_current_ccaps() \
171 ((current->vx_info) ? current->vx_info->vx_ccaps : 0)
173 #define vx_ccaps(c) (vx_current_ccaps() & (c))
175 #define vx_current_bcaps() \
176 (((current->vx_info) && !vx_flags(VXF_STATE_SETUP, 0)) ? \
177 current->vx_info->vx_bcaps : cap_bset)
180 #define VX_DEBUG_ACC_RSS 0
181 #define VX_DEBUG_ACC_VM 0
182 #define VX_DEBUG_ACC_VML 0
185 #if (VX_DEBUG_ACC_RSS) || (VX_DEBUG_ACC_VM) || (VX_DEBUG_ACC_VML)
186 #define vxdprintk(x...) printk("vxd: " x)
188 #define vxdprintk(x...)
191 #define vx_acc_page(m, d, v, r) \
192 __vx_acc_page(&(m->v), m->mm_vx_info, r, d, __FILE__, __LINE__)
194 static inline void __vx_acc_page(unsigned long *v, struct vx_info *vxi,
195 int res, int dir, char *file, int line)
205 atomic_inc(&vxi->limit.res[res]);
207 atomic_dec(&vxi->limit.res[res]);
212 #define vx_acc_pages(m, p, v, r) \
213 __vx_acc_pages(&(m->v), m->mm_vx_info, r, p, __FILE__, __LINE__)
215 static inline void __vx_acc_pages(unsigned long *v, struct vx_info *vxi,
216 int res, int pages, char *file, int line)
218 if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
219 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
220 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
221 vxdprintk("vx_acc_pages [%5d,%2d]: %5d += %5d in %s:%d\n",
222 (vxi?vxi->vx_id:-1), res,
223 (vxi?atomic_read(&vxi->limit.res[res]):0),
230 atomic_add(pages, &vxi->limit.res[res]);
235 #define vx_acc_vmpage(m,d) vx_acc_page(m, d, total_vm, RLIMIT_AS)
236 #define vx_acc_vmlpage(m,d) vx_acc_page(m, d, locked_vm, RLIMIT_MEMLOCK)
237 #define vx_acc_rsspage(m,d) vx_acc_page(m, d, rss, RLIMIT_RSS)
239 #define vx_acc_vmpages(m,p) vx_acc_pages(m, p, total_vm, RLIMIT_AS)
240 #define vx_acc_vmlpages(m,p) vx_acc_pages(m, p, locked_vm, RLIMIT_MEMLOCK)
241 #define vx_acc_rsspages(m,p) vx_acc_pages(m, p, rss, RLIMIT_RSS)
243 #define vx_pages_add(s,r,p) __vx_acc_pages(0, s, r, p, __FILE__, __LINE__)
244 #define vx_pages_sub(s,r,p) __vx_pages_add(s, r, -(p))
246 #define vx_vmpages_inc(m) vx_acc_vmpage(m, 1)
247 #define vx_vmpages_dec(m) vx_acc_vmpage(m,-1)
248 #define vx_vmpages_add(m,p) vx_acc_vmpages(m, p)
249 #define vx_vmpages_sub(m,p) vx_acc_vmpages(m,-(p))
251 #define vx_vmlocked_inc(m) vx_acc_vmlpage(m, 1)
252 #define vx_vmlocked_dec(m) vx_acc_vmlpage(m,-1)
253 #define vx_vmlocked_add(m,p) vx_acc_vmlpages(m, p)
254 #define vx_vmlocked_sub(m,p) vx_acc_vmlpages(m,-(p))
256 #define vx_rsspages_inc(m) vx_acc_rsspage(m, 1)
257 #define vx_rsspages_dec(m) vx_acc_rsspage(m,-1)
258 #define vx_rsspages_add(m,p) vx_acc_rsspages(m, p)
259 #define vx_rsspages_sub(m,p) vx_acc_rsspages(m,-(p))
263 #define vx_pages_avail(m, p, r) \
264 __vx_pages_avail((m)->mm_vx_info, (r), (p), __FILE__, __LINE__)
266 static inline int __vx_pages_avail(struct vx_info *vxi,
267 int res, int pages, char *file, int line)
269 if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
270 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
271 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
272 printk("vx_pages_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
273 (vxi?vxi->vx_id:-1), res,
274 (vxi?vxi->limit.rlim[res]:1),
275 (vxi?atomic_read(&vxi->limit.res[res]):0),
279 if (vxi->limit.rlim[res] == RLIM_INFINITY)
281 if (atomic_read(&vxi->limit.res[res]) + pages < vxi->limit.rlim[res])
286 #define vx_vmpages_avail(m,p) vx_pages_avail(m, p, RLIMIT_AS)
287 #define vx_vmlocked_avail(m,p) vx_pages_avail(m, p, RLIMIT_MEMLOCK)
288 #define vx_rsspages_avail(m,p) vx_pages_avail(m, p, RLIMIT_RSS)
292 #define VX_DEBUG_ACC_FILE 0
293 #define VX_DEBUG_ACC_OPENFD 0
296 #if (VX_DEBUG_ACC_FILE) || (VX_DEBUG_ACC_OPENFD)
297 #define vxdprintk(x...) printk("vxd: " x)
299 #define vxdprintk(x...)
303 #define vx_acc_cres(v,d,r) \
304 __vx_acc_cres((v), (r), (d), __FILE__, __LINE__)
306 static inline void __vx_acc_cres(struct vx_info *vxi,
307 int res, int dir, char *file, int line)
310 if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
311 (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
312 printk("vx_acc_cres[%5d,%2d]: %5d%s in %s:%d\n",
313 (vxi?vxi->vx_id:-1), res,
314 (vxi?atomic_read(&vxi->limit.res[res]):0),
315 (dir>0)?"++":"--", file, line);
317 atomic_inc(&vxi->limit.res[res]);
319 atomic_dec(&vxi->limit.res[res]);
323 #define vx_files_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_NOFILE)
324 #define vx_files_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_NOFILE)
326 #define vx_openfd_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_OPENFD)
327 #define vx_openfd_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_OPENFD)
329 #define vx_cres_avail(v,n,r) \
330 __vx_cres_avail((v), (r), (n), __FILE__, __LINE__)
332 static inline int __vx_cres_avail(struct vx_info *vxi,
333 int res, int num, char *file, int line)
335 if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
336 (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
337 printk("vx_cres_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
338 (vxi?vxi->vx_id:-1), res,
339 (vxi?vxi->limit.rlim[res]:1),
340 (vxi?atomic_read(&vxi->limit.res[res]):0),
344 if (vxi->limit.rlim[res] == RLIM_INFINITY)
346 if (vxi->limit.rlim[res] < atomic_read(&vxi->limit.res[res]) + num)
351 #define vx_files_avail(n) \
352 vx_cres_avail(current->vx_info, (n), RLIMIT_NOFILE)
354 #define vx_openfd_avail(n) \
355 vx_cres_avail(current->vx_info, (n), RLIMIT_OPENFD)
359 #define vx_sock_inc(f) vx_acc_cres(current->vx_info, 1, VLIMIT_SOCK)
360 #define vx_sock_dec(f) vx_acc_cres(current->vx_info,-1, VLIMIT_SOCK)
362 #define vx_sock_avail(n) \
363 vx_cres_avail(current->vx_info, (n), VLIMIT_SOCK)
367 #define FIOC_GETXFLG _IOR('x', 5, long)
368 #define FIOC_SETXFLG _IOW('x', 6, long)
370 /* utsname virtualization */
372 static inline struct new_utsname *vx_new_utsname(void)
374 if (current->vx_info)
375 return ¤t->vx_info->cvirt.utsname;
376 return &system_utsname;
379 #define vx_new_uts(x) ((vx_new_utsname())->x)
381 /* generic flag merging */
383 #define vx_mask_flags(v,f,m) (((v) & ~(m)) | ((f) & (m)))
385 #define vx_mask_mask(v,f,m) (((v) & ~(m)) | ((v) & (f) & (m)))
388 /* socket accounting */
390 #include <linux/socket.h>
392 static inline int vx_sock_type(int family)
396 if (family > 0 && family < 3)
398 else if (family == PF_INET6)
403 #define vx_acc_sock(v,f,p,s) \
404 __vx_acc_sock((v), (f), (p), (s), __FILE__, __LINE__)
406 static inline void __vx_acc_sock(struct vx_info *vxi,
407 int family, int pos, int size, char *file, int line)
410 int type = vx_sock_type(family);
412 atomic_inc(&vxi->cacct.sock[type][pos].count);
413 atomic_add(size, &vxi->cacct.sock[type][pos].total);
417 #define vx_sock_recv(sk,s) \
418 vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 0, (s))
419 #define vx_sock_send(sk,s) \
420 vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 1, (s))
421 #define vx_sock_fail(sk,s) \
422 vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 2, (s))
425 #define sock_vx_init(s) do { \
427 (s)->sk_vx_info = NULL; \
431 /* pid faking stuff */
434 #define vx_map_tgid(v,p) \
435 __vx_map_tgid((v), (p), __FILE__, __LINE__)
437 static inline int __vx_map_tgid(struct vx_info *vxi, int pid,
438 char *file, int line)
440 if (vxi && __vx_flags(vxi->vx_flags, VXF_INFO_INIT, 0)) {
441 vxdprintk("vx_map_tgid: %p/%llx: %d -> %d in %s:%d\n",
442 vxi, vxi->vx_flags, pid,
443 (pid == vxi->vx_initpid)?1:pid,
445 if (pid == vxi->vx_initpid)
451 #define vx_rmap_tgid(v,p) \
452 __vx_rmap_tgid((v), (p), __FILE__, __LINE__)
454 static inline int __vx_rmap_tgid(struct vx_info *vxi, int pid,
455 char *file, int line)
457 if (vxi && __vx_flags(vxi->vx_flags, VXF_INFO_INIT, 0)) {
458 vxdprintk("vx_rmap_tgid: %p/%llx: %d -> %d in %s:%d\n",
459 vxi, vxi->vx_flags, pid,
460 (pid == 1)?vxi->vx_initpid:pid,
462 if ((pid == 1) && vxi->vx_initpid)
463 return vxi->vx_initpid;
469 #define vxdprintk(x...)