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