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;
43 add_stat(tdb, lock_lowlevel, 1);
45 return fcntl(tdb->fd, F_SETLKW, &fl);
47 add_stat(tdb, lock_nonblock, 1);
48 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 /* a byte range locking function - return 0 on success
123 this functions locks/unlocks 1 byte at the specified offset.
125 note that a len of zero means lock to end of file
127 static int tdb_brlock(struct tdb_context *tdb,
128 int rw_type, tdb_off_t offset, tdb_off_t len,
129 enum tdb_lock_flags flags)
133 if (tdb->flags & TDB_NOLOCK) {
137 if (rw_type == F_WRLCK && tdb->read_only) {
138 tdb->ecode = TDB_ERR_RDONLY;
142 /* A 32 bit system cannot open a 64-bit file, but it could have
143 * expanded since then: check here. */
144 if ((size_t)(offset + len) != offset + len) {
145 tdb->ecode = TDB_ERR_IO;
146 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
147 "tdb_brlock: lock on giant offset %llu\n",
148 (long long)(offset + len));
153 ret = fcntl_lock(tdb, rw_type, offset, len,
154 flags & TDB_LOCK_WAIT);
155 } while (ret == -1 && errno == EINTR);
158 tdb->ecode = TDB_ERR_LOCK;
159 /* Generic lock error. errno set by fcntl.
160 * EAGAIN is an expected return from non-blocking
162 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
163 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
164 "tdb_brlock failed (fd=%d) at"
165 " offset %llu rw_type=%d flags=%d len=%llu\n",
166 tdb->fd, (long long)offset, rw_type,
167 flags, (long long)len);
174 static int tdb_brunlock(struct tdb_context *tdb,
175 int rw_type, tdb_off_t offset, size_t len)
179 if (tdb->flags & TDB_NOLOCK) {
184 ret = fcntl_unlock(tdb, rw_type, offset, len);
185 } while (ret == -1 && errno == EINTR);
188 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
189 "tdb_brunlock failed (fd=%d) at offset %llu"
190 " rw_type=%d len=%llu\n",
191 tdb->fd, (long long)offset, rw_type, (long long)len);
197 upgrade a read lock to a write lock. This needs to be handled in a
198 special way as some OSes (such as solaris) have too conservative
199 deadlock detection and claim a deadlock when progress can be
200 made. For those OSes we may loop for a while.
202 int tdb_allrecord_upgrade(struct tdb_context *tdb)
206 if (tdb->allrecord_lock.count != 1) {
207 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
208 "tdb_allrecord_upgrade failed: count %u too high\n",
209 tdb->allrecord_lock.count);
213 if (tdb->allrecord_lock.off != 1) {
214 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
215 "tdb_allrecord_upgrade failed: already upgraded?\n");
221 if (tdb_brlock(tdb, F_WRLCK,
222 TDB_HASH_LOCK_START, 0,
223 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
224 tdb->allrecord_lock.ltype = F_WRLCK;
225 tdb->allrecord_lock.off = 0;
228 if (errno != EDEADLK) {
231 /* sleep for as short a time as we can - more portable than usleep() */
234 select(0, NULL, NULL, NULL, &tv);
236 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
237 "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 int tdb_lock_and_recover(struct tdb_context *tdb)
258 if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
263 if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
264 tdb_allrecord_unlock(tdb, F_WRLCK);
267 ret = tdb_transaction_recover(tdb);
269 tdb_unlock_open(tdb);
270 tdb_allrecord_unlock(tdb, F_WRLCK);
275 /* lock an offset in the database. */
276 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
277 enum tdb_lock_flags flags)
279 struct tdb_lock_type *new_lck;
281 if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
282 tdb->ecode = TDB_ERR_LOCK;
283 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
284 "tdb_nest_lock: invalid offset %llu ltype=%d\n",
285 (long long)offset, ltype);
289 if (tdb->flags & TDB_NOLOCK)
292 add_stat(tdb, locks, 1);
294 new_lck = find_nestlock(tdb, offset);
296 if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
297 tdb->ecode = TDB_ERR_LOCK;
298 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
299 "tdb_nest_lock: offset %llu has read lock\n",
303 /* Just increment the struct, posix locks don't stack. */
308 if (tdb->num_lockrecs
309 && offset >= TDB_HASH_LOCK_START
310 && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
311 tdb->ecode = TDB_ERR_LOCK;
312 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
313 "tdb_nest_lock: already have a hash lock?\n");
317 new_lck = (struct tdb_lock_type *)realloc(
319 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
320 if (new_lck == NULL) {
321 tdb->ecode = TDB_ERR_OOM;
322 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
323 "tdb_nest_lock: unable to allocate %llu lock struct",
324 (long long)(tdb->num_lockrecs + 1));
328 tdb->lockrecs = new_lck;
330 /* Since fcntl locks don't nest, we do a lock for the first one,
331 and simply bump the count for future ones */
332 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
336 /* First time we grab a lock, perhaps someone died in commit? */
337 if (!(flags & TDB_LOCK_NOCHECK)
338 && tdb->num_lockrecs == 0
339 && unlikely(tdb_needs_recovery(tdb))) {
340 tdb_brunlock(tdb, ltype, offset, 1);
342 if (tdb_lock_and_recover(tdb) == -1) {
346 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
351 tdb->lockrecs[tdb->num_lockrecs].off = offset;
352 tdb->lockrecs[tdb->num_lockrecs].count = 1;
353 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
359 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
362 struct tdb_lock_type *lck;
364 if (tdb->flags & TDB_NOLOCK)
367 lck = find_nestlock(tdb, off);
368 if ((lck == NULL) || (lck->count == 0)) {
369 tdb->ecode = TDB_ERR_LOCK;
370 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
371 "tdb_nest_unlock: no lock for %llu\n", (long long)off);
375 if (lck->count > 1) {
381 * This lock has count==1 left, so we need to unlock it in the
382 * kernel. We don't bother with decrementing the in-memory array
383 * element, we're about to overwrite it with the last array element
386 ret = tdb_brunlock(tdb, ltype, off, 1);
389 * Shrink the array by overwriting the element just unlocked with the
390 * last array element.
392 *lck = tdb->lockrecs[--tdb->num_lockrecs];
398 get the transaction lock
400 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
402 return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
406 release the transaction lock
408 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
410 return tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
413 /* We only need to lock individual bytes, but Linux merges consecutive locks
414 * so we lock in contiguous ranges. */
415 static int tdb_lock_gradual(struct tdb_context *tdb,
416 int ltype, enum tdb_lock_flags flags,
417 tdb_off_t off, tdb_off_t len)
420 enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
423 /* 0 would mean to end-of-file... */
425 /* Single hash. Just do blocking lock. */
426 return tdb_brlock(tdb, ltype, off, len, flags);
429 /* First we try non-blocking. */
430 ret = tdb_brlock(tdb, ltype, off, len, nb_flags);
435 /* Try locking first half, then second. */
436 ret = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
440 ret = tdb_lock_gradual(tdb, ltype, flags,
441 off + len / 2, len - len / 2);
443 tdb_brunlock(tdb, ltype, off, len / 2);
449 /* lock/unlock entire database. It can only be upgradable if you have some
450 * other way of guaranteeing exclusivity (ie. transaction write lock). */
451 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
452 enum tdb_lock_flags flags, bool upgradable)
454 /* FIXME: There are no locks on read-only dbs */
455 if (tdb->read_only) {
456 tdb->ecode = TDB_ERR_LOCK;
457 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
458 "tdb_allrecord_lock: read-only\n");
462 if (tdb->allrecord_lock.count
463 && (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) {
464 tdb->allrecord_lock.count++;
468 if (tdb->allrecord_lock.count) {
469 /* a global lock of a different type exists */
470 tdb->ecode = TDB_ERR_LOCK;
471 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
472 "tdb_allrecord_lock: already have %s lock\n",
473 tdb->allrecord_lock.ltype == F_RDLCK
478 if (tdb_has_hash_locks(tdb)) {
479 /* can't combine global and chain locks */
480 tdb->ecode = TDB_ERR_LOCK;
481 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
482 "tdb_allrecord_lock: already have chain lock\n");
486 if (upgradable && ltype != F_RDLCK) {
487 /* tdb error: you can't upgrade a write lock! */
488 tdb->ecode = TDB_ERR_LOCK;
489 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
490 "tdb_allrecord_lock: can't upgrade a write lock\n");
494 add_stat(tdb, locks, 1);
496 /* Lock hashes, gradually. */
497 if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
498 TDB_HASH_LOCK_RANGE)) {
499 if (!(flags & TDB_LOCK_PROBE)) {
500 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
501 "tdb_allrecord_lock hashes failed (%s)\n",
507 /* Lock free lists: there to end of file. */
508 if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
510 if (!(flags & TDB_LOCK_PROBE)) {
511 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
512 "tdb_allrecord_lock freelist failed (%s)\n",
515 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
516 TDB_HASH_LOCK_RANGE);
520 tdb->allrecord_lock.count = 1;
521 /* If it's upgradable, it's actually exclusive so we can treat
522 * it as a write lock. */
523 tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
524 tdb->allrecord_lock.off = upgradable;
526 /* Now check for needing recovery. */
527 if (!(flags & TDB_LOCK_NOCHECK) && unlikely(tdb_needs_recovery(tdb))) {
528 tdb_allrecord_unlock(tdb, ltype);
529 if (tdb_lock_and_recover(tdb) == -1) {
538 int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
540 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
543 void tdb_unlock_open(struct tdb_context *tdb)
545 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
548 bool tdb_has_open_lock(struct tdb_context *tdb)
550 return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
553 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
555 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
556 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
557 TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
560 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
562 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
565 /* unlock entire db */
566 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
568 /* FIXME: There are no locks on read-only dbs */
569 if (tdb->read_only) {
570 tdb->ecode = TDB_ERR_LOCK;
571 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
572 "tdb_allrecord_unlock: read-only\n");
576 if (tdb->allrecord_lock.count == 0) {
577 tdb->ecode = TDB_ERR_LOCK;
578 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
579 "tdb_allrecord_unlock: not locked!\n");
583 /* Upgradable locks are marked as write locks. */
584 if (tdb->allrecord_lock.ltype != ltype
585 && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
586 tdb->ecode = TDB_ERR_LOCK;
587 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
588 "tdb_allrecord_unlock: have %s lock\n",
589 tdb->allrecord_lock.ltype == F_RDLCK
594 if (tdb->allrecord_lock.count > 1) {
595 tdb->allrecord_lock.count--;
599 tdb->allrecord_lock.count = 0;
600 tdb->allrecord_lock.ltype = 0;
602 return tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
605 bool tdb_has_expansion_lock(struct tdb_context *tdb)
607 return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
610 bool tdb_has_hash_locks(struct tdb_context *tdb)
614 for (i=0; i<tdb->num_lockrecs; i++) {
615 if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
616 && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
617 + TDB_HASH_LOCK_RANGE))
623 static bool tdb_has_free_lock(struct tdb_context *tdb)
627 for (i=0; i<tdb->num_lockrecs; i++) {
628 if (tdb->lockrecs[i].off
629 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
635 int tdb_lock_hashes(struct tdb_context *tdb,
637 tdb_len_t hash_range,
638 int ltype, enum tdb_lock_flags waitflag)
640 /* FIXME: Do this properly, using hlock_range */
641 unsigned lock = TDB_HASH_LOCK_START
642 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
644 /* a allrecord lock allows us to avoid per chain locks */
645 if (tdb->allrecord_lock.count &&
646 (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
650 if (tdb->allrecord_lock.count) {
651 tdb->ecode = TDB_ERR_LOCK;
652 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
653 "tdb_lock_hashes: have %s allrecordlock\n",
654 tdb->allrecord_lock.ltype == F_RDLCK
659 if (tdb_has_free_lock(tdb)) {
660 tdb->ecode = TDB_ERR_LOCK;
661 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
662 "tdb_lock_hashes: have free lock already\n");
666 if (tdb_has_expansion_lock(tdb)) {
667 tdb->ecode = TDB_ERR_LOCK;
668 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
669 "tdb_lock_hashes: have expansion lock already\n");
673 return tdb_nest_lock(tdb, lock, ltype, waitflag);
676 int tdb_unlock_hashes(struct tdb_context *tdb,
678 tdb_len_t hash_range, int ltype)
680 unsigned lock = TDB_HASH_LOCK_START
681 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
683 /* a allrecord lock allows us to avoid per chain locks */
684 if (tdb->allrecord_lock.count) {
685 if (tdb->allrecord_lock.ltype == F_RDLCK
686 && ltype == F_WRLCK) {
687 tdb->ecode = TDB_ERR_LOCK;
688 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
689 "tdb_unlock_hashes RO allrecord!\n");
695 return tdb_nest_unlock(tdb, lock, ltype);
698 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
699 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
700 * The result is that on 32 bit systems we don't use lock values > 2^31 on
701 * files that are less than 4GB.
703 static tdb_off_t free_lock_off(tdb_off_t b_off)
705 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
706 + b_off / sizeof(tdb_off_t);
709 int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
710 enum tdb_lock_flags waitflag)
712 assert(b_off >= sizeof(struct tdb_header));
714 /* a allrecord lock allows us to avoid per chain locks */
715 if (tdb->allrecord_lock.count) {
716 if (tdb->allrecord_lock.ltype == F_WRLCK)
718 tdb->ecode = TDB_ERR_LOCK;
719 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
720 "tdb_lock_free_bucket with RO allrecordlock!\n");
725 if (tdb_has_expansion_lock(tdb)) {
726 tdb->ecode = TDB_ERR_LOCK;
727 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
728 "tdb_lock_free_bucket: have expansion lock already\n");
733 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
736 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
738 if (tdb->allrecord_lock.count)
741 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
744 void tdb_lock_init(struct tdb_context *tdb)
746 tdb->num_lockrecs = 0;
747 tdb->lockrecs = NULL;
748 tdb->allrecord_lock.count = 0;