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 "tdb1_private.h"
30 /* list -1 is the alloc list, otherwise a hash chain. */
31 static tdb1_off_t lock_offset(int list)
33 return TDB1_FREELIST_TOP + 4*list;
36 /* a byte range locking function - return 0 on success
37 this functions locks/unlocks 1 byte at the specified offset.
39 On error, errno is also set so that errors are passed back properly
42 note that a len of zero means lock to end of file
44 int tdb1_brlock(struct tdb_context *tdb,
45 int rw_type, tdb1_off_t offset, size_t len,
46 enum tdb_lock_flags flags)
48 enum TDB_ERROR ecode = tdb_brlock(tdb, rw_type, offset, len, flags
50 if (ecode == TDB_SUCCESS)
52 tdb->last_error = ecode;
56 int tdb1_brunlock(struct tdb_context *tdb,
57 int rw_type, tdb1_off_t offset, size_t len)
59 enum TDB_ERROR ecode = tdb_brunlock(tdb, rw_type, offset, len);
60 if (ecode == TDB_SUCCESS)
62 tdb->last_error = ecode;
66 int tdb1_allrecord_upgrade(struct tdb_context *tdb)
68 enum TDB_ERROR ecode = tdb_allrecord_upgrade(tdb, TDB1_FREELIST_TOP);
69 if (ecode == TDB_SUCCESS)
71 tdb->last_error = ecode;
75 static struct tdb_lock *tdb1_find_nestlock(struct tdb_context *tdb,
80 for (i=0; i<tdb->file->num_lockrecs; i++) {
81 if (tdb->file->lockrecs[i].off == offset) {
82 return &tdb->file->lockrecs[i];
88 /* lock an offset in the database. */
89 int tdb1_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
90 enum tdb_lock_flags flags)
94 if (offset >= lock_offset(tdb->tdb1.header.hash_size)) {
95 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
96 "tdb1_lock: invalid offset %u for"
102 ecode = tdb_nest_lock(tdb, offset, ltype, flags | TDB_LOCK_NOCHECK);
103 if (unlikely(ecode != TDB_SUCCESS)) {
104 tdb->last_error = ecode;
110 static int tdb1_lock_and_recover(struct tdb_context *tdb)
114 /* We need to match locking order in transaction commit. */
115 if (tdb1_brlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0,
116 TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
120 if (tdb1_brlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1,
121 TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
122 tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
126 ret = tdb1_transaction_recover(tdb);
128 tdb1_brunlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1);
129 tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
134 static bool have_data_locks(const struct tdb_context *tdb)
138 for (i = 0; i < tdb->file->num_lockrecs; i++) {
139 if (tdb->file->lockrecs[i].off >= lock_offset(-1))
145 static int tdb1_lock_list(struct tdb_context *tdb, int list, int ltype,
146 enum tdb_lock_flags waitflag)
151 /* a allrecord lock allows us to avoid per chain locks */
152 if (tdb->file->allrecord_lock.count &&
153 (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
157 if (tdb->file->allrecord_lock.count) {
158 tdb->last_error = TDB_ERR_LOCK;
161 /* Only check when we grab first data lock. */
162 check = !have_data_locks(tdb);
163 ret = tdb1_nest_lock(tdb, lock_offset(list), ltype, waitflag);
165 if (ret == 0 && check && tdb1_needs_recovery(tdb)) {
166 tdb1_nest_unlock(tdb, lock_offset(list), ltype);
168 if (tdb1_lock_and_recover(tdb) == -1) {
171 return tdb1_lock_list(tdb, list, ltype, waitflag);
177 /* lock a list in the database. list -1 is the alloc list */
178 int tdb1_lock(struct tdb_context *tdb, int list, int ltype)
182 ret = tdb1_lock_list(tdb, list, ltype, TDB_LOCK_WAIT);
183 /* Don't log for EAGAIN and EINTR: they could have overridden lock fns */
184 if (ret && errno != EAGAIN && errno != EINTR) {
185 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
186 "tdb1_lock failed on list %d "
187 "ltype=%d (%s)", list, ltype, strerror(errno));
192 int tdb1_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype)
194 enum TDB_ERROR ecode;
197 if (offset >= lock_offset(tdb->tdb1.header.hash_size)) {
198 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
199 "tdb1_unlock: offset %u invalid (%d)",
200 offset, tdb->tdb1.header.hash_size);
204 ecode = tdb_nest_unlock(tdb, offset, ltype);
205 if (unlikely(ecode != TDB_SUCCESS)) {
206 tdb->last_error = ecode;
212 int tdb1_unlock(struct tdb_context *tdb, int list, int ltype)
214 /* a global lock allows us to avoid per chain locks */
215 if (tdb->file->allrecord_lock.count &&
216 (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
220 if (tdb->file->allrecord_lock.count) {
221 tdb->last_error = TDB_ERR_LOCK;
225 return tdb1_nest_unlock(tdb, lock_offset(list), ltype);
229 get the transaction lock
231 int tdb1_transaction_lock(struct tdb_context *tdb, int ltype,
232 enum tdb_lock_flags lockflags)
234 return tdb1_nest_lock(tdb, TDB1_TRANSACTION_LOCK, ltype, lockflags);
238 release the transaction lock
240 int tdb1_transaction_unlock(struct tdb_context *tdb, int ltype)
242 return tdb1_nest_unlock(tdb, TDB1_TRANSACTION_LOCK, ltype);
245 /* lock/unlock entire database. It can only be upgradable if you have some
246 * other way of guaranteeing exclusivity (ie. transaction write lock).
247 * We do the locking gradually to avoid being starved by smaller locks. */
248 int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
249 enum tdb_lock_flags flags, bool upgradable)
251 enum TDB_ERROR ecode;
253 /* tdb_lock_gradual() doesn't know about tdb->tdb1.traverse_read. */
254 if (tdb->tdb1.traverse_read && !(tdb->flags & TDB_NOLOCK)) {
255 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
257 "tdb1_allrecord_lock during"
258 " tdb1_read_traverse");
262 if (tdb->file->allrecord_lock.count
263 && tdb->file->allrecord_lock.ltype == ltype) {
264 tdb->file->allrecord_lock.count++;
268 if (tdb1_have_extra_locks(tdb)) {
269 /* can't combine global and chain locks */
270 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
272 "tdb1_allrecord_lock holding"
277 if (upgradable && ltype != F_RDLCK) {
278 /* tdb error: you can't upgrade a write lock! */
279 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
281 "tdb1_allrecord_lock cannot"
282 " have upgradable write lock");
286 /* We cover two kinds of locks:
287 * 1) Normal chain locks. Taken for almost all operations.
288 * 3) Individual records locks. Taken after normal or free
291 * It is (1) which cause the starvation problem, so we're only
292 * gradual for that. */
293 ecode = tdb_lock_gradual(tdb, ltype, flags | TDB_LOCK_NOCHECK,
294 TDB1_FREELIST_TOP, tdb->tdb1.header.hash_size * 4);
295 if (ecode != TDB_SUCCESS) {
296 tdb->last_error = ecode;
300 /* Grab individual record locks. */
301 if (tdb1_brlock(tdb, ltype, lock_offset(tdb->tdb1.header.hash_size), 0,
303 tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP,
304 tdb->tdb1.header.hash_size * 4);
308 /* FIXME: Temporary cast. */
309 tdb->file->allrecord_lock.owner = (void *)(struct tdb1_context *)tdb;
310 tdb->file->allrecord_lock.count = 1;
311 /* If it's upgradable, it's actually exclusive so we can treat
312 * it as a write lock. */
313 tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
314 tdb->file->allrecord_lock.off = upgradable;
316 if (tdb1_needs_recovery(tdb)) {
317 tdb1_allrecord_unlock(tdb, ltype);
318 if (tdb1_lock_and_recover(tdb) == -1) {
321 return tdb1_allrecord_lock(tdb, ltype, flags, upgradable);
329 /* unlock entire db */
330 int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype)
332 /* Don't try this during r/o traversal! */
333 if (tdb->tdb1.traverse_read) {
334 tdb->last_error = TDB_ERR_LOCK;
338 if (tdb->file->allrecord_lock.count == 0) {
339 tdb->last_error = TDB_ERR_LOCK;
343 /* Upgradable locks are marked as write locks. */
344 if (tdb->file->allrecord_lock.ltype != ltype
345 && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
346 tdb->last_error = TDB_ERR_LOCK;
350 if (tdb->file->allrecord_lock.count > 1) {
351 tdb->file->allrecord_lock.count--;
355 if (tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP, 0)) {
356 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
357 "tdb1_unlockall failed (%s)", strerror(errno));
361 tdb->file->allrecord_lock.count = 0;
362 tdb->file->allrecord_lock.ltype = 0;
367 /* lock entire database with write lock */
368 int tdb1_lockall(struct tdb_context *tdb)
370 return tdb1_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
373 /* unlock entire database with write lock */
374 int tdb1_unlockall(struct tdb_context *tdb)
376 return tdb1_allrecord_unlock(tdb, F_WRLCK);
379 /* lock entire database with read lock */
380 int tdb1_lockall_read(struct tdb_context *tdb)
382 return tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
385 /* unlock entire database with read lock */
386 int tdb1_unlockall_read(struct tdb_context *tdb)
388 return tdb1_allrecord_unlock(tdb, F_RDLCK);
391 /* lock/unlock one hash chain. This is meant to be used to reduce
392 contention - it cannot guarantee how many records will be locked */
393 int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key)
395 int ret = tdb1_lock(tdb,
396 TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
401 int tdb1_chainunlock(struct tdb_context *tdb, TDB_DATA key)
403 return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
407 int tdb1_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
410 ret = tdb1_lock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
415 int tdb1_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
417 return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
421 /* record lock stops delete underneath */
422 int tdb1_lock_record(struct tdb_context *tdb, tdb1_off_t off)
424 if (tdb->file->allrecord_lock.count) {
427 return off ? tdb1_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
431 Write locks override our own fcntl readlocks, so check it here.
432 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
433 an error to fail to get the lock here.
435 int tdb1_write_lock_record(struct tdb_context *tdb, tdb1_off_t off)
437 struct tdb1_traverse_lock *i;
438 for (i = &tdb->tdb1.travlocks; i; i = i->next)
441 if (tdb->file->allrecord_lock.count) {
442 if (tdb->file->allrecord_lock.ltype == F_WRLCK) {
447 return tdb1_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
450 int tdb1_write_unlock_record(struct tdb_context *tdb, tdb1_off_t off)
452 if (tdb->file->allrecord_lock.count) {
455 return tdb1_brunlock(tdb, F_WRLCK, off, 1);
458 /* fcntl locks don't stack: avoid unlocking someone else's */
459 int tdb1_unlock_record(struct tdb_context *tdb, tdb1_off_t off)
461 struct tdb1_traverse_lock *i;
464 if (tdb->file->allrecord_lock.count) {
470 for (i = &tdb->tdb1.travlocks; i; i = i->next)
473 return (count == 1 ? tdb1_brunlock(tdb, F_RDLCK, off, 1) : 0);
476 bool tdb1_have_extra_locks(struct tdb_context *tdb)
478 unsigned int extra = tdb->file->num_lockrecs;
480 /* A transaction holds the lock for all records. */
481 if (!tdb->tdb1.transaction && tdb->file->allrecord_lock.count) {
485 /* We always hold the active lock if CLEAR_IF_FIRST. */
486 if (tdb1_find_nestlock(tdb, TDB1_ACTIVE_LOCK)) {
490 /* In a transaction, we expect to hold the transaction lock */
491 if (tdb->tdb1.transaction
492 && tdb1_find_nestlock(tdb, TDB1_TRANSACTION_LOCK)) {
499 /* The transaction code uses this to remove all locks. */
500 void tdb1_release_transaction_locks(struct tdb_context *tdb)
502 unsigned int i, active = 0;
504 if (tdb->file->allrecord_lock.count != 0) {
505 tdb1_brunlock(tdb, tdb->file->allrecord_lock.ltype, TDB1_FREELIST_TOP, 0);
506 tdb->file->allrecord_lock.count = 0;
509 for (i=0;i<tdb->file->num_lockrecs;i++) {
510 struct tdb_lock *lck = &tdb->file->lockrecs[i];
512 /* Don't release the active lock! Copy it to first entry. */
513 if (lck->off == TDB1_ACTIVE_LOCK) {
514 tdb->file->lockrecs[active++] = *lck;
516 tdb1_brunlock(tdb, lck->ltype, lck->off, 1);
519 tdb->file->num_lockrecs = active;
520 if (tdb->file->num_lockrecs == 0) {
521 SAFE_FREE(tdb->file->lockrecs);