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 fl
.l_whence
= SEEK_SET
;
47 return fcntl(tdb
->fd
, F_SETLKW
, &fl
);
49 return fcntl(tdb
->fd
, F_SETLK
, &fl
);
52 static int fcntl_unlock(struct tdb_context
*tdb
, int rw
, off_t off
, off_t len
)
55 #if 0 /* Check they matched up locks and unlocks correctly. */
60 locks
= fopen("/proc/locks", "r");
62 while (fgets(line
, 80, locks
)) {
66 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
67 p
= strchr(line
, ':') + 1;
68 if (strncmp(p
, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
70 p
+= strlen(" FLOCK ADVISORY ");
71 if (strncmp(p
, "READ ", strlen("READ ")) == 0)
73 else if (strncmp(p
, "WRITE ", strlen("WRITE ")) == 0)
78 if (atoi(p
) != getpid())
80 p
= strchr(strchr(p
, ' ') + 1, ' ') + 1;
82 p
= strchr(p
, ' ') + 1;
83 if (strncmp(p
, "EOF", 3) == 0)
86 l
= atoi(p
) - start
+ 1;
90 fprintf(stderr
, "Len %u should be %u: %s",
95 fprintf(stderr
, "Type %s wrong: %s",
96 rw
== F_RDLCK
? "READ" : "WRITE", line
);
105 fprintf(stderr
, "Unlock on %u@%u not found!\n",
114 fl
.l_whence
= SEEK_SET
;
119 return fcntl(tdb
->fd
, F_SETLKW
, &fl
);
122 /* list -1 is the alloc list, otherwise a hash chain. */
123 static tdb_off_t
lock_offset(int list
)
125 return FREELIST_TOP
+ 4*list
;
128 /* a byte range locking function - return 0 on success
129 this functions locks/unlocks "len" byte at the specified offset.
131 On error, errno is also set so that errors are passed back properly
134 note that a len of zero means lock to end of file
136 int tdb_brlock(struct tdb_context
*tdb
,
137 int rw_type
, tdb_off_t offset
, size_t len
,
138 enum tdb_lock_flags flags
)
142 if (tdb
->flags
& TDB_NOLOCK
) {
146 if (flags
& TDB_LOCK_MARK_ONLY
) {
150 if ((rw_type
== F_WRLCK
) && (tdb
->read_only
|| tdb
->traverse_read
)) {
151 tdb
->ecode
= TDB_ERR_RDONLY
;
156 ret
= fcntl_lock(tdb
, rw_type
, offset
, len
,
157 flags
& TDB_LOCK_WAIT
);
158 /* Check for a sigalarm break. */
159 if (ret
== -1 && errno
== EINTR
&&
160 tdb
->interrupt_sig_ptr
&&
161 *tdb
->interrupt_sig_ptr
) {
164 } while (ret
== -1 && errno
== EINTR
);
167 tdb
->ecode
= TDB_ERR_LOCK
;
168 /* Generic lock error. errno set by fcntl.
169 * EAGAIN is an expected return from non-blocking
171 if (!(flags
& TDB_LOCK_PROBE
) && errno
!= EAGAIN
) {
172 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
173 tdb
->fd
, offset
, rw_type
, flags
, (int)len
));
180 int tdb_brunlock(struct tdb_context
*tdb
,
181 int rw_type
, tdb_off_t offset
, size_t len
)
185 if (tdb
->flags
& TDB_NOLOCK
) {
190 ret
= fcntl_unlock(tdb
, rw_type
, offset
, len
);
191 } while (ret
== -1 && errno
== EINTR
);
194 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
195 tdb
->fd
, offset
, rw_type
, (int)len
));
201 upgrade a read lock to a write lock. This needs to be handled in a
202 special way as some OSes (such as solaris) have too conservative
203 deadlock detection and claim a deadlock when progress can be
204 made. For those OSes we may loop for a while.
206 int tdb_allrecord_upgrade(struct tdb_context
*tdb
)
210 if (tdb
->allrecord_lock
.count
!= 1) {
211 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
212 "tdb_allrecord_upgrade failed: count %u too high\n",
213 tdb
->allrecord_lock
.count
));
217 if (tdb
->allrecord_lock
.off
!= 1) {
218 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
219 "tdb_allrecord_upgrade failed: already upgraded?\n"));
225 if (tdb_brlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0,
226 TDB_LOCK_WAIT
|TDB_LOCK_PROBE
) == 0) {
227 tdb
->allrecord_lock
.ltype
= F_WRLCK
;
228 tdb
->allrecord_lock
.off
= 0;
231 if (errno
!= EDEADLK
) {
234 /* sleep for as short a time as we can - more portable than usleep() */
237 select(0, NULL
, NULL
, NULL
, &tv
);
239 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_allrecord_upgrade failed\n"));
243 static struct tdb_lock_type
*find_nestlock(struct tdb_context
*tdb
,
248 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
249 if (tdb
->lockrecs
[i
].off
== offset
) {
250 return &tdb
->lockrecs
[i
];
256 /* lock an offset in the database. */
257 int tdb_nest_lock(struct tdb_context
*tdb
, uint32_t offset
, int ltype
,
258 enum tdb_lock_flags flags
)
260 struct tdb_lock_type
*new_lck
;
262 if (offset
>= lock_offset(tdb
->header
.hash_size
)) {
263 tdb
->ecode
= TDB_ERR_LOCK
;
264 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_lock: invalid offset %u for ltype=%d\n",
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
) {
288 tdb
->lockrecs
= new_lck
;
290 /* Since fcntl locks don't nest, we do a lock for the first one,
291 and simply bump the count for future ones */
292 if (tdb_brlock(tdb
, ltype
, offset
, 1, flags
)) {
296 tdb
->lockrecs
[tdb
->num_lockrecs
].off
= offset
;
297 tdb
->lockrecs
[tdb
->num_lockrecs
].count
= 1;
298 tdb
->lockrecs
[tdb
->num_lockrecs
].ltype
= ltype
;
304 static int tdb_lock_and_recover(struct tdb_context
*tdb
)
308 /* We need to match locking order in transaction commit. */
309 if (tdb_brlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0, TDB_LOCK_WAIT
)) {
313 if (tdb_brlock(tdb
, F_WRLCK
, OPEN_LOCK
, 1, TDB_LOCK_WAIT
)) {
314 tdb_brunlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0);
318 ret
= tdb_transaction_recover(tdb
);
320 tdb_brunlock(tdb
, F_WRLCK
, OPEN_LOCK
, 1);
321 tdb_brunlock(tdb
, F_WRLCK
, FREELIST_TOP
, 0);
326 static bool have_data_locks(const struct tdb_context
*tdb
)
330 for (i
= 0; i
< tdb
->num_lockrecs
; i
++) {
331 if (tdb
->lockrecs
[i
].off
>= lock_offset(-1))
338 * A allrecord lock allows us to avoid per chain locks. Check if the allrecord
339 * lock is strong enough.
341 static int tdb_lock_covered_by_allrecord_lock(struct tdb_context
*tdb
,
344 if (ltype
== F_RDLCK
) {
346 * The allrecord_lock is equal (F_RDLCK) or stronger
352 if (tdb
->allrecord_lock
.ltype
== F_RDLCK
) {
354 * We ask for ltype==F_WRLCK, but the allrecord_lock
355 * is too weak. We can't upgrade here, so fail.
357 tdb
->ecode
= TDB_ERR_LOCK
;
362 * Asking for F_WRLCK, allrecord is F_WRLCK as well. Pass.
367 static int tdb_lock_list(struct tdb_context
*tdb
, int list
, int ltype
,
368 enum tdb_lock_flags waitflag
)
373 if (tdb
->allrecord_lock
.count
) {
374 return tdb_lock_covered_by_allrecord_lock(tdb
, ltype
);
378 * Check for recoveries: Someone might have kill -9'ed a process
381 check
= !have_data_locks(tdb
);
382 ret
= tdb_nest_lock(tdb
, lock_offset(list
), ltype
, waitflag
);
384 if (ret
== 0 && check
&& tdb_needs_recovery(tdb
)) {
385 tdb_nest_unlock(tdb
, lock_offset(list
), ltype
, false);
387 if (tdb_lock_and_recover(tdb
) == -1) {
390 return tdb_lock_list(tdb
, list
, ltype
, waitflag
);
395 /* lock a list in the database. list -1 is the alloc list */
396 int tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
)
400 ret
= tdb_lock_list(tdb
, list
, ltype
, TDB_LOCK_WAIT
);
402 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock failed on list %d "
403 "ltype=%d (%s)\n", list
, ltype
, strerror(errno
)));
408 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
409 _PUBLIC_
int tdb_lock_nonblock(struct tdb_context
*tdb
, int list
, int ltype
)
411 return tdb_lock_list(tdb
, list
, ltype
, TDB_LOCK_NOWAIT
);
415 int tdb_nest_unlock(struct tdb_context
*tdb
, uint32_t offset
, int ltype
,
419 struct tdb_lock_type
*lck
;
421 if (tdb
->flags
& TDB_NOLOCK
)
425 if (offset
>= lock_offset(tdb
->header
.hash_size
)) {
426 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: offset %u invalid (%d)\n", offset
, tdb
->header
.hash_size
));
430 lck
= find_nestlock(tdb
, offset
);
431 if ((lck
== NULL
) || (lck
->count
== 0)) {
432 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: count is 0\n"));
436 if (lck
->count
> 1) {
442 * This lock has count==1 left, so we need to unlock it in the
443 * kernel. We don't bother with decrementing the in-memory array
444 * element, we're about to overwrite it with the last array element
451 ret
= tdb_brunlock(tdb
, ltype
, offset
, 1);
455 * Shrink the array by overwriting the element just unlocked with the
456 * last array element.
458 *lck
= tdb
->lockrecs
[--tdb
->num_lockrecs
];
461 * We don't bother with realloc when the array shrinks, but if we have
462 * a completely idle tdb we should get rid of the locked array.
465 if (tdb
->num_lockrecs
== 0) {
466 SAFE_FREE(tdb
->lockrecs
);
470 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: An error occurred unlocking!\n"));
474 _PUBLIC_
int tdb_unlock(struct tdb_context
*tdb
, int list
, int ltype
)
476 /* a global lock allows us to avoid per chain locks */
477 if (tdb
->allrecord_lock
.count
) {
478 return tdb_lock_covered_by_allrecord_lock(tdb
, ltype
);
481 return tdb_nest_unlock(tdb
, lock_offset(list
), ltype
, false);
485 get the transaction lock
487 int tdb_transaction_lock(struct tdb_context
*tdb
, int ltype
,
488 enum tdb_lock_flags lockflags
)
490 return tdb_nest_lock(tdb
, TRANSACTION_LOCK
, ltype
, lockflags
);
494 release the transaction lock
496 int tdb_transaction_unlock(struct tdb_context
*tdb
, int ltype
)
498 return tdb_nest_unlock(tdb
, TRANSACTION_LOCK
, ltype
, false);
501 /* Returns 0 if all done, -1 if error, 1 if ok. */
502 static int tdb_allrecord_check(struct tdb_context
*tdb
, int ltype
,
503 enum tdb_lock_flags flags
, bool upgradable
)
505 /* There are no locks on read-only dbs */
506 if (tdb
->read_only
|| tdb
->traverse_read
) {
507 tdb
->ecode
= TDB_ERR_LOCK
;
511 if (tdb
->allrecord_lock
.count
&& tdb
->allrecord_lock
.ltype
== ltype
) {
512 tdb
->allrecord_lock
.count
++;
516 if (tdb
->allrecord_lock
.count
) {
517 /* a global lock of a different type exists */
518 tdb
->ecode
= TDB_ERR_LOCK
;
522 if (tdb_have_extra_locks(tdb
)) {
523 /* can't combine global and chain locks */
524 tdb
->ecode
= TDB_ERR_LOCK
;
528 if (upgradable
&& ltype
!= F_RDLCK
) {
529 /* tdb error: you can't upgrade a write lock! */
530 tdb
->ecode
= TDB_ERR_LOCK
;
536 /* We only need to lock individual bytes, but Linux merges consecutive locks
537 * so we lock in contiguous ranges. */
538 static int tdb_chainlock_gradual(struct tdb_context
*tdb
,
539 int ltype
, enum tdb_lock_flags flags
,
540 size_t off
, size_t len
)
543 enum tdb_lock_flags nb_flags
= (flags
& ~TDB_LOCK_WAIT
);
546 /* Single record. Just do blocking lock. */
547 return tdb_brlock(tdb
, ltype
, off
, len
, flags
);
550 /* First we try non-blocking. */
551 ret
= tdb_brlock(tdb
, ltype
, off
, len
, nb_flags
);
556 /* Try locking first half, then second. */
557 ret
= tdb_chainlock_gradual(tdb
, ltype
, flags
, off
, len
/ 2);
561 ret
= tdb_chainlock_gradual(tdb
, ltype
, flags
,
562 off
+ len
/ 2, len
- len
/ 2);
564 tdb_brunlock(tdb
, ltype
, off
, len
/ 2);
570 /* lock/unlock entire database. It can only be upgradable if you have some
571 * other way of guaranteeing exclusivity (ie. transaction write lock).
572 * We do the locking gradually to avoid being starved by smaller locks. */
573 int tdb_allrecord_lock(struct tdb_context
*tdb
, int ltype
,
574 enum tdb_lock_flags flags
, bool upgradable
)
576 switch (tdb_allrecord_check(tdb
, ltype
, flags
, upgradable
)) {
583 /* We cover two kinds of locks:
584 * 1) Normal chain locks. Taken for almost all operations.
585 * 2) Individual records locks. Taken after normal or free
588 * It is (1) which cause the starvation problem, so we're only
589 * gradual for that. */
590 if (tdb_chainlock_gradual(tdb
, ltype
, flags
, FREELIST_TOP
,
591 tdb
->header
.hash_size
* 4) == -1) {
595 /* Grab individual record locks. */
596 if (tdb_brlock(tdb
, ltype
, lock_offset(tdb
->header
.hash_size
), 0,
598 tdb_brunlock(tdb
, ltype
, FREELIST_TOP
,
599 tdb
->header
.hash_size
* 4);
603 tdb
->allrecord_lock
.count
= 1;
604 /* If it's upgradable, it's actually exclusive so we can treat
605 * it as a write lock. */
606 tdb
->allrecord_lock
.ltype
= upgradable
? F_WRLCK
: ltype
;
607 tdb
->allrecord_lock
.off
= upgradable
;
609 if (tdb_needs_recovery(tdb
)) {
610 bool mark
= flags
& TDB_LOCK_MARK_ONLY
;
611 tdb_allrecord_unlock(tdb
, ltype
, mark
);
613 tdb
->ecode
= TDB_ERR_LOCK
;
614 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,
615 "tdb_lockall_mark cannot do recovery\n"));
618 if (tdb_lock_and_recover(tdb
) == -1) {
621 return tdb_allrecord_lock(tdb
, ltype
, flags
, upgradable
);
629 /* unlock entire db */
630 int tdb_allrecord_unlock(struct tdb_context
*tdb
, int ltype
, bool mark_lock
)
632 /* There are no locks on read-only dbs */
633 if (tdb
->read_only
|| tdb
->traverse_read
) {
634 tdb
->ecode
= TDB_ERR_LOCK
;
638 if (tdb
->allrecord_lock
.count
== 0) {
639 tdb
->ecode
= TDB_ERR_LOCK
;
643 /* Upgradable locks are marked as write locks. */
644 if (tdb
->allrecord_lock
.ltype
!= ltype
645 && (!tdb
->allrecord_lock
.off
|| ltype
!= F_RDLCK
)) {
646 tdb
->ecode
= TDB_ERR_LOCK
;
650 if (tdb
->allrecord_lock
.count
> 1) {
651 tdb
->allrecord_lock
.count
--;
655 if (!mark_lock
&& tdb_brunlock(tdb
, ltype
, FREELIST_TOP
, 0)) {
656 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlockall failed (%s)\n", strerror(errno
)));
660 tdb
->allrecord_lock
.count
= 0;
661 tdb
->allrecord_lock
.ltype
= 0;
666 /* lock entire database with write lock */
667 _PUBLIC_
int tdb_lockall(struct tdb_context
*tdb
)
669 tdb_trace(tdb
, "tdb_lockall");
670 return tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_WAIT
, false);
673 /* lock entire database with write lock - mark only */
674 _PUBLIC_
int tdb_lockall_mark(struct tdb_context
*tdb
)
676 tdb_trace(tdb
, "tdb_lockall_mark");
677 return tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_MARK_ONLY
, false);
680 /* unlock entire database with write lock - unmark only */
681 _PUBLIC_
int tdb_lockall_unmark(struct tdb_context
*tdb
)
683 tdb_trace(tdb
, "tdb_lockall_unmark");
684 return tdb_allrecord_unlock(tdb
, F_WRLCK
, true);
687 /* lock entire database with write lock - nonblocking varient */
688 _PUBLIC_
int tdb_lockall_nonblock(struct tdb_context
*tdb
)
690 int ret
= tdb_allrecord_lock(tdb
, F_WRLCK
, TDB_LOCK_NOWAIT
, false);
691 tdb_trace_ret(tdb
, "tdb_lockall_nonblock", ret
);
695 /* unlock entire database with write lock */
696 _PUBLIC_
int tdb_unlockall(struct tdb_context
*tdb
)
698 tdb_trace(tdb
, "tdb_unlockall");
699 return tdb_allrecord_unlock(tdb
, F_WRLCK
, false);
702 /* lock entire database with read lock */
703 _PUBLIC_
int tdb_lockall_read(struct tdb_context
*tdb
)
705 tdb_trace(tdb
, "tdb_lockall_read");
706 return tdb_allrecord_lock(tdb
, F_RDLCK
, TDB_LOCK_WAIT
, false);
709 /* lock entire database with read lock - nonblock varient */
710 _PUBLIC_
int tdb_lockall_read_nonblock(struct tdb_context
*tdb
)
712 int ret
= tdb_allrecord_lock(tdb
, F_RDLCK
, TDB_LOCK_NOWAIT
, false);
713 tdb_trace_ret(tdb
, "tdb_lockall_read_nonblock", ret
);
717 /* unlock entire database with read lock */
718 _PUBLIC_
int tdb_unlockall_read(struct tdb_context
*tdb
)
720 tdb_trace(tdb
, "tdb_unlockall_read");
721 return tdb_allrecord_unlock(tdb
, F_RDLCK
, false);
724 /* lock/unlock one hash chain. This is meant to be used to reduce
725 contention - it cannot guarantee how many records will be locked */
726 _PUBLIC_
int tdb_chainlock(struct tdb_context
*tdb
, TDB_DATA key
)
728 int ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
729 tdb_trace_1rec(tdb
, "tdb_chainlock", key
);
733 /* lock/unlock one hash chain, non-blocking. This is meant to be used
734 to reduce contention - it cannot guarantee how many records will be
736 _PUBLIC_
int tdb_chainlock_nonblock(struct tdb_context
*tdb
, TDB_DATA key
)
738 int ret
= tdb_lock_nonblock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
739 tdb_trace_1rec_ret(tdb
, "tdb_chainlock_nonblock", key
, ret
);
743 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
744 _PUBLIC_
int tdb_chainlock_mark(struct tdb_context
*tdb
, TDB_DATA key
)
746 int ret
= tdb_nest_lock(tdb
, lock_offset(BUCKET(tdb
->hash_fn(&key
))),
747 F_WRLCK
, TDB_LOCK_MARK_ONLY
);
748 tdb_trace_1rec(tdb
, "tdb_chainlock_mark", key
);
752 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
753 _PUBLIC_
int tdb_chainlock_unmark(struct tdb_context
*tdb
, TDB_DATA key
)
755 tdb_trace_1rec(tdb
, "tdb_chainlock_unmark", key
);
756 return tdb_nest_unlock(tdb
, lock_offset(BUCKET(tdb
->hash_fn(&key
))),
760 _PUBLIC_
int tdb_chainunlock(struct tdb_context
*tdb
, TDB_DATA key
)
762 tdb_trace_1rec(tdb
, "tdb_chainunlock", key
);
763 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
766 _PUBLIC_
int tdb_chainlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
769 ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
770 tdb_trace_1rec(tdb
, "tdb_chainlock_read", key
);
774 _PUBLIC_
int tdb_chainunlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
776 tdb_trace_1rec(tdb
, "tdb_chainunlock_read", key
);
777 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
780 /* record lock stops delete underneath */
781 int tdb_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
783 if (tdb
->allrecord_lock
.count
) {
786 return off
? tdb_brlock(tdb
, F_RDLCK
, off
, 1, TDB_LOCK_WAIT
) : 0;
790 Write locks override our own fcntl readlocks, so check it here.
791 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
792 an error to fail to get the lock here.
794 int tdb_write_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
796 struct tdb_traverse_lock
*i
;
797 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
800 if (tdb
->allrecord_lock
.count
) {
801 if (tdb
->allrecord_lock
.ltype
== F_WRLCK
) {
806 return tdb_brlock(tdb
, F_WRLCK
, off
, 1, TDB_LOCK_NOWAIT
|TDB_LOCK_PROBE
);
809 int tdb_write_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
811 if (tdb
->allrecord_lock
.count
) {
814 return tdb_brunlock(tdb
, F_WRLCK
, off
, 1);
817 /* fcntl locks don't stack: avoid unlocking someone else's */
818 int tdb_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
820 struct tdb_traverse_lock
*i
;
823 if (tdb
->allrecord_lock
.count
) {
829 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
832 return (count
== 1 ? tdb_brunlock(tdb
, F_RDLCK
, off
, 1) : 0);
835 bool tdb_have_extra_locks(struct tdb_context
*tdb
)
837 unsigned int extra
= tdb
->num_lockrecs
;
839 /* A transaction holds the lock for all records. */
840 if (!tdb
->transaction
&& tdb
->allrecord_lock
.count
) {
844 /* We always hold the active lock if CLEAR_IF_FIRST. */
845 if (find_nestlock(tdb
, ACTIVE_LOCK
)) {
849 /* In a transaction, we expect to hold the transaction lock */
850 if (tdb
->transaction
&& find_nestlock(tdb
, TRANSACTION_LOCK
)) {
857 /* The transaction code uses this to remove all locks. */
858 void tdb_release_transaction_locks(struct tdb_context
*tdb
)
860 unsigned int i
, active
= 0;
862 if (tdb
->allrecord_lock
.count
!= 0) {
863 tdb_brunlock(tdb
, tdb
->allrecord_lock
.ltype
, FREELIST_TOP
, 0);
864 tdb
->allrecord_lock
.count
= 0;
867 for (i
=0;i
<tdb
->num_lockrecs
;i
++) {
868 struct tdb_lock_type
*lck
= &tdb
->lockrecs
[i
];
870 /* Don't release the active lock! Copy it to first entry. */
871 if (lck
->off
== ACTIVE_LOCK
) {
872 tdb
->lockrecs
[active
++] = *lck
;
874 tdb_brunlock(tdb
, lck
->ltype
, lck
->off
, 1);
877 tdb
->num_lockrecs
= active
;
878 if (tdb
->num_lockrecs
== 0) {
879 SAFE_FREE(tdb
->lockrecs
);
883 /* Following functions are added specifically to support CTDB. */
885 /* Don't do actual fcntl locking, just mark tdb locked */
886 int tdb_transaction_write_lock_mark(struct tdb_context
*tdb
);
887 _PUBLIC_
int tdb_transaction_write_lock_mark(struct tdb_context
*tdb
)
889 return tdb_transaction_lock(tdb
, F_WRLCK
, TDB_LOCK_MARK_ONLY
);
892 /* Don't do actual fcntl unlocking, just mark tdb unlocked */
893 int tdb_transaction_write_lock_unmark(struct tdb_context
*tdb
);
894 _PUBLIC_
int tdb_transaction_write_lock_unmark(struct tdb_context
*tdb
)
896 return tdb_nest_unlock(tdb
, TRANSACTION_LOCK
, F_WRLCK
, true);