This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / linux / vs_limit.h
1 #ifndef _VX_VS_LIMIT_H
2 #define _VX_VS_LIMIT_H
3
4
5 // #define VX_DEBUG
6
7 #include <linux/kernel.h>
8 #include <linux/rcupdate.h>
9 #include <linux/sched.h>
10
11 #include "vserver/context.h"
12 #include "vserver/limit.h"
13
14
15 /* file limits */
16
17 #define VX_DEBUG_ACC_FILE       0
18 #define VX_DEBUG_ACC_OPENFD     0
19
20 #if     (VX_DEBUG_ACC_FILE) || (VX_DEBUG_ACC_OPENFD)
21 #define vxdprintk(x...) printk("vxd: " x)
22 #else
23 #define vxdprintk(x...)
24 #endif
25
26
27 #define vx_acc_cres(v,d,r) \
28         __vx_acc_cres((v), (r), (d), __FILE__, __LINE__)
29
30 static inline void __vx_acc_cres(struct vx_info *vxi,
31         int res, int dir, char *file, int line)
32 {
33         if (vxi) {
34         if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
35                         (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
36         printk("vx_acc_cres[%5d,%2d]: %5d%s in %s:%d\n",
37                         (vxi?vxi->vx_id:-1), res,
38                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
39                         (dir>0)?"++":"--", file, line);
40                 if (dir > 0)
41                         atomic_inc(&vxi->limit.rcur[res]);
42                 else
43                         atomic_dec(&vxi->limit.rcur[res]);
44         }
45 }
46
47 #define vx_nproc_inc(p) vx_acc_cres(current->vx_info, 1, RLIMIT_NPROC)
48 #define vx_nproc_dec(p) vx_acc_cres(current->vx_info,-1, RLIMIT_NPROC)
49
50 #define vx_files_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_NOFILE)
51 #define vx_files_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_NOFILE)
52
53 #define vx_openfd_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_OPENFD)
54 #define vx_openfd_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_OPENFD)
55
56 /*
57 #define vx_openfd_inc(f) do {                                   \
58         vx_acc_cres(current->vx_info, 1, RLIMIT_OPENFD);        \
59         printk("vx_openfd_inc: %d[#%d] in %s:%d\n",             \
60                 f, current->xid, __FILE__, __LINE__);           \
61         } while (0)
62
63 #define vx_openfd_dec(f) do {                                   \
64         vx_acc_cres(current->vx_info,-1, RLIMIT_OPENFD);        \
65         printk("vx_openfd_dec: %d[#%d] in %s:%d\n",             \
66                 f, current->xid, __FILE__, __LINE__);           \
67         } while (0)
68 */
69
70 #define vx_cres_avail(v,n,r) \
71         __vx_cres_avail((v), (r), (n), __FILE__, __LINE__)
72
73 static inline int __vx_cres_avail(struct vx_info *vxi,
74                 int res, int num, char *file, int line)
75 {
76         unsigned long value;
77
78         if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
79                 (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
80                 printk("vx_cres_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
81                         (vxi?vxi->vx_id:-1), res,
82                         (vxi?vxi->limit.rlim[res]:1),
83                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
84                         num, file, line);
85         if (!vxi)
86                 return 1;
87         value = atomic_read(&vxi->limit.rcur[res]);     
88         if (value > vxi->limit.rmax[res])
89                 vxi->limit.rmax[res] = value;
90         if (vxi->limit.rlim[res] == RLIM_INFINITY)
91                 return 1;
92         if (value + num <= vxi->limit.rlim[res])
93                 return 1;
94         atomic_inc(&vxi->limit.lhit[res]);
95         return 0;
96 }
97
98 #define vx_nproc_avail(n) \
99         vx_cres_avail(current->vx_info, (n), RLIMIT_NPROC)
100
101 #define vx_files_avail(n) \
102         vx_cres_avail(current->vx_info, (n), RLIMIT_NOFILE)
103
104 #define vx_openfd_avail(n) \
105         vx_cres_avail(current->vx_info, (n), RLIMIT_OPENFD)
106
107
108 /* socket limits */
109
110 #define vx_sock_inc(f)  vx_acc_cres(current->vx_info, 1, VLIMIT_SOCK)
111 #define vx_sock_dec(f)  vx_acc_cres(current->vx_info,-1, VLIMIT_SOCK)
112
113 #define vx_sock_avail(n) \
114         vx_cres_avail(current->vx_info, (n), VLIMIT_SOCK)
115
116
117 #else
118 #warning duplicate inclusion
119 #endif