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 2 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, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "tdb_private.h"
31 /* a byte range locking function - return 0 on success
32 this functions locks/unlocks 1 byte at the specified offset.
34 On error, errno is also set so that errors are passed back properly
37 note that a len of zero means lock to end of file
39 int tdb_brlock_len(struct tdb_context *tdb, tdb_off_t offset,
40 int rw_type, int lck_type, int probe, size_t len)
45 if (tdb->flags & TDB_NOLOCK) {
49 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
50 tdb->ecode = TDB_ERR_RDONLY;
55 fl.l_whence = SEEK_SET;
61 ret = fcntl(tdb->fd,lck_type,&fl);
62 } while (ret == -1 && errno == EINTR);
65 /* Generic lock error. errno set by fcntl.
66 * EAGAIN is an expected return from non-blocking
68 if (!probe && lck_type != F_SETLK) {
69 /* Ensure error code is set for log fun to examine. */
70 tdb->ecode = TDB_ERR_LOCK;
71 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
72 tdb->fd, offset, rw_type, lck_type, len));
74 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
81 upgrade a read lock to a write lock. This needs to be handled in a
82 special way as some OSes (such as solaris) have too conservative
83 deadlock detection and claim a deadlock when progress can be
84 made. For those OSes we may loop for a while.
86 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
91 if (tdb_brlock_len(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
94 if (errno != EDEADLK) {
97 /* sleep for as short a time as we can - more portable than usleep() */
100 select(0, NULL, NULL, NULL, &tv);
102 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
107 /* a byte range locking function - return 0 on success
108 this functions locks/unlocks 1 byte at the specified offset.
110 On error, errno is also set so that errors are passed back properly
111 through tdb_open(). */
112 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
113 int rw_type, int lck_type, int probe)
115 return tdb_brlock_len(tdb, offset, rw_type, lck_type, probe, 1);
118 /* lock a list in the database. list -1 is the alloc list */
119 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
121 if (list < -1 || list >= (int)tdb->header.hash_size) {
122 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
126 if (tdb->flags & TDB_NOLOCK)
129 /* Since fcntl locks don't nest, we do a lock for the first one,
130 and simply bump the count for future ones */
131 if (tdb->locked[list+1].count == 0) {
132 if (tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
133 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d ltype=%d (%s)\n",
134 list, ltype, strerror(errno)));
137 tdb->locked[list+1].ltype = ltype;
140 tdb->locked[list+1].count++;
144 /* unlock the database: returns void because it's too late for errors. */
145 /* changed to return int it may be interesting to know there
146 has been an error --simo */
147 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
151 if (tdb->flags & TDB_NOLOCK)
155 if (list < -1 || list >= (int)tdb->header.hash_size) {
156 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
160 if (tdb->locked[list+1].count==0) {
161 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
165 if (tdb->locked[list+1].count == 1) {
166 /* Down to last nested lock: unlock underneath */
167 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
172 tdb->locked[list+1].count--;
175 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
181 /* lock/unlock entire database */
182 int tdb_lockall(struct tdb_context *tdb)
186 /* There are no locks on read-only dbs */
187 if (tdb->read_only || tdb->traverse_read)
188 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
189 for (i = 0; i < tdb->header.hash_size; i++)
190 if (tdb_lock(tdb, i, F_WRLCK))
193 /* If error, release locks we have... */
194 if (i < tdb->header.hash_size) {
197 for ( j = 0; j < i; j++)
198 tdb_unlock(tdb, j, F_WRLCK);
199 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
204 void tdb_unlockall(struct tdb_context *tdb)
207 for (i=0; i < tdb->header.hash_size; i++)
208 tdb_unlock(tdb, i, F_WRLCK);
211 /* lock/unlock one hash chain. This is meant to be used to reduce
212 contention - it cannot guarantee how many records will be locked */
213 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
215 return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
218 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
220 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
223 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
225 return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
228 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
230 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
235 /* record lock stops delete underneath */
236 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
238 return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
242 Write locks override our own fcntl readlocks, so check it here.
243 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
244 an error to fail to get the lock here.
246 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
248 struct tdb_traverse_lock *i;
249 for (i = &tdb->travlocks; i; i = i->next)
252 return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
256 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
257 an error to fail to get the lock here.
259 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
261 return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
264 /* fcntl locks don't stack: avoid unlocking someone else's */
265 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
267 struct tdb_traverse_lock *i;
272 for (i = &tdb->travlocks; i; i = i->next)
275 return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);