b567438100d730345fcb8c85c6f7be2d7ccb7931
[util-vserver.git] / kernel / limit_int.h
1 #ifndef _VX_LIMIT_INT_H
2 #define _VX_LIMIT_INT_H
3
4
5 #ifdef  __KERNEL__
6
7 #define VXD_RCRES_COND(r)       VXD_CBIT(cres, (r))
8 #define VXD_RLIMIT_COND(r)      VXD_CBIT(limit, (r))
9
10 extern const char *vlimit_name[NUM_LIMITS];
11
12 static inline void __vx_acc_cres(struct vx_info *vxi,
13         int res, int dir, void *_data, char *_file, int _line)
14 {
15         if (VXD_RCRES_COND(res))
16                 vxlprintk(1, "vx_acc_cres[%5d,%s,%2d]: %5ld%s (%p)",
17                         (vxi ? vxi->vx_id : -1), vlimit_name[res], res,
18                         (vxi ? (long)__rlim_get(&vxi->limit, res) : 0),
19                         (dir > 0) ? "++" : "--", _data, _file, _line);
20         if (!vxi)
21                 return;
22
23         if (dir > 0)
24                 __rlim_inc(&vxi->limit, res);
25         else
26                 __rlim_dec(&vxi->limit, res);
27 }
28
29 static inline void __vx_add_cres(struct vx_info *vxi,
30         int res, int amount, void *_data, char *_file, int _line)
31 {
32         if (VXD_RCRES_COND(res))
33                 vxlprintk(1, "vx_add_cres[%5d,%s,%2d]: %5ld += %5d (%p)",
34                         (vxi ? vxi->vx_id : -1), vlimit_name[res], res,
35                         (vxi ? (long)__rlim_get(&vxi->limit, res) : 0),
36                         amount, _data, _file, _line);
37         if (amount == 0)
38                 return;
39         if (!vxi)
40                 return;
41         __rlim_add(&vxi->limit, res, amount);
42 }
43
44 static inline int __vx_cres_avail(struct vx_info *vxi,
45                 int res, int num, char *_file, int _line)
46 {
47         rlim_t value;
48
49         if (VXD_RLIMIT_COND(res))
50                 vxlprintk(1, "vx_cres_avail[%5d,%s,%2d]: %5ld/%5ld > %5ld + %5d",
51                         (vxi ? vxi->vx_id : -1), vlimit_name[res], res,
52                         (vxi ? (long)__rlim_soft(&vxi->limit, res) : -1),
53                         (vxi ? (long)__rlim_hard(&vxi->limit, res) : -1),
54                         (vxi ? (long)__rlim_get(&vxi->limit, res) : 0),
55                         num, _file, _line);
56         if (num == 0)
57                 return 1;
58         if (!vxi)
59                 return 1;
60
61         value = __rlim_get(&vxi->limit, res);
62
63         if (value > __rlim_rmax(&vxi->limit, res))
64                 __rlim_rmax(&vxi->limit, res) = value;
65         else if (value < __rlim_rmin(&vxi->limit, res))
66                 __rlim_rmin(&vxi->limit, res) = value;
67
68         if (__rlim_soft(&vxi->limit, res) == RLIM_INFINITY)
69                 return -1;
70         if (value + num <= __rlim_soft(&vxi->limit, res))
71                 return -1;
72
73         if (__rlim_hard(&vxi->limit, res) == RLIM_INFINITY)
74                 return 1;
75         if (value + num <= __rlim_hard(&vxi->limit, res))
76                 return 1;
77
78         __rlim_hit(&vxi->limit, res);
79         return 0;
80 }
81
82 #endif  /* __KERNEL__ */
83 #endif  /* _VX_LIMIT_INT_H */