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