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->file->fd, F_SETLKW, &fl);
47 add_stat(tdb, lock_nonblock, 1);
48 return fcntl(tdb->file->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!",
114 fl.l_whence = SEEK_SET;
119 return fcntl(tdb->file->fd, F_SETLKW, &fl);
122 /* a byte range locking function - return 0 on success
123 this functions locks len bytes at the specified offset.
125 note that a len of zero means lock to end of file
127 static enum TDB_ERROR 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 return tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
139 "Write lock attempted on read-only database");
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 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
146 "tdb_brlock: lock on giant offset %llu",
147 (long long)(offset + len));
151 ret = fcntl_lock(tdb, rw_type, offset, len,
152 flags & TDB_LOCK_WAIT);
153 } while (ret == -1 && errno == EINTR);
156 /* Generic lock error. errno set by fcntl.
157 * EAGAIN is an expected return from non-blocking
159 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
160 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
161 "tdb_brlock failed (fd=%d) at"
162 " offset %zu rw_type=%d flags=%d len=%zu:"
164 tdb->file->fd, (size_t)offset, rw_type,
165 flags, (size_t)len, strerror(errno));
172 static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
173 int rw_type, tdb_off_t offset, size_t len)
177 if (tdb->flags & TDB_NOLOCK) {
182 ret = fcntl_unlock(tdb, rw_type, offset, len);
183 } while (ret == -1 && errno == EINTR);
186 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
187 "tdb_brunlock failed (fd=%d) at offset %zu"
188 " rw_type=%d len=%zu",
189 tdb->file->fd, (size_t)offset, rw_type,
196 upgrade a read lock to a write lock. This needs to be handled in a
197 special way as some OSes (such as solaris) have too conservative
198 deadlock detection and claim a deadlock when progress can be
199 made. For those OSes we may loop for a while.
201 enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
205 if (tdb->file->allrecord_lock.count != 1) {
206 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
207 "tdb_allrecord_upgrade failed:"
208 " count %u too high",
209 tdb->file->allrecord_lock.count);
212 if (tdb->file->allrecord_lock.off != 1) {
213 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
214 "tdb_allrecord_upgrade failed:"
215 " already upgraded?");
220 if (tdb_brlock(tdb, F_WRLCK,
221 TDB_HASH_LOCK_START, 0,
222 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
223 tdb->file->allrecord_lock.ltype = F_WRLCK;
224 tdb->file->allrecord_lock.off = 0;
227 if (errno != EDEADLK) {
230 /* sleep for as short a time as we can - more portable than usleep() */
233 select(0, NULL, NULL, NULL, &tv);
235 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
236 "tdb_allrecord_upgrade failed");
239 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
244 for (i=0; i<tdb->file->num_lockrecs; i++) {
245 if (tdb->file->lockrecs[i].off == offset) {
246 return &tdb->file->lockrecs[i];
252 enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb)
254 enum TDB_ERROR ecode;
256 ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
258 if (ecode != TDB_SUCCESS) {
262 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
263 if (ecode != TDB_SUCCESS) {
264 tdb_allrecord_unlock(tdb, F_WRLCK);
267 ecode = tdb_transaction_recover(tdb);
268 tdb_unlock_open(tdb);
269 tdb_allrecord_unlock(tdb, F_WRLCK);
274 /* lock an offset in the database. */
275 static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
276 tdb_off_t offset, int ltype,
277 enum tdb_lock_flags flags)
279 struct tdb_lock_type *new_lck;
280 enum TDB_ERROR ecode;
282 if (offset > (TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
283 + tdb->file->map_size / 8)) {
284 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
285 "tdb_nest_lock: invalid offset %zu ltype=%d",
286 (size_t)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 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
299 " offset %zu has read lock",
302 /* Just increment the struct, posix locks don't stack. */
307 if (tdb->file->num_lockrecs
308 && offset >= TDB_HASH_LOCK_START
309 && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
310 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
311 "tdb_nest_lock: already have a hash lock?");
314 new_lck = (struct tdb_lock_type *)realloc(
316 sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
317 if (new_lck == NULL) {
318 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
320 " unable to allocate %zu lock struct",
321 tdb->file->num_lockrecs + 1);
323 tdb->file->lockrecs = new_lck;
325 /* Since fcntl locks don't nest, we do a lock for the first one,
326 and simply bump the count for future ones */
327 ecode = tdb_brlock(tdb, ltype, offset, 1, flags);
328 if (ecode != TDB_SUCCESS) {
332 /* First time we grab a lock, perhaps someone died in commit? */
333 if (!(flags & TDB_LOCK_NOCHECK)
334 && tdb->file->num_lockrecs == 0) {
335 tdb_bool_err berr = tdb_needs_recovery(tdb);
337 tdb_brunlock(tdb, ltype, offset, 1);
341 ecode = tdb_lock_and_recover(tdb);
342 if (ecode == TDB_SUCCESS) {
343 ecode = tdb_brlock(tdb, ltype, offset, 1,
346 if (ecode != TDB_SUCCESS) {
352 tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
353 tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
354 tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
355 tdb->file->num_lockrecs++;
360 static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
361 tdb_off_t off, int ltype)
363 struct tdb_lock_type *lck;
364 enum TDB_ERROR ecode;
366 if (tdb->flags & TDB_NOLOCK)
369 lck = find_nestlock(tdb, off);
370 if ((lck == NULL) || (lck->count == 0)) {
371 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
372 "tdb_nest_unlock: no lock for %zu",
376 if (lck->count > 1) {
382 * This lock has count==1 left, so we need to unlock it in the
383 * kernel. We don't bother with decrementing the in-memory array
384 * element, we're about to overwrite it with the last array element
387 ecode = tdb_brunlock(tdb, ltype, off, 1);
390 * Shrink the array by overwriting the element just unlocked with the
391 * last array element.
393 *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
399 get the transaction lock
401 enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype)
403 return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
407 release the transaction lock
409 void tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
411 tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
414 /* We only need to lock individual bytes, but Linux merges consecutive locks
415 * so we lock in contiguous ranges. */
416 static enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
417 int ltype, enum tdb_lock_flags flags,
418 tdb_off_t off, tdb_off_t len)
420 enum TDB_ERROR ecode;
421 enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
424 /* 0 would mean to end-of-file... */
426 /* Single hash. Just do blocking lock. */
427 return tdb_brlock(tdb, ltype, off, len, flags);
430 /* First we try non-blocking. */
431 if (tdb_brlock(tdb, ltype, off, len, nb_flags) == TDB_SUCCESS) {
435 /* Try locking first half, then second. */
436 ecode = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
437 if (ecode != TDB_SUCCESS)
440 ecode = tdb_lock_gradual(tdb, ltype, flags,
441 off + len / 2, len - len / 2);
442 if (ecode != TDB_SUCCESS) {
443 tdb_brunlock(tdb, ltype, off, len / 2);
448 /* lock/unlock entire database. It can only be upgradable if you have some
449 * other way of guaranteeing exclusivity (ie. transaction write lock). */
450 enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
451 enum tdb_lock_flags flags, bool upgradable)
453 enum TDB_ERROR ecode;
456 if (tdb->file->allrecord_lock.count
458 || tdb->file->allrecord_lock.ltype == F_WRLCK)) {
459 tdb->file->allrecord_lock.count++;
463 if (tdb->file->allrecord_lock.count) {
464 /* a global lock of a different type exists */
465 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
466 "tdb_allrecord_lock: already have %s lock",
467 tdb->file->allrecord_lock.ltype == F_RDLCK
471 if (tdb_has_hash_locks(tdb)) {
472 /* can't combine global and chain locks */
473 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
474 "tdb_allrecord_lock:"
475 " already have chain lock");
478 if (upgradable && ltype != F_RDLCK) {
479 /* tdb error: you can't upgrade a write lock! */
480 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
481 "tdb_allrecord_lock:"
482 " can't upgrade a write lock");
485 add_stat(tdb, locks, 1);
487 /* Lock hashes, gradually. */
488 ecode = tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
489 TDB_HASH_LOCK_RANGE);
490 if (ecode != TDB_SUCCESS) {
491 if (!(flags & TDB_LOCK_PROBE)) {
492 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
493 "tdb_allrecord_lock hashes failed");
498 /* Lock free tables: there to end of file. */
499 ecode = tdb_brlock(tdb, ltype,
500 TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
502 if (ecode != TDB_SUCCESS) {
503 if (!(flags & TDB_LOCK_PROBE)) {
504 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
505 "tdb_allrecord_lock freetables failed");
507 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
508 TDB_HASH_LOCK_RANGE);
512 tdb->file->allrecord_lock.count = 1;
513 /* If it's upgradable, it's actually exclusive so we can treat
514 * it as a write lock. */
515 tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
516 tdb->file->allrecord_lock.off = upgradable;
518 /* Now check for needing recovery. */
519 if (flags & TDB_LOCK_NOCHECK)
522 berr = tdb_needs_recovery(tdb);
523 if (likely(berr == false))
526 tdb_allrecord_unlock(tdb, ltype);
529 ecode = tdb_lock_and_recover(tdb);
530 if (ecode != TDB_SUCCESS) {
536 enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
538 return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
541 void tdb_unlock_open(struct tdb_context *tdb)
543 tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
546 bool tdb_has_open_lock(struct tdb_context *tdb)
548 return !(tdb->flags & TDB_NOLOCK)
549 && find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
552 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
554 /* Lock doesn't protect data, so don't check (we recurse if we do!) */
555 return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
556 TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
559 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
561 tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
564 /* unlock entire db */
565 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
567 if (tdb->file->allrecord_lock.count == 0) {
568 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
569 "tdb_allrecord_unlock: not locked!");
573 /* Upgradable locks are marked as write locks. */
574 if (tdb->file->allrecord_lock.ltype != ltype
575 && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
576 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
577 "tdb_allrecord_unlock: have %s lock",
578 tdb->file->allrecord_lock.ltype == F_RDLCK
583 if (tdb->file->allrecord_lock.count > 1) {
584 tdb->file->allrecord_lock.count--;
588 tdb->file->allrecord_lock.count = 0;
589 tdb->file->allrecord_lock.ltype = 0;
591 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
594 bool tdb_has_expansion_lock(struct tdb_context *tdb)
596 return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
599 bool tdb_has_hash_locks(struct tdb_context *tdb)
603 for (i=0; i<tdb->file->num_lockrecs; i++) {
604 if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
605 && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
606 + TDB_HASH_LOCK_RANGE))
612 static bool tdb_has_free_lock(struct tdb_context *tdb)
616 if (tdb->flags & TDB_NOLOCK)
619 for (i=0; i<tdb->file->num_lockrecs; i++) {
620 if (tdb->file->lockrecs[i].off
621 > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
627 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
629 tdb_len_t hash_range,
630 int ltype, enum tdb_lock_flags waitflag)
632 /* FIXME: Do this properly, using hlock_range */
633 unsigned lock = TDB_HASH_LOCK_START
634 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
636 /* a allrecord lock allows us to avoid per chain locks */
637 if (tdb->file->allrecord_lock.count &&
638 (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
642 if (tdb->file->allrecord_lock.count) {
643 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
645 " already have %s allrecordlock",
646 tdb->file->allrecord_lock.ltype == F_RDLCK
650 if (tdb_has_free_lock(tdb)) {
651 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
652 "tdb_lock_hashes: already have free lock");
655 if (tdb_has_expansion_lock(tdb)) {
656 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
658 " already have expansion lock");
661 return tdb_nest_lock(tdb, lock, ltype, waitflag);
664 enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
666 tdb_len_t hash_range, int ltype)
668 unsigned lock = TDB_HASH_LOCK_START
669 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
671 if (tdb->flags & TDB_NOLOCK)
674 /* a allrecord lock allows us to avoid per chain locks */
675 if (tdb->file->allrecord_lock.count) {
676 if (tdb->file->allrecord_lock.ltype == F_RDLCK
677 && ltype == F_WRLCK) {
678 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
679 "tdb_unlock_hashes RO allrecord!");
684 return tdb_nest_unlock(tdb, lock, ltype);
687 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
688 * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
689 * The result is that on 32 bit systems we don't use lock values > 2^31 on
690 * files that are less than 4GB.
692 static tdb_off_t free_lock_off(tdb_off_t b_off)
694 return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
695 + b_off / sizeof(tdb_off_t);
698 enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
699 enum tdb_lock_flags waitflag)
701 assert(b_off >= sizeof(struct tdb_header));
703 if (tdb->flags & TDB_NOLOCK)
706 /* a allrecord lock allows us to avoid per chain locks */
707 if (tdb->file->allrecord_lock.count) {
708 if (tdb->file->allrecord_lock.ltype == F_WRLCK)
710 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
711 "tdb_lock_free_bucket with"
712 " read-only allrecordlock!");
716 if (tdb_has_expansion_lock(tdb)) {
717 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
718 "tdb_lock_free_bucket:"
719 " already have expansion lock");
723 return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
726 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
728 if (tdb->file->allrecord_lock.count)
731 tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);