2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <afs/sysincludes.h>
18 #include <afsincludes.h>
24 #include <sys/param.h>
27 #include <afs/errors.h>
29 #ifdef AFS_PTHREAD_ENV
31 #endif /* AFS_PTHREAD_ENV */
40 * We currently only include below the errors that
41 * affect us the most. We should add to this list
42 * more code mappings, as necessary.
46 * Convert from the local (host) to the standard
47 * (network) system error code.
50 hton_syserr_conv(register afs_int32 code)
52 register afs_int32 err;
56 #if !defined(AFS_SUN5_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
57 /* EDQUOT doesn't exist on solaris */
58 else if (code == EDQUOT)
68 * Convert from the standard (Network) format to the
69 * local (host) system error code.
72 ntoh_syserr_conv(int code)
74 register afs_int32 err;
76 if (code == VDISKFULL)
78 else if (code == VOVERQUOTA)
79 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
92 * We provide the following because some systems (like aix) would fail if we pass
97 #ifdef AFS_PTHREAD_ENV
99 * This mutex protects the following global variables:
105 pthread_mutex_t osi_malloc_mutex;
106 #define LOCK_MALLOC_STATS assert(pthread_mutex_lock(&osi_malloc_mutex)==0)
107 #define UNLOCK_MALLOC_STATS assert(pthread_mutex_unlock(&osi_malloc_mutex)==0)
109 #define LOCK_MALLOC_STATS
110 #define UNLOCK_MALLOC_STATS
111 #endif /* AFS_PTHREAD_ENV */
112 long osi_alloccnt = 0, osi_allocsize = 0;
113 static const char memZero;
115 osi_alloc(afs_int32 x)
118 * 0-length allocs may return NULL ptr from osi_kalloc, so we special-case
119 * things so that NULL returned iff an error occurred
122 return (char *)&memZero;
127 return (char *)(mem_alloc(x));
131 osi_free(char *x, afs_int32 size)
133 if ((x == &memZero) || !x)
137 osi_allocsize -= size;
145 #if defined(RX_ENABLE_LOCKS) && defined(RX_REFCOUNT_CHECK)
146 int rx_callHoldType = 0;
151 /* What follows is a lock database for RX. There is currently room for 400
152 * locks to be held. Routines panic if there is an error. To port, add
153 * RX_LOCKS_DB versions of MUTEX_{ENTER, EXIT, TRYENTER} and CV_*WAIT to
154 * rx_kmutex.h and define lock macros below.
156 #if (defined(AFS_AIX41_ENV) || (defined(AFS_SGI53_ENV) && defined(MP))) && defined(KERNEL)
159 Simple_lock rxdb_lock;
160 #define RXDB_LOCK_INIT() lock_alloc(&rxdb_lock, LOCK_ALLOC_PIN, 1, 0), \
161 simple_lock_init(&rxdb_lock)
162 #define RXDB_LOCK_ENTER() simple_lock(&rxdb_lock)
163 #define RXDB_LOCK_EXIT() simple_unlock(&rxdb_lock)
164 #else /* AFS_AIX41_ENV */
166 afs_kmutex_t rxdb_lock;
167 #define RXDB_LOCK_INIT() mutex_init(&rxdb_lock, "rxdb lock", 0, 0)
168 #define RXDB_LOCK_ENTER() AFS_MUTEX_ENTER(&rxdb_lock)
169 #define RXDB_LOCK_EXIT() mutex_exit(&rxdb_lock)
170 #endif /* AFS_SGI53_ENV */
171 #endif /* AFS_AIX41_ENV */
174 int RXDB_LockPos = 0;
176 #define RXDB_NLOCKS 32
178 afs_int32 id; /* id of lock holder. */
179 void *a; /* address of lock. */
180 u_short fileId; /* fileID# of RX file. */
186 #define RXDB_HASHSIZE 8
189 struct rxdb_lock_t rxdb_lockList[RXDB_NLOCKS];
190 short rxdb_idHash[RXDB_HASHSIZE];
191 #define RXDB_IDHASH(id) ((((u_long)id)>>1) & (RXDB_HASHSIZE-1))
193 /* Record locations of all locks we enter/exit. */
194 struct rxdb_lockloc_t {
195 u_short fileId; /* fileID# of RX file. */
200 #ifdef RX_LOCKS_COVERAGE
201 #define RXDB_NlockLocs 512
202 #define RXDB_LOCHASHSIZE 256
203 struct rxdb_lockloc_t rxdb_lockLocs[RXDB_NlockLocs];
204 short rxdb_lockLocHash[RXDB_LOCHASHSIZE];
205 #define RXDB_LOCHASH(a) ((((u_long)a)) & (RXDB_LOCHASHSIZE-1))
206 #endif /* RX_LOCKS_COVERAGE */
208 /* Element 0 of each of the above arrays serves as the pointer to the list of
214 static int initted = 0;
225 for (i = 1; i < RXDB_NLOCKS - 1; i++) {
226 rxdb_lockList[i].next = i + 1;
227 rxdb_lockList[i].prev = i - 1;
229 rxdb_lockList[0].next = 1;
230 rxdb_lockList[0].prev = 0;
231 rxdb_lockList[RXDB_NLOCKS - 1].next = 0;
232 rxdb_lockList[RXDB_NLOCKS - 1].prev = RXDB_NLOCKS - 2;
234 #ifdef RX_LOCKS_COVERAGE
235 for (i = 1; i < RXDB_NlockLocs - 1; i++) {
236 rxdb_lockLocs[i].next = i + 1;
237 rxdb_lockLocs[i].prev = i - 1;
239 rxdb_lockLocs[0].next = 1;
240 rxdb_lockLocs[0].prev = 0;
241 rxdb_lockLocs[RXDB_NlockLocs - 1].next = 0;
242 rxdb_lockLocs[RXDB_NlockLocs - 1].prev = RXDB_NlockLocs - 2;
243 #endif /* RX_LOCKS_COVERAGE */
248 #ifdef RX_LOCKS_COVERAGE
250 rxdb_RecordLockLocation(fileId, line)
251 afs_int32 fileId, line;
255 i = RXDB_LOCHASH(line);
257 /* Only enter lock location into list once. */
258 for (j = rxdb_lockLocHash[i]; j; j = rxdb_lockLocs[j].next) {
259 if ((rxdb_lockLocs[j].line == line)
260 && (rxdb_lockLocs[j].fileId == fileId))
264 /* Add lock to list. */
265 j = rxdb_lockLocs[0].next;
267 osi_Panic("rxdb_initLock: used up all the lock locations.\n");
270 /* Fix up free list. */
271 rxdb_lockLocs[0].next = rxdb_lockLocs[j].next;
273 /* Put new element at head of list. */
274 rxdb_lockLocs[j].next = rxdb_lockLocHash[i];
275 rxdb_lockLocs[j].prev = 0;
276 if (rxdb_lockLocHash[i]) {
277 rxdb_lockLocs[rxdb_lockLocHash[i]].prev = j;
279 rxdb_lockLocHash[i] = j;
281 /* Set data in element. */
282 rxdb_lockLocs[j].fileId = fileId;
283 rxdb_lockLocs[j].line = line;
286 #endif /* RX_LOCKS_COVERAGE */
289 /* Set lock as possessed by me. */
291 rxdb_grablock(a, id, fileId, line)
300 #ifdef RX_LOCKS_COVERAGE
301 rxdb_RecordLockLocation(fileId, line);
302 #endif /* RX_LOCKS_COVERAGE */
303 /* Is lock already held by anyone? */
304 for (i = 0; i < RXDB_HASHSIZE; i++) {
305 for (j = rxdb_idHash[i]; j; j = rxdb_lockList[j].next) {
306 if (rxdb_lockList[j].a == a) {
308 osi_Panic("rxdb_grablock: lock already held.");
314 j = rxdb_lockList[0].next;
316 osi_Panic("rxdb_grablock: rxdb_lockList is full.");
318 rxdb_lockList[0].next = rxdb_lockList[j].next;
320 /* Put element at head of list. */
321 rxdb_lockList[j].next = rxdb_idHash[i];
322 rxdb_lockList[j].prev = 0;
323 if (rxdb_idHash[i]) {
324 rxdb_lockList[rxdb_idHash[i]].prev = j;
328 /* Set data into element. */
329 rxdb_lockList[j].a = a;
330 rxdb_lockList[j].id = id;
331 rxdb_lockList[j].fileId = fileId;
332 rxdb_lockList[j].line = line;
338 rxdb_droplock(a, id, fileId, line)
348 #ifdef RX_LOCKS_COVERAGE
349 rxdb_RecordLockLocation(fileId, line);
350 #endif /* RX_LOCKS_COVERAGE */
353 /* Do I have the lock? */
354 i = rxdb_idHash[RXDB_IDHASH(id)];
355 for (j = i; j; j = rxdb_lockList[j].next) {
356 if (rxdb_lockList[j].a == a) {
363 osi_Panic("rxdb_unlock: lock not held by me.\n");
366 /* delete lock from queue. */
370 rxdb_idHash[i] = rxdb_lockList[j].next;
371 rxdb_lockList[rxdb_lockList[j].next].prev = 0;
373 if (rxdb_lockList[j].next)
374 rxdb_lockList[rxdb_lockList[j].next].prev = rxdb_lockList[j].prev;
375 rxdb_lockList[rxdb_lockList[j].prev].next = rxdb_lockList[j].next;
377 /* Put back on free list. */
378 rxdb_lockList[j].next = rxdb_lockList[0].next;
379 rxdb_lockList[j].prev = 0;
380 rxdb_lockList[0].next = j;
385 #endif /* (AIX41 || SGI53) && KERNEL */
387 #endif /* RX_LOCKS_DB */