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 ntdb
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 enum NTDB_ERROR
owner_conflict(struct ntdb_context
*ntdb
, const char *call
)
35 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
36 "%s: lock owned by another ntdb in this process.",
40 /* If we fork, we no longer really own locks. */
41 bool check_lock_pid(struct ntdb_context
*ntdb
, const char *call
, bool log
)
43 /* No locks? No problem! */
44 if (ntdb
->file
->allrecord_lock
.count
== 0
45 && ntdb
->file
->num_lockrecs
== 0) {
49 /* No fork? No problem! */
50 if (ntdb
->file
->locker
== getpid()) {
55 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
56 "%s: fork() detected after lock acquisition!"
57 " (%u vs %u)", call
, ntdb
->file
->locker
, getpid());
62 int ntdb_fcntl_lock(int fd
, int rw
, off_t off
, off_t len
, bool waitflag
,
70 fl
.l_whence
= SEEK_SET
;
75 ret
= fcntl(fd
, F_SETLKW
, &fl
);
77 ret
= fcntl(fd
, F_SETLK
, &fl
);
78 } while (ret
!= 0 && errno
== EINTR
);
82 int ntdb_fcntl_unlock(int fd
, int rw
, off_t off
, off_t len
, void *unused
)
89 fl
.l_whence
= SEEK_SET
;
93 ret
= fcntl(fd
, F_SETLKW
, &fl
);
94 } while (ret
!= 0 && errno
== EINTR
);
98 static int lock(struct ntdb_context
*ntdb
,
99 int rw
, off_t off
, off_t len
, bool waitflag
)
102 if (ntdb
->file
->allrecord_lock
.count
== 0
103 && ntdb
->file
->num_lockrecs
== 0) {
104 ntdb
->file
->locker
= getpid();
107 ntdb
->stats
.lock_lowlevel
++;
108 ret
= ntdb
->lock_fn(ntdb
->file
->fd
, rw
, off
, len
, waitflag
,
111 ntdb
->stats
.lock_nonblock
++;
113 ntdb
->stats
.lock_nonblock_fail
++;
118 static int unlock(struct ntdb_context
*ntdb
, int rw
, off_t off
, off_t len
)
120 #if 0 /* Check they matched up locks and unlocks correctly. */
125 locks
= fopen("/proc/locks", "r");
127 while (fgets(line
, 80, locks
)) {
131 /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
132 p
= strchr(line
, ':') + 1;
133 if (strncmp(p
, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
135 p
+= strlen(" FLOCK ADVISORY ");
136 if (strncmp(p
, "READ ", strlen("READ ")) == 0)
138 else if (strncmp(p
, "WRITE ", strlen("WRITE ")) == 0)
143 if (atoi(p
) != getpid())
145 p
= strchr(strchr(p
, ' ') + 1, ' ') + 1;
147 p
= strchr(p
, ' ') + 1;
148 if (strncmp(p
, "EOF", 3) == 0)
151 l
= atoi(p
) - start
+ 1;
155 fprintf(stderr
, "Len %u should be %u: %s",
160 fprintf(stderr
, "Type %s wrong: %s",
161 rw
== F_RDLCK
? "READ" : "WRITE", line
);
170 fprintf(stderr
, "Unlock on %u@%u not found!",
178 return ntdb
->unlock_fn(ntdb
->file
->fd
, rw
, off
, len
, ntdb
->lock_data
);
181 /* a byte range locking function - return 0 on success
182 this functions locks len bytes at the specified offset.
184 note that a len of zero means lock to end of file
186 static enum NTDB_ERROR
ntdb_brlock(struct ntdb_context
*ntdb
,
187 int rw_type
, ntdb_off_t offset
, ntdb_off_t len
,
188 enum ntdb_lock_flags flags
)
192 if (ntdb
->flags
& NTDB_NOLOCK
) {
196 if (rw_type
== F_WRLCK
&& (ntdb
->flags
& NTDB_RDONLY
)) {
197 return ntdb_logerr(ntdb
, NTDB_ERR_RDONLY
, NTDB_LOG_USE_ERROR
,
198 "Write lock attempted on read-only database");
201 /* A 32 bit system cannot open a 64-bit file, but it could have
202 * expanded since then: check here. */
203 if ((size_t)(offset
+ len
) != offset
+ len
) {
204 return ntdb_logerr(ntdb
, NTDB_ERR_IO
, NTDB_LOG_ERROR
,
205 "ntdb_brlock: lock on giant offset %llu",
206 (long long)(offset
+ len
));
209 ret
= lock(ntdb
, rw_type
, offset
, len
, flags
& NTDB_LOCK_WAIT
);
211 /* Generic lock error. errno set by fcntl.
212 * EAGAIN is an expected return from non-blocking
214 if (!(flags
& NTDB_LOCK_PROBE
)
215 && (errno
!= EAGAIN
&& errno
!= EINTR
)) {
216 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
217 "ntdb_brlock failed (fd=%d) at"
218 " offset %zu rw_type=%d flags=%d len=%zu:"
220 ntdb
->file
->fd
, (size_t)offset
, rw_type
,
221 flags
, (size_t)len
, strerror(errno
));
223 return NTDB_ERR_LOCK
;
228 static enum NTDB_ERROR
ntdb_brunlock(struct ntdb_context
*ntdb
,
229 int rw_type
, ntdb_off_t offset
, size_t len
)
231 if (ntdb
->flags
& NTDB_NOLOCK
) {
235 if (!check_lock_pid(ntdb
, "ntdb_brunlock", false))
236 return NTDB_ERR_LOCK
;
238 if (unlock(ntdb
, rw_type
, offset
, len
) == -1) {
239 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
240 "ntdb_brunlock failed (fd=%d) at offset %zu"
241 " rw_type=%d len=%zu: %s",
242 ntdb
->file
->fd
, (size_t)offset
, rw_type
,
243 (size_t)len
, strerror(errno
));
249 upgrade a read lock to a write lock. This needs to be handled in a
250 special way as some OSes (such as solaris) have too conservative
251 deadlock detection and claim a deadlock when progress can be
252 made. For those OSes we may loop for a while.
254 enum NTDB_ERROR
ntdb_allrecord_upgrade(struct ntdb_context
*ntdb
, off_t start
)
258 if (!check_lock_pid(ntdb
, "ntdb_transaction_prepare_commit", true))
259 return NTDB_ERR_LOCK
;
261 if (ntdb
->file
->allrecord_lock
.count
!= 1) {
262 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
263 "ntdb_allrecord_upgrade failed:"
264 " count %u too high",
265 ntdb
->file
->allrecord_lock
.count
);
268 if (ntdb
->file
->allrecord_lock
.off
!= 1) {
269 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
270 "ntdb_allrecord_upgrade failed:"
271 " already upgraded?");
274 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
) {
275 return owner_conflict(ntdb
, "ntdb_allrecord_upgrade");
280 if (ntdb_brlock(ntdb
, F_WRLCK
, start
, 0,
281 NTDB_LOCK_WAIT
|NTDB_LOCK_PROBE
) == NTDB_SUCCESS
) {
282 ntdb
->file
->allrecord_lock
.ltype
= F_WRLCK
;
283 ntdb
->file
->allrecord_lock
.off
= 0;
286 if (errno
!= EDEADLK
) {
289 /* sleep for as short a time as we can - more portable than usleep() */
292 select(0, NULL
, NULL
, NULL
, &tv
);
295 if (errno
!= EAGAIN
&& errno
!= EINTR
)
296 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
297 "ntdb_allrecord_upgrade failed");
298 return NTDB_ERR_LOCK
;
301 static struct ntdb_lock
*find_nestlock(struct ntdb_context
*ntdb
, ntdb_off_t offset
,
302 const struct ntdb_context
*owner
)
306 for (i
=0; i
<ntdb
->file
->num_lockrecs
; i
++) {
307 if (ntdb
->file
->lockrecs
[i
].off
== offset
) {
308 if (owner
&& ntdb
->file
->lockrecs
[i
].owner
!= owner
)
310 return &ntdb
->file
->lockrecs
[i
];
316 enum NTDB_ERROR
ntdb_lock_and_recover(struct ntdb_context
*ntdb
)
318 enum NTDB_ERROR ecode
;
320 if (!check_lock_pid(ntdb
, "ntdb_transaction_prepare_commit", true))
321 return NTDB_ERR_LOCK
;
323 ecode
= ntdb_allrecord_lock(ntdb
, F_WRLCK
, NTDB_LOCK_WAIT
|NTDB_LOCK_NOCHECK
,
325 if (ecode
!= NTDB_SUCCESS
) {
329 ecode
= ntdb_lock_open(ntdb
, F_WRLCK
, NTDB_LOCK_WAIT
|NTDB_LOCK_NOCHECK
);
330 if (ecode
!= NTDB_SUCCESS
) {
331 ntdb_allrecord_unlock(ntdb
, F_WRLCK
);
334 ecode
= ntdb_transaction_recover(ntdb
);
335 ntdb_unlock_open(ntdb
, F_WRLCK
);
336 ntdb_allrecord_unlock(ntdb
, F_WRLCK
);
341 /* lock an offset in the database. */
342 static enum NTDB_ERROR
ntdb_nest_lock(struct ntdb_context
*ntdb
,
343 ntdb_off_t offset
, int ltype
,
344 enum ntdb_lock_flags flags
)
346 struct ntdb_lock
*new_lck
;
347 enum NTDB_ERROR ecode
;
349 if (offset
> (NTDB_HASH_LOCK_START
+ NTDB_HASH_LOCK_RANGE
350 + ntdb
->file
->map_size
/ 8)) {
351 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
352 "ntdb_nest_lock: invalid offset %zu ltype=%d",
353 (size_t)offset
, ltype
);
356 if (ntdb
->flags
& NTDB_NOLOCK
)
359 if (!check_lock_pid(ntdb
, "ntdb_nest_lock", true)) {
360 return NTDB_ERR_LOCK
;
365 new_lck
= find_nestlock(ntdb
, offset
, NULL
);
367 if (new_lck
->owner
!= ntdb
) {
368 return owner_conflict(ntdb
, "ntdb_nest_lock");
371 if (new_lck
->ltype
== F_RDLCK
&& ltype
== F_WRLCK
) {
372 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
374 " offset %zu has read lock",
377 /* Just increment the struct, posix locks don't stack. */
383 if (ntdb
->file
->num_lockrecs
384 && offset
>= NTDB_HASH_LOCK_START
385 && offset
< NTDB_HASH_LOCK_START
+ NTDB_HASH_LOCK_RANGE
) {
386 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
387 "ntdb_nest_lock: already have a hash lock?");
390 if (ntdb
->file
->lockrecs
== NULL
) {
391 new_lck
= ntdb
->alloc_fn(ntdb
->file
, sizeof(*ntdb
->file
->lockrecs
),
394 new_lck
= (struct ntdb_lock
*)ntdb
->expand_fn(
395 ntdb
->file
->lockrecs
,
396 sizeof(*ntdb
->file
->lockrecs
)
397 * (ntdb
->file
->num_lockrecs
+1),
400 if (new_lck
== NULL
) {
401 return ntdb_logerr(ntdb
, NTDB_ERR_OOM
, NTDB_LOG_ERROR
,
403 " unable to allocate %zu lock struct",
404 ntdb
->file
->num_lockrecs
+ 1);
406 ntdb
->file
->lockrecs
= new_lck
;
408 /* Since fcntl locks don't nest, we do a lock for the first one,
409 and simply bump the count for future ones */
410 ecode
= ntdb_brlock(ntdb
, ltype
, offset
, 1, flags
);
411 if (ecode
!= NTDB_SUCCESS
) {
415 /* First time we grab a lock, perhaps someone died in commit? */
416 if (!(flags
& NTDB_LOCK_NOCHECK
)
417 && ntdb
->file
->num_lockrecs
== 0) {
418 ntdb_bool_err berr
= ntdb_needs_recovery(ntdb
);
420 ntdb_brunlock(ntdb
, ltype
, offset
, 1);
423 return NTDB_OFF_TO_ERR(berr
);
424 ecode
= ntdb_lock_and_recover(ntdb
);
425 if (ecode
== NTDB_SUCCESS
) {
426 ecode
= ntdb_brlock(ntdb
, ltype
, offset
, 1,
429 if (ecode
!= NTDB_SUCCESS
) {
435 ntdb
->file
->lockrecs
[ntdb
->file
->num_lockrecs
].owner
= ntdb
;
436 ntdb
->file
->lockrecs
[ntdb
->file
->num_lockrecs
].off
= offset
;
437 ntdb
->file
->lockrecs
[ntdb
->file
->num_lockrecs
].count
= 1;
438 ntdb
->file
->lockrecs
[ntdb
->file
->num_lockrecs
].ltype
= ltype
;
439 ntdb
->file
->num_lockrecs
++;
444 static enum NTDB_ERROR
ntdb_nest_unlock(struct ntdb_context
*ntdb
,
445 ntdb_off_t off
, int ltype
)
447 struct ntdb_lock
*lck
;
448 enum NTDB_ERROR ecode
;
450 if (ntdb
->flags
& NTDB_NOLOCK
)
453 lck
= find_nestlock(ntdb
, off
, ntdb
);
454 if ((lck
== NULL
) || (lck
->count
== 0)) {
455 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
456 "ntdb_nest_unlock: no lock for %zu",
460 if (lck
->count
> 1) {
466 * This lock has count==1 left, so we need to unlock it in the
467 * kernel. We don't bother with decrementing the in-memory array
468 * element, we're about to overwrite it with the last array element
471 ecode
= ntdb_brunlock(ntdb
, ltype
, off
, 1);
474 * Shrink the array by overwriting the element just unlocked with the
475 * last array element.
477 *lck
= ntdb
->file
->lockrecs
[--ntdb
->file
->num_lockrecs
];
483 get the transaction lock
485 enum NTDB_ERROR
ntdb_transaction_lock(struct ntdb_context
*ntdb
, int ltype
)
487 return ntdb_nest_lock(ntdb
, NTDB_TRANSACTION_LOCK
, ltype
, NTDB_LOCK_WAIT
);
491 release the transaction lock
493 void ntdb_transaction_unlock(struct ntdb_context
*ntdb
, int ltype
)
495 ntdb_nest_unlock(ntdb
, NTDB_TRANSACTION_LOCK
, ltype
);
498 /* We only need to lock individual bytes, but Linux merges consecutive locks
499 * so we lock in contiguous ranges. */
500 static enum NTDB_ERROR
ntdb_lock_gradual(struct ntdb_context
*ntdb
,
501 int ltype
, enum ntdb_lock_flags flags
,
502 ntdb_off_t off
, ntdb_off_t len
)
504 enum NTDB_ERROR ecode
;
505 enum ntdb_lock_flags nb_flags
= (flags
& ~NTDB_LOCK_WAIT
);
508 /* 0 would mean to end-of-file... */
510 /* Single hash. Just do blocking lock. */
511 return ntdb_brlock(ntdb
, ltype
, off
, len
, flags
);
514 /* First we try non-blocking. */
515 ecode
= ntdb_brlock(ntdb
, ltype
, off
, len
, nb_flags
);
516 if (ecode
!= NTDB_ERR_LOCK
) {
520 /* Try locking first half, then second. */
521 ecode
= ntdb_lock_gradual(ntdb
, ltype
, flags
, off
, len
/ 2);
522 if (ecode
!= NTDB_SUCCESS
)
525 ecode
= ntdb_lock_gradual(ntdb
, ltype
, flags
,
526 off
+ len
/ 2, len
- len
/ 2);
527 if (ecode
!= NTDB_SUCCESS
) {
528 ntdb_brunlock(ntdb
, ltype
, off
, len
/ 2);
533 /* lock/unlock entire database. It can only be upgradable if you have some
534 * other way of guaranteeing exclusivity (ie. transaction write lock). */
535 enum NTDB_ERROR
ntdb_allrecord_lock(struct ntdb_context
*ntdb
, int ltype
,
536 enum ntdb_lock_flags flags
, bool upgradable
)
538 enum NTDB_ERROR ecode
;
541 if (ntdb
->flags
& NTDB_NOLOCK
)
544 if (!check_lock_pid(ntdb
, "ntdb_allrecord_lock", true)) {
545 return NTDB_ERR_LOCK
;
548 if (ntdb
->file
->allrecord_lock
.count
) {
549 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
) {
550 return owner_conflict(ntdb
, "ntdb_allrecord_lock");
554 || ntdb
->file
->allrecord_lock
.ltype
== F_WRLCK
) {
555 ntdb
->file
->allrecord_lock
.count
++;
559 /* a global lock of a different type exists */
560 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
561 "ntdb_allrecord_lock: already have %s lock",
562 ntdb
->file
->allrecord_lock
.ltype
== F_RDLCK
566 if (ntdb_has_hash_locks(ntdb
)) {
567 /* can't combine global and chain locks */
568 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
569 "ntdb_allrecord_lock:"
570 " already have chain lock");
573 if (upgradable
&& ltype
!= F_RDLCK
) {
574 /* ntdb error: you can't upgrade a write lock! */
575 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
576 "ntdb_allrecord_lock:"
577 " can't upgrade a write lock");
582 /* Lock hashes, gradually. */
583 ecode
= ntdb_lock_gradual(ntdb
, ltype
, flags
, NTDB_HASH_LOCK_START
,
584 NTDB_HASH_LOCK_RANGE
);
585 if (ecode
!= NTDB_SUCCESS
)
588 /* Lock free tables: there to end of file. */
589 ecode
= ntdb_brlock(ntdb
, ltype
,
590 NTDB_HASH_LOCK_START
+ NTDB_HASH_LOCK_RANGE
,
592 if (ecode
!= NTDB_SUCCESS
) {
593 ntdb_brunlock(ntdb
, ltype
, NTDB_HASH_LOCK_START
,
594 NTDB_HASH_LOCK_RANGE
);
598 ntdb
->file
->allrecord_lock
.owner
= ntdb
;
599 ntdb
->file
->allrecord_lock
.count
= 1;
600 /* If it's upgradable, it's actually exclusive so we can treat
601 * it as a write lock. */
602 ntdb
->file
->allrecord_lock
.ltype
= upgradable
? F_WRLCK
: ltype
;
603 ntdb
->file
->allrecord_lock
.off
= upgradable
;
605 /* Now check for needing recovery. */
606 if (flags
& NTDB_LOCK_NOCHECK
)
609 berr
= ntdb_needs_recovery(ntdb
);
610 if (likely(berr
== false))
613 ntdb_allrecord_unlock(ntdb
, ltype
);
615 return NTDB_OFF_TO_ERR(berr
);
616 ecode
= ntdb_lock_and_recover(ntdb
);
617 if (ecode
!= NTDB_SUCCESS
) {
623 enum NTDB_ERROR
ntdb_lock_open(struct ntdb_context
*ntdb
,
624 int ltype
, enum ntdb_lock_flags flags
)
626 return ntdb_nest_lock(ntdb
, NTDB_OPEN_LOCK
, ltype
, flags
);
629 void ntdb_unlock_open(struct ntdb_context
*ntdb
, int ltype
)
631 ntdb_nest_unlock(ntdb
, NTDB_OPEN_LOCK
, ltype
);
634 bool ntdb_has_open_lock(struct ntdb_context
*ntdb
)
636 return !(ntdb
->flags
& NTDB_NOLOCK
)
637 && find_nestlock(ntdb
, NTDB_OPEN_LOCK
, ntdb
) != NULL
;
640 enum NTDB_ERROR
ntdb_lock_expand(struct ntdb_context
*ntdb
, int ltype
)
642 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
643 return ntdb_nest_lock(ntdb
, NTDB_EXPANSION_LOCK
, ltype
,
644 NTDB_LOCK_WAIT
| NTDB_LOCK_NOCHECK
);
647 void ntdb_unlock_expand(struct ntdb_context
*ntdb
, int ltype
)
649 ntdb_nest_unlock(ntdb
, NTDB_EXPANSION_LOCK
, ltype
);
652 /* unlock entire db */
653 void ntdb_allrecord_unlock(struct ntdb_context
*ntdb
, int ltype
)
655 if (ntdb
->flags
& NTDB_NOLOCK
)
658 if (ntdb
->file
->allrecord_lock
.count
== 0) {
659 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
660 "ntdb_allrecord_unlock: not locked!");
664 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
) {
665 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
666 "ntdb_allrecord_unlock: not locked by us!");
670 /* Upgradable locks are marked as write locks. */
671 if (ntdb
->file
->allrecord_lock
.ltype
!= ltype
672 && (!ntdb
->file
->allrecord_lock
.off
|| ltype
!= F_RDLCK
)) {
673 ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
674 "ntdb_allrecord_unlock: have %s lock",
675 ntdb
->file
->allrecord_lock
.ltype
== F_RDLCK
680 if (ntdb
->file
->allrecord_lock
.count
> 1) {
681 ntdb
->file
->allrecord_lock
.count
--;
685 ntdb
->file
->allrecord_lock
.count
= 0;
686 ntdb
->file
->allrecord_lock
.ltype
= 0;
688 ntdb_brunlock(ntdb
, ltype
, NTDB_HASH_LOCK_START
, 0);
691 bool ntdb_has_expansion_lock(struct ntdb_context
*ntdb
)
693 return find_nestlock(ntdb
, NTDB_EXPANSION_LOCK
, ntdb
) != NULL
;
696 bool ntdb_has_hash_locks(struct ntdb_context
*ntdb
)
700 for (i
=0; i
<ntdb
->file
->num_lockrecs
; i
++) {
701 if (ntdb
->file
->lockrecs
[i
].off
>= NTDB_HASH_LOCK_START
702 && ntdb
->file
->lockrecs
[i
].off
< (NTDB_HASH_LOCK_START
703 + NTDB_HASH_LOCK_RANGE
))
709 static bool ntdb_has_free_lock(struct ntdb_context
*ntdb
)
713 if (ntdb
->flags
& NTDB_NOLOCK
)
716 for (i
=0; i
<ntdb
->file
->num_lockrecs
; i
++) {
717 if (ntdb
->file
->lockrecs
[i
].off
718 > NTDB_HASH_LOCK_START
+ NTDB_HASH_LOCK_RANGE
)
724 enum NTDB_ERROR
ntdb_lock_hashes(struct ntdb_context
*ntdb
,
725 ntdb_off_t hash_lock
,
726 ntdb_len_t hash_range
,
727 int ltype
, enum ntdb_lock_flags waitflag
)
729 /* FIXME: Do this properly, using hlock_range */
730 unsigned l
= NTDB_HASH_LOCK_START
731 + (hash_lock
>> (64 - NTDB_HASH_LOCK_RANGE_BITS
));
733 /* a allrecord lock allows us to avoid per chain locks */
734 if (ntdb
->file
->allrecord_lock
.count
) {
735 if (!check_lock_pid(ntdb
, "ntdb_lock_hashes", true))
736 return NTDB_ERR_LOCK
;
738 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
)
739 return owner_conflict(ntdb
, "ntdb_lock_hashes");
740 if (ltype
== ntdb
->file
->allrecord_lock
.ltype
741 || ltype
== F_RDLCK
) {
745 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
747 " already have %s allrecordlock",
748 ntdb
->file
->allrecord_lock
.ltype
== F_RDLCK
752 if (ntdb_has_free_lock(ntdb
)) {
753 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
754 "ntdb_lock_hashes: already have free lock");
757 if (ntdb_has_expansion_lock(ntdb
)) {
758 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
760 " already have expansion lock");
763 return ntdb_nest_lock(ntdb
, l
, ltype
, waitflag
);
766 enum NTDB_ERROR
ntdb_unlock_hashes(struct ntdb_context
*ntdb
,
767 ntdb_off_t hash_lock
,
768 ntdb_len_t hash_range
, int ltype
)
770 unsigned l
= NTDB_HASH_LOCK_START
771 + (hash_lock
>> (64 - NTDB_HASH_LOCK_RANGE_BITS
));
773 if (ntdb
->flags
& NTDB_NOLOCK
)
776 /* a allrecord lock allows us to avoid per chain locks */
777 if (ntdb
->file
->allrecord_lock
.count
) {
778 if (ntdb
->file
->allrecord_lock
.ltype
== F_RDLCK
779 && ltype
== F_WRLCK
) {
780 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
781 "ntdb_unlock_hashes RO allrecord!");
783 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
) {
784 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_USE_ERROR
,
785 "ntdb_unlock_hashes:"
786 " not locked by us!");
791 return ntdb_nest_unlock(ntdb
, l
, ltype
);
794 /* Hash locks use NTDB_HASH_LOCK_START + the next 30 bits.
795 * Then we begin; bucket offsets are sizeof(ntdb_len_t) apart, so we divide.
796 * The result is that on 32 bit systems we don't use lock values > 2^31 on
797 * files that are less than 4GB.
799 static ntdb_off_t
free_lock_off(ntdb_off_t b_off
)
801 return NTDB_HASH_LOCK_START
+ NTDB_HASH_LOCK_RANGE
802 + b_off
/ sizeof(ntdb_off_t
);
805 enum NTDB_ERROR
ntdb_lock_free_bucket(struct ntdb_context
*ntdb
, ntdb_off_t b_off
,
806 enum ntdb_lock_flags waitflag
)
808 assert(b_off
>= sizeof(struct ntdb_header
));
810 if (ntdb
->flags
& NTDB_NOLOCK
)
813 /* a allrecord lock allows us to avoid per chain locks */
814 if (ntdb
->file
->allrecord_lock
.count
) {
815 if (!check_lock_pid(ntdb
, "ntdb_lock_free_bucket", true))
816 return NTDB_ERR_LOCK
;
818 if (ntdb
->file
->allrecord_lock
.owner
!= ntdb
) {
819 return owner_conflict(ntdb
, "ntdb_lock_free_bucket");
822 if (ntdb
->file
->allrecord_lock
.ltype
== F_WRLCK
)
824 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
825 "ntdb_lock_free_bucket with"
826 " read-only allrecordlock!");
830 if (ntdb_has_expansion_lock(ntdb
)) {
831 return ntdb_logerr(ntdb
, NTDB_ERR_LOCK
, NTDB_LOG_ERROR
,
832 "ntdb_lock_free_bucket:"
833 " already have expansion lock");
837 return ntdb_nest_lock(ntdb
, free_lock_off(b_off
), F_WRLCK
, waitflag
);
840 void ntdb_unlock_free_bucket(struct ntdb_context
*ntdb
, ntdb_off_t b_off
)
842 if (ntdb
->file
->allrecord_lock
.count
)
845 ntdb_nest_unlock(ntdb
, free_lock_off(b_off
), F_WRLCK
);
848 _PUBLIC_
enum NTDB_ERROR
ntdb_lockall(struct ntdb_context
*ntdb
)
850 return ntdb_allrecord_lock(ntdb
, F_WRLCK
, NTDB_LOCK_WAIT
, false);
853 _PUBLIC_
void ntdb_unlockall(struct ntdb_context
*ntdb
)
855 ntdb_allrecord_unlock(ntdb
, F_WRLCK
);
858 _PUBLIC_
enum NTDB_ERROR
ntdb_lockall_read(struct ntdb_context
*ntdb
)
860 return ntdb_allrecord_lock(ntdb
, F_RDLCK
, NTDB_LOCK_WAIT
, false);
863 _PUBLIC_
void ntdb_unlockall_read(struct ntdb_context
*ntdb
)
865 ntdb_allrecord_unlock(ntdb
, F_RDLCK
);
868 void ntdb_lock_cleanup(struct ntdb_context
*ntdb
)
872 /* We don't want to warn: they're allowed to close ntdb after fork. */
873 if (!check_lock_pid(ntdb
, "ntdb_close", false))
876 while (ntdb
->file
->allrecord_lock
.count
877 && ntdb
->file
->allrecord_lock
.owner
== ntdb
) {
878 ntdb_allrecord_unlock(ntdb
, ntdb
->file
->allrecord_lock
.ltype
);
881 for (i
=0; i
<ntdb
->file
->num_lockrecs
; i
++) {
882 if (ntdb
->file
->lockrecs
[i
].owner
== ntdb
) {
883 ntdb_nest_unlock(ntdb
,
884 ntdb
->file
->lockrecs
[i
].off
,
885 ntdb
->file
->lockrecs
[i
].ltype
);