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
30 #include "rx_internal.h"
32 #endif /* AFS_PTHREAD_ENV */
40 #define EDQUOT WSAEDQUOT
42 #endif /* AFS_NT40_ENV */
46 * We currently only include below the errors that
47 * affect us the most. We should add to this list
48 * more code mappings, as necessary.
52 * Convert from the local (host) to the standard
53 * (network) system error code.
56 hton_syserr_conv(afs_int32 code)
63 else if (code == EDQUOT)
73 * Convert from the standard (Network) format to the
74 * local (host) system error code.
77 ntoh_syserr_conv(int code)
81 if (code == VDISKFULL)
83 else if (code == VOVERQUOTA)
97 * We provide the following because some systems (like aix) would fail if we pass
102 #ifdef AFS_PTHREAD_ENV
104 * This mutex protects the following global variables:
110 afs_kmutex_t osi_malloc_mutex;
111 #define LOCK_MALLOC_STATS MUTEX_ENTER(&osi_malloc_mutex);
112 #define UNLOCK_MALLOC_STATS MUTEX_EXIT(&osi_malloc_mutex);
114 #define LOCK_MALLOC_STATS
115 #define UNLOCK_MALLOC_STATS
116 #endif /* AFS_PTHREAD_ENV */
117 long osi_alloccnt = 0, osi_allocsize = 0;
118 static const char memZero;
120 osi_alloc(afs_int32 x)
123 * 0-length allocs may return NULL ptr from osi_kalloc, so we special-case
124 * things so that NULL returned iff an error occurred
127 return (char *)&memZero;
132 return (char *)(mem_alloc(x));
136 osi_free(char *x, afs_int32 size)
138 if ((x == &memZero) || !x)
142 osi_allocsize -= size;
150 #if defined(RX_ENABLE_LOCKS) && defined(RX_REFCOUNT_CHECK)
151 int rx_callHoldType = 0;
156 /* What follows is a lock database for RX. There is currently room for 400
157 * locks to be held. Routines panic if there is an error. To port, add
158 * RX_LOCKS_DB versions of MUTEX_{ENTER, EXIT, TRYENTER} and CV_*WAIT to
159 * rx_kmutex.h and define lock macros below.
161 #if (defined(AFS_AIX41_ENV) || (defined(AFS_SGI53_ENV) && defined(MP))) && defined(KERNEL)
164 Simple_lock rxdb_lock;
165 #define RXDB_LOCK_INIT() lock_alloc(&rxdb_lock, LOCK_ALLOC_PIN, 1, 0), \
166 simple_lock_init(&rxdb_lock)
167 #define RXDB_LOCK_ENTER() simple_lock(&rxdb_lock)
168 #define RXDB_LOCK_EXIT() simple_unlock(&rxdb_lock)
169 #else /* AFS_AIX41_ENV */
171 afs_kmutex_t rxdb_lock;
172 #define RXDB_LOCK_INIT() mutex_init(&rxdb_lock, "rxdb lock", 0, 0)
173 #define RXDB_LOCK_ENTER() AFS_MUTEX_ENTER(&rxdb_lock)
174 #define RXDB_LOCK_EXIT() mutex_exit(&rxdb_lock)
175 #endif /* AFS_SGI53_ENV */
176 #endif /* AFS_AIX41_ENV */
179 int RXDB_LockPos = 0;
181 #define RXDB_NLOCKS 32
183 afs_int32 id; /* id of lock holder. */
184 void *a; /* address of lock. */
185 u_short fileId; /* fileID# of RX file. */
191 #define RXDB_HASHSIZE 8
194 struct rxdb_lock_t rxdb_lockList[RXDB_NLOCKS];
195 short rxdb_idHash[RXDB_HASHSIZE];
196 #define RXDB_IDHASH(id) ((((u_long)id)>>1) & (RXDB_HASHSIZE-1))
198 /* Record locations of all locks we enter/exit. */
199 struct rxdb_lockloc_t {
200 u_short fileId; /* fileID# of RX file. */
205 #ifdef RX_LOCKS_COVERAGE
206 #define RXDB_NlockLocs 512
207 #define RXDB_LOCHASHSIZE 256
208 struct rxdb_lockloc_t rxdb_lockLocs[RXDB_NlockLocs];
209 short rxdb_lockLocHash[RXDB_LOCHASHSIZE];
210 #define RXDB_LOCHASH(a) ((((u_long)a)) & (RXDB_LOCHASHSIZE-1))
211 #endif /* RX_LOCKS_COVERAGE */
213 /* Element 0 of each of the above arrays serves as the pointer to the list of
219 static int initted = 0;
230 for (i = 1; i < RXDB_NLOCKS - 1; i++) {
231 rxdb_lockList[i].next = i + 1;
232 rxdb_lockList[i].prev = i - 1;
234 rxdb_lockList[0].next = 1;
235 rxdb_lockList[0].prev = 0;
236 rxdb_lockList[RXDB_NLOCKS - 1].next = 0;
237 rxdb_lockList[RXDB_NLOCKS - 1].prev = RXDB_NLOCKS - 2;
239 #ifdef RX_LOCKS_COVERAGE
240 for (i = 1; i < RXDB_NlockLocs - 1; i++) {
241 rxdb_lockLocs[i].next = i + 1;
242 rxdb_lockLocs[i].prev = i - 1;
244 rxdb_lockLocs[0].next = 1;
245 rxdb_lockLocs[0].prev = 0;
246 rxdb_lockLocs[RXDB_NlockLocs - 1].next = 0;
247 rxdb_lockLocs[RXDB_NlockLocs - 1].prev = RXDB_NlockLocs - 2;
248 #endif /* RX_LOCKS_COVERAGE */
253 #ifdef RX_LOCKS_COVERAGE
255 rxdb_RecordLockLocation(fileId, line)
256 afs_int32 fileId, line;
260 i = RXDB_LOCHASH(line);
262 /* Only enter lock location into list once. */
263 for (j = rxdb_lockLocHash[i]; j; j = rxdb_lockLocs[j].next) {
264 if ((rxdb_lockLocs[j].line == line)
265 && (rxdb_lockLocs[j].fileId == fileId))
269 /* Add lock to list. */
270 j = rxdb_lockLocs[0].next;
272 osi_Panic("rxdb_initLock: used up all the lock locations.\n");
275 /* Fix up free list. */
276 rxdb_lockLocs[0].next = rxdb_lockLocs[j].next;
278 /* Put new element at head of list. */
279 rxdb_lockLocs[j].next = rxdb_lockLocHash[i];
280 rxdb_lockLocs[j].prev = 0;
281 if (rxdb_lockLocHash[i]) {
282 rxdb_lockLocs[rxdb_lockLocHash[i]].prev = j;
284 rxdb_lockLocHash[i] = j;
286 /* Set data in element. */
287 rxdb_lockLocs[j].fileId = fileId;
288 rxdb_lockLocs[j].line = line;
291 #endif /* RX_LOCKS_COVERAGE */
294 /* Set lock as possessed by me. */
296 rxdb_grablock(a, id, fileId, line)
305 #ifdef RX_LOCKS_COVERAGE
306 rxdb_RecordLockLocation(fileId, line);
307 #endif /* RX_LOCKS_COVERAGE */
308 /* Is lock already held by anyone? */
309 for (i = 0; i < RXDB_HASHSIZE; i++) {
310 for (j = rxdb_idHash[i]; j; j = rxdb_lockList[j].next) {
311 if (rxdb_lockList[j].a == a) {
313 osi_Panic("rxdb_grablock: lock already held.");
319 j = rxdb_lockList[0].next;
321 osi_Panic("rxdb_grablock: rxdb_lockList is full.");
323 rxdb_lockList[0].next = rxdb_lockList[j].next;
325 /* Put element at head of list. */
326 rxdb_lockList[j].next = rxdb_idHash[i];
327 rxdb_lockList[j].prev = 0;
328 if (rxdb_idHash[i]) {
329 rxdb_lockList[rxdb_idHash[i]].prev = j;
333 /* Set data into element. */
334 rxdb_lockList[j].a = a;
335 rxdb_lockList[j].id = id;
336 rxdb_lockList[j].fileId = fileId;
337 rxdb_lockList[j].line = line;
343 rxdb_droplock(a, id, fileId, line)
353 #ifdef RX_LOCKS_COVERAGE
354 rxdb_RecordLockLocation(fileId, line);
355 #endif /* RX_LOCKS_COVERAGE */
358 /* Do I have the lock? */
359 i = rxdb_idHash[RXDB_IDHASH(id)];
360 for (j = i; j; j = rxdb_lockList[j].next) {
361 if (rxdb_lockList[j].a == a) {
368 osi_Panic("rxdb_unlock: lock not held by me.\n");
371 /* delete lock from queue. */
375 rxdb_idHash[i] = rxdb_lockList[j].next;
376 rxdb_lockList[rxdb_lockList[j].next].prev = 0;
378 if (rxdb_lockList[j].next)
379 rxdb_lockList[rxdb_lockList[j].next].prev = rxdb_lockList[j].prev;
380 rxdb_lockList[rxdb_lockList[j].prev].next = rxdb_lockList[j].next;
382 /* Put back on free list. */
383 rxdb_lockList[j].next = rxdb_lockList[0].next;
384 rxdb_lockList[j].prev = 0;
385 rxdb_lockList[0].next = j;
390 #endif /* (AIX41 || SGI53) && KERNEL */
392 #endif /* RX_LOCKS_DB */