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 #define TDB_MARK_LOCK 0x80000000
32 void tdb_setalarm_sigptr(struct tdb_context
*tdb
, volatile sig_atomic_t *ptr
)
34 tdb
->interrupt_sig_ptr
= ptr
;
37 /* a byte range locking function - return 0 on success
38 this functions locks/unlocks 1 byte at the specified offset.
40 On error, errno is also set so that errors are passed back properly
43 note that a len of zero means lock to end of file
45 int tdb_brlock(struct tdb_context
*tdb
, tdb_off_t offset
,
46 int rw_type
, int lck_type
, int probe
, size_t len
)
51 if (tdb
->flags
& TDB_NOLOCK
) {
55 if ((rw_type
== F_WRLCK
) && (tdb
->read_only
|| tdb
->traverse_read
)) {
56 tdb
->ecode
= TDB_ERR_RDONLY
;
61 fl
.l_whence
= SEEK_SET
;
67 ret
= fcntl(tdb
->fd
,lck_type
,&fl
);
69 /* Check for a sigalarm break. */
70 if (ret
== -1 && errno
== EINTR
&&
71 tdb
->interrupt_sig_ptr
&&
72 *tdb
->interrupt_sig_ptr
) {
75 } while (ret
== -1 && errno
== EINTR
);
78 tdb
->ecode
= TDB_ERR_LOCK
;
79 /* Generic lock error. errno set by fcntl.
80 * EAGAIN is an expected return from non-blocking
82 if (!probe
&& lck_type
!= F_SETLK
) {
83 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
84 tdb
->fd
, offset
, rw_type
, lck_type
, (int)len
));
93 upgrade a read lock to a write lock. This needs to be handled in a
94 special way as some OSes (such as solaris) have too conservative
95 deadlock detection and claim a deadlock when progress can be
96 made. For those OSes we may loop for a while.
98 int tdb_brlock_upgrade(struct tdb_context
*tdb
, tdb_off_t offset
, size_t len
)
103 if (tdb_brlock(tdb
, offset
, F_WRLCK
, F_SETLKW
, 1, len
) == 0) {
106 if (errno
!= EDEADLK
) {
109 /* sleep for as short a time as we can - more portable than usleep() */
112 select(0, NULL
, NULL
, NULL
, &tv
);
114 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock_upgrade failed at offset %d\n", offset
));
119 /* lock a list in the database. list -1 is the alloc list */
120 static int _tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
, int op
)
122 struct tdb_lock_type
*new_lck
;
124 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
126 ltype
&= ~TDB_MARK_LOCK
;
128 /* a global lock allows us to avoid per chain locks */
129 if (tdb
->global_lock
.count
&&
130 (ltype
== tdb
->global_lock
.ltype
|| ltype
== F_RDLCK
)) {
134 if (tdb
->global_lock
.count
) {
135 tdb
->ecode
= TDB_ERR_LOCK
;
139 if (list
< -1 || list
>= (int)tdb
->header
.hash_size
) {
140 tdb
->ecode
= TDB_ERR_LOCK
;
141 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_lock: invalid list %d for ltype=%d\n",
145 if (tdb
->flags
& TDB_NOLOCK
)
148 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
149 if (tdb
->lockrecs
[i
].list
== list
) {
150 if (tdb
->lockrecs
[i
].count
== 0) {
152 * Can't happen, see tdb_unlock(). It should
155 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock: "
156 "lck->count == 0 for list %d", list
));
159 * Just increment the in-memory struct, posix locks
162 tdb
->lockrecs
[i
].count
++;
167 new_lck
= (struct tdb_lock_type
*)realloc(
169 sizeof(*tdb
->lockrecs
) * (tdb
->num_lockrecs
+1));
170 if (new_lck
== NULL
) {
174 tdb
->lockrecs
= new_lck
;
176 /* Since fcntl locks don't nest, we do a lock for the first one,
177 and simply bump the count for future ones */
179 tdb
->methods
->tdb_brlock(tdb
,FREELIST_TOP
+4*list
, ltype
, op
,
186 tdb
->lockrecs
[tdb
->num_lockrecs
].list
= list
;
187 tdb
->lockrecs
[tdb
->num_lockrecs
].count
= 1;
188 tdb
->lockrecs
[tdb
->num_lockrecs
].ltype
= ltype
;
189 tdb
->num_lockrecs
+= 1;
194 /* lock a list in the database. list -1 is the alloc list */
195 int tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
)
198 ret
= _tdb_lock(tdb
, list
, ltype
, F_SETLKW
);
200 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock failed on list %d "
201 "ltype=%d (%s)\n", list
, ltype
, strerror(errno
)));
206 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
207 int tdb_lock_nonblock(struct tdb_context
*tdb
, int list
, int ltype
)
209 return _tdb_lock(tdb
, list
, ltype
, F_SETLK
);
213 /* unlock the database: returns void because it's too late for errors. */
214 /* changed to return int it may be interesting to know there
215 has been an error --simo */
216 int tdb_unlock(struct tdb_context
*tdb
, int list
, int ltype
)
220 struct tdb_lock_type
*lck
= NULL
;
221 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
223 ltype
&= ~TDB_MARK_LOCK
;
225 /* a global lock allows us to avoid per chain locks */
226 if (tdb
->global_lock
.count
&&
227 (ltype
== tdb
->global_lock
.ltype
|| ltype
== F_RDLCK
)) {
231 if (tdb
->global_lock
.count
) {
232 tdb
->ecode
= TDB_ERR_LOCK
;
236 if (tdb
->flags
& TDB_NOLOCK
)
240 if (list
< -1 || list
>= (int)tdb
->header
.hash_size
) {
241 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: list %d invalid (%d)\n", list
, tdb
->header
.hash_size
));
245 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
246 if (tdb
->lockrecs
[i
].list
== list
) {
247 lck
= &tdb
->lockrecs
[i
];
252 if ((lck
== NULL
) || (lck
->count
== 0)) {
253 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: count is 0\n"));
257 if (lck
->count
> 1) {
263 * This lock has count==1 left, so we need to unlock it in the
264 * kernel. We don't bother with decrementing the in-memory array
265 * element, we're about to overwrite it with the last array element
272 ret
= tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
+4*list
, F_UNLCK
,
278 * Shrink the array by overwriting the element just unlocked with the
279 * last array element.
282 if (tdb
->num_lockrecs
> 1) {
283 *lck
= tdb
->lockrecs
[tdb
->num_lockrecs
-1];
285 tdb
->num_lockrecs
-= 1;
288 * We don't bother with realloc when the array shrinks, but if we have
289 * a completely idle tdb we should get rid of the locked array.
292 if (tdb
->num_lockrecs
== 0) {
293 SAFE_FREE(tdb
->lockrecs
);
297 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: An error occurred unlocking!\n"));
302 get the transaction lock
304 int tdb_transaction_lock(struct tdb_context
*tdb
, int ltype
)
306 if (tdb
->global_lock
.count
) {
309 if (tdb
->transaction_lock_count
> 0) {
310 tdb
->transaction_lock_count
++;
314 if (tdb
->methods
->tdb_brlock(tdb
, TRANSACTION_LOCK
, ltype
,
315 F_SETLKW
, 0, 1) == -1) {
316 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_lock: failed to get transaction lock\n"));
317 tdb
->ecode
= TDB_ERR_LOCK
;
320 tdb
->transaction_lock_count
++;
325 release the transaction lock
327 int tdb_transaction_unlock(struct tdb_context
*tdb
)
330 if (tdb
->global_lock
.count
) {
333 if (tdb
->transaction_lock_count
> 1) {
334 tdb
->transaction_lock_count
--;
337 ret
= tdb
->methods
->tdb_brlock(tdb
, TRANSACTION_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
339 tdb
->transaction_lock_count
= 0;
347 /* lock/unlock entire database */
348 static int _tdb_lockall(struct tdb_context
*tdb
, int ltype
, int op
)
350 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
352 ltype
&= ~TDB_MARK_LOCK
;
354 /* There are no locks on read-only dbs */
355 if (tdb
->read_only
|| tdb
->traverse_read
) {
356 tdb
->ecode
= TDB_ERR_LOCK
;
360 if (tdb
->global_lock
.count
&& tdb
->global_lock
.ltype
== ltype
) {
361 tdb
->global_lock
.count
++;
365 if (tdb
->global_lock
.count
) {
366 /* a global lock of a different type exists */
367 tdb
->ecode
= TDB_ERR_LOCK
;
371 if (tdb
->num_locks
!= 0) {
372 /* can't combine global and chain locks */
373 tdb
->ecode
= TDB_ERR_LOCK
;
378 tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
, ltype
, op
,
379 0, 4*tdb
->header
.hash_size
)) {
380 if (op
== F_SETLKW
) {
381 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lockall failed (%s)\n", strerror(errno
)));
386 tdb
->global_lock
.count
= 1;
387 tdb
->global_lock
.ltype
= ltype
;
394 /* unlock entire db */
395 static int _tdb_unlockall(struct tdb_context
*tdb
, int ltype
)
397 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
399 ltype
&= ~TDB_MARK_LOCK
;
401 /* There are no locks on read-only dbs */
402 if (tdb
->read_only
|| tdb
->traverse_read
) {
403 tdb
->ecode
= TDB_ERR_LOCK
;
407 if (tdb
->global_lock
.ltype
!= ltype
|| tdb
->global_lock
.count
== 0) {
408 tdb
->ecode
= TDB_ERR_LOCK
;
412 if (tdb
->global_lock
.count
> 1) {
413 tdb
->global_lock
.count
--;
418 tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
, F_UNLCK
, F_SETLKW
,
419 0, 4*tdb
->header
.hash_size
)) {
420 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlockall failed (%s)\n", strerror(errno
)));
424 tdb
->global_lock
.count
= 0;
425 tdb
->global_lock
.ltype
= 0;
430 /* lock entire database with write lock */
431 int tdb_lockall(struct tdb_context
*tdb
)
433 tdb_trace(tdb
, "tdb_lockall");
434 return _tdb_lockall(tdb
, F_WRLCK
, F_SETLKW
);
437 /* lock entire database with write lock - mark only */
438 int tdb_lockall_mark(struct tdb_context
*tdb
)
440 tdb_trace(tdb
, "tdb_lockall_mark");
441 return _tdb_lockall(tdb
, F_WRLCK
| TDB_MARK_LOCK
, F_SETLKW
);
444 /* unlock entire database with write lock - unmark only */
445 int tdb_lockall_unmark(struct tdb_context
*tdb
)
447 tdb_trace(tdb
, "tdb_lockall_unmark");
448 return _tdb_unlockall(tdb
, F_WRLCK
| TDB_MARK_LOCK
);
451 /* lock entire database with write lock - nonblocking varient */
452 int tdb_lockall_nonblock(struct tdb_context
*tdb
)
454 int ret
= _tdb_lockall(tdb
, F_WRLCK
, F_SETLK
);
455 tdb_trace_ret(tdb
, "tdb_lockall_nonblock", ret
);
459 /* unlock entire database with write lock */
460 int tdb_unlockall(struct tdb_context
*tdb
)
462 tdb_trace(tdb
, "tdb_unlockall");
463 return _tdb_unlockall(tdb
, F_WRLCK
);
466 /* lock entire database with read lock */
467 int tdb_lockall_read(struct tdb_context
*tdb
)
469 tdb_trace(tdb
, "tdb_lockall_read");
470 return _tdb_lockall(tdb
, F_RDLCK
, F_SETLKW
);
473 /* lock entire database with read lock - nonblock varient */
474 int tdb_lockall_read_nonblock(struct tdb_context
*tdb
)
476 int ret
= _tdb_lockall(tdb
, F_RDLCK
, F_SETLK
);
477 tdb_trace_ret(tdb
, "tdb_lockall_read_nonblock", ret
);
481 /* unlock entire database with read lock */
482 int tdb_unlockall_read(struct tdb_context
*tdb
)
484 tdb_trace(tdb
, "tdb_unlockall_read");
485 return _tdb_unlockall(tdb
, F_RDLCK
);
488 /* lock/unlock one hash chain. This is meant to be used to reduce
489 contention - it cannot guarantee how many records will be locked */
490 int tdb_chainlock(struct tdb_context
*tdb
, TDB_DATA key
)
492 int ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
493 tdb_trace_1rec(tdb
, "tdb_chainlock", key
);
497 /* lock/unlock one hash chain, non-blocking. This is meant to be used
498 to reduce contention - it cannot guarantee how many records will be
500 int tdb_chainlock_nonblock(struct tdb_context
*tdb
, TDB_DATA key
)
502 int ret
= tdb_lock_nonblock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
503 tdb_trace_1rec_ret(tdb
, "tdb_chainlock_nonblock", key
, ret
);
507 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
508 int tdb_chainlock_mark(struct tdb_context
*tdb
, TDB_DATA key
)
510 int ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
| TDB_MARK_LOCK
);
511 tdb_trace_1rec(tdb
, "tdb_chainlock_mark", key
);
515 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
516 int tdb_chainlock_unmark(struct tdb_context
*tdb
, TDB_DATA key
)
518 tdb_trace_1rec(tdb
, "tdb_chainlock_unmark", key
);
519 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
| TDB_MARK_LOCK
);
522 int tdb_chainunlock(struct tdb_context
*tdb
, TDB_DATA key
)
524 tdb_trace_1rec(tdb
, "tdb_chainunlock", key
);
525 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
528 int tdb_chainlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
531 ret
= tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
532 tdb_trace_1rec(tdb
, "tdb_chainlock_read", key
);
536 int tdb_chainunlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
538 tdb_trace_1rec(tdb
, "tdb_chainunlock_read", key
);
539 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
544 /* record lock stops delete underneath */
545 int tdb_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
547 if (tdb
->global_lock
.count
) {
550 return off
? tdb
->methods
->tdb_brlock(tdb
, off
, F_RDLCK
, F_SETLKW
, 0, 1) : 0;
554 Write locks override our own fcntl readlocks, so check it here.
555 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
556 an error to fail to get the lock here.
558 int tdb_write_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
560 struct tdb_traverse_lock
*i
;
561 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
564 return tdb
->methods
->tdb_brlock(tdb
, off
, F_WRLCK
, F_SETLK
, 1, 1);
568 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
569 an error to fail to get the lock here.
571 int tdb_write_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
573 return tdb
->methods
->tdb_brlock(tdb
, off
, F_UNLCK
, F_SETLK
, 0, 1);
576 /* fcntl locks don't stack: avoid unlocking someone else's */
577 int tdb_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
579 struct tdb_traverse_lock
*i
;
582 if (tdb
->global_lock
.count
) {
588 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
591 return (count
== 1 ? tdb
->methods
->tdb_brlock(tdb
, off
, F_UNLCK
, F_SETLKW
, 0, 1) : 0);