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 static int fcntl_lock(struct tdb_context *tdb,
33 int rw, off_t off, off_t len, bool waitflag)
38 fl.l_whence = SEEK_SET;
44 return fcntl(tdb->fd, F_SETLKW, &fl);
46 return fcntl(tdb->fd, F_SETLK, &fl);
49 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
52 #if 0 /* Check they matched up locks and unlocks correctly. */
57 locks = fopen("/proc/locks", "r");
59 while (fgets(line, 80, locks)) {
63 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
64 p = strchr(line, ':') + 1;
65 if (strncmp(p, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
67 p += strlen(" FLOCK ADVISORY ");
68 if (strncmp(p, "READ ", strlen("READ ")) == 0)
70 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
75 if (atoi(p) != getpid())
77 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
79 p = strchr(p, ' ') + 1;
80 if (strncmp(p, "EOF", 3) == 0)
83 l = atoi(p) - start + 1;
87 fprintf(stderr, "Len %u should be %u: %s",
92 fprintf(stderr, "Type %s wrong: %s",
93 rw == F_RDLCK ? "READ" : "WRITE", line);
102 fprintf(stderr, "Unlock on %u@%u not found!\n",
111 fl.l_whence = SEEK_SET;
116 return fcntl(tdb->fd, F_SETLKW, &fl);
119 /* a byte range locking function - return 0 on success
120 this functions locks/unlocks 1 byte at the specified offset.
122 note that a len of zero means lock to end of file
124 static int tdb_brlock(struct tdb_context *tdb,
125 int rw_type, tdb_off_t offset, tdb_off_t len,
126 enum tdb_lock_flags flags)
130 if (tdb->flags & TDB_NOLOCK) {
134 if (rw_type == F_WRLCK && tdb->read_only) {
135 tdb->ecode = TDB_ERR_RDONLY;
139 /* A 32 bit system cannot open a 64-bit file, but it could have
140 * expanded since then: check here. */
141 if ((size_t)(offset + len) != offset + len) {
142 tdb->ecode = TDB_ERR_IO;
143 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
144 "tdb_brlock: lock on giant offset %llu\n",
145 (long long)(offset + len));
150 ret = fcntl_lock(tdb, rw_type, offset, len,
151 flags & TDB_LOCK_WAIT);
152 } while (ret == -1 && errno == EINTR);
155 tdb->ecode = TDB_ERR_LOCK;
156 /* Generic lock error. errno set by fcntl.
157 * EAGAIN is an expected return from non-blocking
159 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
160 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
161 "tdb_brlock failed (fd=%d) at"
162 " offset %llu rw_type=%d flags=%d len=%llu\n",
163 tdb->fd, (long long)offset, rw_type,
164 flags, (long long)len);
171 static int tdb_brunlock(struct tdb_context *tdb,
172 int rw_type, tdb_off_t offset, size_t len)
176 if (tdb->flags & TDB_NOLOCK) {
181 ret = fcntl_unlock(tdb, rw_type, offset, len);
182 } while (ret == -1 && errno == EINTR);
185 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
186 "tdb_brunlock failed (fd=%d) at offset %llu"
187 " rw_type=%d len=%llu\n",
188 tdb->fd, (long long)offset, rw_type, (long long)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_allrecord_upgrade(struct tdb_context *tdb)
204 if (tdb->allrecord_lock.count != 1) {
205 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
206 "tdb_allrecord_upgrade failed: count %u too high\n",
207 tdb->allrecord_lock.count);
211 if (tdb->allrecord_lock.off != 1) {
212 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
213 "tdb_allrecord_upgrade failed: already upgraded?\n");
219 if (tdb_brlock(tdb, F_WRLCK,
221 + (1ULL << tdb->header.v.hash_bits), 0,
222 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
223 tdb->allrecord_lock.ltype = F_WRLCK;
224 tdb->allrecord_lock.off = 0;
227 if (errno != EDEADLK) {
230 /* sleep for as short a time as we can - more portable than usleep() */
233 select(0, NULL, NULL, NULL, &tv);
235 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
236 "tdb_allrecord_upgrade failed\n");
241 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
246 for (i=0; i<tdb->num_lockrecs; i++) {
247 if (tdb->lockrecs[i].off == offset) {
248 return &tdb->lockrecs[i];
254 /* lock an offset in the database. */
255 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
256 enum tdb_lock_flags flags)
258 struct tdb_lock_type *new_lck;
260 if (offset >= TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
261 tdb->ecode = TDB_ERR_LOCK;
262 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
263 "tdb_nest_lock: invalid offset %llu ltype=%d\n",
264 (long long)offset, ltype);
268 if (tdb->flags & TDB_NOLOCK)
271 new_lck = find_nestlock(tdb, offset);
274 * Just increment the in-memory struct, posix locks
281 new_lck = (struct tdb_lock_type *)realloc(
283 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
284 if (new_lck == NULL) {
285 tdb->ecode = TDB_ERR_OOM;
286 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
287 "tdb_nest_lock: unable to allocate %llu lock struct",
288 (long long)(tdb->num_lockrecs + 1));
292 tdb->lockrecs = new_lck;
294 /* Since fcntl locks don't nest, we do a lock for the first one,
295 and simply bump the count for future ones */
296 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
300 tdb->lockrecs[tdb->num_lockrecs].off = offset;
301 tdb->lockrecs[tdb->num_lockrecs].count = 1;
302 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
308 static int tdb_lock_and_recover(struct tdb_context *tdb)
314 /* We need to match locking order in transaction commit. */
315 if (tdb_brlock(tdb, F_WRLCK, FREELIST_TOP, 0, TDB_LOCK_WAIT)) {
319 if (tdb_brlock(tdb, F_WRLCK, OPEN_LOCK, 1, TDB_LOCK_WAIT)) {
320 tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
324 ret = tdb_transaction_recover(tdb);
326 tdb_brunlock(tdb, F_WRLCK, OPEN_LOCK, 1);
327 tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
336 static bool tdb_needs_recovery(struct tdb_context *tdb)
342 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
345 struct tdb_lock_type *lck;
347 if (tdb->flags & TDB_NOLOCK)
350 lck = find_nestlock(tdb, off);
351 if ((lck == NULL) || (lck->count == 0)) {
352 tdb->ecode = TDB_ERR_LOCK;
353 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
354 "tdb_nest_unlock: no lock for %llu\n", (long long)off);
358 if (lck->count > 1) {
364 * This lock has count==1 left, so we need to unlock it in the
365 * kernel. We don't bother with decrementing the in-memory array
366 * element, we're about to overwrite it with the last array element
369 ret = tdb_brunlock(tdb, ltype, off, 1);
372 * Shrink the array by overwriting the element just unlocked with the
373 * last array element.
375 *lck = tdb->lockrecs[--tdb->num_lockrecs];
382 get the transaction lock
384 int tdb_transaction_lock(struct tdb_context *tdb, int ltype,
385 enum tdb_lock_flags lockflags)
387 return tdb_nest_lock(tdb, TRANSACTION_LOCK, ltype, lockflags);
391 release the transaction lock
393 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
395 return tdb_nest_unlock(tdb, TRANSACTION_LOCK, ltype, false);
399 /* We only need to lock individual bytes, but Linux merges consecutive locks
400 * so we lock in contiguous ranges. */
401 static int tdb_lock_gradual(struct tdb_context *tdb,
402 int ltype, enum tdb_lock_flags flags,
403 tdb_off_t off, tdb_off_t len)
406 enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
409 /* 0 would mean to end-of-file... */
411 /* Single hash. Just do blocking lock. */
412 return tdb_brlock(tdb, ltype, off, len, flags);
415 /* First we try non-blocking. */
416 ret = tdb_brlock(tdb, ltype, off, len, nb_flags);
421 /* Try locking first half, then second. */
422 ret = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
426 ret = tdb_lock_gradual(tdb, ltype, flags,
427 off + len / 2, len - len / 2);
429 tdb_brunlock(tdb, ltype, off, len / 2);
435 /* lock/unlock entire database. It can only be upgradable if you have some
436 * other way of guaranteeing exclusivity (ie. transaction write lock). */
437 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
438 enum tdb_lock_flags flags, bool upgradable)
440 /* FIXME: There are no locks on read-only dbs */
441 if (tdb->read_only) {
442 tdb->ecode = TDB_ERR_LOCK;
443 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
444 "tdb_allrecord_lock: read-only\n");
448 if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
449 tdb->allrecord_lock.count++;
453 if (tdb->allrecord_lock.count) {
454 /* a global lock of a different type exists */
455 tdb->ecode = TDB_ERR_LOCK;
456 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
457 "tdb_allrecord_lock: already have %s lock\n",
458 tdb->allrecord_lock.ltype == F_RDLCK
463 if (tdb_has_locks(tdb)) {
464 /* can't combine global and chain locks */
465 tdb->ecode = TDB_ERR_LOCK;
466 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
467 "tdb_allrecord_lock: already have chain lock\n");
471 if (upgradable && ltype != F_RDLCK) {
472 /* tdb error: you can't upgrade a write lock! */
473 tdb->ecode = TDB_ERR_LOCK;
474 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
475 "tdb_allrecord_lock: can't upgrade a write lock\n");
480 /* Lock hashes, gradually. */
481 if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
482 TDB_HASH_LOCK_RANGE)) {
483 if (!(flags & TDB_LOCK_PROBE)) {
484 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
485 "tdb_allrecord_lock hashes failed (%s)\n",
491 /* Lock free lists: there to end of file. */
492 if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
494 if (!(flags & TDB_LOCK_PROBE)) {
495 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
496 "tdb_allrecord_lock freelist failed (%s)\n",
499 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
500 TDB_HASH_LOCK_RANGE);
504 tdb->allrecord_lock.count = 1;
505 /* If it's upgradable, it's actually exclusive so we can treat
506 * it as a write lock. */
507 tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
508 tdb->allrecord_lock.off = upgradable;
510 /* Now check for needing recovery. */
511 if (unlikely(tdb_needs_recovery(tdb))) {
512 tdb_allrecord_unlock(tdb, ltype);
513 if (tdb_lock_and_recover(tdb) == -1) {
522 int tdb_lock_open(struct tdb_context *tdb)
524 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT);
527 void tdb_unlock_open(struct tdb_context *tdb)
529 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
532 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
534 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype, TDB_LOCK_WAIT);
537 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
539 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
542 /* unlock entire db */
543 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
545 /* FIXME: There are no locks on read-only dbs */
546 if (tdb->read_only) {
547 tdb->ecode = TDB_ERR_LOCK;
548 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
549 "tdb_allrecord_unlock: read-only\n");
553 if (tdb->allrecord_lock.count == 0) {
554 tdb->ecode = TDB_ERR_LOCK;
555 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
556 "tdb_allrecord_unlock: not locked!\n");
560 /* Upgradable locks are marked as write locks. */
561 if (tdb->allrecord_lock.ltype != ltype
562 && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
563 tdb->ecode = TDB_ERR_LOCK;
564 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
565 "tdb_allrecord_unlock: have %s lock\n",
566 tdb->allrecord_lock.ltype == F_RDLCK
571 if (tdb->allrecord_lock.count > 1) {
572 tdb->allrecord_lock.count--;
576 tdb->allrecord_lock.count = 0;
577 tdb->allrecord_lock.ltype = 0;
579 return tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
582 bool tdb_has_expansion_lock(struct tdb_context *tdb)
584 return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
587 bool tdb_has_locks(struct tdb_context *tdb)
589 return tdb->allrecord_lock.count || tdb->num_lockrecs;
593 /* lock entire database with write lock */
594 int tdb_lockall(struct tdb_context *tdb)
596 tdb_trace(tdb, "tdb_lockall");
597 return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
600 /* lock entire database with write lock - nonblocking varient */
601 int tdb_lockall_nonblock(struct tdb_context *tdb)
603 int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
604 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
608 /* unlock entire database with write lock */
609 int tdb_unlockall(struct tdb_context *tdb)
611 tdb_trace(tdb, "tdb_unlockall");
612 return tdb_allrecord_unlock(tdb, F_WRLCK);
615 /* lock entire database with read lock */
616 int tdb_lockall_read(struct tdb_context *tdb)
618 tdb_trace(tdb, "tdb_lockall_read");
619 return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
622 /* lock entire database with read lock - nonblock varient */
623 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
625 int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
626 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
630 /* unlock entire database with read lock */
631 int tdb_unlockall_read(struct tdb_context *tdb)
633 tdb_trace(tdb, "tdb_unlockall_read");
634 return tdb_allrecord_unlock(tdb, F_RDLCK);
638 static bool tdb_has_free_lock(struct tdb_context *tdb)
642 for (i=0; i<tdb->num_lockrecs; i++) {
643 if (tdb->lockrecs[i].off
644 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
650 int tdb_lock_hashes(struct tdb_context *tdb,
652 tdb_len_t hash_range,
653 int ltype, enum tdb_lock_flags waitflag)
655 /* FIXME: Do this properly, using hlock_range */
656 unsigned lock = TDB_HASH_LOCK_START
657 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
659 /* a allrecord lock allows us to avoid per chain locks */
660 if (tdb->allrecord_lock.count &&
661 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
665 if (tdb->allrecord_lock.count) {
666 tdb->ecode = TDB_ERR_LOCK;
667 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
668 "tdb_lock_hashes: have %s allrecordlock\n",
669 tdb->allrecord_lock.ltype == F_RDLCK
674 if (tdb_has_free_lock(tdb)) {
675 tdb->ecode = TDB_ERR_LOCK;
676 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
677 "tdb_lock_hashes: have free lock already\n");
681 if (tdb_has_expansion_lock(tdb)) {
682 tdb->ecode = TDB_ERR_LOCK;
683 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
684 "tdb_lock_hashes: have expansion lock already\n");
688 return tdb_nest_lock(tdb, lock, ltype, waitflag);
691 int tdb_unlock_hashes(struct tdb_context *tdb,
693 tdb_len_t hash_range, int ltype)
695 unsigned lock = TDB_HASH_LOCK_START
696 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
698 /* a allrecord lock allows us to avoid per chain locks */
699 if (tdb->allrecord_lock.count) {
700 if (tdb->allrecord_lock.ltype == F_RDLCK
701 && ltype == F_WRLCK) {
702 tdb->ecode = TDB_ERR_LOCK;
703 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
704 "tdb_unlock_hashes RO allrecord!\n");
710 return tdb_nest_unlock(tdb, lock, ltype);
713 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
714 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
715 * The result is that on 32 bit systems we don't use lock values > 2^31 on
716 * files that are less than 4GB.
718 static tdb_off_t free_lock_off(tdb_off_t b_off)
720 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
721 + b_off / sizeof(tdb_off_t);
724 int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
725 enum tdb_lock_flags waitflag)
727 assert(b_off >= sizeof(struct tdb_header));
729 /* a allrecord lock allows us to avoid per chain locks */
730 if (tdb->allrecord_lock.count) {
731 if (tdb->allrecord_lock.ltype == F_WRLCK)
733 tdb->ecode = TDB_ERR_LOCK;
734 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
735 "tdb_lock_free_bucket with RO allrecordlock!\n");
740 if (tdb_has_expansion_lock(tdb)) {
741 tdb->ecode = TDB_ERR_LOCK;
742 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
743 "tdb_lock_free_bucket: have expansion lock already\n");
748 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
751 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
753 if (tdb->allrecord_lock.count)
756 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
759 /* Even if the entry isn't in this hash bucket, you'd have to lock this
760 * bucket to find it. */
761 static int chainlock(struct tdb_context *tdb, const TDB_DATA *key,
762 int ltype, enum tdb_lock_flags waitflag,
766 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
768 ret = tdb_lock_hashes(tdb, h, 1, ltype, waitflag);
769 tdb_trace_1rec(tdb, func, *key);
773 /* lock/unlock one hash chain. This is meant to be used to reduce
774 contention - it cannot guarantee how many records will be locked */
775 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
777 return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock");
780 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
782 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
783 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
784 return tdb_unlock_hashes(tdb, h, 1, F_WRLCK);
788 /* lock/unlock one hash chain, non-blocking. This is meant to be used
789 to reduce contention - it cannot guarantee how many records will be
791 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
793 return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT,
794 "tdb_chainlock_nonblock");
797 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
799 return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
800 "tdb_chainlock_read");
803 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
805 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
806 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
807 return tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1),
811 /* record lock stops delete underneath */
812 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
814 if (tdb->allrecord_lock.count) {
817 return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
821 Write locks override our own fcntl readlocks, so check it here.
822 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
823 an error to fail to get the lock here.
825 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
827 struct tdb_traverse_lock *i;
828 for (i = &tdb->travlocks; i; i = i->next)
831 if (tdb->allrecord_lock.count) {
832 if (tdb->allrecord_lock.ltype == F_WRLCK) {
837 return tdb_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
840 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
842 if (tdb->allrecord_lock.count) {
845 return tdb_brunlock(tdb, F_WRLCK, off, 1);
848 /* fcntl locks don't stack: avoid unlocking someone else's */
849 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
851 struct tdb_traverse_lock *i;
854 if (tdb->allrecord_lock.count) {
860 for (i = &tdb->travlocks; i; i = i->next)
863 return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
866 /* The transaction code uses this to remove all locks. */
867 void tdb_release_transaction_locks(struct tdb_context *tdb)
871 if (tdb->allrecord_lock.count != 0) {
872 tdb_off_t hash_size, free_size;
874 hash_size = (1ULL << tdb->header.v.hash_bits)
876 free_size = tdb->header.v.free_zones
877 * (tdb->header.v.free_buckets + 1) * sizeof(tdb_off_t);
879 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
880 tdb->header.v.hash_off, hash_size);
881 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
882 tdb->header.v.free_off, free_size);
883 tdb->allrecord_lock.count = 0;
884 tdb->allrecord_lock.ltype = 0;
887 for (i = 0; i<tdb->num_lockrecs; i++) {
888 struct tdb_lock_type *lck = &tdb->lockrecs[i];
890 tdb_brunlock(tdb, lck->ltype, lck->off, 1);
892 tdb->num_lockrecs = 0;
893 SAFE_FREE(tdb->lockrecs);
894 tdb->header_uptodate = false;
898 void tdb_lock_init(struct tdb_context *tdb)
900 tdb->num_lockrecs = 0;
901 tdb->lockrecs = NULL;
902 tdb->allrecord_lock.count = 0;