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>
15 #include <sys/types.h>
19 #include <netinet/in.h>
22 #include <afs/afsutil.h>
24 #include <afs/bubasics.h>
25 #include "budb_errs.h"
27 #include "budb_internal.h"
28 #include "error_macros.h"
29 #include "afs/audit.h"
31 #define DBH_POS(ptr) ( (char *) (ptr) - (char *) &db.h )
33 afs_int32 FreeAllLocks(struct rx_call *, afs_uint32);
34 afs_int32 FreeLock(struct rx_call *, afs_uint32);
35 afs_int32 GetInstanceId(struct rx_call *, afs_uint32 *);
36 afs_int32 GetLock(struct rx_call *, afs_uint32, afs_int32, afs_int32,
40 SBUDB_FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
44 code = FreeAllLocks(call, instanceId);
45 osi_auditU(call, BUDB_FrALckEvent, code, AUD_END);
50 FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
52 db_lockP startPtr, endPtr;
53 struct ubik_trans *ut;
56 if (callPermitted(call) == 0)
57 return (BUDB_NOTPERMITTED);
59 code = InitRPC(&ut, LOCKWRITE, 1);
63 startPtr = &db.h.textLocks[0];
64 endPtr = &db.h.textLocks[TB_NUM - 1];
65 while (startPtr <= endPtr) {
66 if ((ntohl(startPtr->lockState) == 1)
67 && (ntohl(startPtr->instanceId) == instanceId)
69 /* release the lock */
70 startPtr->lockState = 0; /* unlock it */
71 startPtr->lockTime = 0;
72 startPtr->expires = 0;
73 startPtr->instanceId = 0;
74 dbwrite(ut, DBH_POS(startPtr), (char *)startPtr,
79 code = ubik_EndTrans(ut);
84 SBUDB_FreeLock(struct rx_call *call, afs_uint32 lockHandle)
88 code = FreeLock(call, lockHandle);
89 osi_auditU(call, BUDB_FreLckEvent, code, AUD_END);
94 FreeLock(struct rx_call *call, afs_uint32 lockHandle)
97 struct ubik_trans *ut;
100 if (callPermitted(call) == 0)
101 return (BUDB_NOTPERMITTED);
103 code = InitRPC(&ut, LOCKWRITE, 1);
107 if (checkLockHandle(ut, lockHandle) == 0)
108 ABORT(BUDB_BADARGUMENT);
110 lockPtr = &db.h.textLocks[lockHandle - 1];
112 lockPtr->lockState = 0; /* unlock it */
113 lockPtr->lockTime = 0;
114 lockPtr->expires = 0;
115 lockPtr->instanceId = 0;
116 dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
118 code = ubik_EndTrans(ut);
127 SBUDB_GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
131 code = GetInstanceId(call, instanceId);
132 osi_auditU(call, BUDB_GetIIdEvent, code, AUD_END);
137 GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
139 struct ubik_trans *ut;
141 afs_int32 instanceValue;
143 LogDebug(4, "GetInstanceId:\n");
145 /* *** Allow anyone to get the instance id ***
146 * if ( callPermitted(call) == 0 )
147 * return(BUDB_NOTPERMITTED);
150 code = InitRPC(&ut, LOCKWRITE, 1);
154 instanceValue = ntohl(db.h.lastInstanceId) + 1;
156 set_header_word(ut, lastInstanceId, htonl(instanceValue));
158 code = ubik_EndTrans(ut);
164 SBUDB_GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
165 afs_int32 expiration, afs_uint32 *lockHandle)
169 code = GetLock(call, instanceId, lockName, expiration, lockHandle);
170 osi_auditU(call, BUDB_GetLckEvent, code, AUD_END);
175 GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
176 afs_int32 expiration, afs_uint32 *lockHandle)
180 struct ubik_trans *ut;
184 if (callPermitted(call) == 0)
185 return (BUDB_NOTPERMITTED);
187 if ((lockName < 0) || (lockName >= TB_NUM))
188 return (BUDB_BADARGUMENT);
190 /* get the current time */
191 gettimeofday(&tv, 0);
193 code = InitRPC(&ut, LOCKWRITE, 1);
197 lockPtr = &db.h.textLocks[lockName];
199 if ((ntohl(lockPtr->lockState) != 0) /* lock set */
200 &&(ntohl(lockPtr->expires) > tv.tv_sec) /* not expired */
202 if (ntohl(lockPtr->instanceId) == instanceId)
203 code = BUDB_SELFLOCKED;
209 lockPtr->lockState = htonl(1); /* lock it */
210 lockPtr->lockTime = htonl(tv.tv_sec); /* when locked */
211 lockPtr->expires = htonl(tv.tv_sec + expiration);
212 lockPtr->instanceId = htonl(instanceId);
213 code = dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
217 *lockHandle = (afs_uint32) (lockName + 1);
218 code = ubik_EndTrans(ut);
229 * 0 - if invalid handle
230 * 1 - if handle is valid
233 checkLockHandle(struct ubik_trans *ut, afs_uint32 lockHandle)
235 return (((lockHandle > 0) && (lockHandle <= TB_NUM)) ? 1 : 0);