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 */
39 #define EDQUOT WSAEDQUOT
41 #endif /* AFS_NT40_ENV */
45 * We currently only include below the errors that
46 * affect us the most. We should add to this list
47 * more code mappings, as necessary.
51 * Convert from the local (host) to the standard
52 * (network) system error code.
55 hton_syserr_conv(afs_int32 code)
62 else if (code == EDQUOT)
72 * Convert from the standard (Network) format to the
73 * local (host) system error code.
76 ntoh_syserr_conv(int code)
80 if (code == VDISKFULL)
82 else if (code == VOVERQUOTA)
96 * We provide the following because some systems (like aix) would fail if we pass
101 #ifdef AFS_PTHREAD_ENV
103 * This mutex protects the following global variables:
109 afs_kmutex_t osi_malloc_mutex;
110 #define LOCK_MALLOC_STATS MUTEX_ENTER(&osi_malloc_mutex);
111 #define UNLOCK_MALLOC_STATS MUTEX_EXIT(&osi_malloc_mutex);
113 #define LOCK_MALLOC_STATS
114 #define UNLOCK_MALLOC_STATS
115 #endif /* AFS_PTHREAD_ENV */
116 long osi_alloccnt = 0, osi_allocsize = 0;
117 static const char memZero;
119 osi_alloc(afs_int32 x)
122 * 0-length allocs may return NULL ptr from osi_kalloc, so we special-case
123 * things so that NULL returned iff an error occurred
126 return (char *)&memZero;
131 return (char *)(mem_alloc(x));
135 osi_free(char *x, afs_int32 size)
137 if ((x == &memZero) || !x)
141 osi_allocsize -= size;
149 #if defined(RX_ENABLE_LOCKS) && defined(RX_REFCOUNT_CHECK)
150 int rx_callHoldType = 0;
155 /* What follows is a lock database for RX. There is currently room for 400
156 * locks to be held. Routines panic if there is an error. To port, add
157 * RX_LOCKS_DB versions of MUTEX_{ENTER, EXIT, TRYENTER} and CV_*WAIT to
158 * rx_kmutex.h and define lock macros below.
160 #if (defined(AFS_AIX41_ENV) || (defined(AFS_SGI53_ENV) && defined(MP))) && defined(KERNEL)
163 Simple_lock rxdb_lock;
164 #define RXDB_LOCK_INIT() lock_alloc(&rxdb_lock, LOCK_ALLOC_PIN, 1, 0), \
165 simple_lock_init(&rxdb_lock)
166 #define RXDB_LOCK_ENTER() simple_lock(&rxdb_lock)
167 #define RXDB_LOCK_EXIT() simple_unlock(&rxdb_lock)
168 #else /* AFS_AIX41_ENV */
170 afs_kmutex_t rxdb_lock;
171 #define RXDB_LOCK_INIT() mutex_init(&rxdb_lock, "rxdb lock", 0, 0)
172 #define RXDB_LOCK_ENTER() AFS_MUTEX_ENTER(&rxdb_lock)
173 #define RXDB_LOCK_EXIT() mutex_exit(&rxdb_lock)
174 #endif /* AFS_SGI53_ENV */
175 #endif /* AFS_AIX41_ENV */
178 int RXDB_LockPos = 0;
180 #define RXDB_NLOCKS 32
182 afs_int32 id; /* id of lock holder. */
183 void *a; /* address of lock. */
184 u_short fileId; /* fileID# of RX file. */
190 #define RXDB_HASHSIZE 8
193 struct rxdb_lock_t rxdb_lockList[RXDB_NLOCKS];
194 short rxdb_idHash[RXDB_HASHSIZE];
195 #define RXDB_IDHASH(id) ((((u_long)id)>>1) & (RXDB_HASHSIZE-1))
197 /* Record locations of all locks we enter/exit. */
198 struct rxdb_lockloc_t {
199 u_short fileId; /* fileID# of RX file. */
204 #ifdef RX_LOCKS_COVERAGE
205 #define RXDB_NlockLocs 512
206 #define RXDB_LOCHASHSIZE 256
207 struct rxdb_lockloc_t rxdb_lockLocs[RXDB_NlockLocs];
208 short rxdb_lockLocHash[RXDB_LOCHASHSIZE];
209 #define RXDB_LOCHASH(a) ((((u_long)a)) & (RXDB_LOCHASHSIZE-1))
210 #endif /* RX_LOCKS_COVERAGE */
212 /* Element 0 of each of the above arrays serves as the pointer to the list of
218 static int initted = 0;
229 for (i = 1; i < RXDB_NLOCKS - 1; i++) {
230 rxdb_lockList[i].next = i + 1;
231 rxdb_lockList[i].prev = i - 1;
233 rxdb_lockList[0].next = 1;
234 rxdb_lockList[0].prev = 0;
235 rxdb_lockList[RXDB_NLOCKS - 1].next = 0;
236 rxdb_lockList[RXDB_NLOCKS - 1].prev = RXDB_NLOCKS - 2;
238 #ifdef RX_LOCKS_COVERAGE
239 for (i = 1; i < RXDB_NlockLocs - 1; i++) {
240 rxdb_lockLocs[i].next = i + 1;
241 rxdb_lockLocs[i].prev = i - 1;
243 rxdb_lockLocs[0].next = 1;
244 rxdb_lockLocs[0].prev = 0;
245 rxdb_lockLocs[RXDB_NlockLocs - 1].next = 0;
246 rxdb_lockLocs[RXDB_NlockLocs - 1].prev = RXDB_NlockLocs - 2;
247 #endif /* RX_LOCKS_COVERAGE */
252 #ifdef RX_LOCKS_COVERAGE
254 rxdb_RecordLockLocation(fileId, line)
255 afs_int32 fileId, line;
259 i = RXDB_LOCHASH(line);
261 /* Only enter lock location into list once. */
262 for (j = rxdb_lockLocHash[i]; j; j = rxdb_lockLocs[j].next) {
263 if ((rxdb_lockLocs[j].line == line)
264 && (rxdb_lockLocs[j].fileId == fileId))
268 /* Add lock to list. */
269 j = rxdb_lockLocs[0].next;
271 osi_Panic("rxdb_initLock: used up all the lock locations.\n");
274 /* Fix up free list. */
275 rxdb_lockLocs[0].next = rxdb_lockLocs[j].next;
277 /* Put new element at head of list. */
278 rxdb_lockLocs[j].next = rxdb_lockLocHash[i];
279 rxdb_lockLocs[j].prev = 0;
280 if (rxdb_lockLocHash[i]) {
281 rxdb_lockLocs[rxdb_lockLocHash[i]].prev = j;
283 rxdb_lockLocHash[i] = j;
285 /* Set data in element. */
286 rxdb_lockLocs[j].fileId = fileId;
287 rxdb_lockLocs[j].line = line;
290 #endif /* RX_LOCKS_COVERAGE */
293 /* Set lock as possessed by me. */
295 rxdb_grablock(a, id, fileId, line)
304 #ifdef RX_LOCKS_COVERAGE
305 rxdb_RecordLockLocation(fileId, line);
306 #endif /* RX_LOCKS_COVERAGE */
307 /* Is lock already held by anyone? */
308 for (i = 0; i < RXDB_HASHSIZE; i++) {
309 for (j = rxdb_idHash[i]; j; j = rxdb_lockList[j].next) {
310 if (rxdb_lockList[j].a == a) {
312 osi_Panic("rxdb_grablock: lock already held.");
318 j = rxdb_lockList[0].next;
320 osi_Panic("rxdb_grablock: rxdb_lockList is full.");
322 rxdb_lockList[0].next = rxdb_lockList[j].next;
324 /* Put element at head of list. */
325 rxdb_lockList[j].next = rxdb_idHash[i];
326 rxdb_lockList[j].prev = 0;
327 if (rxdb_idHash[i]) {
328 rxdb_lockList[rxdb_idHash[i]].prev = j;
332 /* Set data into element. */
333 rxdb_lockList[j].a = a;
334 rxdb_lockList[j].id = id;
335 rxdb_lockList[j].fileId = fileId;
336 rxdb_lockList[j].line = line;
342 rxdb_droplock(a, id, fileId, line)
352 #ifdef RX_LOCKS_COVERAGE
353 rxdb_RecordLockLocation(fileId, line);
354 #endif /* RX_LOCKS_COVERAGE */
357 /* Do I have the lock? */
358 i = rxdb_idHash[RXDB_IDHASH(id)];
359 for (j = i; j; j = rxdb_lockList[j].next) {
360 if (rxdb_lockList[j].a == a) {
367 osi_Panic("rxdb_unlock: lock not held by me.\n");
370 /* delete lock from queue. */
374 rxdb_idHash[i] = rxdb_lockList[j].next;
375 rxdb_lockList[rxdb_lockList[j].next].prev = 0;
377 if (rxdb_lockList[j].next)
378 rxdb_lockList[rxdb_lockList[j].next].prev = rxdb_lockList[j].prev;
379 rxdb_lockList[rxdb_lockList[j].prev].next = rxdb_lockList[j].next;
381 /* Put back on free list. */
382 rxdb_lockList[j].next = rxdb_lockList[0].next;
383 rxdb_lockList[j].prev = 0;
384 rxdb_lockList[0].next = j;
389 #endif /* (AIX41 || SGI53) && KERNEL */
391 #endif /* RX_LOCKS_DB */