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) {
559 if (!(flags & TDB_LOCK_PROBE)) {
560 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
561 "tdb_allrecord_lock hashes failed");
566 /* Lock free tables: there to end of file. */
567 ecode = tdb_brlock(tdb, ltype,
568 TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
570 if (ecode != TDB_SUCCESS) {
571 if (!(flags & TDB_LOCK_PROBE)) {
572 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
573 "tdb_allrecord_lock freetables failed");
575 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
576 TDB_HASH_LOCK_RANGE);
580 tdb->file->allrecord_lock.owner = tdb;
581 tdb->file->allrecord_lock.count = 1;
582 /* If it's upgradable, it's actually exclusive so we can treat
583 * it as a write lock. */
584 tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
585 tdb->file->allrecord_lock.off = upgradable;
587 /* Now check for needing recovery. */
588 if (flags & TDB_LOCK_NOCHECK)
591 berr = tdb_needs_recovery(tdb);
592 if (likely(berr == false))
595 tdb_allrecord_unlock(tdb, ltype);
598 ecode = tdb_lock_and_recover(tdb);
599 if (ecode != TDB_SUCCESS) {
605 enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
607 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
610 void tdb_unlock_open(struct tdb_context *tdb)
612 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
615 bool tdb_has_open_lock(struct tdb_context *tdb)
617 return !(tdb->flags & TDB_NOLOCK)
618 && find_nestlock(tdb, TDB_OPEN_LOCK, tdb) != NULL;
621 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
623 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
624 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
625 TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
628 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
630 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
633 /* unlock entire db */
634 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
636 if (tdb->flags & TDB_NOLOCK)
639 if (tdb->file->allrecord_lock.count == 0) {
640 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
641 "tdb_allrecord_unlock: not locked!");
645 if (tdb->file->allrecord_lock.owner != tdb) {
646 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
647 "tdb_allrecord_unlock: not locked by us!");
651 /* Upgradable locks are marked as write locks. */
652 if (tdb->file->allrecord_lock.ltype != ltype
653 && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
654 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
655 "tdb_allrecord_unlock: have %s lock",
656 tdb->file->allrecord_lock.ltype == F_RDLCK
661 if (tdb->file->allrecord_lock.count > 1) {
662 tdb->file->allrecord_lock.count--;
666 tdb->file->allrecord_lock.count = 0;
667 tdb->file->allrecord_lock.ltype = 0;
669 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
672 bool tdb_has_expansion_lock(struct tdb_context *tdb)
674 return find_nestlock(tdb, TDB_EXPANSION_LOCK, tdb) != NULL;
677 bool tdb_has_hash_locks(struct tdb_context *tdb)
681 for (i=0; i<tdb->file->num_lockrecs; i++) {
682 if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
683 && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
684 + TDB_HASH_LOCK_RANGE))
690 static bool tdb_has_free_lock(struct tdb_context *tdb)
694 if (tdb->flags & TDB_NOLOCK)
697 for (i=0; i<tdb->file->num_lockrecs; i++) {
698 if (tdb->file->lockrecs[i].off
699 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
705 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
707 tdb_len_t hash_range,
708 int ltype, enum tdb_lock_flags waitflag)
710 /* FIXME: Do this properly, using hlock_range */
711 unsigned lock = TDB_HASH_LOCK_START
712 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
714 /* a allrecord lock allows us to avoid per chain locks */
715 if (tdb->file->allrecord_lock.count) {
716 if (!check_lock_pid(tdb, "tdb_lock_hashes", true))
719 if (tdb->file->allrecord_lock.owner != tdb)
720 return owner_conflict(tdb, "tdb_lock_hashes");
721 if (ltype == tdb->file->allrecord_lock.ltype
722 || ltype == F_RDLCK) {
726 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
728 " already have %s allrecordlock",
729 tdb->file->allrecord_lock.ltype == F_RDLCK
733 if (tdb_has_free_lock(tdb)) {
734 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
735 "tdb_lock_hashes: already have free lock");
738 if (tdb_has_expansion_lock(tdb)) {
739 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
741 " already have expansion lock");
744 return tdb_nest_lock(tdb, lock, ltype, waitflag);
747 enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
749 tdb_len_t hash_range, int ltype)
751 unsigned lock = TDB_HASH_LOCK_START
752 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
754 if (tdb->flags & TDB_NOLOCK)
757 /* a allrecord lock allows us to avoid per chain locks */
758 if (tdb->file->allrecord_lock.count) {
759 if (tdb->file->allrecord_lock.ltype == F_RDLCK
760 && ltype == F_WRLCK) {
761 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
762 "tdb_unlock_hashes RO allrecord!");
767 return tdb_nest_unlock(tdb, lock, ltype);
770 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
771 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
772 * The result is that on 32 bit systems we don't use lock values > 2^31 on
773 * files that are less than 4GB.
775 static tdb_off_t free_lock_off(tdb_off_t b_off)
777 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
778 + b_off / sizeof(tdb_off_t);
781 enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
782 enum tdb_lock_flags waitflag)
784 assert(b_off >= sizeof(struct tdb_header));
786 if (tdb->flags & TDB_NOLOCK)
789 /* a allrecord lock allows us to avoid per chain locks */
790 if (tdb->file->allrecord_lock.count) {
791 if (!check_lock_pid(tdb, "tdb_lock_free_bucket", true))
794 if (tdb->file->allrecord_lock.ltype == F_WRLCK)
796 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
797 "tdb_lock_free_bucket with"
798 " read-only allrecordlock!");
802 if (tdb_has_expansion_lock(tdb)) {
803 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
804 "tdb_lock_free_bucket:"
805 " already have expansion lock");
809 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
812 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
814 if (tdb->file->allrecord_lock.count)
817 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
820 enum TDB_ERROR tdb_lockall(struct tdb_context *tdb)
822 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
825 void tdb_unlockall(struct tdb_context *tdb)
827 tdb_allrecord_unlock(tdb, F_WRLCK);
830 enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb)
832 return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
835 void tdb_unlockall_read(struct tdb_context *tdb)
837 tdb_allrecord_unlock(tdb, F_RDLCK);
840 void tdb_lock_cleanup(struct tdb_context *tdb)
844 while (tdb->file->allrecord_lock.count
845 && tdb->file->allrecord_lock.owner == tdb) {
846 tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
849 for (i=0; i<tdb->file->num_lockrecs; i++) {
850 if (tdb->file->lockrecs[i].owner == tdb) {
852 tdb->file->lockrecs[i].off,
853 tdb->file->lockrecs[i].ltype);