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
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 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
32 tdb->interrupt_sig_ptr = ptr;
35 static int fcntl_lock(struct tdb_context *tdb,
36 int rw, off_t off, off_t len, bool waitflag)
41 fl.l_whence = SEEK_SET;
47 return fcntl(tdb->fd, F_SETLKW, &fl);
49 return fcntl(tdb->fd, F_SETLK, &fl);
52 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
55 #if 0 /* Check they matched up locks and unlocks correctly. */
60 locks = fopen("/proc/locks", "r");
62 while (fgets(line, 80, locks)) {
66 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
67 p = strchr(line, ':') + 1;
68 if (strncmp(p, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
70 p += strlen(" FLOCK ADVISORY ");
71 if (strncmp(p, "READ ", strlen("READ ")) == 0)
73 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
78 if (atoi(p) != getpid())
80 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
82 p = strchr(p, ' ') + 1;
83 if (strncmp(p, "EOF", 3) == 0)
86 l = atoi(p) - start + 1;
90 fprintf(stderr, "Len %u should be %u: %s",
95 fprintf(stderr, "Type %s wrong: %s",
96 rw == F_RDLCK ? "READ" : "WRITE", line);
105 fprintf(stderr, "Unlock on %u@%u not found!\n",
114 fl.l_whence = SEEK_SET;
119 return fcntl(tdb->fd, F_SETLKW, &fl);
122 /* a byte range locking function - return 0 on success
123 this functions locks/unlocks 1 byte at the specified offset.
125 On error, errno is also set so that errors are passed back properly
128 note that a len of zero means lock to end of file
130 int tdb_brlock(struct tdb_context *tdb,
131 int rw_type, tdb_off_t offset, size_t len,
132 enum tdb_lock_flags flags)
136 if (tdb->flags & TDB_NOLOCK) {
140 if (flags & TDB_LOCK_MARK_ONLY) {
144 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
145 tdb->ecode = TDB_ERR_RDONLY;
150 ret = fcntl_lock(tdb, rw_type, offset, len,
151 flags & TDB_LOCK_WAIT);
152 /* Check for a sigalarm break. */
153 if (ret == -1 && errno == EINTR &&
154 tdb->interrupt_sig_ptr &&
155 *tdb->interrupt_sig_ptr) {
158 } while (ret == -1 && errno == EINTR);
161 tdb->ecode = TDB_ERR_LOCK;
162 /* Generic lock error. errno set by fcntl.
163 * EAGAIN is an expected return from non-blocking
165 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
166 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
167 tdb->fd, offset, rw_type, flags, (int)len));
174 int tdb_brunlock(struct tdb_context *tdb,
175 int rw_type, tdb_off_t offset, size_t len)
179 if (tdb->flags & TDB_NOLOCK) {
184 ret = fcntl_unlock(tdb, rw_type, offset, len);
185 } while (ret == -1 && errno == EINTR);
188 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
189 tdb->fd, offset, rw_type, (int)len));
195 upgrade a read lock to a write lock. This needs to be handled in a
196 special way as some OSes (such as solaris) have too conservative
197 deadlock detection and claim a deadlock when progress can be
198 made. For those OSes we may loop for a while.
200 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
205 if (tdb_brlock(tdb, F_WRLCK, offset, len,
206 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
209 if (errno != EDEADLK) {
212 /* sleep for as short a time as we can - more portable than usleep() */
215 select(0, NULL, NULL, NULL, &tv);
217 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
221 /* list -1 is the alloc list, otherwise a hash chain. */
222 static tdb_off_t lock_offset(int list)
224 return FREELIST_TOP + 4*list;
227 /* lock an offset in the database. */
228 int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
229 enum tdb_lock_flags flags)
231 struct tdb_lock_type *new_lck;
234 if (offset >= lock_offset(tdb->header.hash_size)) {
235 tdb->ecode = TDB_ERR_LOCK;
236 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid offset %u for ltype=%d\n",
240 if (tdb->flags & TDB_NOLOCK)
243 for (i=0; i<tdb->num_lockrecs; i++) {
244 if (tdb->lockrecs[i].off == offset) {
245 if (tdb->lockrecs[i].count == 0) {
247 * Can't happen, see tdb_unlock(). It should
250 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
251 "lck->count == 0 for offset %u",
255 * Just increment the in-memory struct, posix locks
258 tdb->lockrecs[i].count++;
263 new_lck = (struct tdb_lock_type *)realloc(
265 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
266 if (new_lck == NULL) {
270 tdb->lockrecs = new_lck;
272 /* Since fcntl locks don't nest, we do a lock for the first one,
273 and simply bump the count for future ones */
274 if (tdb->methods->brlock(tdb, ltype, offset, 1, flags)) {
280 tdb->lockrecs[tdb->num_lockrecs].off = offset;
281 tdb->lockrecs[tdb->num_lockrecs].count = 1;
282 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
283 tdb->num_lockrecs += 1;
288 /* lock a list in the database. list -1 is the alloc list */
289 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
293 /* a allrecord lock allows us to avoid per chain locks */
294 if (tdb->allrecord_lock.count &&
295 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
299 if (tdb->allrecord_lock.count) {
300 tdb->ecode = TDB_ERR_LOCK;
303 ret = tdb_nest_lock(tdb, lock_offset(list), ltype,
307 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
308 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
313 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
314 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
316 /* a allrecord lock allows us to avoid per chain locks */
317 if (tdb->allrecord_lock.count &&
318 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
322 if (tdb->allrecord_lock.count) {
323 tdb->ecode = TDB_ERR_LOCK;
327 return tdb_nest_lock(tdb, lock_offset(list), ltype, TDB_LOCK_NOWAIT);
331 int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
336 struct tdb_lock_type *lck = NULL;
338 if (tdb->flags & TDB_NOLOCK)
342 if (offset >= lock_offset(tdb->header.hash_size)) {
343 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: offset %u invalid (%d)\n", offset, tdb->header.hash_size));
347 for (i=0; i<tdb->num_lockrecs; i++) {
348 if (tdb->lockrecs[i].off == offset) {
349 lck = &tdb->lockrecs[i];
354 if ((lck == NULL) || (lck->count == 0)) {
355 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
359 if (lck->count > 1) {
365 * This lock has count==1 left, so we need to unlock it in the
366 * kernel. We don't bother with decrementing the in-memory array
367 * element, we're about to overwrite it with the last array element
374 ret = tdb->methods->brunlock(tdb, ltype, offset, 1);
379 * Shrink the array by overwriting the element just unlocked with the
380 * last array element.
383 if (tdb->num_lockrecs > 1) {
384 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
386 tdb->num_lockrecs -= 1;
389 * We don't bother with realloc when the array shrinks, but if we have
390 * a completely idle tdb we should get rid of the locked array.
393 if (tdb->num_lockrecs == 0) {
394 SAFE_FREE(tdb->lockrecs);
398 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
402 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
404 /* a global lock allows us to avoid per chain locks */
405 if (tdb->allrecord_lock.count &&
406 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
410 if (tdb->allrecord_lock.count) {
411 tdb->ecode = TDB_ERR_LOCK;
415 return tdb_nest_unlock(tdb, lock_offset(list), ltype, false);
419 get the transaction lock
421 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
423 if (tdb->allrecord_lock.count) {
426 if (tdb->transaction_lock_count > 0) {
427 tdb->transaction_lock_count++;
431 if (tdb->methods->brlock(tdb, ltype, TRANSACTION_LOCK, 1, TDB_LOCK_WAIT) == -1) {
432 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
433 tdb->ecode = TDB_ERR_LOCK;
436 tdb->transaction_lock_count++;
441 release the transaction lock
443 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
446 if (tdb->allrecord_lock.count) {
449 if (tdb->transaction_lock_count > 1) {
450 tdb->transaction_lock_count--;
453 ret = tdb->methods->brunlock(tdb, ltype, TRANSACTION_LOCK, 1);
455 tdb->transaction_lock_count = 0;
463 /* lock/unlock entire database */
464 static int _tdb_lockall(struct tdb_context *tdb, int ltype,
465 enum tdb_lock_flags flags)
467 /* There are no locks on read-only dbs */
468 if (tdb->read_only || tdb->traverse_read) {
469 tdb->ecode = TDB_ERR_LOCK;
473 if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
474 tdb->allrecord_lock.count++;
478 if (tdb->allrecord_lock.count) {
479 /* a global lock of a different type exists */
480 tdb->ecode = TDB_ERR_LOCK;
484 if (tdb->num_locks != 0) {
485 /* can't combine global and chain locks */
486 tdb->ecode = TDB_ERR_LOCK;
490 if (tdb->methods->brlock(tdb, ltype,
491 FREELIST_TOP, 4*tdb->header.hash_size,
493 if (flags & TDB_LOCK_WAIT) {
494 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
499 tdb->allrecord_lock.count = 1;
500 tdb->allrecord_lock.ltype = ltype;
507 /* unlock entire db */
508 static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
510 /* There are no locks on read-only dbs */
511 if (tdb->read_only || tdb->traverse_read) {
512 tdb->ecode = TDB_ERR_LOCK;
516 if (tdb->allrecord_lock.ltype != ltype || tdb->allrecord_lock.count == 0) {
517 tdb->ecode = TDB_ERR_LOCK;
521 if (tdb->allrecord_lock.count > 1) {
522 tdb->allrecord_lock.count--;
527 tdb->methods->brunlock(tdb, ltype,
528 FREELIST_TOP, 4*tdb->header.hash_size)) {
529 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
533 tdb->allrecord_lock.count = 0;
534 tdb->allrecord_lock.ltype = 0;
539 /* lock entire database with write lock */
540 int tdb_lockall(struct tdb_context *tdb)
542 tdb_trace(tdb, "tdb_lockall");
543 return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_WAIT);
546 /* lock entire database with write lock - mark only */
547 int tdb_lockall_mark(struct tdb_context *tdb)
549 tdb_trace(tdb, "tdb_lockall_mark");
550 return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY);
553 /* unlock entire database with write lock - unmark only */
554 int tdb_lockall_unmark(struct tdb_context *tdb)
556 tdb_trace(tdb, "tdb_lockall_unmark");
557 return _tdb_unlockall(tdb, F_WRLCK, true);
560 /* lock entire database with write lock - nonblocking varient */
561 int tdb_lockall_nonblock(struct tdb_context *tdb)
563 int ret = _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_NOWAIT);
564 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
568 /* unlock entire database with write lock */
569 int tdb_unlockall(struct tdb_context *tdb)
571 tdb_trace(tdb, "tdb_unlockall");
572 return _tdb_unlockall(tdb, F_WRLCK, false);
575 /* lock entire database with read lock */
576 int tdb_lockall_read(struct tdb_context *tdb)
578 tdb_trace(tdb, "tdb_lockall_read");
579 return _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_WAIT);
582 /* lock entire database with read lock - nonblock varient */
583 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
585 int ret = _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_NOWAIT);
586 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
590 /* unlock entire database with read lock */
591 int tdb_unlockall_read(struct tdb_context *tdb)
593 tdb_trace(tdb, "tdb_unlockall_read");
594 return _tdb_unlockall(tdb, F_RDLCK, false);
597 /* lock/unlock one hash chain. This is meant to be used to reduce
598 contention - it cannot guarantee how many records will be locked */
599 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
601 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
602 tdb_trace_1rec(tdb, "tdb_chainlock", key);
606 /* lock/unlock one hash chain, non-blocking. This is meant to be used
607 to reduce contention - it cannot guarantee how many records will be
609 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
611 int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
612 tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
616 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
617 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
619 int ret = tdb_nest_lock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
620 F_WRLCK, TDB_LOCK_MARK_ONLY);
621 tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
625 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
626 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
628 tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
629 return tdb_nest_unlock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
633 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
635 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
636 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
639 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
642 ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
643 tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
647 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
649 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
650 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
655 /* record lock stops delete underneath */
656 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
658 if (tdb->allrecord_lock.count) {
661 return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
665 Write locks override our own fcntl readlocks, so check it here.
666 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
667 an error to fail to get the lock here.
669 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
671 struct tdb_traverse_lock *i;
672 for (i = &tdb->travlocks; i; i = i->next)
675 return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
678 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
680 return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
683 /* fcntl locks don't stack: avoid unlocking someone else's */
684 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
686 struct tdb_traverse_lock *i;
689 if (tdb->allrecord_lock.count) {
695 for (i = &tdb->travlocks; i; i = i->next)
698 return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);