forked from openafs/openafs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_lock.c
235 lines (189 loc) · 5.37 KB
/
db_lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright 2000, International Business Machines Corporation and others.
* All Rights Reserved.
*
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
*/
#include <afsconfig.h>
#include <afs/param.h>
#include <sys/types.h>
#ifdef AFS_NT40_ENV
#include <winsock2.h>
#else
#include <netinet/in.h>
#include <sys/time.h>
#endif
#include <afs/afsutil.h>
#include <ubik.h>
#include <afs/bubasics.h>
#include "budb_errs.h"
#include "database.h"
#include "budb_internal.h"
#include "error_macros.h"
#include "afs/audit.h"
#define DBH_POS(ptr) ( (char *) (ptr) - (char *) &db.h )
afs_int32 FreeAllLocks(struct rx_call *, afs_uint32);
afs_int32 FreeLock(struct rx_call *, afs_uint32);
afs_int32 GetInstanceId(struct rx_call *, afs_uint32 *);
afs_int32 GetLock(struct rx_call *, afs_uint32, afs_int32, afs_int32,
afs_uint32 *);
afs_int32
SBUDB_FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
{
afs_int32 code;
code = FreeAllLocks(call, instanceId);
osi_auditU(call, BUDB_FrALckEvent, code, AUD_END);
return code;
}
afs_int32
FreeAllLocks(struct rx_call *call, afs_uint32 instanceId)
{
db_lockP startPtr, endPtr;
struct ubik_trans *ut;
afs_int32 code;
if (callPermitted(call) == 0)
return (BUDB_NOTPERMITTED);
code = InitRPC(&ut, LOCKWRITE, 1);
if (code)
return (code);
startPtr = &db.h.textLocks[0];
endPtr = &db.h.textLocks[TB_NUM - 1];
while (startPtr <= endPtr) {
if ((ntohl(startPtr->lockState) == 1)
&& (ntohl(startPtr->instanceId) == instanceId)
) {
/* release the lock */
startPtr->lockState = 0; /* unlock it */
startPtr->lockTime = 0;
startPtr->expires = 0;
startPtr->instanceId = 0;
dbwrite(ut, DBH_POS(startPtr), (char *)startPtr,
sizeof(db_lockT));
}
startPtr++;
}
code = ubik_EndTrans(ut);
return (code);
}
afs_int32
SBUDB_FreeLock(struct rx_call *call, afs_uint32 lockHandle)
{
afs_int32 code;
code = FreeLock(call, lockHandle);
osi_auditU(call, BUDB_FreLckEvent, code, AUD_END);
return code;
}
afs_int32
FreeLock(struct rx_call *call, afs_uint32 lockHandle)
{
db_lockP lockPtr = 0;
struct ubik_trans *ut;
afs_int32 code;
if (callPermitted(call) == 0)
return (BUDB_NOTPERMITTED);
code = InitRPC(&ut, LOCKWRITE, 1);
if (code)
return (code);
if (checkLockHandle(ut, lockHandle) == 0)
ABORT(BUDB_BADARGUMENT);
lockPtr = &db.h.textLocks[lockHandle - 1];
lockPtr->lockState = 0; /* unlock it */
lockPtr->lockTime = 0;
lockPtr->expires = 0;
lockPtr->instanceId = 0;
dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
code = ubik_EndTrans(ut);
return (code);
abort_exit:
ubik_AbortTrans(ut);
return (code);
}
afs_int32
SBUDB_GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
{
afs_int32 code;
code = GetInstanceId(call, instanceId);
osi_auditU(call, BUDB_GetIIdEvent, code, AUD_END);
return code;
}
afs_int32
GetInstanceId(struct rx_call *call, afs_uint32 *instanceId)
{
struct ubik_trans *ut;
afs_int32 code;
afs_int32 instanceValue;
LogDebug(4, "GetInstanceId:\n");
/* *** Allow anyone to get the instance id ***
* if ( callPermitted(call) == 0 )
* return(BUDB_NOTPERMITTED);
*/
code = InitRPC(&ut, LOCKWRITE, 1);
if (code)
return (code);
instanceValue = ntohl(db.h.lastInstanceId) + 1;
set_header_word(ut, lastInstanceId, htonl(instanceValue));
code = ubik_EndTrans(ut);
return (code);
}
afs_int32
SBUDB_GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
afs_int32 expiration, afs_uint32 *lockHandle)
{
afs_int32 code;
code = GetLock(call, instanceId, lockName, expiration, lockHandle);
osi_auditU(call, BUDB_GetLckEvent, code, AUD_END);
return code;
}
afs_int32
GetLock(struct rx_call *call, afs_uint32 instanceId, afs_int32 lockName,
afs_int32 expiration, afs_uint32 *lockHandle)
{
struct timeval tv;
db_lockP lockPtr;
struct ubik_trans *ut;
afs_int32 code;
if (callPermitted(call) == 0)
return (BUDB_NOTPERMITTED);
if ((lockName < 0) || (lockName >= TB_NUM))
return (BUDB_BADARGUMENT);
/* get the current time */
gettimeofday(&tv, 0);
code = InitRPC(&ut, LOCKWRITE, 1);
if (code)
return (code);
lockPtr = &db.h.textLocks[lockName];
if ((ntohl(lockPtr->lockState) != 0) /* lock set */
&&(ntohl(lockPtr->expires) > tv.tv_sec) /* not expired */
) {
if (ntohl(lockPtr->instanceId) == instanceId)
code = BUDB_SELFLOCKED;
else
code = BUDB_LOCKED;
goto abort_exit;
}
lockPtr->lockState = htonl(1); /* lock it */
lockPtr->lockTime = htonl(tv.tv_sec); /* when locked */
lockPtr->expires = htonl(tv.tv_sec + expiration);
lockPtr->instanceId = htonl(instanceId);
code = dbwrite(ut, DBH_POS(lockPtr), (char *)lockPtr, sizeof(db_lockT));
if (code)
ABORT(code);
*lockHandle = (afs_uint32) (lockName + 1);
code = ubik_EndTrans(ut);
return (code);
abort_exit:
ubik_AbortTrans(ut);
return (code);
}
/* checkLockHandle
* exit:
* 0 - if invalid handle
* 1 - if handle is valid
*/
int
checkLockHandle(struct ubik_trans *ut, afs_uint32 lockHandle)
{
return (((lockHandle > 0) && (lockHandle <= TB_NUM)) ? 1 : 0);
}