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 <afs/afsutil.h>
17 #include <afs/audit.h>
18 #include <afs/bubasics.h>
20 #include "budb_errs.h"
22 #include "budb_internal.h"
23 #include "error_macros.h"
25 #define DBH_POS(ptr) ( (char *) (ptr) - (char *) &db.h )
27 afs_int32 FreeAllLocks(struct rx_call *, afs_uint32);
28 afs_int32 FreeLock(struct rx_call *, afs_uint32);
29 afs_int32 GetInstanceId(struct rx_call *, afs_uint32 *);
30 afs_int32 GetLock(struct rx_call *, afs_uint32, afs_int32, afs_int32,
34 SBUDB_FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
38 code = FreeAllLocks(call, instanceId);
39 osi_auditU(call, BUDB_FrALckEvent, code, AUD_END);
44 FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
46 db_lockP startPtr, endPtr;
47 struct ubik_trans *ut;
50 if (callPermitted(call) == 0)
51 return (BUDB_NOTPERMITTED);
53 code = InitRPC(&ut, LOCKWRITE, 1);
57 startPtr = &db.h.textLocks[0];
58 endPtr = &db.h.textLocks[TB_NUM - 1];
59 while (startPtr <= endPtr) {
60 if ((ntohl(startPtr->lockState) == 1)
61 && (ntohl(startPtr->instanceId) == instanceId)
63 /* release the lock */
64 startPtr->lockState = 0; /* unlock it */
65 startPtr->lockTime = 0;
66 startPtr->expires = 0;
67 startPtr->instanceId = 0;
68 dbwrite(ut, DBH_POS(startPtr), (char *)startPtr,
73 code = ubik_EndTrans(ut);
78 SBUDB_FreeLock(struct rx_call *call, afs_uint32 lockHandle)
82 code = FreeLock(call, lockHandle);
83 osi_auditU(call, BUDB_FreLckEvent, code, AUD_END);
88 FreeLock(struct rx_call *call, afs_uint32 lockHandle)
91 struct ubik_trans *ut;
94 if (callPermitted(call) == 0)
95 return (BUDB_NOTPERMITTED);
97 code = InitRPC(&ut, LOCKWRITE, 1);
101 if (checkLockHandle(ut, lockHandle) == 0)
102 ABORT(BUDB_BADARGUMENT);
104 lockPtr = &db.h.textLocks[lockHandle - 1];
106 lockPtr->lockState = 0; /* unlock it */
107 lockPtr->lockTime = 0;
108 lockPtr->expires = 0;
109 lockPtr->instanceId = 0;
110 dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
112 code = ubik_EndTrans(ut);
121 SBUDB_GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
125 code = GetInstanceId(call, instanceId);
126 osi_auditU(call, BUDB_GetIIdEvent, code, AUD_END);
131 GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
133 struct ubik_trans *ut;
135 afs_int32 instanceValue;
137 LogDebug(4, "GetInstanceId:\n");
139 /* *** Allow anyone to get the instance id ***
140 * if ( callPermitted(call) == 0 )
141 * return(BUDB_NOTPERMITTED);
144 code = InitRPC(&ut, LOCKWRITE, 1);
148 instanceValue = ntohl(db.h.lastInstanceId) + 1;
150 set_header_word(ut, lastInstanceId, htonl(instanceValue));
152 code = ubik_EndTrans(ut);
158 SBUDB_GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
159 afs_int32 expiration, afs_uint32 *lockHandle)
163 code = GetLock(call, instanceId, lockName, expiration, lockHandle);
164 osi_auditU(call, BUDB_GetLckEvent, code, AUD_END);
169 GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
170 afs_int32 expiration, afs_uint32 *lockHandle)
174 struct ubik_trans *ut;
178 if (callPermitted(call) == 0)
179 return (BUDB_NOTPERMITTED);
181 if ((lockName < 0) || (lockName >= TB_NUM))
182 return (BUDB_BADARGUMENT);
184 /* get the current time */
185 gettimeofday(&tv, 0);
187 code = InitRPC(&ut, LOCKWRITE, 1);
191 lockPtr = &db.h.textLocks[lockName];
193 if ((ntohl(lockPtr->lockState) != 0) /* lock set */
194 &&(ntohl(lockPtr->expires) > tv.tv_sec) /* not expired */
196 if (ntohl(lockPtr->instanceId) == instanceId)
197 code = BUDB_SELFLOCKED;
203 lockPtr->lockState = htonl(1); /* lock it */
204 lockPtr->lockTime = htonl(tv.tv_sec); /* when locked */
205 lockPtr->expires = htonl(tv.tv_sec + expiration);
206 lockPtr->instanceId = htonl(instanceId);
207 code = dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
211 *lockHandle = (afs_uint32) (lockName + 1);
212 code = ubik_EndTrans(ut);
223 * 0 - if invalid handle
224 * 1 - if handle is valid
227 checkLockHandle(struct ubik_trans *ut, afs_uint32 lockHandle)
229 return (((lockHandle > 0) && (lockHandle <= TB_NUM)) ? 1 : 0);