This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / include / linux / vinline.h
1 #error THIS FILE SHOULD NO LONGER BE USED
2
3
4 #ifndef _VX_INLINE_H
5 #define _VX_INLINE_H
6
7
8 // #define VX_DEBUG
9
10 #include <linux/kernel.h>
11 #include <linux/rcupdate.h>
12 #include <linux/sched.h>
13
14 #include "vserver/context.h"
15 #include "vserver/limit.h"
16 #include "vserver/cvirt.h"
17
18 #if defined(VX_DEBUG)
19 #define vxdprintk(x...) printk("vxd: " x)
20 #else
21 #define vxdprintk(x...)
22 #endif
23
24
25
26 extern int proc_pid_vx_info(struct task_struct *, char *);
27
28
29 #define get_vx_info(i)  __get_vx_info(i,__FILE__,__LINE__)
30
31 static inline struct vx_info *__get_vx_info(struct vx_info *vxi,
32         const char *_file, int _line)
33 {
34         if (!vxi)
35                 return NULL;
36         vxdprintk("get_vx_info(%p[#%d.%d])\t%s:%d\n",
37                 vxi, vxi?vxi->vx_id:0, vxi?atomic_read(&vxi->vx_usecnt):0,
38                 _file, _line);
39         atomic_inc(&vxi->vx_usecnt);
40         return vxi;
41 }
42
43
44 #define free_vx_info(vxi)       \
45         call_rcu(&vxi->vx_rcu, rcu_free_vx_info, vxi);
46
47 #define put_vx_info(i)  __put_vx_info(i,__FILE__,__LINE__)
48
49 static inline void __put_vx_info(struct vx_info *vxi, const char *_file, int _line)
50 {
51         if (!vxi)
52                 return;
53         vxdprintk("put_vx_info(%p[#%d.%d])\t%s:%d\n",
54                 vxi, vxi?vxi->vx_id:0, vxi?atomic_read(&vxi->vx_usecnt):0,
55                 _file, _line);
56         if (atomic_dec_and_test(&vxi->vx_usecnt))
57                 free_vx_info(vxi);
58 }
59
60 #define set_vx_info(p,i) __set_vx_info(p,i,__FILE__,__LINE__)
61
62 static inline void __set_vx_info(struct vx_info **vxp, struct vx_info *vxi,
63         const char *_file, int _line)
64 {
65         BUG_ON(*vxp);
66         if (!vxi)
67                 return;
68         vxdprintk("set_vx_info(%p[#%d.%d.%d])\t%s:%d\n",
69                 vxi, vxi?vxi->vx_id:0,
70                 vxi?atomic_read(&vxi->vx_usecnt):0,
71                 vxi?atomic_read(&vxi->vx_refcnt):0,
72                 _file, _line);
73         atomic_inc(&vxi->vx_refcnt);
74         *vxp = __get_vx_info(vxi, _file, _line);
75 }
76
77 #define clr_vx_info(p)  __clr_vx_info(p,__FILE__,__LINE__)
78
79 static inline void __clr_vx_info(struct vx_info **vxp,
80         const char *_file, int _line)
81 {
82         struct vx_info *vxo = *vxp;
83
84         if (!vxo)
85                 return;
86         vxdprintk("clr_vx_info(%p[#%d.%d.%d])\t%s:%d\n",
87                 vxo, vxo?vxo->vx_id:0,
88                 vxo?atomic_read(&vxo->vx_usecnt):0,
89                 vxo?atomic_read(&vxo->vx_refcnt):0,
90                 _file, _line);
91         *vxp = NULL;
92         wmb();
93         if (vxo && atomic_dec_and_test(&vxo->vx_refcnt))
94                 unhash_vx_info(vxo);
95         __put_vx_info(vxo, _file, _line);
96 }
97
98
99 #define task_get_vx_info(i)     __task_get_vx_info(i,__FILE__,__LINE__)
100
101 static __inline__ struct vx_info *__task_get_vx_info(struct task_struct *p,
102         const char *_file, int _line)
103 {
104         struct vx_info *vxi;
105         
106         task_lock(p);
107         vxi = __get_vx_info(p->vx_info, _file, _line);
108         task_unlock(p);
109         return vxi;
110 }
111
112
113 #define vx_verify_info(p,i)     \
114         __vx_verify_info((p)->vx_info,i,__FILE__,__LINE__)
115
116 static __inline__ void __vx_verify_info(
117         struct vx_info *vxa, struct vx_info *vxb,
118         const char *_file, int _line)
119 {
120         if (vxa == vxb)
121                 return;
122         printk(KERN_ERR "vx bad assumption (%p==%p) at %s:%d\n",
123                 vxa, vxb, _file, _line);
124 }
125
126
127 #define vx_task_xid(t)  ((t)->xid)
128
129 #define vx_current_xid() vx_task_xid(current)
130
131 #define vx_check(c,m)   __vx_check(vx_current_xid(),c,m)
132
133 #define vx_weak_check(c,m)      ((m) ? vx_check(c,m) : 1)
134
135
136 /*
137  * check current context for ADMIN/WATCH and
138  * optionally agains supplied argument
139  */
140 static __inline__ int __vx_check(xid_t cid, xid_t id, unsigned int mode)
141 {
142         if (mode & VX_ARG_MASK) {
143                 if ((mode & VX_IDENT) &&
144                         (id == cid))
145                         return 1;
146         }
147         if (mode & VX_ATR_MASK) {
148                 if ((mode & VX_DYNAMIC) &&
149                         (id >= MIN_D_CONTEXT) &&
150                         (id <= MAX_S_CONTEXT))
151                         return 1;
152                 if ((mode & VX_STATIC) &&
153                         (id > 1) && (id < MIN_D_CONTEXT))
154                         return 1;
155         }
156         return (((mode & VX_ADMIN) && (cid == 0)) ||
157                 ((mode & VX_WATCH) && (cid == 1)));
158 }
159
160
161 #define __vx_flags(v,m,f)       (((v) & (m)) ^ (f))
162
163 #define __vx_task_flags(t,m,f) \
164         (((t) && ((t)->vx_info)) ? \
165                 __vx_flags((t)->vx_info->vx_flags,(m),(f)) : 0)
166
167 #define vx_current_flags() \
168         ((current->vx_info) ? current->vx_info->vx_flags : 0)
169
170 #define vx_flags(m,f)   __vx_flags(vx_current_flags(),(m),(f))
171
172
173 #define vx_current_ccaps() \
174         ((current->vx_info) ? current->vx_info->vx_ccaps : 0)
175
176 #define vx_ccaps(c)     (vx_current_ccaps() & (c))
177
178 #define vx_current_bcaps() \
179         (((current->vx_info) && !vx_flags(VXF_STATE_SETUP, 0)) ? \
180         current->vx_info->vx_bcaps : cap_bset)
181
182
183 #define VX_DEBUG_ACC_RSS   0
184 #define VX_DEBUG_ACC_VM    0
185 #define VX_DEBUG_ACC_VML   0
186
187 #undef  vxdprintk
188 #if     (VX_DEBUG_ACC_RSS) || (VX_DEBUG_ACC_VM) || (VX_DEBUG_ACC_VML)
189 #define vxdprintk(x...) printk("vxd: " x)
190 #else
191 #define vxdprintk(x...)
192 #endif
193
194 #define vx_acc_page(m, d, v, r) \
195         __vx_acc_page(&(m->v), m->mm_vx_info, r, d, __FILE__, __LINE__)
196
197 static inline void __vx_acc_page(unsigned long *v, struct vx_info *vxi,
198                 int res, int dir, char *file, int line)
199 {
200         if (v) {
201                 if (dir > 0)
202                         ++(*v);
203                 else
204                         --(*v);
205         }
206         if (vxi) {
207                 if (dir > 0)
208                         atomic_inc(&vxi->limit.res[res]);
209                 else
210                         atomic_dec(&vxi->limit.res[res]);
211         }
212 }
213
214
215 #define vx_acc_pages(m, p, v, r) \
216         __vx_acc_pages(&(m->v), m->mm_vx_info, r, p, __FILE__, __LINE__)
217
218 static inline void __vx_acc_pages(unsigned long *v, struct vx_info *vxi,
219                 int res, int pages, char *file, int line)
220 {
221         if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
222                 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
223                 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
224                 vxdprintk("vx_acc_pages  [%5d,%2d]: %5d += %5d in %s:%d\n",
225                         (vxi?vxi->vx_id:-1), res,
226                         (vxi?atomic_read(&vxi->limit.res[res]):0),
227                         pages, file, line);
228         if (pages == 0)
229                 return;
230         if (v)
231                 *v += pages;
232         if (vxi)
233                 atomic_add(pages, &vxi->limit.res[res]);
234 }
235
236
237
238 #define vx_acc_vmpage(m,d)     vx_acc_page(m, d, total_vm,  RLIMIT_AS)
239 #define vx_acc_vmlpage(m,d)    vx_acc_page(m, d, locked_vm, RLIMIT_MEMLOCK)
240 #define vx_acc_rsspage(m,d)    vx_acc_page(m, d, rss,       RLIMIT_RSS)
241
242 #define vx_acc_vmpages(m,p)    vx_acc_pages(m, p, total_vm,  RLIMIT_AS)
243 #define vx_acc_vmlpages(m,p)   vx_acc_pages(m, p, locked_vm, RLIMIT_MEMLOCK)
244 #define vx_acc_rsspages(m,p)   vx_acc_pages(m, p, rss,       RLIMIT_RSS)
245
246 #define vx_pages_add(s,r,p)    __vx_acc_pages(0, s, r, p, __FILE__, __LINE__)
247 #define vx_pages_sub(s,r,p)    __vx_pages_add(s, r, -(p))
248
249 #define vx_vmpages_inc(m)      vx_acc_vmpage(m, 1)
250 #define vx_vmpages_dec(m)      vx_acc_vmpage(m,-1)
251 #define vx_vmpages_add(m,p)    vx_acc_vmpages(m, p)
252 #define vx_vmpages_sub(m,p)    vx_acc_vmpages(m,-(p))
253
254 #define vx_vmlocked_inc(m)     vx_acc_vmlpage(m, 1)
255 #define vx_vmlocked_dec(m)     vx_acc_vmlpage(m,-1)
256 #define vx_vmlocked_add(m,p)   vx_acc_vmlpages(m, p)
257 #define vx_vmlocked_sub(m,p)   vx_acc_vmlpages(m,-(p))
258
259 #define vx_rsspages_inc(m)     vx_acc_rsspage(m, 1)
260 #define vx_rsspages_dec(m)     vx_acc_rsspage(m,-1)
261 #define vx_rsspages_add(m,p)   vx_acc_rsspages(m, p)
262 #define vx_rsspages_sub(m,p)   vx_acc_rsspages(m,-(p))
263
264
265
266 #define vx_pages_avail(m, p, r) \
267         __vx_pages_avail((m)->mm_vx_info, (r), (p), __FILE__, __LINE__)
268
269 static inline int __vx_pages_avail(struct vx_info *vxi,
270                 int res, int pages, char *file, int line)
271 {
272         if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
273                 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
274                 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
275                 printk("vx_pages_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
276                         (vxi?vxi->vx_id:-1), res,
277                         (vxi?vxi->limit.rlim[res]:1),
278                         (vxi?atomic_read(&vxi->limit.res[res]):0),
279                         pages, file, line);
280         if (!vxi)
281                 return 1;
282         if (vxi->limit.rlim[res] == RLIM_INFINITY)
283                 return 1;
284         if (atomic_read(&vxi->limit.res[res]) + pages < vxi->limit.rlim[res])
285                 return 1;
286         return 0;
287 }
288
289 #define vx_vmpages_avail(m,p)  vx_pages_avail(m, p, RLIMIT_AS)
290 #define vx_vmlocked_avail(m,p) vx_pages_avail(m, p, RLIMIT_MEMLOCK)
291 #define vx_rsspages_avail(m,p) vx_pages_avail(m, p, RLIMIT_RSS)
292
293 /* file limits */
294
295 #define VX_DEBUG_ACC_FILE       0
296 #define VX_DEBUG_ACC_OPENFD     0
297
298 #undef  vxdprintk
299 #if     (VX_DEBUG_ACC_FILE) || (VX_DEBUG_ACC_OPENFD)
300 #define vxdprintk(x...) printk("vxd: " x)
301 #else
302 #define vxdprintk(x...)
303 #endif
304
305
306 #define vx_acc_cres(v,d,r) \
307         __vx_acc_cres((v), (r), (d), __FILE__, __LINE__)
308
309 static inline void __vx_acc_cres(struct vx_info *vxi,
310         int res, int dir, char *file, int line)
311 {
312         if (vxi) {
313         if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
314                         (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
315         printk("vx_acc_cres[%5d,%2d]: %5d%s in %s:%d\n",
316                         (vxi?vxi->vx_id:-1), res,
317                         (vxi?atomic_read(&vxi->limit.res[res]):0),
318                         (dir>0)?"++":"--", file, line);
319                 if (dir > 0)
320                         atomic_inc(&vxi->limit.res[res]);
321                 else
322                         atomic_dec(&vxi->limit.res[res]);
323         }
324 }
325
326 #define vx_files_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_NOFILE)
327 #define vx_files_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_NOFILE)
328
329 #define vx_openfd_inc(f) vx_acc_cres(current->vx_info, 1, RLIMIT_OPENFD)
330 #define vx_openfd_dec(f) vx_acc_cres(current->vx_info,-1, RLIMIT_OPENFD)
331
332 #define vx_cres_avail(v,n,r) \
333         __vx_cres_avail((v), (r), (n), __FILE__, __LINE__)
334
335 static inline int __vx_cres_avail(struct vx_info *vxi,
336                 int res, int num, char *file, int line)
337 {
338         if ((res == RLIMIT_NOFILE && VX_DEBUG_ACC_FILE) ||
339                 (res == RLIMIT_OPENFD && VX_DEBUG_ACC_OPENFD))
340                 printk("vx_cres_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
341                         (vxi?vxi->vx_id:-1), res,
342                         (vxi?vxi->limit.rlim[res]:1),
343                         (vxi?atomic_read(&vxi->limit.res[res]):0),
344                         num, file, line);
345         if (!vxi)
346                 return 1;
347         if (vxi->limit.rlim[res] == RLIM_INFINITY)
348                 return 1;
349         if (vxi->limit.rlim[res] < atomic_read(&vxi->limit.res[res]) + num)
350                 return 0;
351         return 1;
352 }
353
354 #define vx_files_avail(n) \
355         vx_cres_avail(current->vx_info, (n), RLIMIT_NOFILE)
356
357 #define vx_openfd_avail(n) \
358         vx_cres_avail(current->vx_info, (n), RLIMIT_OPENFD)
359
360 /* socket limits */
361
362 #define vx_sock_inc(f)  vx_acc_cres(current->vx_info, 1, VLIMIT_SOCK)
363 #define vx_sock_dec(f)  vx_acc_cres(current->vx_info,-1, VLIMIT_SOCK)
364
365 #define vx_sock_avail(n) \
366         vx_cres_avail(current->vx_info, (n), VLIMIT_SOCK)
367
368 /* procfs ioctls */
369
370 #define FIOC_GETXFLG    _IOR('x', 5, long)
371 #define FIOC_SETXFLG    _IOW('x', 6, long)
372
373 /* utsname virtualization */
374
375 static inline struct new_utsname *vx_new_utsname(void)
376 {
377         if (current->vx_info)
378                 return &current->vx_info->cvirt.utsname;
379         return &system_utsname;
380 }
381
382 #define vx_new_uts(x)           ((vx_new_utsname())->x)
383
384 /* generic flag merging */
385
386 #define vx_mask_flags(v,f,m)    (((v) & ~(m)) | ((f) & (m)))
387
388 #define vx_mask_mask(v,f,m)     (((v) & ~(m)) | ((v) & (f) & (m)))
389
390
391 /* socket accounting */
392
393 #include <linux/socket.h>
394
395 static inline int vx_sock_type(int family)
396 {
397         int type = 4;
398
399         if (family > 0 && family < 3)
400                 type = family;
401         else if (family == PF_INET6)
402                 type = 3;
403         return type;
404 }
405
406 #define vx_acc_sock(v,f,p,s) \
407         __vx_acc_sock((v), (f), (p), (s), __FILE__, __LINE__)
408
409 static inline void __vx_acc_sock(struct vx_info *vxi,
410         int family, int pos, int size, char *file, int line)
411 {
412         if (vxi) {
413                 int type = vx_sock_type(family);
414
415                 atomic_inc(&vxi->cacct.sock[type][pos].count);
416                 atomic_add(size, &vxi->cacct.sock[type][pos].total);
417         }
418 }
419
420 #define vx_sock_recv(sk,s) \
421         vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 0, (s))
422 #define vx_sock_send(sk,s) \
423         vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 1, (s))
424 #define vx_sock_fail(sk,s) \
425         vx_acc_sock((sk)->sk_vx_info, (sk)->sk_family, 2, (s))
426
427
428 #define sock_vx_init(s)  do {           \
429         (s)->sk_xid = 0;                \
430         (s)->sk_vx_info = NULL;         \
431         } while (0)
432
433
434 /* pid faking stuff */
435
436
437 #define vx_map_tgid(v,p) \
438         __vx_map_tgid((v), (p), __FILE__, __LINE__)
439
440 static inline int __vx_map_tgid(struct vx_info *vxi, int pid,
441         char *file, int line)
442 {
443         if (vxi && __vx_flags(vxi->vx_flags, VXF_INFO_INIT, 0)) {
444                 vxdprintk("vx_map_tgid: %p/%llx: %d -> %d in %s:%d\n",
445                         vxi, vxi->vx_flags, pid,
446                         (pid == vxi->vx_initpid)?1:pid,
447                         file, line);
448                 if (pid == vxi->vx_initpid)
449                         return 1;
450         }
451         return pid;
452 }
453
454 #define vx_rmap_tgid(v,p) \
455         __vx_rmap_tgid((v), (p), __FILE__, __LINE__)
456
457 static inline int __vx_rmap_tgid(struct vx_info *vxi, int pid,
458         char *file, int line)
459 {
460         if (vxi && __vx_flags(vxi->vx_flags, VXF_INFO_INIT, 0)) {
461                 vxdprintk("vx_rmap_tgid: %p/%llx: %d -> %d in %s:%d\n",
462                         vxi, vxi->vx_flags, pid,
463                         (pid == 1)?vxi->vx_initpid:pid,
464                         file, line);
465                 if ((pid == 1) && vxi->vx_initpid)
466                         return vxi->vx_initpid;
467         }
468         return pid;
469 }
470
471 #undef  vxdprintk
472 #define vxdprintk(x...)
473
474 #endif