Move GLOCK initialisation to platform directories
[openafs.git] / src / afs / IRIX / osi_machdep.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 /*
11  * IRIX OSI header file. Extends afs_osi.h.
12  *
13  * afs_osi.h includes this file, which is the only way this file should
14  * be included in a source file. This file can redefine macros declared in
15  * afs_osi.h.
16  */
17 #ifndef _OSI_MACHDEP_H_
18 #define _OSI_MACHDEP_H_
19
20 #include <sys/sema.h>
21 #include <sys/pda.h>
22 extern kmutex_t afs_global_lock;
23
24
25 #undef osi_Time
26 extern time_t time;
27 #define osi_Time() (time)
28
29 /* This gets redefined from ucred to cred in osi_vfs.h, just do it right */
30 typedef struct cred afs_ucred_t;
31 typedef struct proc afs_proc_t;
32
33 #undef gop_lookupname
34 #define gop_lookupname(fnamep,segflg,followlink,compvpp) lookupname((fnamep),(segflg),(followlink),NULL,(compvpp), NULL)
35
36 #undef gop_lookupname_user
37 #define gop_lookupname_user(fnamep,segflg,followlink,compvpp) lookupname((fnamep),(segflg),(followlink),NULL,(compvpp), NULL)
38
39 #define osi_vnhold(avc, r)  do { VN_HOLD(AFSTOV(avc)); } while(0)
40
41 #undef afs_osi_Alloc_NoSleep
42 extern void *afs_osi_Alloc_NoSleep(size_t size);
43
44
45 #ifdef AFS_SGI64_ENV
46 #include <sys/flock.h>
47 extern flid_t osi_flid;
48 #define v_op v_bh.bh_first->bd_ops
49 #define v_data v_bh.bh_first->bd_pdata
50 #define vfs_data vfs_bh.bh_first->bd_pdata
51 #endif /* AFS_SGI64_ENV */
52
53 #define ifnet_flags(x) (x?(x)->if_flags:0)
54
55 /*
56  * Global lock, semaphore, mutex and state vector support.
57  */
58 #define SV_INIT(cv, nm, t, c)   cv_init(cv, nm, t, c)
59 /* Spinlock macros */
60 #define SV_TYPE sv_t
61 #define SV_SIGNAL(cv)           sv_signal(cv)
62 #define SPINLOCK_INIT(l, nm)    spinlock_init((l),(nm))
63 #define SPLOCK(l)               mp_mutex_spinlock(&(l))
64 #define SPUNLOCK(l,s)           mp_mutex_spinunlock(&(l),s)
65 #define SP_WAIT(l, s, cv, p)    mp_sv_wait_sig(cv, p, (void*)(&(l)), s)
66 #ifdef AFS_SGI64_ENV
67 #ifdef AFS_SGI65_ENV
68 /* Add PLTWAIT for afsd's to wait so we don't rack up the load average. */
69 #define AFSD_PRI() ((kt_basepri(curthreadp) == PTIME_SHARE) ? PZERO : (PZERO|PLTWAIT))
70 #undef AFS_MUTEX_ENTER
71 #define AFS_MUTEX_ENTER(mp) \
72   MACRO_BEGIN \
73     register struct kthread *_kthreadP; \
74     while(mutex_tryenter(mp) == 0) { \
75       _kthreadP = (struct kthread*)mutex_owner(mp); \
76       if (_kthreadP != NULL && _kthreadP->k_sonproc == CPU_NONE) { \
77           mutex_lock(mp, AFSD_PRI()); \
78           break; \
79       } \
80     } \
81   MACRO_END
82
83 #else /* AFS_SGI65_ENV */
84 /* Add PLTWAIT for afsd's to wait so we don't rack up the load average. */
85 #define AFSD_PRI() ((curprocp && curprocp->p_rss==0) ? (PZERO|PLTWAIT) : PZERO)
86
87 #define AFS_MUTEX_ENTER(mp) \
88   MACRO_BEGIN \
89     kthread_t *kt; \
90     while(mutex_tryenter(mp) == 0) { \
91         kt = mutex_owner(mp); \
92         if (kt != NULL && kt->k_sonproc == CPU_NONE) { \
93             mutex_lock(mp, AFSD_PRI()); \
94             break; \
95         } \
96     } \
97   MACRO_END
98 #endif /* AFS_SGI65_ENV */
99
100 #define cv_timedwait(cv, l, t)  {                               \
101                                 sv_timedwait(cv, AFSD_PRI(), l, 0, 0, &(t), \
102                                              (struct timespec*)0);      \
103                                 AFS_MUTEX_ENTER(l); \
104                                 }
105 #ifdef cv_wait
106 #undef cv_wait
107 #endif
108 #define cv_wait(cv, l) {                                        \
109                         sv_wait(cv, AFSD_PRI(),  l, 0); \
110                         AFS_MUTEX_ENTER(l); \
111                        }
112 #else /* AFS_SGI64_ENV */
113 #ifdef AFS_SGI62_ENV
114
115 #define AFS_MUTEX_ENTER(mp) \
116   MACRO_BEGIN \
117     register struct proc *_procP; \
118     while(mutex_tryenter(mp) == 0) { \
119         _procP = mutex_owner(mp); \
120         if (_procP != NULL && _procP->p_sonproc == CPU_NONE) { \
121             mutex_enter(mp); \
122             break; \
123         } \
124     } \
125   MACRO_END
126
127 #else /* AFS_SGI62_ENV */
128
129 #define AFS_MUTEX_ENTER(mp)     mutex_enter(mp)
130
131 #endif /* AFS_SGI62_ENV */
132
133 #define cv_timedwait(cv, l, t)  {                               \
134                                   sv_timedwait(cv, l, t);       \
135                                   AFS_GLOCK();                  \
136                                 }
137 #endif /* AFS_SGI64_ENV */
138
139 #if defined(KERNEL)
140 #if defined(MP)
141 #define _MP_NETLOCKS            /* to get sblock to work right */
142
143 /* On SGI mutex_owned doesn't work, so simulate this by remembering the owning
144  * thread explicitly.  This is only used for debugging so could be disabled for
145  * production builds.
146  *
147  * CAUTION -- The ISAFS_(RX)?GLOCK macros are not safe to use when the lock is
148  *     not held if the test may be made at interrupt level as the code may
149  *     appear to be running as the process that is (or last was) running at
150  *     non-interrupt level. Worse yet, the interrupt may occur just as the 
151  *     process is exiting, in which case, the pid may change from the start
152  *     of the interrupt to the end, since the u area has been changed. So,
153  *     at interrupt level, I'm using the base of the current interrupt stack.
154  *     Note that afs_osinet.c also modifies afs_global_owner for osi_Sleep and 
155  *     afs_osi_Wakeup. Changes made here should be reflected there as well.
156  * NOTE - As of 6.2, we can no longer use mutexes in interrupts, so the above
157  *     concern no longer exists.
158  */
159
160 #ifdef AFS_SGI64_ENV
161 #ifdef AFS_SGI65_ENV
162 /* Irix does not check for deadlocks unless it's a debug kernel. */
163 #define AFS_ASSERT_GNOTME() \
164     (!ISAFS_GLOCK() || (panic("afs global lock held be me"), 0))
165 #define AFS_GLOCK() \
166         { AFS_ASSERT_GNOTME(); AFS_MUTEX_ENTER(&afs_global_lock); }
167 #else
168 #define AFS_GLOCK() AFS_MUTEX_ENTER(&afs_global_lock)
169 #endif
170 #define AFS_GUNLOCK()  { AFS_ASSERT_GLOCK(); mutex_exit(&afs_global_lock); }
171 #define ISAFS_GLOCK() mutex_mine(&afs_global_lock)
172 #else
173 extern long afs_global_owner;
174 #define AFS_GLOCK() \
175   MACRO_BEGIN \
176     AFS_MUTEX_ENTER(&afs_global_lock) ; \
177     afs_global_owner = osi_ThreadUnique(); \
178   MACRO_END
179 #define AFS_GUNLOCK() \
180     { AFS_ASSERT_GLOCK(); afs_global_owner = 0; mutex_exit(&afs_global_lock); }
181 #define ISAFS_GLOCK() (osi_ThreadUnique() == afs_global_owner)
182 #endif /* AFS_SGI64_ENV */
183 #else /* MP */
184 #define AFS_GLOCK()
185 #define AFS_GUNLOCK()
186 #define ISAFS_GLOCK() 1
187
188 #define SPLVAR      register int splvar
189 #define NETPRI      splvar=splnet()
190 #define USERPRI     splx(splvar)
191
192
193 #endif /* MP */
194
195 #endif /* KERNEL  */
196
197 #if defined(AFS_SGI62_ENV)
198 # define osi_InitGlock() \
199         mutex_init(&afs_global_lock, MUTEX_DEFAULT, "afs_global_lock");
200 #else
201 # define osi_InitGlock() \
202         mutex_init(&afs_global_lock, "afs_global_lock", MUTEX_DEFAULT, NULL);
203 #endif
204
205 #ifdef AFS_SGI64_ENV
206 #define gop_rdwr(rw,gp,base,len,offset,segflg,ioflag,ulimit,cr,aresid) \
207    vn_rdwr((rw),(gp),(base),(len),(offset),(segflg),(ioflag),(ulimit),(cr),\
208            (int *)(aresid), &osi_flid)
209 #else
210 #define gop_rdwr(rw,gp,base,len,offset,segflg,ioflag,ulimit,cr,aresid) \
211    vn_rdwr((rw),(gp),(base),(len),(offset),(segflg),(ioflag),(ulimit),(cr), \
212            (int *)(aresid))
213 #endif
214
215 #ifdef AFS_SGI64_ENV
216 #undef suser
217 #define suser()         cap_able(CAP_DEVICE_MGT)
218 #endif
219 #define afs_suser(x)    suser()
220
221 #define afs_hz HZ
222
223 #ifdef AFS_SGI64_ENV
224 #undef setuerror
225 #undef getuerror
226 #endif
227
228
229 /* OS independent user structure stuff */
230 /*
231  * OSI_GET_CURRENT_PID
232  */
233 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
234 #define OSI_GET_CURRENT_PID() (u.u_procp->p_pid)
235 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
236
237 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
238 #define OSI_GET_CURRENT_PID() current_pid()
239 #endif /* AFS_SGI64_ENV */
240
241 #if defined(AFS_SGI65_ENV)
242 #define OSI_GET_CURRENT_PID() proc_pid(curproc())
243 #endif
244
245 #define getpid()  OSI_GET_CURRENT_PID()
246
247 /*
248  * OSI_GET_CURRENT_PROCP
249  */
250 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
251 #define OSI_GET_CURRENT_PROCP() (u.u_procp)
252 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
253
254 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
255 #define OSI_GET_CURRENT_PROCP() curprocp
256 #endif /* AFS_SGI64_ENV */
257
258 #if defined(AFS_SGI65_ENV)
259 #define OSI_GET_CURRENT_PROCP() UT_TO_PROC(curuthread)
260 #endif
261
262
263 /*
264  * OSI_GET_LOCKID
265  *
266  * Prior to IRIX 6.4, pid sufficed, now we need kthread.
267  */
268 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
269 #define OSI_GET_LOCKID() (u.u_procp->p_pid)
270 #define OSI_NO_LOCKID (-1)
271 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
272
273 #if defined(AFS_SGI64_ENV)
274 /* IRIX returns k_id, but this way, we've got the thread address for debugging. */
275 #define OSI_GET_LOCKID() \
276         (private.p_curkthread ? (uint64_t)private.p_curkthread : (uint64_t)0)
277 #define OSI_NO_LOCKID ((uint64_t)-1)
278 #endif /* AFS_SGI64_ENV */
279
280 /*
281  * OSI_GET_CURRENT_CRED
282  */
283 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
284 #define OSI_GET_CURRENT_CRED() (u.u_cred)
285 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
286
287 #if defined(AFS_SGI64_ENV)
288 #define OSI_GET_CURRENT_CRED() get_current_cred()
289 #endif /* AFS_SGI64_ENV */
290
291 #define osi_curcred()           OSI_GET_CURRENT_CRED()
292
293 /*
294  * OSI_SET_CURRENT_CRED
295  */
296 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
297 #define OSI_SET_CURRENT_CRED(x) u.u_cred=x
298 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
299
300 #if defined(AFS_SGI64_ENV)
301 #define OSI_SET_CURRENT_CRED(C) set_current_cred((C))
302 #endif /* AFS_SGI64_ENV */
303
304 /*
305  * OSI_GET_CURRENT_ABI
306  */
307 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
308 #define OSI_GET_CURRENT_ABI() (u.u_procp->p_abi)
309 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
310
311 #if defined(AFS_SGI64_ENV)
312 #define OSI_GET_CURRENT_ABI() get_current_abi()
313 #endif /* AFS_SGI64_ENV */
314
315 /*
316  * OSI_GET_CURRENT_SYSID
317  */
318 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
319 #define OSI_GET_CURRENT_SYSID() (u.u_procp->p_sysid)
320 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
321
322 #if defined(AFS_SGI64_ENV)
323 #define OSI_GET_CURRENT_SYSID() (curprocp->p_flid.fl_sysid)
324 #endif /* AFS_SGI64_ENV */
325
326 /*
327  * OSI_GET_CURRENT_COMM
328  */
329 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
330 #define OSI_GET_CURRENT_COMM() (u.u_comm)
331 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
332
333 #if defined(AFS_SGI64_ENV)
334 #define OSI_GET_CURRENT_COMM() (curprocp->p_comm)
335 #endif /* AFS_SGI64_ENV */
336
337 /*
338  * OSI_GET_CURRENT_CDIR
339  */
340 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
341 #define OSI_GET_CURRENT_CDIR() (u.u_cdir)
342 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
343
344 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
345 #define OSI_GET_CURRENT_CDIR() (curprocp->p_cdir)
346 #endif /* AFS_SGI64_ENV */
347
348 #if defined(AFS_SGI65_ENV)
349 #define OSI_GET_CURRENT_CDIR() (curuthread->ut_cdir)
350 #endif /* AFS_SGI65_ENV */
351
352
353 /*
354  * OSI_GET_CURRENT_RDIR
355  */
356 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
357 #define OSI_GET_CURRENT_RDIR() (u.u_rdir)
358 #endif /* AFS_SGI_ENV but not AFS_SGI64_ENV */
359
360 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
361 #define OSI_GET_CURRENT_RDIR() (curprocp->p_rdir)
362 #endif /* AFS_SGI64_ENV */
363
364 #if defined(AFS_SGI65_ENV)
365 #define OSI_GET_CURRENT_RDIR() (curuthread->ut_rdir)
366 #endif /* AFS_SGI65_ENV */
367
368
369
370 /* Macros for vcache/vnode and vfs arguments to vnode and vfs ops.
371  *
372  * Note that the _CONVERT routines get the ";" here so that argument lists
373  * can have arguments after the OSI_x_CONVERT macro is called.
374  */
375 #ifdef AFS_SGI64_ENV
376 #undef OSI_VN_ARG
377 #define OSI_VN_ARG(V) bhv_##V
378 #undef OSI_VN_DECL
379 #define OSI_VN_DECL(V)  bhv_desc_t *bhv_##V
380 #undef OSI_VN_CONVERT
381 #define OSI_VN_CONVERT(V) struct vnode * V = (struct vnode*)BHV_TO_VNODE(bhv_##V)
382 #undef OSI_VC_ARG
383 #define OSI_VC_ARG(V) bhv_##V
384 #undef OSI_VC_DECL
385 #define OSI_VC_DECL(V)  bhv_desc_t *bhv_##V
386 #undef OSI_VC_CONVERT
387 #define OSI_VC_CONVERT(V) struct vcache * V = VTOAFS(BHV_TO_VNODE(bhv_##V))
388 #undef OSI_VFS_ARG
389 #define OSI_VFS_ARG(V) bhv_##V
390 #undef OSI_VFS_DECL
391 #define OSI_VFS_DECL(V)  bhv_desc_t *bhv_##V
392 #undef OSI_VFS_CONVERT
393 #define OSI_VFS_CONVERT(V) struct vfs * V = (struct vfs*)bhvtovfs(bhv_##V)
394 #endif /* AFS_SGI64_ENV */
395
396
397
398
399 #endif /* _OSI_MACHDEP_H_ */