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>
16 #include <sys/types.h>
20 #include <netinet/in.h>
23 #include <afs/afsutil.h>
25 #include <afs/bubasics.h>
26 #include "budb_errs.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(), FreeLock(), GetInstanceId(), GetLock();
36 SBUDB_FreeAllLocks(call, instanceId)
38 afs_uint32 instanceId;
42 code = FreeAllLocks(call, instanceId);
43 osi_auditU(call, BUDB_FrALckEvent, code, AUD_END);
48 FreeAllLocks(call, instanceId)
50 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(call, lockHandle)
86 afs_uint32 lockHandle;
90 code = FreeLock(call, lockHandle);
91 osi_auditU(call, BUDB_FreLckEvent, code, AUD_END);
96 FreeLock(call, lockHandle)
98 afs_uint32 lockHandle;
100 db_lockP lockPtr = 0;
101 struct ubik_trans *ut;
104 if (callPermitted(call) == 0)
105 return (BUDB_NOTPERMITTED);
107 code = InitRPC(&ut, LOCKWRITE, 1);
111 if (checkLockHandle(ut, lockHandle) == 0)
112 ABORT(BUDB_BADARGUMENT);
114 lockPtr = &db.h.textLocks[lockHandle - 1];
116 lockPtr->lockState = 0; /* unlock it */
117 lockPtr->lockTime = 0;
118 lockPtr->expires = 0;
119 lockPtr->instanceId = 0;
120 dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
122 code = ubik_EndTrans(ut);
131 SBUDB_GetInstanceId(call, instanceId)
132 struct rx_call *call;
133 afs_uint32 *instanceId;
137 code = GetInstanceId(call, instanceId);
138 osi_auditU(call, BUDB_GetIIdEvent, code, AUD_END);
143 GetInstanceId(call, instanceId)
144 struct rx_call *call;
145 afs_uint32 *instanceId;
147 struct ubik_trans *ut;
149 afs_int32 instanceValue;
151 LogDebug(4, "GetInstanceId:\n");
153 /* *** Allow anyone to get the instance id ***
154 * if ( callPermitted(call) == 0 )
155 * return(BUDB_NOTPERMITTED);
158 code = InitRPC(&ut, LOCKWRITE, 1);
162 instanceValue = ntohl(db.h.lastInstanceId) + 1;
164 set_header_word(ut, lastInstanceId, htonl(instanceValue));
166 code = ubik_EndTrans(ut);
172 SBUDB_GetLock(call, instanceId, lockName, expiration, lockHandle)
173 struct rx_call *call;
174 afs_uint32 instanceId;
176 afs_int32 expiration;
177 afs_uint32 *lockHandle;
181 code = GetLock(call, instanceId, lockName, expiration, lockHandle);
182 osi_auditU(call, BUDB_GetLckEvent, code, AUD_END);
187 GetLock(call, instanceId, lockName, expiration, lockHandle)
188 struct rx_call *call;
189 afs_uint32 instanceId;
191 afs_int32 expiration;
192 afs_uint32 *lockHandle;
196 struct ubik_trans *ut;
200 if (callPermitted(call) == 0)
201 return (BUDB_NOTPERMITTED);
203 if ((lockName < 0) || (lockName >= TB_NUM))
204 return (BUDB_BADARGUMENT);
206 /* get the current time */
207 gettimeofday(&tv, 0);
209 code = InitRPC(&ut, LOCKWRITE, 1);
213 lockPtr = &db.h.textLocks[lockName];
215 if ((ntohl(lockPtr->lockState) != 0) /* lock set */
216 &&(ntohl(lockPtr->expires) > tv.tv_sec) /* not expired */
218 if (ntohl(lockPtr->instanceId) == instanceId)
219 code = BUDB_SELFLOCKED;
225 lockPtr->lockState = htonl(1); /* lock it */
226 lockPtr->lockTime = htonl(tv.tv_sec); /* when locked */
227 lockPtr->expires = htonl(tv.tv_sec + expiration);
228 lockPtr->instanceId = htonl(instanceId);
229 code = dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
233 *lockHandle = (afs_uint32) (lockName + 1);
234 code = ubik_EndTrans(ut);
245 * 0 - if invalid handle
246 * 1 - if handle is valid
249 checkLockHandle(ut, lockHandle)
250 struct ubik_trans *ut;
251 afs_uint32 lockHandle;
253 return (((lockHandle > 0) && (lockHandle <= TB_NUM)) ? 1 : 0);