vserver 1.9.5.x5
[linux-2.6.git] / include / linux / vs_limit.h
1 #ifndef _VX_VS_LIMIT_H
2 #define _VX_VS_LIMIT_H
3
4
5 #include "vserver/limit.h"
6 #include "vserver/debug.h"
7
8
9 #define vx_acc_cres(v,d,p,r) \
10         __vx_acc_cres(v, r, d, p, __FILE__, __LINE__)
11
12 #define vx_acc_cres_cond(x,d,p,r) \
13         __vx_acc_cres(((x) == vx_current_xid()) ? current->vx_info : 0, \
14         r, d, p, __FILE__, __LINE__)
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, RLIMIT_LOCKS) ||
22                 VXD_RLIMIT(res, VLIMIT_NSOCK) ||
23                 VXD_RLIMIT(res, VLIMIT_OPENFD))
24                 vxlprintk(1, "vx_acc_cres[%5d,%s,%2d]: %5d%s (%p)",
25                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
26                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
27                         (dir>0)?"++":"--", _data, _file, _line);
28         if (vxi) {
29                 if (dir > 0)
30                         atomic_inc(&vxi->limit.rcur[res]);
31                 else
32                         atomic_dec(&vxi->limit.rcur[res]);
33         }
34 }
35
36 #define vx_add_cres(v,a,p,r) \
37         __vx_add_cres(v, r, a, p, __FILE__, __LINE__)
38 #define vx_sub_cres(v,a,p,r)            vx_add_cres(v,-(a),p,r)
39
40 #define vx_add_cres_cond(x,a,p,r) \
41         __vx_add_cres(((x) == vx_current_xid()) ? current->vx_info : 0, \
42         r, a, p, __FILE__, __LINE__)
43 #define vx_sub_cres_cond(x,a,p,r)       vx_add_cres_cond(x,-(a),p,r)
44
45
46 static inline void __vx_add_cres(struct vx_info *vxi,
47         int res, int amount, void *_data, char *_file, int _line)
48 {
49         if (VXD_RLIMIT(res, RLIMIT_MSGQUEUE))
50                 vxlprintk(1, "vx_add_cres[%5d,%s,%2d]: %5d += %5d (%p)",
51                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
52                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
53                         amount, _data, _file, _line);
54         if (amount == 0)
55                 return;
56         if (vxi)
57                 atomic_add(amount, &vxi->limit.rcur[res]);
58 }
59
60
61 /* process and file limits */
62
63 #define vx_nproc_inc(p) \
64         vx_acc_cres((p)->vx_info, 1, p, RLIMIT_NPROC)
65
66 #define vx_nproc_dec(p) \
67         vx_acc_cres((p)->vx_info,-1, p, RLIMIT_NPROC)
68
69 #define vx_files_inc(f) \
70         vx_acc_cres_cond((f)->f_xid, 1, f, RLIMIT_NOFILE)
71
72 #define vx_files_dec(f) \
73         vx_acc_cres_cond((f)->f_xid,-1, f, RLIMIT_NOFILE)
74
75 #define vx_locks_inc(l) \
76         vx_acc_cres_cond((l)->fl_xid, 1, l, RLIMIT_LOCKS)
77
78 #define vx_locks_dec(l) \
79         vx_acc_cres_cond((l)->fl_xid,-1, l, RLIMIT_LOCKS)
80
81 #define vx_openfd_inc(f) \
82         vx_acc_cres(current->vx_info, 1, (void *)(long)(f), VLIMIT_OPENFD)
83
84 #define vx_openfd_dec(f) \
85         vx_acc_cres(current->vx_info,-1, (void *)(long)(f), VLIMIT_OPENFD)
86
87
88 #define vx_cres_avail(v,n,r) \
89         __vx_cres_avail(v, r, n, __FILE__, __LINE__)
90
91 static inline int __vx_cres_avail(struct vx_info *vxi,
92                 int res, int num, char *_file, int _line)
93 {
94         unsigned long value;
95
96         if (VXD_RLIMIT(res, RLIMIT_NOFILE) ||
97                 VXD_RLIMIT(res, RLIMIT_NPROC) ||
98                 VXD_RLIMIT(res, VLIMIT_NSOCK) ||
99                 VXD_RLIMIT(res, VLIMIT_OPENFD))
100                 vxlprintk(1, "vx_cres_avail[%5d,%s,%2d]: %5ld > %5d + %5d",
101                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
102                         (vxi?vxi->limit.rlim[res]:1),
103                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
104                         num, _file, _line);
105         if (!vxi)
106                 return 1;
107         value = atomic_read(&vxi->limit.rcur[res]);
108         if (value > vxi->limit.rmax[res])
109                 vxi->limit.rmax[res] = value;
110         if (vxi->limit.rlim[res] == RLIM_INFINITY)
111                 return 1;
112         if (value + num <= vxi->limit.rlim[res])
113                 return 1;
114         atomic_inc(&vxi->limit.lhit[res]);
115         return 0;
116 }
117
118 #define vx_nproc_avail(n) \
119         vx_cres_avail(current->vx_info, n, RLIMIT_NPROC)
120
121 #define vx_files_avail(n) \
122         vx_cres_avail(current->vx_info, n, RLIMIT_NOFILE)
123
124 #define vx_locks_avail(n) \
125         vx_cres_avail(current->vx_info, n, RLIMIT_LOCKS)
126
127 #define vx_openfd_avail(n) \
128         vx_cres_avail(current->vx_info, n, VLIMIT_OPENFD)
129
130
131 /* socket limits */
132
133 #define vx_sock_inc(s) \
134         vx_acc_cres((s)->sk_vx_info, 1, s, VLIMIT_NSOCK)
135
136 #define vx_sock_dec(s) \
137         vx_acc_cres((s)->sk_vx_info,-1, s, VLIMIT_NSOCK)
138
139 #define vx_sock_avail(n) \
140         vx_cres_avail(current->vx_info, n, VLIMIT_NSOCK)
141
142
143 /* ipc resource limits */
144
145 #define vx_ipcmsg_add(v,u,a) \
146         vx_add_cres(v, a, u, RLIMIT_MSGQUEUE)
147
148 #define vx_ipcmsg_sub(v,u,a) \
149         vx_sub_cres(v, a, u, RLIMIT_MSGQUEUE)
150
151 #define vx_ipcmsg_avail(v,a) \
152         vx_cres_avail(v, a, RLIMIT_MSGQUEUE)
153
154
155 #define vx_ipcshm_add(v,k,a) \
156         vx_add_cres(v, a, (void *)(long)(k), VLIMIT_SHMEM)
157
158 #define vx_ipcshm_sub(v,k,a) \
159         vx_sub_cres(v, a, (void *)(long)(k), VLIMIT_SHMEM)
160
161 #define vx_ipcshm_avail(v,a) \
162         vx_cres_avail(v, a, VLIMIT_SHMEM)
163
164
165 #else
166 #warning duplicate inclusion
167 #endif