Free memory from afs_events
[openafs.git] / src / afs / afs_osi.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef _AFS_OSI_
11 #define _AFS_OSI_
12
13 #include "h/types.h"
14 #include "h/param.h"
15
16 #ifdef AFS_FBSD_ENV
17 #include <sys/condvar.h>
18 #endif
19
20 #ifdef AFS_SGI_ENV
21 #include "sys/sema.h"           /* for kcondvar_t */
22 #endif
23
24 #ifdef AFS_NBSD_ENV
25 #include <sys/lock.h>
26 #endif
27
28 #ifdef AFS_LINUX20_ENV
29 #ifndef _LINUX_CODA_FS_I
30 #define _LINUX_CODA_FS_I
31 #define _CODA_HEADER_
32 struct coda_inode_info {
33 };
34 #endif
35 #ifndef _LINUX_XFS_FS_I
36 #define _LINUX_XFS_FS_I
37 struct xfs_inode_info {
38 };
39 #endif
40 #include "h/fs.h"
41 #include "h/mm.h"
42 #endif
43
44 #if defined(AFS_NBSD50_ENV)
45 # if !defined(DEF_CADDR_T)
46 typedef char * caddr_t;
47 #define DEF_CADDR_T
48 # endif
49 #endif
50
51
52 /* this is just a dummy type decl, we're really using struct sockets here */
53 struct osi_socket {
54     int junk;
55 };
56
57 struct osi_stat {
58     afs_int32 size;             /* file size in bytes */
59     afs_int32 mtime;            /* modification date */
60     afs_int32 atime;            /* access time */
61 };
62
63 struct osi_file {
64     afs_int32 size;             /* file size in bytes XXX Must be first field XXX */
65 #ifdef AFS_LINUX26_ENV
66     struct file *filp;          /* May need this if we really open the file. */
67 #else
68 #ifdef AFS_LINUX22_ENV
69     struct dentry dentry;       /* merely to hold the pointer to the inode. */
70     struct file file;           /* May need this if we really open the file. */
71 #else
72     struct vnode *vnode;
73 #endif
74 #endif
75 #if     defined(AFS_HPUX102_ENV)
76     k_off_t offset;
77 #else
78 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
79     afs_offs_t offset;
80 #else
81     afs_int32 offset;
82 #endif
83 #endif
84     int (*proc) (struct osi_file * afile, afs_int32 code);      /* proc, which, if not null, is called on writes */
85     char *rock;                 /* rock passed to proc */
86 #if defined(UKERNEL)
87     int fd;                     /* file descriptor for user space files */
88 #endif                          /* defined(UKERNEL) */
89 };
90
91 struct osi_dev {
92 #if defined(AFS_XBSD_ENV)
93     struct mount *mp;
94     struct vnode *held_vnode;
95 #elif defined(AFS_AIX42_ENV)
96     dev_t dev;
97 #else
98     afs_int32 dev;
99 #endif
100 };
101
102 struct afs_osi_WaitHandle {
103 #ifdef AFS_FBSD_ENV
104     struct cv wh_condvar;
105     int wh_inited;
106 #else
107     caddr_t proc;               /* process waiting */
108 #endif
109 };
110
111 #define osi_SetFileProc(x,p)    ((x)->proc=(p))
112 #define osi_SetFileRock(x,r)    ((x)->rock=(r))
113 #define osi_GetFileProc(x)      ((x)->proc)
114 #define osi_GetFileRock(x)      ((x)->rock)
115
116 #ifdef  AFS_TEXT_ENV
117 #define osi_FlushText(vp) if (hcmp((vp)->f.m.DataVersion, (vp)->flushDV) > 0) \
118                             osi_FlushText_really(vp)
119 #else
120 #define osi_FlushText(vp)
121 #endif
122
123
124 #define AFSOP_STOP_RXEVENT   214        /* stop rx event deamon */
125 #define AFSOP_STOP_COMPLETE  215        /* afs has been shutdown */
126 #define AFSOP_STOP_RXK_LISTENER   217   /* stop rx listener daemon */
127
128
129 #define osi_NPACKETS    20      /* number of cluster pkts to alloc */
130
131 /*
132  * Various definitions for osi_sleep and its event hash table
133  * DFBSD and SUNOS have no osi_sleep, and HPUX has its own hack for this stuff
134  */
135 #define AFS_EVHASHSIZE  128     /* size of afs_evhasht, must be power of 2 */
136 typedef struct afs_event {
137     struct afs_event *next;     /* next in hash chain */
138     char *event;                /* lwp event: an address */
139     int refcount;               /* Is it in use? */
140     int seq;                    /* Sequence number: this is incremented
141                                  * by wakeup calls; wait will not return until
142                                  * it changes */
143 #if defined(AFS_AIX_ENV)
144     tid_t cond;
145 #elif defined(AFS_DARWIN_ENV)
146 #ifdef AFS_DARWIN80_ENV
147     lck_mtx_t *lck;
148     thread_t owner;
149 #endif
150     /* no cond member */
151 #elif defined(AFS_FBSD_ENV) || defined(AFS_OBSD_ENV)
152     int cond;                   /* "all this gluck should probably be replaced by CVs" */
153 #elif defined(AFS_LINUX_ENV) || defined(AFS_LINUX24_ENV)
154     wait_queue_head_t cond;
155 #elif defined(AFS_NBSD_ENV) || defined(AFS_SOLARIS_ENV) || defined(AFS_SGI_ENV)
156     kcondvar_t cond;            /* Currently associated condition variable */
157 #endif
158 } afs_event_t;
159 extern afs_event_t *afs_evhasht[AFS_EVHASHSIZE];        /* Hash table for events */
160 extern void shutdown_osisleep(void);
161
162
163 /*
164  * Default vnode related macros
165  *
166  * Darwin, all of the BSDs, and Linux have their own
167  */
168 #if !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_LINUX20_ENV)
169 # define        vType(vc)           (vc)->v.v_type
170 # define        vSetType(vc,type)   (vc)->v.v_type = (type)
171 # define        vSetVfsp(vc,vfsp)   (vc)->v.v_vfsp = (vfsp)
172 extern struct vnodeops *afs_ops;
173 # define        IsAfsVnode(v)       ((v)->v_op == afs_ops)
174 # define        SetAfsVnode(v)      (v)->v_op = afs_ops
175 #endif
176
177 struct vcache;
178 extern int osi_TryEvictVCache(struct vcache *, int *, int);
179 extern struct vcache *osi_NewVnode(void);
180 extern void osi_PrePopulateVCache(struct vcache *);
181 extern void osi_PostPopulateVCache(struct vcache *);
182 extern void osi_AttachVnode(struct vcache *, int seq);
183
184 /*
185  * In IRIX 6.5 and NetBSD we cannot have DEBUG turned on since certain
186  * system-defined structures are a different size with DEBUG on, the
187  * kernel is compiled without DEBUG on, and the resulting differences
188  * would break our ability to interact with the rest of the kernel.
189  *
190  * Is DEBUG only for turning the ASSERT() macro?  If so, we should
191  * be able to eliminate DEBUG entirely.
192  */
193 #if !defined(AFS_SGI65_ENV) && !defined(AFS_NBSD_ENV)
194 #ifndef DEBUG
195 #define DEBUG   1               /* Default is to enable debugging/logging */
196 #endif
197 #endif
198
199 /*
200  * Time related macros
201  */
202 #define osi_GetuTime(x) osi_GetTime(x)
203
204 /* osi_timeval_t exists because SGI 6.x has two sizes of timeval. */
205 /** In 64 bit Solaris the timeval structure has members that are 64 bit
206   * In the GetTime() interface we expect pointers to afs_int32. So the need to
207   * define osi_timeval_t to have 32 bit members. To make this less ambiguous
208   * we now use 32 bit quantities consistently all over the code.
209   * In 64 bit HP-UX the timeval structure has a 64 bit member.
210   */
211
212 #if defined(AFS_HPUX_ENV) || defined(AFS_LINUX_64BIT_KERNEL) || (defined(AFS_SGI61_ENV) && defined(KERNEL) && defined(_K64U64))
213 typedef struct {
214     afs_int32 tv_sec;
215     afs_int32 tv_usec;
216 } osi_timeval_t;
217 typedef struct {
218     afs_int32 tv_sec;
219     afs_int32 tv_usec;
220 } osi_timeval32_t;
221 #elif defined(AFS_SUN5_ENV)
222 typedef struct timeval32 osi_timeval_t;
223 typedef struct timeval32 osi_timeval32_t;
224 #else
225 typedef struct timeval osi_timeval_t;
226 typedef struct timeval osi_timeval32_t;
227 #endif /* AFS_SGI61_ENV */
228
229 #ifndef UKERNEL
230 #define osi_getpid()            getpid()
231 #endif
232
233 /*
234  * osi_ThreadUnique() should yield a value that can be found in ps
235  * output in order to draw correspondences between ICL traces and what
236  * is going on in the system.  So if ps cannot show thread IDs it is
237  * likely to be the process ID instead.
238  */
239 #ifdef AFS_FBSD_ENV
240 /* should use curthread, but 'ps' can't display it */
241 #define osi_ThreadUnique()      (curproc->p_pid)
242 #elif defined(AFS_LINUX_ENV)
243 #define osi_ThreadUnique()      (current->pid)
244 #elif defined(UKERNEL)
245 #define osi_ThreadUnique()      osi_getpid()
246 #else
247 #define osi_ThreadUnique()      getpid()
248 #endif
249
250
251
252 #ifdef AFS_GLOBAL_SUNLOCK
253 #define AFS_ASSERT_GLOCK() \
254     do { if (!ISAFS_GLOCK()) osi_Panic("afs global lock not held at %s:%d\n", __FILE__, __LINE__); } while (0)
255 #endif /* AFS_GLOBAL_SUNLOCK */
256
257 #ifdef RX_ENABLE_LOCKS
258 #define RX_AFS_GLOCK()          AFS_GLOCK()
259 #define RX_AFS_GUNLOCK()        AFS_GUNLOCK()
260 #else
261 #define RX_AFS_GLOCK()
262 #define RX_AFS_GUNLOCK()
263 #endif
264
265
266
267 #ifndef KERNEL
268 #define AFS_GLOCK()
269 #define AFS_GUNLOCK()
270 #define ISAFS_GLOCK() 1
271 #define AFS_ASSERT_GLOCK()
272 #endif
273
274 /* On an MP that uses multithreading, splnet is not sufficient to provide
275  * mutual exclusion because the other processors will not see it.  On some
276  * early multiprocessors (SunOS413 & SGI5.2) splnet actually obtains a global
277  * mutex, which this works in the UP expected way, it means that the whole MP
278  * can only take one interrupt at a time; a serious performance penalty. */
279
280 #if ((defined(AFS_GLOBAL_SUNLOCK) || defined(RX_ENABLE_LOCKS)) && !defined(AFS_HPUX_ENV)) || !defined(KERNEL)
281 #define SPLVAR
282 #define NETPRI
283 #define USERPRI
284 #endif
285
286 /*
287  * vnode/vcache ref count manipulation
288  */
289 #if defined(UKERNEL)
290 #define AFS_RELE(vp) do { VN_RELE(vp); } while (0)
291 #else /* defined(UKERNEL) */
292 #define AFS_RELE(vp) do { AFS_GUNLOCK(); VN_RELE(vp); AFS_GLOCK(); } while (0)
293 #endif /* defined(UKERNEL) */
294
295 /*
296  * For some reason we do bare refcount manipulation in some places, for some
297  * platforms.  The assumption is apparently that either we wouldn't call
298  * afs_inactive anyway (because we know the ref count is high), or that it's
299  * OK not to call it (because we don't expect CUnlinked or CDirty).
300  * (Also, of course, the vnode is assumed to be one of ours.  Can't use this
301  * macro for V-file vnodes.)
302  */
303 /* osi_vnhold is defined in PLATFORM/osi_machdep.h */
304 #define AFS_FAST_HOLD(vp) osi_vnhold((vp), 0)
305
306 #ifdef AFS_AIX_ENV
307 #define AFS_FAST_RELE(vp) VREFCOUNT_DEC(vp)
308 #else
309 #define AFS_FAST_RELE(vp) AFS_RELE(AFSTOV(vp))
310 #endif
311
312 /*
313  * MP safe versions of routines to copy memory between user space
314  * and kernel space. Call these to avoid taking page faults while
315  * holding the global lock.
316  */
317 #if defined(CAST_USER_ADDR_T) && !defined(UKERNEL) && !defined(AFS_DARWIN100_ENV)
318 #define __U(X) CAST_USER_ADDR_T((X))
319 #else
320 #define __U(X) (X)
321 #endif
322 #ifdef AFS_GLOBAL_SUNLOCK
323
324 #define AFS_COPYIN(SRC,DST,LEN,CODE)                            \
325         do {                                                    \
326             int haveGlock = ISAFS_GLOCK();                      \
327             if (haveGlock)                                      \
328                 AFS_GUNLOCK();                                  \
329             CODE = copyin(__U((SRC)),(DST),(LEN));      \
330             if (haveGlock)                                      \
331                 AFS_GLOCK();                                    \
332         } while(0)
333
334 #define AFS_COPYINSTR(SRC,DST,LEN,CNT,CODE)                     \
335         do {                                                    \
336             int haveGlock = ISAFS_GLOCK();                      \
337             if (haveGlock)                                      \
338                 AFS_GUNLOCK();                                  \
339             CODE = copyinstr(__U((SRC)),(DST),(LEN),(CNT));             \
340             if (haveGlock)                                      \
341                 AFS_GLOCK();                                    \
342         } while(0)
343
344 #define AFS_COPYOUT(SRC,DST,LEN,CODE)                           \
345         do {                                                    \
346             int haveGlock = ISAFS_GLOCK();                      \
347             if (haveGlock)                                      \
348                 AFS_GUNLOCK();                                  \
349             CODE = copyout((SRC),__U((DST)),(LEN));                     \
350             if (haveGlock)                                      \
351                 AFS_GLOCK();                                    \
352         } while(0)
353
354 #if defined(AFS_DARWIN80_ENV)
355 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
356         do {                                                    \
357             int haveGlock = ISAFS_GLOCK();                      \
358             if (haveGlock)                                      \
359                 AFS_GUNLOCK();                                  \
360             uio_setrw((UIO),(RW));                              \
361             CODE = uiomove((SRC),(LEN),(UIO));                  \
362             if (haveGlock)                                      \
363                 AFS_GLOCK();                                    \
364         } while(0)
365 #else
366 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
367 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
368         do {                                                    \
369             int haveGlock = ISAFS_GLOCK();                      \
370             if (haveGlock)                                      \
371                 AFS_GUNLOCK();                                  \
372             (UIO)->uio_rw = (RW);                               \
373             CODE = uiomove((SRC),(LEN),(UIO));                  \
374             if (haveGlock)                                      \
375                 AFS_GLOCK();                                    \
376         } while(0)
377 #else
378 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
379         do {                                                    \
380             int haveGlock = ISAFS_GLOCK();                      \
381             if (haveGlock)                                      \
382                 AFS_GUNLOCK();                                  \
383             CODE = uiomove((SRC),(LEN),(RW),(UIO));             \
384             if (haveGlock)                                      \
385                 AFS_GLOCK();                                    \
386         } while(0)
387 #endif
388 #endif /* AFS_DARWIN80_ENV */
389
390 #else /* AFS_GLOBAL_SUNLOCK */
391
392 #define AFS_COPYIN(SRC,DST,LEN,CODE)                            \
393         do {                                                    \
394             CODE = copyin(__U((SRC)),(DST),(LEN));                      \
395         } while(0)
396
397 #define AFS_COPYINSTR(SRC,DST,LEN,CNT,CODE)                     \
398         do {                                                    \
399             CODE = copyinstr(__U((SRC)),(DST),(LEN),(CNT));             \
400         } while(0)
401
402 #define AFS_COPYOUT(SRC,DST,LEN,CODE)                           \
403         do {                                                    \
404             CODE = copyout((SRC),__U((DST)),(LEN));                     \
405         } while(0)
406
407 #if defined(AFS_DARWIN80_ENV)
408 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
409         do {                                                    \
410             uio_setrw((UIO),(RW));                              \
411             CODE = uiomove((SRC),(LEN),(UIO));                  \
412         } while(0)
413 #elif defined(AFS_DARWIN_ENV) || (defined(AFS_XBSD_ENV) && !defined(AFS_NBSD40_ENV))
414 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
415         do {                                                    \
416             (UIO)->uio_rw = (RW);                               \
417             CODE = uiomove((SRC),(LEN),(UIO));                  \
418         } while(0)
419 #else
420 #define AFS_UIOMOVE(SRC,LEN,RW,UIO,CODE)                        \
421         do {                                                    \
422             CODE = uiomove((SRC),(LEN),(RW),(UIO));             \
423         } while(0)
424 #endif
425
426 #endif /* AFS_GLOBAL_SUNLOCK */
427
428 #ifdef AFS_DARWIN80_ENV
429 #define AFS_UIO_OFFSET(uio) uio_offset(uio)
430 #define AFS_UIO_RESID(uio) (int)uio_resid(uio)
431 #define AFS_UIO_SETOFFSET(uio, off) uio_setoffset(uio, off)
432 #define AFS_UIO_SETRESID(uio, val) uio_setresid(uio, val)
433 #else
434 #define AFS_UIO_OFFSET(uio) (uio)->uio_offset
435 #define AFS_UIO_RESID(uio) (uio)->uio_resid
436 #define AFS_UIO_SETOFFSET(uio, off) (uio)->uio_offset = off
437 #define AFS_UIO_SETRESID(uio, val) (uio)->uio_resid = val
438 #endif
439
440
441 /*
442  * encapsulation of kernel data structure accesses
443  */
444 #ifndef UKERNEL
445 #define setuerror(erval)        u.u_error = (erval)
446 #define getuerror()             u.u_error
447 #endif
448
449 /* Macros for vcache/vnode and vfs arguments to vnode and vfs ops.
450  * These are required for IRIX 6.4 and later, which pass behavior pointers.
451  * Note that the _CONVERT routines get the ";" here so that argument lists
452  * can have arguments after the OSI_x_CONVERT macro is called.
453  */
454 #define OSI_VN_ARG(V) V
455 #define OSI_VN_DECL(V) struct vnode *V
456 #define OSI_VN_CONVERT(V)
457 #define OSI_VC_ARG(V) V
458 #define OSI_VC_DECL(V) struct vcache *V
459 #define OSI_VC_CONVERT(V)
460 #define OSI_VFS_ARG(V) V
461 #define OSI_VFS_DECL(V) struct vfs *V
462 #define OSI_VFS_CONVERT(V)
463
464
465 /*
466 ** Macro for Solaris 2.6 returns 1 if file is larger than 2GB; else returns 0
467 */
468 #define AfsLargeFileUio(uio)       0
469 #define AfsLargeFileSize(pos, off) 0
470
471 /* Now include system specific OSI header file. It will redefine macros
472  * defined here as required by the OS.
473  */
474 #include "osi_machdep.h"
475
476 /* Declare any structures which use these macros after the OSI implementation
477  * has had the opportunity to redefine them.
478  */
479 extern afs_ucred_t afs_osi_cred, *afs_osi_credp;
480
481 #ifndef osi_curcred
482 #define osi_curcred() (u.u_cred)
483 #endif
484
485 #endif /* _AFS_OSI_ */