r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / tdb / common / lock.c
blob8500438a4e7887dca3c0bd54b6870ab8ab875148
1 /*
2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "tdb_private.h"
31 /* a byte range locking function - return 0 on success
32 this functions locks/unlocks 1 byte at the specified offset.
34 On error, errno is also set so that errors are passed back properly
35 through tdb_open().
37 note that a len of zero means lock to end of file
39 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
40 int rw_type, int lck_type, int probe, size_t len)
42 struct flock fl;
43 int ret;
45 if (tdb->flags & TDB_NOLOCK) {
46 return 0;
49 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
50 tdb->ecode = TDB_ERR_RDONLY;
51 return -1;
54 fl.l_type = rw_type;
55 fl.l_whence = SEEK_SET;
56 fl.l_start = offset;
57 fl.l_len = len;
58 fl.l_pid = 0;
60 do {
61 ret = fcntl(tdb->fd,lck_type,&fl);
62 } while (ret == -1 && errno == EINTR);
64 if (ret == -1) {
65 /* Generic lock error. errno set by fcntl.
66 * EAGAIN is an expected return from non-blocking
67 * locks. */
68 if (!probe && lck_type != F_SETLK) {
69 /* Ensure error code is set for log fun to examine. */
70 tdb->ecode = TDB_ERR_LOCK;
71 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
72 tdb->fd, offset, rw_type, lck_type, (int)len));
74 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
76 return 0;
81 upgrade a read lock to a write lock. This needs to be handled in a
82 special way as some OSes (such as solaris) have too conservative
83 deadlock detection and claim a deadlock when progress can be
84 made. For those OSes we may loop for a while.
86 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
88 int count = 1000;
89 while (count--) {
90 struct timeval tv;
91 if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
92 return 0;
94 if (errno != EDEADLK) {
95 break;
97 /* sleep for as short a time as we can - more portable than usleep() */
98 tv.tv_sec = 0;
99 tv.tv_usec = 1;
100 select(0, NULL, NULL, NULL, &tv);
102 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
103 return -1;
107 /* lock a list in the database. list -1 is the alloc list */
108 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
110 struct tdb_lock_type *new_lck;
111 int i;
113 /* a global lock allows us to avoid per chain locks */
114 if (tdb->global_lock.count &&
115 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
116 return 0;
119 if (tdb->global_lock.count) {
120 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
123 if (list < -1 || list >= (int)tdb->header.hash_size) {
124 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
125 list, ltype));
126 return -1;
128 if (tdb->flags & TDB_NOLOCK)
129 return 0;
131 for (i=0; i<tdb->num_lockrecs; i++) {
132 if (tdb->lockrecs[i].list == list) {
133 if (tdb->lockrecs[i].count == 0) {
135 * Can't happen, see tdb_unlock(). It should
136 * be an assert.
138 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
139 "lck->count == 0 for list %d", list));
142 * Just increment the in-memory struct, posix locks
143 * don't stack.
145 tdb->lockrecs[i].count++;
146 return 0;
150 new_lck = (struct tdb_lock_type *)realloc(
151 tdb->lockrecs,
152 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
153 if (new_lck == NULL) {
154 errno = ENOMEM;
155 return -1;
157 tdb->lockrecs = new_lck;
159 /* Since fcntl locks don't nest, we do a lock for the first one,
160 and simply bump the count for future ones */
161 if (tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW,
162 0, 1)) {
163 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
164 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
165 return -1;
168 tdb->num_locks++;
170 tdb->lockrecs[tdb->num_lockrecs].list = list;
171 tdb->lockrecs[tdb->num_lockrecs].count = 1;
172 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
173 tdb->num_lockrecs += 1;
175 return 0;
178 /* unlock the database: returns void because it's too late for errors. */
179 /* changed to return int it may be interesting to know there
180 has been an error --simo */
181 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
183 int ret = -1;
184 int i;
185 struct tdb_lock_type *lck = NULL;
187 /* a global lock allows us to avoid per chain locks */
188 if (tdb->global_lock.count &&
189 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
190 return 0;
193 if (tdb->global_lock.count) {
194 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
197 if (tdb->flags & TDB_NOLOCK)
198 return 0;
200 /* Sanity checks */
201 if (list < -1 || list >= (int)tdb->header.hash_size) {
202 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
203 return ret;
206 for (i=0; i<tdb->num_lockrecs; i++) {
207 if (tdb->lockrecs[i].list == list) {
208 lck = &tdb->lockrecs[i];
209 break;
213 if ((lck == NULL) || (lck->count == 0)) {
214 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
215 return -1;
218 if (lck->count > 1) {
219 lck->count--;
220 return 0;
224 * This lock has count==1 left, so we need to unlock it in the
225 * kernel. We don't bother with decrementing the in-memory array
226 * element, we're about to overwrite it with the last array element
227 * anyway.
230 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
231 F_SETLKW, 0, 1);
232 tdb->num_locks--;
235 * Shrink the array by overwriting the element just unlocked with the
236 * last array element.
239 if (tdb->num_lockrecs > 1) {
240 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
242 tdb->num_lockrecs -= 1;
245 * We don't bother with realloc when the array shrinks, but if we have
246 * a completely idle tdb we should get rid of the locked array.
249 if (tdb->num_lockrecs == 0) {
250 SAFE_FREE(tdb->lockrecs);
253 if (ret)
254 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
255 return ret;
260 /* lock/unlock entire database */
261 static int _tdb_lockall(struct tdb_context *tdb, int ltype)
263 /* There are no locks on read-only dbs */
264 if (tdb->read_only || tdb->traverse_read)
265 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
267 if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
268 tdb->global_lock.count++;
269 return 0;
272 if (tdb->global_lock.count) {
273 /* a global lock of a different type exists */
274 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
277 if (tdb->num_locks != 0) {
278 /* can't combine global and chain locks */
279 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
282 if (tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, F_SETLKW,
283 0, 4*tdb->header.hash_size)) {
284 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
285 return -1;
288 tdb->global_lock.count = 1;
289 tdb->global_lock.ltype = ltype;
291 return 0;
294 /* unlock entire db */
295 static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
297 /* There are no locks on read-only dbs */
298 if (tdb->read_only || tdb->traverse_read) {
299 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
302 if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
303 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
306 if (tdb->global_lock.count > 1) {
307 tdb->global_lock.count--;
308 return 0;
311 if (tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW,
312 0, 4*tdb->header.hash_size)) {
313 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
314 return -1;
317 tdb->global_lock.count = 0;
318 tdb->global_lock.ltype = 0;
320 return 0;
323 /* lock entire database with write lock */
324 int tdb_lockall(struct tdb_context *tdb)
326 return _tdb_lockall(tdb, F_WRLCK);
329 /* unlock entire database with write lock */
330 int tdb_unlockall(struct tdb_context *tdb)
332 return _tdb_unlockall(tdb, F_WRLCK);
335 /* lock entire database with read lock */
336 int tdb_lockall_read(struct tdb_context *tdb)
338 return _tdb_lockall(tdb, F_RDLCK);
341 /* unlock entire database with read lock */
342 int tdb_unlockall_read(struct tdb_context *tdb)
344 return _tdb_unlockall(tdb, F_RDLCK);
347 /* lock/unlock one hash chain. This is meant to be used to reduce
348 contention - it cannot guarantee how many records will be locked */
349 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
351 return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
354 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
356 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
359 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
361 return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
364 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
366 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
371 /* record lock stops delete underneath */
372 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
374 return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
378 Write locks override our own fcntl readlocks, so check it here.
379 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
380 an error to fail to get the lock here.
382 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
384 struct tdb_traverse_lock *i;
385 for (i = &tdb->travlocks; i; i = i->next)
386 if (i->off == off)
387 return -1;
388 return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
392 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
393 an error to fail to get the lock here.
395 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
397 return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
400 /* fcntl locks don't stack: avoid unlocking someone else's */
401 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
403 struct tdb_traverse_lock *i;
404 uint32_t count = 0;
406 if (off == 0)
407 return 0;
408 for (i = &tdb->travlocks; i; i = i->next)
409 if (i->off == off)
410 count++;
411 return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);