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/>.
30 #include <ccan/build_assert/build_assert.h>
32 /* If we were threaded, we could wait for unlock, but we're not, so fail. */
33 static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call)
35 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
36 "%s: lock owned by another tdb in this process.",
40 /* If we fork, we no longer really own locks. */
41 static bool check_lock_pid(struct tdb_context *tdb,
42 const char *call, bool log)
44 /* No locks? No problem! */
45 if (tdb->file->allrecord_lock.count == 0
46 && tdb->file->num_lockrecs == 0) {
50 /* No fork? No problem! */
51 if (tdb->file->locker == getpid()) {
56 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
57 "%s: fork() detected after lock acquisition!"
58 " (%u vs %u)", call, tdb->file->locker, getpid());
63 int tdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
71 fl.l_whence = SEEK_SET;
76 ret = fcntl(fd, F_SETLKW, &fl);
78 ret = fcntl(fd, F_SETLK, &fl);
79 } while (ret != 0 && errno == EINTR);
83 int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *unused)
90 fl.l_whence = SEEK_SET;
94 ret = fcntl(fd, F_SETLKW, &fl);
95 } while (ret != 0 && errno == EINTR);
99 static int lock(struct tdb_context *tdb,
100 int rw, off_t off, off_t len, bool waitflag)
103 if (tdb->file->allrecord_lock.count == 0
104 && tdb->file->num_lockrecs == 0) {
105 tdb->file->locker = getpid();
108 tdb->stats.lock_lowlevel++;
109 ret = tdb->lock_fn(tdb->file->fd, rw, off, len, waitflag,
112 tdb->stats.lock_nonblock++;
114 tdb->stats.lock_nonblock_fail++;
119 static int unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
121 #if 0 /* Check they matched up locks and unlocks correctly. */
126 locks = fopen("/proc/locks", "r");
128 while (fgets(line, 80, locks)) {
132 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
133 p = strchr(line, ':') + 1;
134 if (strncmp(p, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
136 p += strlen(" FLOCK ADVISORY ");
137 if (strncmp(p, "READ ", strlen("READ ")) == 0)
139 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
144 if (atoi(p) != getpid())
146 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
148 p = strchr(p, ' ') + 1;
149 if (strncmp(p, "EOF", 3) == 0)
152 l = atoi(p) - start + 1;
156 fprintf(stderr, "Len %u should be %u: %s",
161 fprintf(stderr, "Type %s wrong: %s",
162 rw == F_RDLCK ? "READ" : "WRITE", line);
171 fprintf(stderr, "Unlock on %u@%u not found!",
179 return tdb->unlock_fn(tdb->file->fd, rw, off, len, tdb->lock_data);
182 /* a byte range locking function - return 0 on success
183 this functions locks len bytes at the specified offset.
185 note that a len of zero means lock to end of file
187 static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
188 int rw_type, tdb_off_t offset, tdb_off_t len,
189 enum tdb_lock_flags flags)
193 if (tdb->flags & TDB_NOLOCK) {
197 if (rw_type == F_WRLCK && tdb->read_only) {
198 return tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
199 "Write lock attempted on read-only database");
202 /* A 32 bit system cannot open a 64-bit file, but it could have
203 * expanded since then: check here. */
204 if ((size_t)(offset + len) != offset + len) {
205 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
206 "tdb_brlock: lock on giant offset %llu",
207 (long long)(offset + len));
210 ret = lock(tdb, rw_type, offset, len, flags & TDB_LOCK_WAIT);
212 /* Generic lock error. errno set by fcntl.
213 * EAGAIN is an expected return from non-blocking
215 if (!(flags & TDB_LOCK_PROBE)
216 && (errno != EAGAIN && errno != EINTR)) {
217 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
218 "tdb_brlock failed (fd=%d) at"
219 " offset %zu rw_type=%d flags=%d len=%zu:"
221 tdb->file->fd, (size_t)offset, rw_type,
222 flags, (size_t)len, strerror(errno));
229 static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
230 int rw_type, tdb_off_t offset, size_t len)
232 if (tdb->flags & TDB_NOLOCK) {
236 if (!check_lock_pid(tdb, "tdb_brunlock", true))
239 if (unlock(tdb, rw_type, offset, len) == -1) {
240 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
241 "tdb_brunlock failed (fd=%d) at offset %zu"
242 " rw_type=%d len=%zu: %s",
243 tdb->file->fd, (size_t)offset, rw_type,
244 (size_t)len, strerror(errno));
250 upgrade a read lock to a write lock. This needs to be handled in a
251 special way as some OSes (such as solaris) have too conservative
252 deadlock detection and claim a deadlock when progress can be
253 made. For those OSes we may loop for a while.
255 enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
259 if (!check_lock_pid(tdb, "tdb_transaction_prepare_commit", true))
262 if (tdb->file->allrecord_lock.count != 1) {
263 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
264 "tdb_allrecord_upgrade failed:"
265 " count %u too high",
266 tdb->file->allrecord_lock.count);
269 if (tdb->file->allrecord_lock.off != 1) {
270 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
271 "tdb_allrecord_upgrade failed:"
272 " already upgraded?");
275 if (tdb->file->allrecord_lock.owner != tdb) {
276 return owner_conflict(tdb, "tdb_allrecord_upgrade");
281 if (tdb_brlock(tdb, F_WRLCK,
282 TDB_HASH_LOCK_START, 0,
283 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
284 tdb->file->allrecord_lock.ltype = F_WRLCK;
285 tdb->file->allrecord_lock.off = 0;
288 if (errno != EDEADLK) {
291 /* sleep for as short a time as we can - more portable than usleep() */
294 select(0, NULL, NULL, NULL, &tv);
297 if (errno != EAGAIN && errno != EINTR)
298 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
299 "tdb_allrecord_upgrade failed");
303 static struct tdb_lock *find_nestlock(struct tdb_context *tdb, tdb_off_t offset,
304 const struct tdb_context *owner)
308 for (i=0; i<tdb->file->num_lockrecs; i++) {
309 if (tdb->file->lockrecs[i].off == offset) {
310 if (owner && tdb->file->lockrecs[i].owner != owner)
312 return &tdb->file->lockrecs[i];
318 enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb)
320 enum TDB_ERROR ecode;
322 if (!check_lock_pid(tdb, "tdb_transaction_prepare_commit", true))
325 ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
327 if (ecode != TDB_SUCCESS) {
331 ecode = tdb_lock_open(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
332 if (ecode != TDB_SUCCESS) {
333 tdb_allrecord_unlock(tdb, F_WRLCK);
336 ecode = tdb_transaction_recover(tdb);
337 tdb_unlock_open(tdb, F_WRLCK);
338 tdb_allrecord_unlock(tdb, F_WRLCK);
343 /* lock an offset in the database. */
344 static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
345 tdb_off_t offset, int ltype,
346 enum tdb_lock_flags flags)
348 struct tdb_lock *new_lck;
349 enum TDB_ERROR ecode;
351 if (offset > (TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
352 + tdb->file->map_size / 8)) {
353 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
354 "tdb_nest_lock: invalid offset %zu ltype=%d",
355 (size_t)offset, ltype);
358 if (tdb->flags & TDB_NOLOCK)
361 if (!check_lock_pid(tdb, "tdb_nest_lock", true)) {
367 new_lck = find_nestlock(tdb, offset, NULL);
369 if (new_lck->owner != tdb) {
370 return owner_conflict(tdb, "tdb_nest_lock");
373 if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
374 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
376 " offset %zu has read lock",
379 /* Just increment the struct, posix locks don't stack. */
385 if (tdb->file->num_lockrecs
386 && offset >= TDB_HASH_LOCK_START
387 && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
388 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
389 "tdb_nest_lock: already have a hash lock?");
393 new_lck = (struct tdb_lock *)realloc(
395 sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
396 if (new_lck == NULL) {
397 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
399 " unable to allocate %zu lock struct",
400 tdb->file->num_lockrecs + 1);
402 tdb->file->lockrecs = new_lck;
404 /* Since fcntl locks don't nest, we do a lock for the first one,
405 and simply bump the count for future ones */
406 ecode = tdb_brlock(tdb, ltype, offset, 1, flags);
407 if (ecode != TDB_SUCCESS) {
411 /* First time we grab a lock, perhaps someone died in commit? */
412 if (!(flags & TDB_LOCK_NOCHECK)
413 && tdb->file->num_lockrecs == 0) {
414 tdb_bool_err berr = tdb_needs_recovery(tdb);
416 tdb_brunlock(tdb, ltype, offset, 1);
420 ecode = tdb_lock_and_recover(tdb);
421 if (ecode == TDB_SUCCESS) {
422 ecode = tdb_brlock(tdb, ltype, offset, 1,
425 if (ecode != TDB_SUCCESS) {
431 tdb->file->lockrecs[tdb->file->num_lockrecs].owner = tdb;
432 tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
433 tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
434 tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
435 tdb->file->num_lockrecs++;
440 static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
441 tdb_off_t off, int ltype)
443 struct tdb_lock *lck;
444 enum TDB_ERROR ecode;
446 if (tdb->flags & TDB_NOLOCK)
449 lck = find_nestlock(tdb, off, tdb);
450 if ((lck == NULL) || (lck->count == 0)) {
451 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
452 "tdb_nest_unlock: no lock for %zu",
456 if (lck->count > 1) {
462 * This lock has count==1 left, so we need to unlock it in the
463 * kernel. We don't bother with decrementing the in-memory array
464 * element, we're about to overwrite it with the last array element
467 ecode = tdb_brunlock(tdb, ltype, off, 1);
470 * Shrink the array by overwriting the element just unlocked with the
471 * last array element.
473 *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
479 get the transaction lock
481 enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype)
483 return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
487 release the transaction lock
489 void tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
491 tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
494 /* We only need to lock individual bytes, but Linux merges consecutive locks
495 * so we lock in contiguous ranges. */
496 static enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
497 int ltype, enum tdb_lock_flags flags,
498 tdb_off_t off, tdb_off_t len)
500 enum TDB_ERROR ecode;
501 enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
504 /* 0 would mean to end-of-file... */
506 /* Single hash. Just do blocking lock. */
507 return tdb_brlock(tdb, ltype, off, len, flags);
510 /* First we try non-blocking. */
511 if (tdb_brlock(tdb, ltype, off, len, nb_flags) == TDB_SUCCESS) {
515 /* Try locking first half, then second. */
516 ecode = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
517 if (ecode != TDB_SUCCESS)
520 ecode = tdb_lock_gradual(tdb, ltype, flags,
521 off + len / 2, len - len / 2);
522 if (ecode != TDB_SUCCESS) {
523 tdb_brunlock(tdb, ltype, off, len / 2);
528 /* lock/unlock entire database. It can only be upgradable if you have some
529 * other way of guaranteeing exclusivity (ie. transaction write lock). */
530 enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
531 enum tdb_lock_flags flags, bool upgradable)
533 enum TDB_ERROR ecode;
536 if (tdb->flags & TDB_NOLOCK)
539 if (!check_lock_pid(tdb, "tdb_allrecord_lock", true)) {
543 if (tdb->file->allrecord_lock.count) {
544 if (tdb->file->allrecord_lock.owner != tdb) {
545 return owner_conflict(tdb, "tdb_allrecord_lock");
549 || tdb->file->allrecord_lock.ltype == F_WRLCK) {
550 tdb->file->allrecord_lock.count++;
554 /* a global lock of a different type exists */
555 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
556 "tdb_allrecord_lock: already have %s lock",
557 tdb->file->allrecord_lock.ltype == F_RDLCK
561 if (tdb_has_hash_locks(tdb)) {
562 /* can't combine global and chain locks */
563 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
564 "tdb_allrecord_lock:"
565 " already have chain lock");
568 if (upgradable && ltype != F_RDLCK) {
569 /* tdb error: you can't upgrade a write lock! */
570 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
571 "tdb_allrecord_lock:"
572 " can't upgrade a write lock");
577 /* Lock hashes, gradually. */
578 ecode = tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
579 TDB_HASH_LOCK_RANGE);
580 if (ecode != TDB_SUCCESS)
583 /* Lock free tables: there to end of file. */
584 ecode = tdb_brlock(tdb, ltype,
585 TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
587 if (ecode != TDB_SUCCESS) {
588 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
589 TDB_HASH_LOCK_RANGE);
593 tdb->file->allrecord_lock.owner = tdb;
594 tdb->file->allrecord_lock.count = 1;
595 /* If it's upgradable, it's actually exclusive so we can treat
596 * it as a write lock. */
597 tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
598 tdb->file->allrecord_lock.off = upgradable;
600 /* Now check for needing recovery. */
601 if (flags & TDB_LOCK_NOCHECK)
604 berr = tdb_needs_recovery(tdb);
605 if (likely(berr == false))
608 tdb_allrecord_unlock(tdb, ltype);
611 ecode = tdb_lock_and_recover(tdb);
612 if (ecode != TDB_SUCCESS) {
618 enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb,
619 int ltype, enum tdb_lock_flags flags)
621 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, ltype, flags);
624 void tdb_unlock_open(struct tdb_context *tdb, int ltype)
626 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, ltype);
629 bool tdb_has_open_lock(struct tdb_context *tdb)
631 return !(tdb->flags & TDB_NOLOCK)
632 && find_nestlock(tdb, TDB_OPEN_LOCK, tdb) != NULL;
635 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
637 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
638 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
639 TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
642 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
644 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
647 /* unlock entire db */
648 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
650 if (tdb->flags & TDB_NOLOCK)
653 if (tdb->file->allrecord_lock.count == 0) {
654 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
655 "tdb_allrecord_unlock: not locked!");
659 if (tdb->file->allrecord_lock.owner != tdb) {
660 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
661 "tdb_allrecord_unlock: not locked by us!");
665 /* Upgradable locks are marked as write locks. */
666 if (tdb->file->allrecord_lock.ltype != ltype
667 && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
668 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
669 "tdb_allrecord_unlock: have %s lock",
670 tdb->file->allrecord_lock.ltype == F_RDLCK
675 if (tdb->file->allrecord_lock.count > 1) {
676 tdb->file->allrecord_lock.count--;
680 tdb->file->allrecord_lock.count = 0;
681 tdb->file->allrecord_lock.ltype = 0;
683 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
686 bool tdb_has_expansion_lock(struct tdb_context *tdb)
688 return find_nestlock(tdb, TDB_EXPANSION_LOCK, tdb) != NULL;
691 bool tdb_has_hash_locks(struct tdb_context *tdb)
695 for (i=0; i<tdb->file->num_lockrecs; i++) {
696 if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
697 && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
698 + TDB_HASH_LOCK_RANGE))
704 static bool tdb_has_free_lock(struct tdb_context *tdb)
708 if (tdb->flags & TDB_NOLOCK)
711 for (i=0; i<tdb->file->num_lockrecs; i++) {
712 if (tdb->file->lockrecs[i].off
713 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
719 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
721 tdb_len_t hash_range,
722 int ltype, enum tdb_lock_flags waitflag)
724 /* FIXME: Do this properly, using hlock_range */
725 unsigned l = TDB_HASH_LOCK_START
726 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
728 /* a allrecord lock allows us to avoid per chain locks */
729 if (tdb->file->allrecord_lock.count) {
730 if (!check_lock_pid(tdb, "tdb_lock_hashes", true))
733 if (tdb->file->allrecord_lock.owner != tdb)
734 return owner_conflict(tdb, "tdb_lock_hashes");
735 if (ltype == tdb->file->allrecord_lock.ltype
736 || ltype == F_RDLCK) {
740 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
742 " already have %s allrecordlock",
743 tdb->file->allrecord_lock.ltype == F_RDLCK
747 if (tdb_has_free_lock(tdb)) {
748 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
749 "tdb_lock_hashes: already have free lock");
752 if (tdb_has_expansion_lock(tdb)) {
753 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
755 " already have expansion lock");
758 return tdb_nest_lock(tdb, l, ltype, waitflag);
761 enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
763 tdb_len_t hash_range, int ltype)
765 unsigned l = TDB_HASH_LOCK_START
766 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
768 if (tdb->flags & TDB_NOLOCK)
771 /* a allrecord lock allows us to avoid per chain locks */
772 if (tdb->file->allrecord_lock.count) {
773 if (tdb->file->allrecord_lock.ltype == F_RDLCK
774 && ltype == F_WRLCK) {
775 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
776 "tdb_unlock_hashes RO allrecord!");
781 return tdb_nest_unlock(tdb, l, ltype);
784 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
785 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
786 * The result is that on 32 bit systems we don't use lock values > 2^31 on
787 * files that are less than 4GB.
789 static tdb_off_t free_lock_off(tdb_off_t b_off)
791 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
792 + b_off / sizeof(tdb_off_t);
795 enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
796 enum tdb_lock_flags waitflag)
798 assert(b_off >= sizeof(struct tdb_header));
800 if (tdb->flags & TDB_NOLOCK)
803 /* a allrecord lock allows us to avoid per chain locks */
804 if (tdb->file->allrecord_lock.count) {
805 if (!check_lock_pid(tdb, "tdb_lock_free_bucket", true))
808 if (tdb->file->allrecord_lock.ltype == F_WRLCK)
810 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
811 "tdb_lock_free_bucket with"
812 " read-only allrecordlock!");
816 if (tdb_has_expansion_lock(tdb)) {
817 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
818 "tdb_lock_free_bucket:"
819 " already have expansion lock");
823 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
826 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
828 if (tdb->file->allrecord_lock.count)
831 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
834 enum TDB_ERROR tdb_lockall(struct tdb_context *tdb)
836 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
839 void tdb_unlockall(struct tdb_context *tdb)
841 tdb_allrecord_unlock(tdb, F_WRLCK);
844 enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb)
846 return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
849 void tdb_unlockall_read(struct tdb_context *tdb)
851 tdb_allrecord_unlock(tdb, F_RDLCK);
854 void tdb_lock_cleanup(struct tdb_context *tdb)
858 /* We don't want to warn: they're allowed to close tdb after fork. */
859 if (!check_lock_pid(tdb, "tdb_close", false))
862 while (tdb->file->allrecord_lock.count
863 && tdb->file->allrecord_lock.owner == tdb) {
864 tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
867 for (i=0; i<tdb->file->num_lockrecs; i++) {
868 if (tdb->file->lockrecs[i].owner == tdb) {
870 tdb->file->lockrecs[i].off,
871 tdb->file->lockrecs[i].ltype);