samba-3.5.8 for ARM
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.5.8 / lib / tdb / common / lock.c
blob0984e516ea14cd88530fa29d75fe05fbbf4bde39
1 /*
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
12 ** under the LGPL
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 "tdb_private.h"
30 #define TDB_MARK_LOCK 0x80000000
32 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
34 tdb->interrupt_sig_ptr = ptr;
37 /* a byte range locking function - return 0 on success
38 this functions locks/unlocks 1 byte at the specified offset.
40 On error, errno is also set so that errors are passed back properly
41 through tdb_open().
43 note that a len of zero means lock to end of file
45 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
46 int rw_type, int lck_type, int probe, size_t len)
48 struct flock fl;
49 int ret;
51 if (tdb->flags & TDB_NOLOCK) {
52 return 0;
55 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
56 tdb->ecode = TDB_ERR_RDONLY;
57 return -1;
60 fl.l_type = rw_type;
61 fl.l_whence = SEEK_SET;
62 fl.l_start = offset;
63 fl.l_len = len;
64 fl.l_pid = 0;
66 do {
67 ret = fcntl(tdb->fd,lck_type,&fl);
69 /* Check for a sigalarm break. */
70 if (ret == -1 && errno == EINTR &&
71 tdb->interrupt_sig_ptr &&
72 *tdb->interrupt_sig_ptr) {
73 break;
75 } while (ret == -1 && errno == EINTR);
77 if (ret == -1) {
78 tdb->ecode = TDB_ERR_LOCK;
79 /* Generic lock error. errno set by fcntl.
80 * EAGAIN is an expected return from non-blocking
81 * locks. */
82 if (!probe && lck_type != F_SETLK) {
83 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
84 tdb->fd, offset, rw_type, lck_type, (int)len));
86 return -1;
88 return 0;
93 upgrade a read lock to a write lock. This needs to be handled in a
94 special way as some OSes (such as solaris) have too conservative
95 deadlock detection and claim a deadlock when progress can be
96 made. For those OSes we may loop for a while.
98 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
100 int count = 1000;
101 while (count--) {
102 struct timeval tv;
103 if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
104 return 0;
106 if (errno != EDEADLK) {
107 break;
109 /* sleep for as short a time as we can - more portable than usleep() */
110 tv.tv_sec = 0;
111 tv.tv_usec = 1;
112 select(0, NULL, NULL, NULL, &tv);
114 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
115 return -1;
119 /* lock a list in the database. list -1 is the alloc list */
120 static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
122 struct tdb_lock_type *new_lck;
123 int i;
124 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
126 ltype &= ~TDB_MARK_LOCK;
128 /* a global lock allows us to avoid per chain locks */
129 if (tdb->global_lock.count &&
130 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
131 return 0;
134 if (tdb->global_lock.count) {
135 tdb->ecode = TDB_ERR_LOCK;
136 return -1;
139 if (list < -1 || list >= (int)tdb->header.hash_size) {
140 tdb->ecode = TDB_ERR_LOCK;
141 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
142 list, ltype));
143 return -1;
145 if (tdb->flags & TDB_NOLOCK)
146 return 0;
148 for (i=0; i<tdb->num_lockrecs; i++) {
149 if (tdb->lockrecs[i].list == list) {
150 if (tdb->lockrecs[i].count == 0) {
152 * Can't happen, see tdb_unlock(). It should
153 * be an assert.
155 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
156 "lck->count == 0 for list %d", list));
159 * Just increment the in-memory struct, posix locks
160 * don't stack.
162 tdb->lockrecs[i].count++;
163 return 0;
167 new_lck = (struct tdb_lock_type *)realloc(
168 tdb->lockrecs,
169 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
170 if (new_lck == NULL) {
171 errno = ENOMEM;
172 return -1;
174 tdb->lockrecs = new_lck;
176 /* Since fcntl locks don't nest, we do a lock for the first one,
177 and simply bump the count for future ones */
178 if (!mark_lock &&
179 tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list, ltype, op,
180 0, 1)) {
181 return -1;
184 tdb->num_locks++;
186 tdb->lockrecs[tdb->num_lockrecs].list = list;
187 tdb->lockrecs[tdb->num_lockrecs].count = 1;
188 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
189 tdb->num_lockrecs += 1;
191 return 0;
194 /* lock a list in the database. list -1 is the alloc list */
195 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
197 int ret;
198 ret = _tdb_lock(tdb, list, ltype, F_SETLKW);
199 if (ret) {
200 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
201 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
203 return ret;
206 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
207 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
209 return _tdb_lock(tdb, list, ltype, F_SETLK);
213 /* unlock the database: returns void because it's too late for errors. */
214 /* changed to return int it may be interesting to know there
215 has been an error --simo */
216 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
218 int ret = -1;
219 int i;
220 struct tdb_lock_type *lck = NULL;
221 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
223 ltype &= ~TDB_MARK_LOCK;
225 /* a global lock allows us to avoid per chain locks */
226 if (tdb->global_lock.count &&
227 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
228 return 0;
231 if (tdb->global_lock.count) {
232 tdb->ecode = TDB_ERR_LOCK;
233 return -1;
236 if (tdb->flags & TDB_NOLOCK)
237 return 0;
239 /* Sanity checks */
240 if (list < -1 || list >= (int)tdb->header.hash_size) {
241 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
242 return ret;
245 for (i=0; i<tdb->num_lockrecs; i++) {
246 if (tdb->lockrecs[i].list == list) {
247 lck = &tdb->lockrecs[i];
248 break;
252 if ((lck == NULL) || (lck->count == 0)) {
253 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
254 return -1;
257 if (lck->count > 1) {
258 lck->count--;
259 return 0;
263 * This lock has count==1 left, so we need to unlock it in the
264 * kernel. We don't bother with decrementing the in-memory array
265 * element, we're about to overwrite it with the last array element
266 * anyway.
269 if (mark_lock) {
270 ret = 0;
271 } else {
272 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
273 F_SETLKW, 0, 1);
275 tdb->num_locks--;
278 * Shrink the array by overwriting the element just unlocked with the
279 * last array element.
282 if (tdb->num_lockrecs > 1) {
283 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
285 tdb->num_lockrecs -= 1;
288 * We don't bother with realloc when the array shrinks, but if we have
289 * a completely idle tdb we should get rid of the locked array.
292 if (tdb->num_lockrecs == 0) {
293 SAFE_FREE(tdb->lockrecs);
296 if (ret)
297 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
298 return ret;
302 get the transaction lock
304 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
306 if (tdb->global_lock.count) {
307 return 0;
309 if (tdb->transaction_lock_count > 0) {
310 tdb->transaction_lock_count++;
311 return 0;
314 if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype,
315 F_SETLKW, 0, 1) == -1) {
316 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
317 tdb->ecode = TDB_ERR_LOCK;
318 return -1;
320 tdb->transaction_lock_count++;
321 return 0;
325 release the transaction lock
327 int tdb_transaction_unlock(struct tdb_context *tdb)
329 int ret;
330 if (tdb->global_lock.count) {
331 return 0;
333 if (tdb->transaction_lock_count > 1) {
334 tdb->transaction_lock_count--;
335 return 0;
337 ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
338 if (ret == 0) {
339 tdb->transaction_lock_count = 0;
341 return ret;
347 /* lock/unlock entire database */
348 static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
350 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
352 ltype &= ~TDB_MARK_LOCK;
354 /* There are no locks on read-only dbs */
355 if (tdb->read_only || tdb->traverse_read) {
356 tdb->ecode = TDB_ERR_LOCK;
357 return -1;
360 if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
361 tdb->global_lock.count++;
362 return 0;
365 if (tdb->global_lock.count) {
366 /* a global lock of a different type exists */
367 tdb->ecode = TDB_ERR_LOCK;
368 return -1;
371 if (tdb->num_locks != 0) {
372 /* can't combine global and chain locks */
373 tdb->ecode = TDB_ERR_LOCK;
374 return -1;
377 if (!mark_lock &&
378 tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, op,
379 0, 4*tdb->header.hash_size)) {
380 if (op == F_SETLKW) {
381 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
383 return -1;
386 tdb->global_lock.count = 1;
387 tdb->global_lock.ltype = ltype;
389 return 0;
394 /* unlock entire db */
395 static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
397 bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
399 ltype &= ~TDB_MARK_LOCK;
401 /* There are no locks on read-only dbs */
402 if (tdb->read_only || tdb->traverse_read) {
403 tdb->ecode = TDB_ERR_LOCK;
404 return -1;
407 if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
408 tdb->ecode = TDB_ERR_LOCK;
409 return -1;
412 if (tdb->global_lock.count > 1) {
413 tdb->global_lock.count--;
414 return 0;
417 if (!mark_lock &&
418 tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW,
419 0, 4*tdb->header.hash_size)) {
420 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
421 return -1;
424 tdb->global_lock.count = 0;
425 tdb->global_lock.ltype = 0;
427 return 0;
430 /* lock entire database with write lock */
431 int tdb_lockall(struct tdb_context *tdb)
433 tdb_trace(tdb, "tdb_lockall");
434 return _tdb_lockall(tdb, F_WRLCK, F_SETLKW);
437 /* lock entire database with write lock - mark only */
438 int tdb_lockall_mark(struct tdb_context *tdb)
440 tdb_trace(tdb, "tdb_lockall_mark");
441 return _tdb_lockall(tdb, F_WRLCK | TDB_MARK_LOCK, F_SETLKW);
444 /* unlock entire database with write lock - unmark only */
445 int tdb_lockall_unmark(struct tdb_context *tdb)
447 tdb_trace(tdb, "tdb_lockall_unmark");
448 return _tdb_unlockall(tdb, F_WRLCK | TDB_MARK_LOCK);
451 /* lock entire database with write lock - nonblocking varient */
452 int tdb_lockall_nonblock(struct tdb_context *tdb)
454 int ret = _tdb_lockall(tdb, F_WRLCK, F_SETLK);
455 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
456 return ret;
459 /* unlock entire database with write lock */
460 int tdb_unlockall(struct tdb_context *tdb)
462 tdb_trace(tdb, "tdb_unlockall");
463 return _tdb_unlockall(tdb, F_WRLCK);
466 /* lock entire database with read lock */
467 int tdb_lockall_read(struct tdb_context *tdb)
469 tdb_trace(tdb, "tdb_lockall_read");
470 return _tdb_lockall(tdb, F_RDLCK, F_SETLKW);
473 /* lock entire database with read lock - nonblock varient */
474 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
476 int ret = _tdb_lockall(tdb, F_RDLCK, F_SETLK);
477 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
478 return ret;
481 /* unlock entire database with read lock */
482 int tdb_unlockall_read(struct tdb_context *tdb)
484 tdb_trace(tdb, "tdb_unlockall_read");
485 return _tdb_unlockall(tdb, F_RDLCK);
488 /* lock/unlock one hash chain. This is meant to be used to reduce
489 contention - it cannot guarantee how many records will be locked */
490 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
492 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
493 tdb_trace_1rec(tdb, "tdb_chainlock", key);
494 return ret;
497 /* lock/unlock one hash chain, non-blocking. This is meant to be used
498 to reduce contention - it cannot guarantee how many records will be
499 locked */
500 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
502 int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
503 tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
504 return ret;
507 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
508 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
510 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
511 tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
512 return ret;
515 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
516 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
518 tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
519 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
522 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
524 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
525 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
528 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
530 int ret;
531 ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
532 tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
533 return ret;
536 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
538 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
539 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
544 /* record lock stops delete underneath */
545 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
547 if (tdb->global_lock.count) {
548 return 0;
550 return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
554 Write locks override our own fcntl readlocks, so check it here.
555 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
556 an error to fail to get the lock here.
558 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
560 struct tdb_traverse_lock *i;
561 for (i = &tdb->travlocks; i; i = i->next)
562 if (i->off == off)
563 return -1;
564 return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
568 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
569 an error to fail to get the lock here.
571 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
573 return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
576 /* fcntl locks don't stack: avoid unlocking someone else's */
577 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
579 struct tdb_traverse_lock *i;
580 uint32_t count = 0;
582 if (tdb->global_lock.count) {
583 return 0;
586 if (off == 0)
587 return 0;
588 for (i = &tdb->travlocks; i; i = i->next)
589 if (i->off == off)
590 count++;
591 return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);