VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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_nproc_inc(p) \
38         vx_acc_cres(current->vx_info, 1, (p), RLIMIT_NPROC)
39 #define vx_nproc_dec(p) \
40         vx_acc_cres(current->vx_info,-1, (p), RLIMIT_NPROC)
41
42 #define vx_files_inc(f) \
43         vx_acc_cres(current->vx_info, 1, (f), RLIMIT_NOFILE)
44 #define vx_files_dec(f) \
45         vx_acc_cres(current->vx_info,-1, (f), RLIMIT_NOFILE)
46
47 /*
48 #define vx_openfd_inc(f) do {                                   \
49         vx_acc_cres(current->vx_info, 1, RLIMIT_OPENFD);        \
50         printk("vx_openfd_inc: %d[#%d] in %s:%d\n",             \
51                 f, current->xid, __FILE__, __LINE__);           \
52         } while (0)
53
54 #define vx_openfd_dec(f) do {                                   \
55         vx_acc_cres(current->vx_info,-1, RLIMIT_OPENFD);        \
56         printk("vx_openfd_dec: %d[#%d] in %s:%d\n",             \
57                 f, current->xid, __FILE__, __LINE__);           \
58         } while (0)
59 */
60
61 #define vx_cres_avail(v,n,r) \
62         __vx_cres_avail((v), (r), (n), __FILE__, __LINE__)
63
64 static inline int __vx_cres_avail(struct vx_info *vxi,
65                 int res, int num, char *_file, int _line)
66 {
67         unsigned long value;
68
69         if (VXD_RLIMIT(res, RLIMIT_NOFILE) ||
70                 VXD_RLIMIT(res, RLIMIT_NPROC) ||
71                 VXD_RLIMIT(res, VLIMIT_NSOCK))
72                 vxlprintk(1, "vx_cres_avail[%5d,%s,%2d]: %5ld > %5d + %5d",
73                         (vxi?vxi->vx_id:-1), vlimit_name[res], res,
74                         (vxi?vxi->limit.rlim[res]:1),
75                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
76                         num, _file, _line);
77         if (!vxi)
78                 return 1;
79         value = atomic_read(&vxi->limit.rcur[res]);     
80         if (value > vxi->limit.rmax[res])
81                 vxi->limit.rmax[res] = value;
82         if (vxi->limit.rlim[res] == RLIM_INFINITY)
83                 return 1;
84         if (value + num <= vxi->limit.rlim[res])
85                 return 1;
86         atomic_inc(&vxi->limit.lhit[res]);
87         return 0;
88 }
89
90 #define vx_nproc_avail(n) \
91         vx_cres_avail(current->vx_info, (n), RLIMIT_NPROC)
92
93 #define vx_files_avail(n) \
94         vx_cres_avail(current->vx_info, (n), RLIMIT_NOFILE)
95
96
97 /* socket limits */
98
99 #define vx_sock_inc(s) \
100         vx_acc_cres((s)->sk_vx_info, 1, (s), VLIMIT_NSOCK)
101 #define vx_sock_dec(s) \
102         vx_acc_cres((s)->sk_vx_info,-1, (s), VLIMIT_NSOCK)
103
104 #define vx_sock_avail(n) \
105         vx_cres_avail(current->vx_info, (n), VLIMIT_NSOCK)
106
107 #else
108 #warning duplicate inclusion
109 #endif