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