tdb: cleanup: split brlock and brunlock methods.
[Samba/id10ts.git] / lib / tdb / common / lock.c
blob515bffee598f01df50f73524d214b21b72f79510
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 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
32 tdb->interrupt_sig_ptr = ptr;
35 static int fcntl_lock(struct tdb_context *tdb,
36 int rw, off_t off, off_t len, bool waitflag)
38 struct flock fl;
40 fl.l_type = rw;
41 fl.l_whence = SEEK_SET;
42 fl.l_start = off;
43 fl.l_len = len;
44 fl.l_pid = 0;
46 if (waitflag)
47 return fcntl(tdb->fd, F_SETLKW, &fl);
48 else
49 return fcntl(tdb->fd, F_SETLK, &fl);
52 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
54 struct flock fl;
55 #if 0 /* Check they matched up locks and unlocks correctly. */
56 char line[80];
57 FILE *locks;
58 bool found = false;
60 locks = fopen("/proc/locks", "r");
62 while (fgets(line, 80, locks)) {
63 char *p;
64 int type, start, l;
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 ")))
69 continue;
70 p += strlen(" FLOCK ADVISORY ");
71 if (strncmp(p, "READ ", strlen("READ ")) == 0)
72 type = F_RDLCK;
73 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
74 type = F_WRLCK;
75 else
76 abort();
77 p += 6;
78 if (atoi(p) != getpid())
79 continue;
80 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
81 start = atoi(p);
82 p = strchr(p, ' ') + 1;
83 if (strncmp(p, "EOF", 3) == 0)
84 l = 0;
85 else
86 l = atoi(p) - start + 1;
88 if (off == start) {
89 if (len != l) {
90 fprintf(stderr, "Len %u should be %u: %s",
91 (int)len, l, line);
92 abort();
94 if (type != rw) {
95 fprintf(stderr, "Type %s wrong: %s",
96 rw == F_RDLCK ? "READ" : "WRITE", line);
97 abort();
99 found = true;
100 break;
104 if (!found) {
105 fprintf(stderr, "Unlock on %u@%u not found!\n",
106 (int)off, (int)len);
107 abort();
110 fclose(locks);
111 #endif
113 fl.l_type = F_UNLCK;
114 fl.l_whence = SEEK_SET;
115 fl.l_start = off;
116 fl.l_len = len;
117 fl.l_pid = 0;
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 On error, errno is also set so that errors are passed back properly
126 through tdb_open().
128 note that a len of zero means lock to end of file
130 int tdb_brlock(struct tdb_context *tdb,
131 int rw_type, tdb_off_t offset, size_t len,
132 enum tdb_lock_flags flags)
134 int ret;
136 if (tdb->flags & TDB_NOLOCK) {
137 return 0;
140 if (flags & TDB_LOCK_MARK_ONLY) {
141 return 0;
144 if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
145 tdb->ecode = TDB_ERR_RDONLY;
146 return -1;
149 do {
150 ret = fcntl_lock(tdb, rw_type, offset, len,
151 flags & TDB_LOCK_WAIT);
152 /* Check for a sigalarm break. */
153 if (ret == -1 && errno == EINTR &&
154 tdb->interrupt_sig_ptr &&
155 *tdb->interrupt_sig_ptr) {
156 break;
158 } while (ret == -1 && errno == EINTR);
160 if (ret == -1) {
161 tdb->ecode = TDB_ERR_LOCK;
162 /* Generic lock error. errno set by fcntl.
163 * EAGAIN is an expected return from non-blocking
164 * locks. */
165 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
166 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
167 tdb->fd, offset, rw_type, flags, (int)len));
169 return -1;
171 return 0;
174 int tdb_brunlock(struct tdb_context *tdb,
175 int rw_type, tdb_off_t offset, size_t len)
177 int ret;
179 if (tdb->flags & TDB_NOLOCK) {
180 return 0;
183 do {
184 ret = fcntl_unlock(tdb, rw_type, offset, len);
185 } while (ret == -1 && errno == EINTR);
187 if (ret == -1) {
188 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
189 tdb->fd, offset, rw_type, (int)len));
191 return ret;
195 upgrade a read lock to a write lock. This needs to be handled in a
196 special way as some OSes (such as solaris) have too conservative
197 deadlock detection and claim a deadlock when progress can be
198 made. For those OSes we may loop for a while.
200 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
202 int count = 1000;
203 while (count--) {
204 struct timeval tv;
205 if (tdb_brlock(tdb, F_WRLCK, offset, len,
206 TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
207 return 0;
209 if (errno != EDEADLK) {
210 break;
212 /* sleep for as short a time as we can - more portable than usleep() */
213 tv.tv_sec = 0;
214 tv.tv_usec = 1;
215 select(0, NULL, NULL, NULL, &tv);
217 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
218 return -1;
222 /* lock a list in the database. list -1 is the alloc list */
223 static int _tdb_lock(struct tdb_context *tdb, int list, int ltype,
224 enum tdb_lock_flags flags)
226 struct tdb_lock_type *new_lck;
227 int i;
229 /* a global lock allows us to avoid per chain locks */
230 if (tdb->global_lock.count &&
231 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
232 return 0;
235 if (tdb->global_lock.count) {
236 tdb->ecode = TDB_ERR_LOCK;
237 return -1;
240 if (list < -1 || list >= (int)tdb->header.hash_size) {
241 tdb->ecode = TDB_ERR_LOCK;
242 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
243 list, ltype));
244 return -1;
246 if (tdb->flags & TDB_NOLOCK)
247 return 0;
249 for (i=0; i<tdb->num_lockrecs; i++) {
250 if (tdb->lockrecs[i].list == list) {
251 if (tdb->lockrecs[i].count == 0) {
253 * Can't happen, see tdb_unlock(). It should
254 * be an assert.
256 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
257 "lck->count == 0 for list %d", list));
260 * Just increment the in-memory struct, posix locks
261 * don't stack.
263 tdb->lockrecs[i].count++;
264 return 0;
268 new_lck = (struct tdb_lock_type *)realloc(
269 tdb->lockrecs,
270 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
271 if (new_lck == NULL) {
272 errno = ENOMEM;
273 return -1;
275 tdb->lockrecs = new_lck;
277 /* Since fcntl locks don't nest, we do a lock for the first one,
278 and simply bump the count for future ones */
279 if (tdb->methods->brlock(tdb, ltype, FREELIST_TOP+4*list, 1, flags)) {
280 return -1;
283 tdb->num_locks++;
285 tdb->lockrecs[tdb->num_lockrecs].list = list;
286 tdb->lockrecs[tdb->num_lockrecs].count = 1;
287 tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
288 tdb->num_lockrecs += 1;
290 return 0;
293 /* lock a list in the database. list -1 is the alloc list */
294 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
296 int ret;
298 ret = _tdb_lock(tdb, list, ltype, TDB_LOCK_WAIT);
299 if (ret) {
300 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
301 "ltype=%d (%s)\n", list, ltype, strerror(errno)));
303 return ret;
306 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
307 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
309 return _tdb_lock(tdb, list, ltype, TDB_LOCK_NOWAIT);
313 static int _tdb_unlock(struct tdb_context *tdb, int list, int ltype,
314 bool mark_lock)
316 int ret = -1;
317 int i;
318 struct tdb_lock_type *lck = NULL;
320 /* a global lock allows us to avoid per chain locks */
321 if (tdb->global_lock.count &&
322 (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
323 return 0;
326 if (tdb->global_lock.count) {
327 tdb->ecode = TDB_ERR_LOCK;
328 return -1;
331 if (tdb->flags & TDB_NOLOCK)
332 return 0;
334 /* Sanity checks */
335 if (list < -1 || list >= (int)tdb->header.hash_size) {
336 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
337 return ret;
340 for (i=0; i<tdb->num_lockrecs; i++) {
341 if (tdb->lockrecs[i].list == list) {
342 lck = &tdb->lockrecs[i];
343 break;
347 if ((lck == NULL) || (lck->count == 0)) {
348 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
349 return -1;
352 if (lck->count > 1) {
353 lck->count--;
354 return 0;
358 * This lock has count==1 left, so we need to unlock it in the
359 * kernel. We don't bother with decrementing the in-memory array
360 * element, we're about to overwrite it with the last array element
361 * anyway.
364 if (mark_lock) {
365 ret = 0;
366 } else {
367 ret = tdb->methods->brunlock(tdb, ltype,
368 FREELIST_TOP+4*list, 1);
370 tdb->num_locks--;
373 * Shrink the array by overwriting the element just unlocked with the
374 * last array element.
377 if (tdb->num_lockrecs > 1) {
378 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
380 tdb->num_lockrecs -= 1;
383 * We don't bother with realloc when the array shrinks, but if we have
384 * a completely idle tdb we should get rid of the locked array.
387 if (tdb->num_lockrecs == 0) {
388 SAFE_FREE(tdb->lockrecs);
391 if (ret)
392 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
393 return ret;
396 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
398 return _tdb_unlock(tdb, list, ltype, false);
402 get the transaction lock
404 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
406 if (tdb->global_lock.count) {
407 return 0;
409 if (tdb->transaction_lock_count > 0) {
410 tdb->transaction_lock_count++;
411 return 0;
414 if (tdb->methods->brlock(tdb, ltype, TRANSACTION_LOCK, 1, TDB_LOCK_WAIT) == -1) {
415 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
416 tdb->ecode = TDB_ERR_LOCK;
417 return -1;
419 tdb->transaction_lock_count++;
420 return 0;
424 release the transaction lock
426 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
428 int ret;
429 if (tdb->global_lock.count) {
430 return 0;
432 if (tdb->transaction_lock_count > 1) {
433 tdb->transaction_lock_count--;
434 return 0;
436 ret = tdb->methods->brunlock(tdb, ltype, TRANSACTION_LOCK, 1);
437 if (ret == 0) {
438 tdb->transaction_lock_count = 0;
440 return ret;
446 /* lock/unlock entire database */
447 static int _tdb_lockall(struct tdb_context *tdb, int ltype,
448 enum tdb_lock_flags flags)
450 /* There are no locks on read-only dbs */
451 if (tdb->read_only || tdb->traverse_read) {
452 tdb->ecode = TDB_ERR_LOCK;
453 return -1;
456 if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
457 tdb->global_lock.count++;
458 return 0;
461 if (tdb->global_lock.count) {
462 /* a global lock of a different type exists */
463 tdb->ecode = TDB_ERR_LOCK;
464 return -1;
467 if (tdb->num_locks != 0) {
468 /* can't combine global and chain locks */
469 tdb->ecode = TDB_ERR_LOCK;
470 return -1;
473 if (tdb->methods->brlock(tdb, ltype,
474 FREELIST_TOP, 4*tdb->header.hash_size,
475 flags)) {
476 if (flags & TDB_LOCK_WAIT) {
477 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
479 return -1;
482 tdb->global_lock.count = 1;
483 tdb->global_lock.ltype = ltype;
485 return 0;
490 /* unlock entire db */
491 static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
493 /* There are no locks on read-only dbs */
494 if (tdb->read_only || tdb->traverse_read) {
495 tdb->ecode = TDB_ERR_LOCK;
496 return -1;
499 if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
500 tdb->ecode = TDB_ERR_LOCK;
501 return -1;
504 if (tdb->global_lock.count > 1) {
505 tdb->global_lock.count--;
506 return 0;
509 if (!mark_lock &&
510 tdb->methods->brunlock(tdb, ltype,
511 FREELIST_TOP, 4*tdb->header.hash_size)) {
512 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
513 return -1;
516 tdb->global_lock.count = 0;
517 tdb->global_lock.ltype = 0;
519 return 0;
522 /* lock entire database with write lock */
523 int tdb_lockall(struct tdb_context *tdb)
525 tdb_trace(tdb, "tdb_lockall");
526 return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_WAIT);
529 /* lock entire database with write lock - mark only */
530 int tdb_lockall_mark(struct tdb_context *tdb)
532 tdb_trace(tdb, "tdb_lockall_mark");
533 return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY);
536 /* unlock entire database with write lock - unmark only */
537 int tdb_lockall_unmark(struct tdb_context *tdb)
539 tdb_trace(tdb, "tdb_lockall_unmark");
540 return _tdb_unlockall(tdb, F_WRLCK, true);
543 /* lock entire database with write lock - nonblocking varient */
544 int tdb_lockall_nonblock(struct tdb_context *tdb)
546 int ret = _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_NOWAIT);
547 tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
548 return ret;
551 /* unlock entire database with write lock */
552 int tdb_unlockall(struct tdb_context *tdb)
554 tdb_trace(tdb, "tdb_unlockall");
555 return _tdb_unlockall(tdb, F_WRLCK, false);
558 /* lock entire database with read lock */
559 int tdb_lockall_read(struct tdb_context *tdb)
561 tdb_trace(tdb, "tdb_lockall_read");
562 return _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_WAIT);
565 /* lock entire database with read lock - nonblock varient */
566 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
568 int ret = _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_NOWAIT);
569 tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
570 return ret;
573 /* unlock entire database with read lock */
574 int tdb_unlockall_read(struct tdb_context *tdb)
576 tdb_trace(tdb, "tdb_unlockall_read");
577 return _tdb_unlockall(tdb, F_RDLCK, false);
580 /* lock/unlock one hash chain. This is meant to be used to reduce
581 contention - it cannot guarantee how many records will be locked */
582 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
584 int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
585 tdb_trace_1rec(tdb, "tdb_chainlock", key);
586 return ret;
589 /* lock/unlock one hash chain, non-blocking. This is meant to be used
590 to reduce contention - it cannot guarantee how many records will be
591 locked */
592 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
594 int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
595 tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
596 return ret;
599 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
600 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
602 int ret = _tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK,
603 TDB_LOCK_MARK_ONLY);
604 tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
605 return ret;
608 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
609 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
611 tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
612 return _tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK, true);
615 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
617 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
618 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
621 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
623 int ret;
624 ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
625 tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
626 return ret;
629 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
631 tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
632 return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
637 /* record lock stops delete underneath */
638 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
640 if (tdb->global_lock.count) {
641 return 0;
643 return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
647 Write locks override our own fcntl readlocks, so check it here.
648 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
649 an error to fail to get the lock here.
651 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
653 struct tdb_traverse_lock *i;
654 for (i = &tdb->travlocks; i; i = i->next)
655 if (i->off == off)
656 return -1;
657 return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
660 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
662 return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
665 /* fcntl locks don't stack: avoid unlocking someone else's */
666 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
668 struct tdb_traverse_lock *i;
669 uint32_t count = 0;
671 if (tdb->global_lock.count) {
672 return 0;
675 if (off == 0)
676 return 0;
677 for (i = &tdb->travlocks; i; i = i->next)
678 if (i->off == off)
679 count++;
680 return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);