lib/tdb: TDB_TRACE support (for developers)
[Samba.git] / lib / tdb / common / lock.c
blob3414049cb61a1df7f6c5ad0281eb44438815eafb
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 3 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, see <http://www.gnu.org/licenses/>.
28 #include "tdb_private.h"
30 #define TDB_MARK_LOCK 0x80000000
32 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
34 tdb->interrupt_sig_ptr = ptr;
37 /* a byte range locking function - return 0 on success
38 this functions locks/unlocks 1 byte at the specified offset.
40 On error, errno is also set so that errors are passed back properly
41 through tdb_open().
43 note that a len of zero means lock to end of file
45 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
46 int rw_type, int lck_type, int probe, size_t len)
48 struct flock fl;
49 int ret;
51 if (tdb->flags & TDB_NOLOCK) {
52 return 0;
55 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
56 tdb->ecode = TDB_ERR_RDONLY;
57 return -1;
60 fl.l_type = rw_type;
61 fl.l_whence = SEEK_SET;
62 fl.l_start = offset;
63 fl.l_len = len;
64 fl.l_pid = 0;
66 do {
67 ret = fcntl(tdb->fd,lck_type,&fl);
69 /* Check for a sigalarm break. */
70 if (ret == -1 && errno == EINTR &&
71 tdb->interrupt_sig_ptr &&
72 *tdb->interrupt_sig_ptr) {
73 break;
75 } while (ret == -1 && errno == EINTR);
77 if (ret == -1) {
78 /* Generic lock error. errno set by fcntl.
79 * EAGAIN is an expected return from non-blocking
80 * locks. */
81 if (!probe && lck_type != F_SETLK) {
82 /* Ensure error code is set for log fun to examine. */
83 tdb->ecode = TDB_ERR_LOCK;
84 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
85 tdb->fd, offset, rw_type, lck_type, (int)len));
87 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
89 return 0;
94 upgrade a read lock to a write lock. This needs to be handled in a
95 special way as some OSes (such as solaris) have too conservative
96 deadlock detection and claim a deadlock when progress can be
97 made. For those OSes we may loop for a while.
99 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
101 int count = 1000;
102 while (count--) {
103 struct timeval tv;
104 if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
105 return 0;
107 if (errno != EDEADLK) {
108 break;
110 /* sleep for as short a time as we can - more portable than usleep() */
111 tv.tv_sec = 0;
112 tv.tv_usec = 1;
113 select(0, NULL, NULL, NULL, &tv);
115 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
116 return -1;
120 /* lock a list in the database. list -1 is the alloc list */
121 static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
123 struct tdb_lock_type *new_lck;
124 int i;
125 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
127 ltype &= ~TDB_MARK_LOCK;
129 /* a global lock allows us to avoid per chain locks */
130 if (tdb->global_lock.count &&
131 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
132 return 0;
135 if (tdb->global_lock.count) {
136 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
139 if (list < -1 || list >= (int)tdb->header.hash_size) {
140 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
141 list, ltype));
142 return -1;
144 if (tdb->flags & TDB_NOLOCK)
145 return 0;
147 for (i=0; i<tdb->num_lockrecs; i++) {
148 if (tdb->lockrecs[i].list == list) {
149 if (tdb->lockrecs[i].count == 0) {
151 * Can't happen, see tdb_unlock(). It should
152 * be an assert.
154 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
155 "lck->count == 0 for list %d", list));
158 * Just increment the in-memory struct, posix locks
159 * don't stack.
161 tdb->lockrecs[i].count++;
162 return 0;
166 new_lck = (struct tdb_lock_type *)realloc(
167 tdb->lockrecs,
168 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
169 if (new_lck == NULL) {
170 errno = ENOMEM;
171 return -1;
173 tdb->lockrecs = new_lck;
175 /* Since fcntl locks don't nest, we do a lock for the first one,
176 and simply bump the count for future ones */
177 if (!mark_lock &&
178 tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list, ltype, op,
179 0, 1)) {
180 return -1;
183 tdb->num_locks++;
185 tdb->lockrecs[tdb->num_lockrecs].list = list;
186 tdb->lockrecs[tdb->num_lockrecs].count = 1;
187 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
188 tdb->num_lockrecs += 1;
190 return 0;
193 /* lock a list in the database. list -1 is the alloc list */
194 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
196 int ret;
197 ret = _tdb_lock(tdb, list, ltype, F_SETLKW);
198 if (ret) {
199 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
200 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
202 return ret;
205 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
206 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
208 return _tdb_lock(tdb, list, ltype, F_SETLK);
212 /* unlock the database: returns void because it's too late for errors. */
213 /* changed to return int it may be interesting to know there
214 has been an error --simo */
215 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
217 int ret = -1;
218 int i;
219 struct tdb_lock_type *lck = NULL;
220 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
222 ltype &= ~TDB_MARK_LOCK;
224 /* a global lock allows us to avoid per chain locks */
225 if (tdb->global_lock.count &&
226 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
227 return 0;
230 if (tdb->global_lock.count) {
231 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
234 if (tdb->flags & TDB_NOLOCK)
235 return 0;
237 /* Sanity checks */
238 if (list < -1 || list >= (int)tdb->header.hash_size) {
239 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
240 return ret;
243 for (i=0; i<tdb->num_lockrecs; i++) {
244 if (tdb->lockrecs[i].list == list) {
245 lck = &tdb->lockrecs[i];
246 break;
250 if ((lck == NULL) || (lck->count == 0)) {
251 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
252 return -1;
255 if (lck->count > 1) {
256 lck->count--;
257 return 0;
261 * This lock has count==1 left, so we need to unlock it in the
262 * kernel. We don't bother with decrementing the in-memory array
263 * element, we're about to overwrite it with the last array element
264 * anyway.
267 if (mark_lock) {
268 ret = 0;
269 } else {
270 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
271 F_SETLKW, 0, 1);
273 tdb->num_locks--;
276 * Shrink the array by overwriting the element just unlocked with the
277 * last array element.
280 if (tdb->num_lockrecs > 1) {
281 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
283 tdb->num_lockrecs -= 1;
286 * We don't bother with realloc when the array shrinks, but if we have
287 * a completely idle tdb we should get rid of the locked array.
290 if (tdb->num_lockrecs == 0) {
291 SAFE_FREE(tdb->lockrecs);
294 if (ret)
295 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
296 return ret;
300 get the transaction lock
302 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
304 if (tdb->global_lock.count) {
305 return 0;
307 if (tdb->transaction_lock_count > 0) {
308 tdb->transaction_lock_count++;
309 return 0;
312 if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype,
313 F_SETLKW, 0, 1) == -1) {
314 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
315 tdb->ecode = TDB_ERR_LOCK;
316 return -1;
318 tdb->transaction_lock_count++;
319 return 0;
323 release the transaction lock
325 int tdb_transaction_unlock(struct tdb_context *tdb)
327 int ret;
328 if (tdb->global_lock.count) {
329 return 0;
331 if (tdb->transaction_lock_count > 1) {
332 tdb->transaction_lock_count--;
333 return 0;
335 ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
336 if (ret == 0) {
337 tdb->transaction_lock_count = 0;
339 return ret;
345 /* lock/unlock entire database */
346 static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
348 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
350 ltype &= ~TDB_MARK_LOCK;
352 /* There are no locks on read-only dbs */
353 if (tdb->read_only || tdb->traverse_read)
354 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
356 if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
357 tdb->global_lock.count++;
358 return 0;
361 if (tdb->global_lock.count) {
362 /* a global lock of a different type exists */
363 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
366 if (tdb->num_locks != 0) {
367 /* can't combine global and chain locks */
368 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
371 if (!mark_lock &&
372 tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, op,
373 0, 4*tdb->header.hash_size)) {
374 if (op == F_SETLKW) {
375 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
377 return -1;
380 tdb->global_lock.count = 1;
381 tdb->global_lock.ltype = ltype;
383 return 0;
388 /* unlock entire db */
389 static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
391 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
393 ltype &= ~TDB_MARK_LOCK;
395 /* There are no locks on read-only dbs */
396 if (tdb->read_only || tdb->traverse_read) {
397 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
400 if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
401 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
404 if (tdb->global_lock.count > 1) {
405 tdb->global_lock.count--;
406 return 0;
409 if (!mark_lock &&
410 tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW,
411 0, 4*tdb->header.hash_size)) {
412 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
413 return -1;
416 tdb->global_lock.count = 0;
417 tdb->global_lock.ltype = 0;
419 return 0;
422 /* lock entire database with write lock */
423 int tdb_lockall(struct tdb_context *tdb)
425 tdb_trace(tdb, "tdb_lockall");
426 return _tdb_lockall(tdb, F_WRLCK, F_SETLKW);
429 /* lock entire database with write lock - mark only */
430 int tdb_lockall_mark(struct tdb_context *tdb)
432 tdb_trace(tdb, "tdb_lockall_mark");
433 return _tdb_lockall(tdb, F_WRLCK | TDB_MARK_LOCK, F_SETLKW);
436 /* unlock entire database with write lock - unmark only */
437 int tdb_lockall_unmark(struct tdb_context *tdb)
439 tdb_trace(tdb, "tdb_lockall_unmark");
440 return _tdb_unlockall(tdb, F_WRLCK | TDB_MARK_LOCK);
443 /* lock entire database with write lock - nonblocking varient */
444 int tdb_lockall_nonblock(struct tdb_context *tdb)
446 int ret = _tdb_lockall(tdb, F_WRLCK, F_SETLK);
447 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
448 return ret;
451 /* unlock entire database with write lock */
452 int tdb_unlockall(struct tdb_context *tdb)
454 tdb_trace(tdb, "tdb_unlockall");
455 return _tdb_unlockall(tdb, F_WRLCK);
458 /* lock entire database with read lock */
459 int tdb_lockall_read(struct tdb_context *tdb)
461 tdb_trace(tdb, "tdb_lockall_read");
462 return _tdb_lockall(tdb, F_RDLCK, F_SETLKW);
465 /* lock entire database with read lock - nonblock varient */
466 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
468 int ret = _tdb_lockall(tdb, F_RDLCK, F_SETLK);
469 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
470 return ret;
473 /* unlock entire database with read lock */
474 int tdb_unlockall_read(struct tdb_context *tdb)
476 tdb_trace(tdb, "tdb_unlockall_read");
477 return _tdb_unlockall(tdb, F_RDLCK);
480 /* lock/unlock one hash chain. This is meant to be used to reduce
481 contention - it cannot guarantee how many records will be locked */
482 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
484 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
485 tdb_trace_1rec(tdb, "tdb_chainlock", key);
486 return ret;
489 /* lock/unlock one hash chain, non-blocking. This is meant to be used
490 to reduce contention - it cannot guarantee how many records will be
491 locked */
492 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
494 int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
495 tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
496 return ret;
499 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
500 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
502 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
503 tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
504 return ret;
507 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
508 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
510 tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
511 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
514 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
516 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
517 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
520 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
522 int ret;
523 ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
524 tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
525 return ret;
528 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
530 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
531 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
536 /* record lock stops delete underneath */
537 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
539 if (tdb->global_lock.count) {
540 return 0;
542 return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
546 Write locks override our own fcntl readlocks, so check it here.
547 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
548 an error to fail to get the lock here.
550 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
552 struct tdb_traverse_lock *i;
553 for (i = &tdb->travlocks; i; i = i->next)
554 if (i->off == off)
555 return -1;
556 return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
560 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
561 an error to fail to get the lock here.
563 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
565 return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
568 /* fcntl locks don't stack: avoid unlocking someone else's */
569 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
571 struct tdb_traverse_lock *i;
572 uint32_t count = 0;
574 if (tdb->global_lock.count) {
575 return 0;
578 if (off == 0)
579 return 0;
580 for (i = &tdb->travlocks; i; i = i->next)
581 if (i->off == off)
582 count++;
583 return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);