vserver 1.9.3
[linux-2.6.git] / include / linux / vs_limit.h
1 #ifndef _VX_VS_LIMIT_H
2 #define _VX_VS_LIMIT_H
3
4 #include <linux/kernel.h>
5 #include <linux/rcupdate.h>
6 #include <linux/sched.h>
7
8 #include "vserver/context.h"
9 #include "vserver/limit.h"
10 #include "vserver/debug.h"
11
12
13 /* file limits */
14
15
16 static inline void __vx_acc_cres(struct vx_info *vxi,
17         int res, int dir, void *_data, char *_file, int _line)
18 {
19         if (VXD_RLIMIT(res, RLIMIT_NOFILE) ||
20                 VXD_RLIMIT(res, RLIMIT_NPROC) ||
21                 VXD_RLIMIT(res, VLIMIT_NSOCK))
22                 vxlprintk(1, "vx_acc_cres[%5d,%s,%2d]: %5d%s (%p)",
23                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
24                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
25                         (dir>0)?"++":"--", _data, _file, _line);
26         if (vxi) {
27                 if (dir > 0)
28                         atomic_inc(&vxi->limit.rcur[res]);
29                 else
30                         atomic_dec(&vxi->limit.rcur[res]);
31         }
32 }
33
34 #define vx_acc_cres(v,d,p,r) \
35         __vx_acc_cres((v), (r), (d), (p), __FILE__, __LINE__)
36
37 #define vx_acc_cres_cond(x,d,p,r) \
38         __vx_acc_cres(((x) == current->xid) ? current->vx_info : 0,\
39         (r), (d), (p), __FILE__, __LINE__)
40
41 #define vx_nproc_inc(p) \
42         vx_acc_cres((p)->vx_info, 1, (p), RLIMIT_NPROC)
43
44 #define vx_nproc_dec(p) \
45         vx_acc_cres((p)->vx_info,-1, (p), RLIMIT_NPROC)
46
47 #define vx_files_inc(f) \
48         vx_acc_cres_cond((f)->f_xid, 1, (f), RLIMIT_NOFILE)
49
50 #define vx_files_dec(f) \
51         vx_acc_cres_cond((f)->f_xid,-1, (f), RLIMIT_NOFILE)
52
53
54 #define vx_cres_avail(v,n,r) \
55         __vx_cres_avail((v), (r), (n), __FILE__, __LINE__)
56
57 static inline int __vx_cres_avail(struct vx_info *vxi,
58                 int res, int num, char *_file, int _line)
59 {
60         unsigned long value;
61
62         if (VXD_RLIMIT(res, RLIMIT_NOFILE) ||
63                 VXD_RLIMIT(res, RLIMIT_NPROC) ||
64                 VXD_RLIMIT(res, VLIMIT_NSOCK))
65                 vxlprintk(1, "vx_cres_avail[%5d,%s,%2d]: %5ld > %5d + %5d",
66                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
67                         (vxi?vxi->limit.rlim[res]:1),
68                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
69                         num, _file, _line);
70         if (!vxi)
71                 return 1;
72         value = atomic_read(&vxi->limit.rcur[res]);
73         if (value > vxi->limit.rmax[res])
74                 vxi->limit.rmax[res] = value;
75         if (vxi->limit.rlim[res] == RLIM_INFINITY)
76                 return 1;
77         if (value + num <= vxi->limit.rlim[res])
78                 return 1;
79         atomic_inc(&vxi->limit.lhit[res]);
80         return 0;
81 }
82
83 #define vx_nproc_avail(n) \
84         vx_cres_avail(current->vx_info, (n), RLIMIT_NPROC)
85
86 #define vx_files_avail(n) \
87         vx_cres_avail(current->vx_info, (n), RLIMIT_NOFILE)
88
89
90 /* socket limits */
91
92 #define vx_sock_inc(s) \
93         vx_acc_cres((s)->sk_vx_info, 1, (s), VLIMIT_NSOCK)
94 #define vx_sock_dec(s) \
95         vx_acc_cres((s)->sk_vx_info,-1, (s), VLIMIT_NSOCK)
96
97 #define vx_sock_avail(n) \
98         vx_cres_avail(current->vx_info, (n), VLIMIT_NSOCK)
99
100 #else
101 #warning duplicate inclusion
102 #endif