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 /* list -1 is the alloc list, otherwise a hash chain. */
123 static tdb_off_t lock_offset(int list)
125 return FREELIST_TOP + 4*list;
128 /* a byte range locking function - return 0 on success
129 this functions locks/unlocks 1 byte at the specified offset.
131 On error, errno is also set so that errors are passed back properly
134 note that a len of zero means lock to end of file
136 int tdb_brlock(struct tdb_context *tdb,
137 int rw_type, tdb_off_t offset, size_t len,
138 enum tdb_lock_flags flags)
142 if (tdb->flags & TDB_NOLOCK) {
146 if (flags & TDB_LOCK_MARK_ONLY) {
150 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
151 tdb->ecode = TDB_ERR_RDONLY;
156 if (tdb->transaction && offset >= lock_offset(-1) && len != 0) {
157 tdb->ecode = TDB_ERR_RDONLY;
158 TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_brlock attempted in transaction at offset %d rw_type=%d flags=%d len=%d\n",
159 offset, rw_type, flags, (int)len));
164 ret = fcntl_lock(tdb, rw_type, offset, len,
165 flags & TDB_LOCK_WAIT);
166 /* Check for a sigalarm break. */
167 if (ret == -1 && errno == EINTR &&
168 tdb->interrupt_sig_ptr &&
169 *tdb->interrupt_sig_ptr) {
172 } while (ret == -1 && errno == EINTR);
175 tdb->ecode = TDB_ERR_LOCK;
176 /* Generic lock error. errno set by fcntl.
177 * EAGAIN is an expected return from non-blocking
179 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
180 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
181 tdb->fd, offset, rw_type, flags, (int)len));
188 int tdb_brunlock(struct tdb_context *tdb,
189 int rw_type, tdb_off_t offset, size_t len)
193 if (tdb->flags & TDB_NOLOCK) {
198 ret = fcntl_unlock(tdb, rw_type, offset, len);
199 } while (ret == -1 && errno == EINTR);
202 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
203 tdb->fd, offset, rw_type, (int)len));
209 upgrade a read lock to a write lock. This needs to be handled in a
210 special way as some OSes (such as solaris) have too conservative
211 deadlock detection and claim a deadlock when progress can be
212 made. For those OSes we may loop for a while.
214 int tdb_allrecord_upgrade(struct tdb_context *tdb)
218 if (tdb->allrecord_lock.count != 1) {
219 TDB_LOG((tdb, TDB_DEBUG_ERROR,
220 "tdb_allrecord_upgrade failed: count %u too high\n",
221 tdb->allrecord_lock.count));
225 if (tdb->allrecord_lock.off != 1) {
226 TDB_LOG((tdb, TDB_DEBUG_ERROR,
227 "tdb_allrecord_upgrade failed: already upgraded?\n"));
233 if (tdb_brlock(tdb, F_WRLCK, FREELIST_TOP, 0,
234 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
235 tdb->allrecord_lock.ltype = F_WRLCK;
236 tdb->allrecord_lock.off = 0;
239 if (errno != EDEADLK) {
242 /* sleep for as short a time as we can - more portable than usleep() */
245 select(0, NULL, NULL, NULL, &tv);
247 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_allrecord_upgrade failed\n"));
251 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
256 for (i=0; i<tdb->num_lockrecs; i++) {
257 if (tdb->lockrecs[i].off == offset) {
258 return &tdb->lockrecs[i];
264 /* lock an offset in the database. */
265 int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
266 enum tdb_lock_flags flags)
268 struct tdb_lock_type *new_lck;
270 if (offset >= lock_offset(tdb->header.hash_size)) {
271 tdb->ecode = TDB_ERR_LOCK;
272 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid offset %u for ltype=%d\n",
276 if (tdb->flags & TDB_NOLOCK)
279 new_lck = find_nestlock(tdb, offset);
282 * Just increment the in-memory struct, posix locks
289 new_lck = (struct tdb_lock_type *)realloc(
291 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
292 if (new_lck == NULL) {
296 tdb->lockrecs = new_lck;
298 /* Since fcntl locks don't nest, we do a lock for the first one,
299 and simply bump the count for future ones */
300 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
304 tdb->lockrecs[tdb->num_lockrecs].off = offset;
305 tdb->lockrecs[tdb->num_lockrecs].count = 1;
306 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
312 static int tdb_lock_list(struct tdb_context *tdb, int list, int ltype,
313 enum tdb_lock_flags waitflag)
317 /* a allrecord lock allows us to avoid per chain locks */
318 if (tdb->allrecord_lock.count &&
319 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
323 if (tdb->allrecord_lock.count) {
324 tdb->ecode = TDB_ERR_LOCK;
327 ret = tdb_nest_lock(tdb, lock_offset(list), ltype, waitflag);
332 /* lock a list in the database. list -1 is the alloc list */
333 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
337 ret = tdb_lock_list(tdb, list, ltype, TDB_LOCK_WAIT);
339 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
340 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
345 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
346 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
348 return tdb_lock_list(tdb, list, ltype, TDB_LOCK_NOWAIT);
352 int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
356 struct tdb_lock_type *lck;
358 if (tdb->flags & TDB_NOLOCK)
362 if (offset >= lock_offset(tdb->header.hash_size)) {
363 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: offset %u invalid (%d)\n", offset, tdb->header.hash_size));
367 lck = find_nestlock(tdb, offset);
368 if ((lck == NULL) || (lck->count == 0)) {
369 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
373 if (lck->count > 1) {
379 * This lock has count==1 left, so we need to unlock it in the
380 * kernel. We don't bother with decrementing the in-memory array
381 * element, we're about to overwrite it with the last array element
388 ret = tdb_brunlock(tdb, ltype, offset, 1);
392 * Shrink the array by overwriting the element just unlocked with the
393 * last array element.
395 *lck = tdb->lockrecs[--tdb->num_lockrecs];
398 * We don't bother with realloc when the array shrinks, but if we have
399 * a completely idle tdb we should get rid of the locked array.
402 if (tdb->num_lockrecs == 0) {
403 SAFE_FREE(tdb->lockrecs);
407 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
411 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
413 /* a global lock allows us to avoid per chain locks */
414 if (tdb->allrecord_lock.count &&
415 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
419 if (tdb->allrecord_lock.count) {
420 tdb->ecode = TDB_ERR_LOCK;
424 return tdb_nest_unlock(tdb, lock_offset(list), ltype, false);
428 get the transaction lock
430 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
432 return tdb_nest_lock(tdb, TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
436 release the transaction lock
438 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
440 return tdb_nest_unlock(tdb, TRANSACTION_LOCK, ltype, false);
444 /* lock/unlock entire database. It can only be upgradable if you have some
445 * other way of guaranteeing exclusivity (ie. transaction write lock). */
446 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
447 enum tdb_lock_flags flags, bool upgradable)
449 /* There are no locks on read-only dbs */
450 if (tdb->read_only || tdb->traverse_read) {
451 tdb->ecode = TDB_ERR_LOCK;
455 if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
456 tdb->allrecord_lock.count++;
460 if (tdb->allrecord_lock.count) {
461 /* a global lock of a different type exists */
462 tdb->ecode = TDB_ERR_LOCK;
466 if (tdb_have_extra_locks(tdb)) {
467 /* can't combine global and chain locks */
468 tdb->ecode = TDB_ERR_LOCK;
472 if (upgradable && ltype != F_RDLCK) {
473 /* tdb error: you can't upgrade a write lock! */
474 tdb->ecode = TDB_ERR_LOCK;
478 if (tdb_brlock(tdb, ltype, FREELIST_TOP, 0, flags)) {
479 if (flags & TDB_LOCK_WAIT) {
480 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
485 tdb->allrecord_lock.count = 1;
486 /* If it's upgradable, it's actually exclusive so we can treat
487 * it as a write lock. */
488 tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
489 tdb->allrecord_lock.off = upgradable;
496 /* unlock entire db */
497 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype, bool mark_lock)
499 /* There are no locks on read-only dbs */
500 if (tdb->read_only || tdb->traverse_read) {
501 tdb->ecode = TDB_ERR_LOCK;
505 if (tdb->allrecord_lock.count == 0) {
506 tdb->ecode = TDB_ERR_LOCK;
510 /* Upgradable locks are marked as write locks. */
511 if (tdb->allrecord_lock.ltype != ltype
512 && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
513 tdb->ecode = TDB_ERR_LOCK;
517 if (tdb->allrecord_lock.count > 1) {
518 tdb->allrecord_lock.count--;
522 if (!mark_lock && tdb_brunlock(tdb, ltype, FREELIST_TOP, 0)) {
523 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
527 tdb->allrecord_lock.count = 0;
528 tdb->allrecord_lock.ltype = 0;
533 /* lock entire database with write lock */
534 int tdb_lockall(struct tdb_context *tdb)
536 tdb_trace(tdb, "tdb_lockall");
537 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
540 /* lock entire database with write lock - mark only */
541 int tdb_lockall_mark(struct tdb_context *tdb)
543 tdb_trace(tdb, "tdb_lockall_mark");
544 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY, false);
547 /* unlock entire database with write lock - unmark only */
548 int tdb_lockall_unmark(struct tdb_context *tdb)
550 tdb_trace(tdb, "tdb_lockall_unmark");
551 return tdb_allrecord_unlock(tdb, F_WRLCK, true);
554 /* lock entire database with write lock - nonblocking varient */
555 int tdb_lockall_nonblock(struct tdb_context *tdb)
557 int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
558 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
562 /* unlock entire database with write lock */
563 int tdb_unlockall(struct tdb_context *tdb)
565 tdb_trace(tdb, "tdb_unlockall");
566 return tdb_allrecord_unlock(tdb, F_WRLCK, false);
569 /* lock entire database with read lock */
570 int tdb_lockall_read(struct tdb_context *tdb)
572 tdb_trace(tdb, "tdb_lockall_read");
573 return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
576 /* lock entire database with read lock - nonblock varient */
577 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
579 int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
580 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
584 /* unlock entire database with read lock */
585 int tdb_unlockall_read(struct tdb_context *tdb)
587 tdb_trace(tdb, "tdb_unlockall_read");
588 return tdb_allrecord_unlock(tdb, F_RDLCK, false);
591 /* lock/unlock one hash chain. This is meant to be used to reduce
592 contention - it cannot guarantee how many records will be locked */
593 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
595 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
596 tdb_trace_1rec(tdb, "tdb_chainlock", key);
600 /* lock/unlock one hash chain, non-blocking. This is meant to be used
601 to reduce contention - it cannot guarantee how many records will be
603 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
605 int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
606 tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
610 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
611 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
613 int ret = tdb_nest_lock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
614 F_WRLCK, TDB_LOCK_MARK_ONLY);
615 tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
619 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
620 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
622 tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
623 return tdb_nest_unlock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
627 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
629 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
630 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
633 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
636 ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
637 tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
641 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
643 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
644 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
649 /* record lock stops delete underneath */
650 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
652 if (tdb->allrecord_lock.count) {
655 return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
659 Write locks override our own fcntl readlocks, so check it here.
660 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
661 an error to fail to get the lock here.
663 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
665 struct tdb_traverse_lock *i;
666 for (i = &tdb->travlocks; i; i = i->next)
669 if (tdb->allrecord_lock.count) {
670 if (tdb->allrecord_lock.ltype == F_WRLCK) {
675 return tdb_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 if (tdb->allrecord_lock.count) {
683 return tdb_brunlock(tdb, F_WRLCK, off, 1);
686 /* fcntl locks don't stack: avoid unlocking someone else's */
687 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
689 struct tdb_traverse_lock *i;
692 if (tdb->allrecord_lock.count) {
698 for (i = &tdb->travlocks; i; i = i->next)
701 return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
704 bool tdb_have_extra_locks(struct tdb_context *tdb)
706 unsigned int extra = tdb->num_lockrecs;
708 /* A transaction holds the lock for all records. */
709 if (!tdb->transaction && tdb->allrecord_lock.count) {
713 /* We always hold the active lock if CLEAR_IF_FIRST. */
714 if (find_nestlock(tdb, ACTIVE_LOCK)) {
718 /* In a transaction, we expect to hold the transaction lock */
719 if (tdb->transaction && find_nestlock(tdb, TRANSACTION_LOCK)) {
726 /* The transaction code uses this to remove all locks. Note that this
727 may include OPEN_LOCK. */
728 void tdb_release_extra_locks(struct tdb_context *tdb)
730 unsigned int i, extra = 0;
732 if (tdb->allrecord_lock.count != 0) {
733 tdb_brunlock(tdb, tdb->allrecord_lock.ltype, FREELIST_TOP, 0);
734 tdb->allrecord_lock.count = 0;
737 for (i=0;i<tdb->num_lockrecs;i++) {
738 struct tdb_lock_type *lck = &tdb->lockrecs[i];
740 /* Don't release transaction or active locks! */
741 if (tdb->transaction && lck->off == TRANSACTION_LOCK) {
742 tdb->lockrecs[extra++] = *lck;
743 } else if (lck->off == ACTIVE_LOCK) {
744 tdb->lockrecs[extra++] = *lck;
746 tdb_brunlock(tdb, lck->ltype, lck->off, 1);
749 tdb->num_lockrecs = extra;
750 if (tdb->num_lockrecs == 0) {
751 SAFE_FREE(tdb->lockrecs);