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