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 static int fcntl_lock(struct tdb_context *tdb,
64 int rw, off_t off, off_t len, bool waitflag)
69 fl.l_whence = SEEK_SET;
74 if (tdb->file->allrecord_lock.count == 0
75 && tdb->file->num_lockrecs == 0) {
76 tdb->file->locker = getpid();
79 add_stat(tdb, lock_lowlevel, 1);
81 return fcntl(tdb->file->fd, F_SETLKW, &fl);
83 add_stat(tdb, lock_nonblock, 1);
84 return fcntl(tdb->file->fd, F_SETLK, &fl);
88 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
91 #if 0 /* Check they matched up locks and unlocks correctly. */
96 locks = fopen("/proc/locks", "r");
98 while (fgets(line, 80, locks)) {
102 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
103 p = strchr(line, ':') + 1;
104 if (strncmp(p, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
106 p += strlen(" FLOCK ADVISORY ");
107 if (strncmp(p, "READ ", strlen("READ ")) == 0)
109 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
114 if (atoi(p) != getpid())
116 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
118 p = strchr(p, ' ') + 1;
119 if (strncmp(p, "EOF", 3) == 0)
122 l = atoi(p) - start + 1;
126 fprintf(stderr, "Len %u should be %u: %s",
131 fprintf(stderr, "Type %s wrong: %s",
132 rw == F_RDLCK ? "READ" : "WRITE", line);
141 fprintf(stderr, "Unlock on %u@%u not found!",
150 fl.l_whence = SEEK_SET;
155 return fcntl(tdb->file->fd, F_SETLKW, &fl);
158 /* a byte range locking function - return 0 on success
159 this functions locks len bytes at the specified offset.
161 note that a len of zero means lock to end of file
163 static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
164 int rw_type, tdb_off_t offset, tdb_off_t len,
165 enum tdb_lock_flags flags)
169 if (tdb->flags & TDB_NOLOCK) {
173 if (rw_type == F_WRLCK && tdb->read_only) {
174 return tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
175 "Write lock attempted on read-only database");
178 /* A 32 bit system cannot open a 64-bit file, but it could have
179 * expanded since then: check here. */
180 if ((size_t)(offset + len) != offset + len) {
181 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
182 "tdb_brlock: lock on giant offset %llu",
183 (long long)(offset + len));
187 ret = fcntl_lock(tdb, rw_type, offset, len,
188 flags & TDB_LOCK_WAIT);
189 } while (ret == -1 && errno == EINTR);
192 /* Generic lock error. errno set by fcntl.
193 * EAGAIN is an expected return from non-blocking
195 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
196 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
197 "tdb_brlock failed (fd=%d) at"
198 " offset %zu rw_type=%d flags=%d len=%zu:"
200 tdb->file->fd, (size_t)offset, rw_type,
201 flags, (size_t)len, strerror(errno));
208 static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
209 int rw_type, tdb_off_t offset, size_t len)
213 if (tdb->flags & TDB_NOLOCK) {
218 ret = fcntl_unlock(tdb, rw_type, offset, len);
219 } while (ret == -1 && errno == EINTR);
221 /* If we fail, *then* we verify that we owned the lock. If not, ok. */
222 if (ret == -1 && check_lock_pid(tdb, "tdb_brunlock", false)) {
223 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
224 "tdb_brunlock failed (fd=%d) at offset %zu"
225 " rw_type=%d len=%zu",
226 tdb->file->fd, (size_t)offset, rw_type,
233 upgrade a read lock to a write lock. This needs to be handled in a
234 special way as some OSes (such as solaris) have too conservative
235 deadlock detection and claim a deadlock when progress can be
236 made. For those OSes we may loop for a while.
238 enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
242 if (!check_lock_pid(tdb, "tdb_transaction_prepare_commit", true))
245 if (tdb->file->allrecord_lock.count != 1) {
246 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
247 "tdb_allrecord_upgrade failed:"
248 " count %u too high",
249 tdb->file->allrecord_lock.count);
252 if (tdb->file->allrecord_lock.off != 1) {
253 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
254 "tdb_allrecord_upgrade failed:"
255 " already upgraded?");
258 if (tdb->file->allrecord_lock.owner != tdb) {
259 return owner_conflict(tdb, "tdb_allrecord_upgrade");
264 if (tdb_brlock(tdb, F_WRLCK,
265 TDB_HASH_LOCK_START, 0,
266 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
267 tdb->file->allrecord_lock.ltype = F_WRLCK;
268 tdb->file->allrecord_lock.off = 0;
271 if (errno != EDEADLK) {
274 /* sleep for as short a time as we can - more portable than usleep() */
277 select(0, NULL, NULL, NULL, &tv);
279 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
280 "tdb_allrecord_upgrade failed");
283 static struct tdb_lock *find_nestlock(struct tdb_context *tdb, tdb_off_t offset,
284 const struct tdb_context *owner)
288 for (i=0; i<tdb->file->num_lockrecs; i++) {
289 if (tdb->file->lockrecs[i].off == offset) {
290 if (owner && tdb->file->lockrecs[i].owner != owner)
292 return &tdb->file->lockrecs[i];
298 enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb)
300 enum TDB_ERROR ecode;
302 if (!check_lock_pid(tdb, "tdb_transaction_prepare_commit", true))
305 ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
307 if (ecode != TDB_SUCCESS) {
311 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
312 if (ecode != TDB_SUCCESS) {
313 tdb_allrecord_unlock(tdb, F_WRLCK);
316 ecode = tdb_transaction_recover(tdb);
317 tdb_unlock_open(tdb);
318 tdb_allrecord_unlock(tdb, F_WRLCK);
323 /* lock an offset in the database. */
324 static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
325 tdb_off_t offset, int ltype,
326 enum tdb_lock_flags flags)
328 struct tdb_lock *new_lck;
329 enum TDB_ERROR ecode;
331 if (offset > (TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
332 + tdb->file->map_size / 8)) {
333 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
334 "tdb_nest_lock: invalid offset %zu ltype=%d",
335 (size_t)offset, ltype);
338 if (tdb->flags & TDB_NOLOCK)
341 if (!check_lock_pid(tdb, "tdb_nest_lock", true)) {
345 add_stat(tdb, locks, 1);
347 new_lck = find_nestlock(tdb, offset, NULL);
349 if (new_lck->owner != tdb) {
350 return owner_conflict(tdb, "tdb_nest_lock");
353 if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
354 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
356 " offset %zu has read lock",
359 /* Just increment the struct, posix locks don't stack. */
364 if (tdb->file->num_lockrecs
365 && offset >= TDB_HASH_LOCK_START
366 && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
367 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
368 "tdb_nest_lock: already have a hash lock?");
371 new_lck = (struct tdb_lock *)realloc(
373 sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
374 if (new_lck == NULL) {
375 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
377 " unable to allocate %zu lock struct",
378 tdb->file->num_lockrecs + 1);
380 tdb->file->lockrecs = new_lck;
382 /* Since fcntl locks don't nest, we do a lock for the first one,
383 and simply bump the count for future ones */
384 ecode = tdb_brlock(tdb, ltype, offset, 1, flags);
385 if (ecode != TDB_SUCCESS) {
389 /* First time we grab a lock, perhaps someone died in commit? */
390 if (!(flags & TDB_LOCK_NOCHECK)
391 && tdb->file->num_lockrecs == 0) {
392 tdb_bool_err berr = tdb_needs_recovery(tdb);
394 tdb_brunlock(tdb, ltype, offset, 1);
398 ecode = tdb_lock_and_recover(tdb);
399 if (ecode == TDB_SUCCESS) {
400 ecode = tdb_brlock(tdb, ltype, offset, 1,
403 if (ecode != TDB_SUCCESS) {
409 tdb->file->lockrecs[tdb->file->num_lockrecs].owner = tdb;
410 tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
411 tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
412 tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
413 tdb->file->num_lockrecs++;
418 static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
419 tdb_off_t off, int ltype)
421 struct tdb_lock *lck;
422 enum TDB_ERROR ecode;
424 if (tdb->flags & TDB_NOLOCK)
427 lck = find_nestlock(tdb, off, tdb);
428 if ((lck == NULL) || (lck->count == 0)) {
429 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
430 "tdb_nest_unlock: no lock for %zu",
434 if (lck->count > 1) {
440 * This lock has count==1 left, so we need to unlock it in the
441 * kernel. We don't bother with decrementing the in-memory array
442 * element, we're about to overwrite it with the last array element
445 ecode = tdb_brunlock(tdb, ltype, off, 1);
448 * Shrink the array by overwriting the element just unlocked with the
449 * last array element.
451 *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
457 get the transaction lock
459 enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype)
461 return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
465 release the transaction lock
467 void tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
469 tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
472 /* We only need to lock individual bytes, but Linux merges consecutive locks
473 * so we lock in contiguous ranges. */
474 static enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
475 int ltype, enum tdb_lock_flags flags,
476 tdb_off_t off, tdb_off_t len)
478 enum TDB_ERROR ecode;
479 enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
482 /* 0 would mean to end-of-file... */
484 /* Single hash. Just do blocking lock. */
485 return tdb_brlock(tdb, ltype, off, len, flags);
488 /* First we try non-blocking. */
489 if (tdb_brlock(tdb, ltype, off, len, nb_flags) == TDB_SUCCESS) {
493 /* Try locking first half, then second. */
494 ecode = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
495 if (ecode != TDB_SUCCESS)
498 ecode = tdb_lock_gradual(tdb, ltype, flags,
499 off + len / 2, len - len / 2);
500 if (ecode != TDB_SUCCESS) {
501 tdb_brunlock(tdb, ltype, off, len / 2);
506 /* lock/unlock entire database. It can only be upgradable if you have some
507 * other way of guaranteeing exclusivity (ie. transaction write lock). */
508 enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
509 enum tdb_lock_flags flags, bool upgradable)
511 enum TDB_ERROR ecode;
514 if (tdb->flags & TDB_NOLOCK)
517 if (!check_lock_pid(tdb, "tdb_allrecord_lock", true)) {
521 if (tdb->file->allrecord_lock.count) {
522 if (tdb->file->allrecord_lock.owner != tdb) {
523 return owner_conflict(tdb, "tdb_allrecord_lock");
527 || tdb->file->allrecord_lock.ltype == F_WRLCK) {
528 tdb->file->allrecord_lock.count++;
532 /* a global lock of a different type exists */
533 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
534 "tdb_allrecord_lock: already have %s lock",
535 tdb->file->allrecord_lock.ltype == F_RDLCK
539 if (tdb_has_hash_locks(tdb)) {
540 /* can't combine global and chain locks */
541 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
542 "tdb_allrecord_lock:"
543 " already have chain lock");
546 if (upgradable && ltype != F_RDLCK) {
547 /* tdb error: you can't upgrade a write lock! */
548 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
549 "tdb_allrecord_lock:"
550 " can't upgrade a write lock");
553 add_stat(tdb, locks, 1);
555 /* Lock hashes, gradually. */
556 ecode = tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
557 TDB_HASH_LOCK_RANGE);
558 if (ecode != TDB_SUCCESS)
561 /* Lock free tables: there to end of file. */
562 ecode = tdb_brlock(tdb, ltype,
563 TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
565 if (ecode != TDB_SUCCESS) {
566 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
567 TDB_HASH_LOCK_RANGE);
571 tdb->file->allrecord_lock.owner = tdb;
572 tdb->file->allrecord_lock.count = 1;
573 /* If it's upgradable, it's actually exclusive so we can treat
574 * it as a write lock. */
575 tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
576 tdb->file->allrecord_lock.off = upgradable;
578 /* Now check for needing recovery. */
579 if (flags & TDB_LOCK_NOCHECK)
582 berr = tdb_needs_recovery(tdb);
583 if (likely(berr == false))
586 tdb_allrecord_unlock(tdb, ltype);
589 ecode = tdb_lock_and_recover(tdb);
590 if (ecode != TDB_SUCCESS) {
596 enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
598 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
601 void tdb_unlock_open(struct tdb_context *tdb)
603 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
606 bool tdb_has_open_lock(struct tdb_context *tdb)
608 return !(tdb->flags & TDB_NOLOCK)
609 && find_nestlock(tdb, TDB_OPEN_LOCK, tdb) != NULL;
612 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
614 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
615 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
616 TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
619 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
621 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
624 /* unlock entire db */
625 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
627 if (tdb->flags & TDB_NOLOCK)
630 if (tdb->file->allrecord_lock.count == 0) {
631 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
632 "tdb_allrecord_unlock: not locked!");
636 if (tdb->file->allrecord_lock.owner != tdb) {
637 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
638 "tdb_allrecord_unlock: not locked by us!");
642 /* Upgradable locks are marked as write locks. */
643 if (tdb->file->allrecord_lock.ltype != ltype
644 && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
645 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
646 "tdb_allrecord_unlock: have %s lock",
647 tdb->file->allrecord_lock.ltype == F_RDLCK
652 if (tdb->file->allrecord_lock.count > 1) {
653 tdb->file->allrecord_lock.count--;
657 tdb->file->allrecord_lock.count = 0;
658 tdb->file->allrecord_lock.ltype = 0;
660 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
663 bool tdb_has_expansion_lock(struct tdb_context *tdb)
665 return find_nestlock(tdb, TDB_EXPANSION_LOCK, tdb) != NULL;
668 bool tdb_has_hash_locks(struct tdb_context *tdb)
672 for (i=0; i<tdb->file->num_lockrecs; i++) {
673 if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
674 && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
675 + TDB_HASH_LOCK_RANGE))
681 static bool tdb_has_free_lock(struct tdb_context *tdb)
685 if (tdb->flags & TDB_NOLOCK)
688 for (i=0; i<tdb->file->num_lockrecs; i++) {
689 if (tdb->file->lockrecs[i].off
690 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
696 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
698 tdb_len_t hash_range,
699 int ltype, enum tdb_lock_flags waitflag)
701 /* FIXME: Do this properly, using hlock_range */
702 unsigned lock = TDB_HASH_LOCK_START
703 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
705 /* a allrecord lock allows us to avoid per chain locks */
706 if (tdb->file->allrecord_lock.count) {
707 if (!check_lock_pid(tdb, "tdb_lock_hashes", true))
710 if (tdb->file->allrecord_lock.owner != tdb)
711 return owner_conflict(tdb, "tdb_lock_hashes");
712 if (ltype == tdb->file->allrecord_lock.ltype
713 || ltype == F_RDLCK) {
717 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
719 " already have %s allrecordlock",
720 tdb->file->allrecord_lock.ltype == F_RDLCK
724 if (tdb_has_free_lock(tdb)) {
725 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
726 "tdb_lock_hashes: already have free lock");
729 if (tdb_has_expansion_lock(tdb)) {
730 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
732 " already have expansion lock");
735 return tdb_nest_lock(tdb, lock, ltype, waitflag);
738 enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
740 tdb_len_t hash_range, int ltype)
742 unsigned lock = TDB_HASH_LOCK_START
743 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
745 if (tdb->flags & TDB_NOLOCK)
748 /* a allrecord lock allows us to avoid per chain locks */
749 if (tdb->file->allrecord_lock.count) {
750 if (tdb->file->allrecord_lock.ltype == F_RDLCK
751 && ltype == F_WRLCK) {
752 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
753 "tdb_unlock_hashes RO allrecord!");
758 return tdb_nest_unlock(tdb, lock, ltype);
761 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
762 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
763 * The result is that on 32 bit systems we don't use lock values > 2^31 on
764 * files that are less than 4GB.
766 static tdb_off_t free_lock_off(tdb_off_t b_off)
768 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
769 + b_off / sizeof(tdb_off_t);
772 enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
773 enum tdb_lock_flags waitflag)
775 assert(b_off >= sizeof(struct tdb_header));
777 if (tdb->flags & TDB_NOLOCK)
780 /* a allrecord lock allows us to avoid per chain locks */
781 if (tdb->file->allrecord_lock.count) {
782 if (!check_lock_pid(tdb, "tdb_lock_free_bucket", true))
785 if (tdb->file->allrecord_lock.ltype == F_WRLCK)
787 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
788 "tdb_lock_free_bucket with"
789 " read-only allrecordlock!");
793 if (tdb_has_expansion_lock(tdb)) {
794 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
795 "tdb_lock_free_bucket:"
796 " already have expansion lock");
800 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
803 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
805 if (tdb->file->allrecord_lock.count)
808 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
811 enum TDB_ERROR tdb_lockall(struct tdb_context *tdb)
813 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
816 void tdb_unlockall(struct tdb_context *tdb)
818 tdb_allrecord_unlock(tdb, F_WRLCK);
821 enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb)
823 return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
826 void tdb_unlockall_read(struct tdb_context *tdb)
828 tdb_allrecord_unlock(tdb, F_RDLCK);
831 void tdb_lock_cleanup(struct tdb_context *tdb)
835 while (tdb->file->allrecord_lock.count
836 && tdb->file->allrecord_lock.owner == tdb) {
837 tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
840 for (i=0; i<tdb->file->num_lockrecs; i++) {
841 if (tdb->file->lockrecs[i].owner == tdb) {
843 tdb->file->lockrecs[i].off,
844 tdb->file->lockrecs[i].ltype);