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 _PUBLIC_
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 #ifdef USE_TDB_MUTEX_LOCKING
44 if (tdb_mutex_lock(tdb
, rw
, off
, len
, waitflag
, &ret
)) {
51 fl
.l_whence
= SEEK_SET
;
56 cmd
= waitflag
? F_SETLKW
: F_SETLK
;
58 return fcntl(tdb
->fd
, cmd
, &fl
);
61 static int fcntl_unlock(struct tdb_context
*tdb
, int rw
, off_t off
, off_t len
)
64 #if 0 /* Check they matched up locks and unlocks correctly. */
69 locks
= fopen("/proc/locks", "r");
71 while (fgets(line
, 80, locks
)) {
75 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
76 p
= strchr(line
, ':') + 1;
77 if (strncmp(p
, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
79 p
+= strlen(" FLOCK ADVISORY ");
80 if (strncmp(p
, "READ ", strlen("READ ")) == 0)
82 else if (strncmp(p
, "WRITE ", strlen("WRITE ")) == 0)
87 if (atoi(p
) != getpid())
89 p
= strchr(strchr(p
, ' ') + 1, ' ') + 1;
91 p
= strchr(p
, ' ') + 1;
92 if (strncmp(p
, "EOF", 3) == 0)
95 l
= atoi(p
) - start
+ 1;
99 fprintf(stderr
, "Len %u should be %u: %s",
104 fprintf(stderr
, "Type %s wrong: %s",
105 rw
== F_RDLCK
? "READ" : "WRITE", line
);
114 fprintf(stderr
, "Unlock on %u@%u not found!\n",
122 #ifdef USE_TDB_MUTEX_LOCKING
125 if (tdb_mutex_unlock(tdb
, rw
, off
, len
, &ret
)) {
132 fl
.l_whence
= SEEK_SET
;
137 return fcntl(tdb
->fd
, F_SETLKW
, &fl
);
140 /* list -1 is the alloc list, otherwise a hash chain. */
141 static tdb_off_t
lock_offset(int list
)
143 return FREELIST_TOP
+ 4*list
;
146 /* a byte range locking function - return 0 on success
147 this functions locks/unlocks "len" byte at the specified offset.
149 On error, errno is also set so that errors are passed back properly
152 note that a len of zero means lock to end of file
154 int tdb_brlock(struct tdb_context
*tdb
,
155 int rw_type
, tdb_off_t offset
, size_t len
,
156 enum tdb_lock_flags flags
)
160 if (tdb
->flags
& TDB_NOLOCK
) {
164 if (flags
& TDB_LOCK_MARK_ONLY
) {
168 if ((rw_type
== F_WRLCK
) && (tdb
->read_only
|| tdb
->traverse_read
)) {
169 tdb
->ecode
= TDB_ERR_RDONLY
;
174 ret
= fcntl_lock(tdb
, rw_type
, offset
, len
,
175 flags
& TDB_LOCK_WAIT
);
176 /* Check for a sigalarm break. */
177 if (ret
== -1 && errno
== EINTR
&&
178 tdb
->interrupt_sig_ptr
&&
179 *tdb
->interrupt_sig_ptr
) {
182 } while (ret
== -1 && errno
== EINTR
);
185 tdb
->ecode
= TDB_ERR_LOCK
;
186 /* Generic lock error. errno set by fcntl.
187 * EAGAIN is an expected return from non-blocking
189 if (!(flags
& TDB_LOCK_PROBE
) && errno
!= EAGAIN
) {
190 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock failed (fd=%d) at offset %u rw_type=%d flags=%d len=%zu\n",
191 tdb
->fd
, offset
, rw_type
, flags
, len
));
198 int tdb_brunlock(struct tdb_context
*tdb
,
199 int rw_type
, tdb_off_t offset
, size_t len
)
203 if (tdb
->flags
& TDB_NOLOCK
) {
208 ret
= fcntl_unlock(tdb
, rw_type
, offset
, len
);
209 } while (ret
== -1 && errno
== EINTR
);
212 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brunlock failed (fd=%d) at offset %u rw_type=%u len=%zu\n",
213 tdb
->fd
, offset
, rw_type
, len
));
219 * Do a tdb_brlock in a loop. Some OSes (such as solaris) have too
220 * conservative deadlock detection and claim a deadlock when progress can be
221 * made. For those OSes we may loop for a while.
224 static int tdb_brlock_retry(struct tdb_context
*tdb
,
225 int rw_type
, tdb_off_t offset
, size_t len
,
226 enum tdb_lock_flags flags
)
234 ret
= tdb_brlock(tdb
, rw_type
, offset
, len
, flags
);
238 if (errno
!= EDEADLK
) {
241 /* sleep for as short a time as we can - more portable than usleep() */
244 select(0, NULL
, NULL
, NULL
, &tv
);
250 upgrade a read lock to a write lock.
252 int tdb_allrecord_upgrade(struct tdb_context
*tdb
)
256 if (tdb
->allrecord_lock
.count
!= 1) {
257 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
258 "tdb_allrecord_upgrade failed: count %u too high\n",
259 tdb
->allrecord_lock
.count
));
263 if (tdb
->allrecord_lock
.off
!= 1) {
264 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
265 "tdb_allrecord_upgrade failed: already upgraded?\n"));
269 if (tdb_have_mutexes(tdb
)) {
270 ret
= tdb_mutex_allrecord_upgrade(tdb
);
274 ret
= tdb_brlock_retry(tdb
, F_WRLCK
, lock_offset(tdb
->hash_size
),
275 0, TDB_LOCK_WAIT
|TDB_LOCK_PROBE
);
277 tdb_mutex_allrecord_downgrade(tdb
);
280 ret
= tdb_brlock_retry(tdb
, F_WRLCK
, FREELIST_TOP
, 0,
281 TDB_LOCK_WAIT
|TDB_LOCK_PROBE
);
285 tdb
->allrecord_lock
.ltype
= F_WRLCK
;
286 tdb
->allrecord_lock
.off
= 0;
290 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_allrecord_upgrade failed\n"));
294 static struct tdb_lock_type
*find_nestlock(struct tdb_context
*tdb
,
299 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
300 if (tdb
->lockrecs
[i
].off
== offset
) {
301 return &tdb
->lockrecs
[i
];
307 /* lock an offset in the database. */
308 int tdb_nest_lock(struct tdb_context
*tdb
, uint32_t offset
, int ltype
,
309 enum tdb_lock_flags flags
)
311 struct tdb_lock_type
*new_lck
;
313 if (offset
>= lock_offset(tdb
->hash_size
)) {
314 tdb
->ecode
= TDB_ERR_LOCK
;
315 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_lock: invalid offset %u for ltype=%d\n",
319 if (tdb
->flags
& TDB_NOLOCK
)
322 new_lck
= find_nestlock(tdb
, offset
);
325 * Just increment the in-memory struct, posix locks
332 if (tdb
->num_lockrecs
== tdb
->lockrecs_array_length
) {
333 new_lck
= (struct tdb_lock_type
*)realloc(
335 sizeof(*tdb
->lockrecs
) * (tdb
->num_lockrecs
+1));
336 if (new_lck
== NULL
) {
340 tdb
->lockrecs_array_length
= tdb
->num_lockrecs
+1;
341 tdb
->lockrecs
= new_lck
;
344 /* Since fcntl locks don't nest, we do a lock for the first one,
345 and simply bump the count for future ones */
346 if (tdb_brlock(tdb
, ltype
, offset
, 1, flags
)) {
350 new_lck
= &tdb
->lockrecs
[tdb
->num_lockrecs
];
352 new_lck
->off
= offset
;
354 new_lck
->ltype
= ltype
;
360 static int tdb_lock_and_recover(struct tdb_context
*tdb
)
364 /* We need to match locking order in transaction commit. */
365 if (tdb_brlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0, TDB_LOCK_WAIT
)) {
369 if (tdb_brlock(tdb
, F_WRLCK
, OPEN_LOCK
, 1, TDB_LOCK_WAIT
)) {
370 tdb_brunlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0);
374 ret
= tdb_transaction_recover(tdb
);
376 tdb_brunlock(tdb
, F_WRLCK
, OPEN_LOCK
, 1);
377 tdb_brunlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0);
382 static bool have_data_locks(const struct tdb_context
*tdb
)
386 for (i
= 0; i
< tdb
->num_lockrecs
; i
++) {
387 if (tdb
->lockrecs
[i
].off
>= lock_offset(-1))
394 * A allrecord lock allows us to avoid per chain locks. Check if the allrecord
395 * lock is strong enough.
397 static int tdb_lock_covered_by_allrecord_lock(struct tdb_context
*tdb
,
400 if (ltype
== F_RDLCK
) {
402 * The allrecord_lock is equal (F_RDLCK) or stronger
408 if (tdb
->allrecord_lock
.ltype
== F_RDLCK
) {
410 * We ask for ltype==F_WRLCK, but the allrecord_lock
411 * is too weak. We can't upgrade here, so fail.
413 tdb
->ecode
= TDB_ERR_LOCK
;
418 * Asking for F_WRLCK, allrecord is F_WRLCK as well. Pass.
423 static int tdb_lock_list(struct tdb_context
*tdb
, int list
, int ltype
,
424 enum tdb_lock_flags waitflag
)
429 if (tdb
->allrecord_lock
.count
) {
430 return tdb_lock_covered_by_allrecord_lock(tdb
, ltype
);
434 * Check for recoveries: Someone might have kill -9'ed a process
437 check
= !have_data_locks(tdb
);
438 ret
= tdb_nest_lock(tdb
, lock_offset(list
), ltype
, waitflag
);
440 if (ret
== 0 && check
&& tdb_needs_recovery(tdb
)) {
441 tdb_nest_unlock(tdb
, lock_offset(list
), ltype
, false);
443 if (tdb_lock_and_recover(tdb
) == -1) {
446 return tdb_lock_list(tdb
, list
, ltype
, waitflag
);
451 /* lock a list in the database. list -1 is the alloc list */
452 int tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
)
456 ret
= tdb_lock_list(tdb
, list
, ltype
, TDB_LOCK_WAIT
);
458 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock failed on list %d "
459 "ltype=%d (%s)\n", list
, ltype
, strerror(errno
)));
464 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
465 _PUBLIC_
int tdb_lock_nonblock(struct tdb_context
*tdb
, int list
, int ltype
)
467 return tdb_lock_list(tdb
, list
, ltype
, TDB_LOCK_NOWAIT
);
471 int tdb_nest_unlock(struct tdb_context
*tdb
, uint32_t offset
, int ltype
,
475 struct tdb_lock_type
*lck
;
477 if (tdb
->flags
& TDB_NOLOCK
)
481 if (offset
>= lock_offset(tdb
->hash_size
)) {
482 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: offset %u invalid (%d)\n", offset
, tdb
->hash_size
));
486 lck
= find_nestlock(tdb
, offset
);
487 if ((lck
== NULL
) || (lck
->count
== 0)) {
488 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: count is 0\n"));
492 if (lck
->count
> 1) {
498 * This lock has count==1 left, so we need to unlock it in the
499 * kernel. We don't bother with decrementing the in-memory array
500 * element, we're about to overwrite it with the last array element
507 ret
= tdb_brunlock(tdb
, ltype
, offset
, 1);
511 * Shrink the array by overwriting the element just unlocked with the
512 * last array element.
514 *lck
= tdb
->lockrecs
[--tdb
->num_lockrecs
];
517 * We don't bother with realloc when the array shrinks, but if we have
518 * a completely idle tdb we should get rid of the locked array.
522 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: An error occurred unlocking!\n"));
526 _PUBLIC_
int tdb_unlock(struct tdb_context
*tdb
, int list
, int ltype
)
528 /* a global lock allows us to avoid per chain locks */
529 if (tdb
->allrecord_lock
.count
) {
530 return tdb_lock_covered_by_allrecord_lock(tdb
, ltype
);
533 return tdb_nest_unlock(tdb
, lock_offset(list
), ltype
, false);
537 get the transaction lock
539 int tdb_transaction_lock(struct tdb_context
*tdb
, int ltype
,
540 enum tdb_lock_flags lockflags
)
542 return tdb_nest_lock(tdb
, TRANSACTION_LOCK
, ltype
, lockflags
);
546 release the transaction lock
548 int tdb_transaction_unlock(struct tdb_context
*tdb
, int ltype
)
550 return tdb_nest_unlock(tdb
, TRANSACTION_LOCK
, ltype
, false);
553 /* Returns 0 if all done, -1 if error, 1 if ok. */
554 static int tdb_allrecord_check(struct tdb_context
*tdb
, int ltype
,
555 enum tdb_lock_flags flags
, bool upgradable
)
557 /* There are no locks on read-only dbs */
558 if (tdb
->read_only
|| tdb
->traverse_read
) {
559 tdb
->ecode
= TDB_ERR_LOCK
;
563 if (tdb
->allrecord_lock
.count
&& tdb
->allrecord_lock
.ltype
== ltype
) {
564 tdb
->allrecord_lock
.count
++;
568 if (tdb
->allrecord_lock
.count
) {
569 /* a global lock of a different type exists */
570 tdb
->ecode
= TDB_ERR_LOCK
;
574 if (tdb_have_extra_locks(tdb
)) {
575 /* can't combine global and chain locks */
576 tdb
->ecode
= TDB_ERR_LOCK
;
580 if (upgradable
&& ltype
!= F_RDLCK
) {
581 /* tdb error: you can't upgrade a write lock! */
582 tdb
->ecode
= TDB_ERR_LOCK
;
588 /* We only need to lock individual bytes, but Linux merges consecutive locks
589 * so we lock in contiguous ranges. */
590 static int tdb_chainlock_gradual(struct tdb_context
*tdb
,
591 int ltype
, enum tdb_lock_flags flags
,
592 size_t off
, size_t len
)
595 enum tdb_lock_flags nb_flags
= (flags
& ~TDB_LOCK_WAIT
);
598 /* Single record. Just do blocking lock. */
599 return tdb_brlock(tdb
, ltype
, off
, len
, flags
);
602 /* First we try non-blocking. */
603 ret
= tdb_brlock(tdb
, ltype
, off
, len
, nb_flags
);
608 /* Try locking first half, then second. */
609 ret
= tdb_chainlock_gradual(tdb
, ltype
, flags
, off
, len
/ 2);
613 ret
= tdb_chainlock_gradual(tdb
, ltype
, flags
,
614 off
+ len
/ 2, len
- len
/ 2);
616 tdb_brunlock(tdb
, ltype
, off
, len
/ 2);
622 /* lock/unlock entire database. It can only be upgradable if you have some
623 * other way of guaranteeing exclusivity (ie. transaction write lock).
624 * We do the locking gradually to avoid being starved by smaller locks. */
625 int tdb_allrecord_lock(struct tdb_context
*tdb
, int ltype
,
626 enum tdb_lock_flags flags
, bool upgradable
)
630 switch (tdb_allrecord_check(tdb
, ltype
, flags
, upgradable
)) {
637 /* We cover two kinds of locks:
638 * 1) Normal chain locks. Taken for almost all operations.
639 * 2) Individual records locks. Taken after normal or free
642 * It is (1) which cause the starvation problem, so we're only
643 * gradual for that. */
645 if (tdb_have_mutexes(tdb
)) {
646 ret
= tdb_mutex_allrecord_lock(tdb
, ltype
, flags
);
648 ret
= tdb_chainlock_gradual(tdb
, ltype
, flags
, FREELIST_TOP
,
656 /* Grab individual record locks. */
657 if (tdb_brlock(tdb
, ltype
, lock_offset(tdb
->hash_size
), 0,
659 if (tdb_have_mutexes(tdb
)) {
660 tdb_mutex_allrecord_unlock(tdb
);
662 tdb_brunlock(tdb
, ltype
, FREELIST_TOP
,
668 tdb
->allrecord_lock
.count
= 1;
669 /* If it's upgradable, it's actually exclusive so we can treat
670 * it as a write lock. */
671 tdb
->allrecord_lock
.ltype
= upgradable
? F_WRLCK
: ltype
;
672 tdb
->allrecord_lock
.off
= upgradable
;
674 if (tdb_needs_recovery(tdb
)) {
675 bool mark
= flags
& TDB_LOCK_MARK_ONLY
;
676 tdb_allrecord_unlock(tdb
, ltype
, mark
);
678 tdb
->ecode
= TDB_ERR_LOCK
;
679 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
680 "tdb_lockall_mark cannot do recovery\n"));
683 if (tdb_lock_and_recover(tdb
) == -1) {
686 return tdb_allrecord_lock(tdb
, ltype
, flags
, upgradable
);
694 /* unlock entire db */
695 int tdb_allrecord_unlock(struct tdb_context
*tdb
, int ltype
, bool mark_lock
)
697 /* There are no locks on read-only dbs */
698 if (tdb
->read_only
|| tdb
->traverse_read
) {
699 tdb
->ecode
= TDB_ERR_LOCK
;
703 if (tdb
->allrecord_lock
.count
== 0) {
704 tdb
->ecode
= TDB_ERR_LOCK
;
708 /* Upgradable locks are marked as write locks. */
709 if (tdb
->allrecord_lock
.ltype
!= ltype
710 && (!tdb
->allrecord_lock
.off
|| ltype
!= F_RDLCK
)) {
711 tdb
->ecode
= TDB_ERR_LOCK
;
715 if (tdb
->allrecord_lock
.count
> 1) {
716 tdb
->allrecord_lock
.count
--;
723 if (tdb_have_mutexes(tdb
)) {
724 ret
= tdb_mutex_allrecord_unlock(tdb
);
726 ret
= tdb_brunlock(tdb
, ltype
,
727 lock_offset(tdb
->hash_size
),
731 ret
= tdb_brunlock(tdb
, ltype
, FREELIST_TOP
, 0);
735 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlockall failed "
736 "(%s)\n", strerror(errno
)));
741 tdb
->allrecord_lock
.count
= 0;
742 tdb
->allrecord_lock
.ltype
= 0;
747 /* lock entire database with write lock */
748 _PUBLIC_
int tdb_lockall(struct tdb_context
*tdb
)
750 tdb_trace(tdb
, "tdb_lockall");
751 return tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_WAIT
, false);
754 /* lock entire database with write lock - mark only */
755 _PUBLIC_
int tdb_lockall_mark(struct tdb_context
*tdb
)
757 tdb_trace(tdb
, "tdb_lockall_mark");
758 return tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_MARK_ONLY
, false);
761 /* unlock entire database with write lock - unmark only */
762 _PUBLIC_
int tdb_lockall_unmark(struct tdb_context
*tdb
)
764 tdb_trace(tdb
, "tdb_lockall_unmark");
765 return tdb_allrecord_unlock(tdb
, F_WRLCK
, true);
768 /* lock entire database with write lock - nonblocking varient */
769 _PUBLIC_
int tdb_lockall_nonblock(struct tdb_context
*tdb
)
771 int ret
= tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_NOWAIT
, false);
772 tdb_trace_ret(tdb
, "tdb_lockall_nonblock", ret
);
776 /* unlock entire database with write lock */
777 _PUBLIC_
int tdb_unlockall(struct tdb_context
*tdb
)
779 tdb_trace(tdb
, "tdb_unlockall");
780 return tdb_allrecord_unlock(tdb
, F_WRLCK
, false);
783 /* lock entire database with read lock */
784 _PUBLIC_
int tdb_lockall_read(struct tdb_context
*tdb
)
786 tdb_trace(tdb
, "tdb_lockall_read");
787 return tdb_allrecord_lock(tdb
, F_RDLCK
, TDB_LOCK_WAIT
, false);
790 /* lock entire database with read lock - nonblock varient */
791 _PUBLIC_
int tdb_lockall_read_nonblock(struct tdb_context
*tdb
)
793 int ret
= tdb_allrecord_lock(tdb
, F_RDLCK
, TDB_LOCK_NOWAIT
, false);
794 tdb_trace_ret(tdb
, "tdb_lockall_read_nonblock", ret
);
798 /* unlock entire database with read lock */
799 _PUBLIC_
int tdb_unlockall_read(struct tdb_context
*tdb
)
801 tdb_trace(tdb
, "tdb_unlockall_read");
802 return tdb_allrecord_unlock(tdb
, F_RDLCK
, false);
805 /* lock/unlock one hash chain. This is meant to be used to reduce
806 contention - it cannot guarantee how many records will be locked */
807 _PUBLIC_
int tdb_chainlock(struct tdb_context
*tdb
, TDB_DATA key
)
809 int ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
810 tdb_trace_1rec(tdb
, "tdb_chainlock", key
);
814 /* lock/unlock one hash chain, non-blocking. This is meant to be used
815 to reduce contention - it cannot guarantee how many records will be
817 _PUBLIC_
int tdb_chainlock_nonblock(struct tdb_context
*tdb
, TDB_DATA key
)
819 int ret
= tdb_lock_nonblock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
820 tdb_trace_1rec_ret(tdb
, "tdb_chainlock_nonblock", key
, ret
);
824 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
825 _PUBLIC_
int tdb_chainlock_mark(struct tdb_context
*tdb
, TDB_DATA key
)
827 int ret
= tdb_nest_lock(tdb
, lock_offset(BUCKET(tdb
->hash_fn(&key
))),
828 F_WRLCK
, TDB_LOCK_MARK_ONLY
);
829 tdb_trace_1rec(tdb
, "tdb_chainlock_mark", key
);
833 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
834 _PUBLIC_
int tdb_chainlock_unmark(struct tdb_context
*tdb
, TDB_DATA key
)
836 tdb_trace_1rec(tdb
, "tdb_chainlock_unmark", key
);
837 return tdb_nest_unlock(tdb
, lock_offset(BUCKET(tdb
->hash_fn(&key
))),
841 _PUBLIC_
int tdb_chainunlock(struct tdb_context
*tdb
, TDB_DATA key
)
843 tdb_trace_1rec(tdb
, "tdb_chainunlock", key
);
844 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
847 _PUBLIC_
int tdb_chainlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
850 ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
851 tdb_trace_1rec(tdb
, "tdb_chainlock_read", key
);
855 _PUBLIC_
int tdb_chainunlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
857 tdb_trace_1rec(tdb
, "tdb_chainunlock_read", key
);
858 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
861 /* record lock stops delete underneath */
862 int tdb_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
864 if (tdb
->allrecord_lock
.count
) {
867 return off
? tdb_brlock(tdb
, F_RDLCK
, off
, 1, TDB_LOCK_WAIT
) : 0;
871 Write locks override our own fcntl readlocks, so check it here.
872 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
873 an error to fail to get the lock here.
875 int tdb_write_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
877 struct tdb_traverse_lock
*i
;
878 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
881 if (tdb
->allrecord_lock
.count
) {
882 if (tdb
->allrecord_lock
.ltype
== F_WRLCK
) {
887 return tdb_brlock(tdb
, F_WRLCK
, off
, 1, TDB_LOCK_NOWAIT
|TDB_LOCK_PROBE
);
890 int tdb_write_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
892 if (tdb
->allrecord_lock
.count
) {
895 return tdb_brunlock(tdb
, F_WRLCK
, off
, 1);
898 /* fcntl locks don't stack: avoid unlocking someone else's */
899 int tdb_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
901 struct tdb_traverse_lock
*i
;
904 if (tdb
->allrecord_lock
.count
) {
910 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
913 return (count
== 1 ? tdb_brunlock(tdb
, F_RDLCK
, off
, 1) : 0);
916 bool tdb_have_extra_locks(struct tdb_context
*tdb
)
918 unsigned int extra
= tdb
->num_lockrecs
;
920 /* A transaction holds the lock for all records. */
921 if (!tdb
->transaction
&& tdb
->allrecord_lock
.count
) {
925 /* We always hold the active lock if CLEAR_IF_FIRST. */
926 if (find_nestlock(tdb
, ACTIVE_LOCK
)) {
930 /* In a transaction, we expect to hold the transaction lock */
931 if (tdb
->transaction
&& find_nestlock(tdb
, TRANSACTION_LOCK
)) {
938 /* The transaction code uses this to remove all locks. */
939 void tdb_release_transaction_locks(struct tdb_context
*tdb
)
941 unsigned int i
, active
= 0;
943 if (tdb
->allrecord_lock
.count
!= 0) {
944 tdb_allrecord_unlock(tdb
, tdb
->allrecord_lock
.ltype
, false);
945 tdb
->allrecord_lock
.count
= 0;
948 for (i
=0;i
<tdb
->num_lockrecs
;i
++) {
949 struct tdb_lock_type
*lck
= &tdb
->lockrecs
[i
];
951 /* Don't release the active lock! Copy it to first entry. */
952 if (lck
->off
== ACTIVE_LOCK
) {
953 tdb
->lockrecs
[active
++] = *lck
;
955 tdb_brunlock(tdb
, lck
->ltype
, lck
->off
, 1);
958 tdb
->num_lockrecs
= active
;
961 /* Following functions are added specifically to support CTDB. */
963 /* Don't do actual fcntl locking, just mark tdb locked */
964 int tdb_transaction_write_lock_mark(struct tdb_context
*tdb
);
965 _PUBLIC_
int tdb_transaction_write_lock_mark(struct tdb_context
*tdb
)
967 return tdb_transaction_lock(tdb
, F_WRLCK
, TDB_LOCK_MARK_ONLY
);
970 /* Don't do actual fcntl unlocking, just mark tdb unlocked */
971 int tdb_transaction_write_lock_unmark(struct tdb_context
*tdb
);
972 _PUBLIC_
int tdb_transaction_write_lock_unmark(struct tdb_context
*tdb
)
974 return tdb_nest_unlock(tdb
, TRANSACTION_LOCK
, F_WRLCK
, true);