]> git.ozlabs.org Git - ccan/blob - ccan/tdb/lock.c
a59ba365213e473630617ee69ff1f238585d6782
[ccan] / ccan / tdb / lock.c
1  /* 
2    Unix SMB/CIFS implementation.
3
4    trivial database library
5
6    Copyright (C) Andrew Tridgell              1999-2005
7    Copyright (C) Paul `Rusty' Russell              2000
8    Copyright (C) Jeremy Allison                    2000-2003
9    
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
13    
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.
18
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.
23
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/>.
26 */
27
28 #include "tdb_private.h"
29
30 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
31 {
32         tdb->interrupt_sig_ptr = ptr;
33 }
34
35 static int fcntl_lock(struct tdb_context *tdb,
36                       int rw, off_t off, off_t len, bool waitflag)
37 {
38         struct flock fl;
39
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;
45
46         if (waitflag)
47                 return fcntl(tdb->fd, F_SETLKW, &fl);
48         else
49                 return fcntl(tdb->fd, F_SETLK, &fl);
50 }
51
52 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
53 {
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;
59
60         locks = fopen("/proc/locks", "r");
61
62         while (fgets(line, 80, locks)) {
63                 char *p;
64                 int type, start, l;
65
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;
87
88                 if (off == start) {
89                         if (len != l) {
90                                 fprintf(stderr, "Len %u should be %u: %s",
91                                         (int)len, l, line);
92                                 abort();
93                         }
94                         if (type != rw) {
95                                 fprintf(stderr, "Type %s wrong: %s",
96                                         rw == F_RDLCK ? "READ" : "WRITE", line);
97                                 abort();
98                         }
99                         found = true;
100                         break;
101                 }
102         }
103
104         if (!found) {
105                 fprintf(stderr, "Unlock on %u@%u not found!\n",
106                         (int)off, (int)len);
107                 abort();
108         }
109
110         fclose(locks);
111 #endif
112
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;
118
119         return fcntl(tdb->fd, F_SETLKW, &fl);
120 }
121
122 /* a byte range locking function - return 0 on success
123    this functions locks/unlocks 1 byte at the specified offset.
124
125    On error, errno is also set so that errors are passed back properly
126    through tdb_open(). 
127
128    note that a len of zero means lock to end of file
129 */
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)
133 {
134         int ret;
135
136         if (tdb->flags & TDB_NOLOCK) {
137                 return 0;
138         }
139
140         if (flags & TDB_LOCK_MARK_ONLY) {
141                 return 0;
142         }
143
144         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
145                 tdb->ecode = TDB_ERR_RDONLY;
146                 return -1;
147         }
148
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;
157                 }
158         } while (ret == -1 && errno == EINTR);
159
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));
168                 }
169                 return -1;
170         }
171         return 0;
172 }
173
174 int tdb_brunlock(struct tdb_context *tdb,
175                  int rw_type, tdb_off_t offset, size_t len)
176 {
177         int ret;
178
179         if (tdb->flags & TDB_NOLOCK) {
180                 return 0;
181         }
182
183         do {
184                 ret = fcntl_unlock(tdb, rw_type, offset, len);
185         } while (ret == -1 && errno == EINTR);
186
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));
190         }
191         return ret;
192 }
193
194 /*
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.  
199 */
200 int tdb_allrecord_upgrade(struct tdb_context *tdb)
201 {
202         int count = 1000;
203
204         if (tdb->allrecord_lock.count != 1) {
205                 TDB_LOG((tdb, TDB_DEBUG_ERROR,
206                          "tdb_allrecord_upgrade failed: count %u too high\n",
207                          tdb->allrecord_lock.count));
208                 return -1;
209         }
210
211         if (tdb->allrecord_lock.off != 1) {
212                 TDB_LOG((tdb, TDB_DEBUG_ERROR,
213                          "tdb_allrecord_upgrade failed: already upgraded?\n"));
214                 return -1;
215         }
216
217         while (count--) {
218                 struct timeval tv;
219                 if (tdb_brlock(tdb, F_WRLCK, FREELIST_TOP, 0,
220                                TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
221                         tdb->allrecord_lock.ltype = F_WRLCK;
222                         tdb->allrecord_lock.off = 0;
223                         return 0;
224                 }
225                 if (errno != EDEADLK) {
226                         break;
227                 }
228                 /* sleep for as short a time as we can - more portable than usleep() */
229                 tv.tv_sec = 0;
230                 tv.tv_usec = 1;
231                 select(0, NULL, NULL, NULL, &tv);
232         }
233         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_allrecord_upgrade failed\n"));
234         return -1;
235 }
236
237 /* list -1 is the alloc list, otherwise a hash chain. */
238 static tdb_off_t lock_offset(int list)
239 {
240         return FREELIST_TOP + 4*list;
241 }
242
243 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
244                                            tdb_off_t offset)
245 {
246         unsigned int i;
247
248         for (i=0; i<tdb->num_lockrecs; i++) {
249                 if (tdb->lockrecs[i].off == offset) {
250                         return &tdb->lockrecs[i];
251                 }
252         }
253         return NULL;
254 }
255
256 /* lock an offset in the database. */
257 int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
258                   enum tdb_lock_flags flags)
259 {
260         struct tdb_lock_type *new_lck;
261
262         if (offset >= lock_offset(tdb->header.hash_size)) {
263                 tdb->ecode = TDB_ERR_LOCK;
264                 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid offset %u for ltype=%d\n",
265                          offset, ltype));
266                 return -1;
267         }
268         if (tdb->flags & TDB_NOLOCK)
269                 return 0;
270
271         new_lck = find_nestlock(tdb, offset);
272         if (new_lck) {
273                 /*
274                  * Just increment the in-memory struct, posix locks
275                  * don't stack.
276                  */
277                 new_lck->count++;
278                 return 0;
279         }
280
281         new_lck = (struct tdb_lock_type *)realloc(
282                 tdb->lockrecs,
283                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
284         if (new_lck == NULL) {
285                 errno = ENOMEM;
286                 return -1;
287         }
288         tdb->lockrecs = new_lck;
289
290         /* Since fcntl locks don't nest, we do a lock for the first one,
291            and simply bump the count for future ones */
292         if (tdb->methods->brlock(tdb, ltype, offset, 1, flags)) {
293                 return -1;
294         }
295
296         tdb->lockrecs[tdb->num_lockrecs].off = offset;
297         tdb->lockrecs[tdb->num_lockrecs].count = 1;
298         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
299         tdb->num_lockrecs++;
300
301         return 0;
302 }
303
304 /* lock a list in the database. list -1 is the alloc list */
305 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
306 {
307         int ret;
308
309         /* a allrecord lock allows us to avoid per chain locks */
310         if (tdb->allrecord_lock.count &&
311             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
312                 return 0;
313         }
314
315         if (tdb->allrecord_lock.count) {
316                 tdb->ecode = TDB_ERR_LOCK;
317                 ret = -1;
318         } else {
319                 ret = tdb_nest_lock(tdb, lock_offset(list), ltype,
320                                     TDB_LOCK_WAIT);
321         }
322         if (ret) {
323                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
324                          "ltype=%d (%s)\n",  list, ltype, strerror(errno)));
325         }
326         return ret;
327 }
328
329 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
330 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
331 {
332         /* a allrecord lock allows us to avoid per chain locks */
333         if (tdb->allrecord_lock.count &&
334             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
335                 return 0;
336         }
337
338         if (tdb->allrecord_lock.count) {
339                 tdb->ecode = TDB_ERR_LOCK;
340                 return -1;
341         }
342
343         return tdb_nest_lock(tdb, lock_offset(list), ltype, TDB_LOCK_NOWAIT);
344 }
345
346
347 int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
348                     bool mark_lock)
349 {
350         int ret = -1;
351         struct tdb_lock_type *lck;
352
353         if (tdb->flags & TDB_NOLOCK)
354                 return 0;
355
356         /* Sanity checks */
357         if (offset >= lock_offset(tdb->header.hash_size)) {
358                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: offset %u invalid (%d)\n", offset, tdb->header.hash_size));
359                 return ret;
360         }
361
362         lck = find_nestlock(tdb, offset);
363         if ((lck == NULL) || (lck->count == 0)) {
364                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
365                 return -1;
366         }
367
368         if (lck->count > 1) {
369                 lck->count--;
370                 return 0;
371         }
372
373         /*
374          * This lock has count==1 left, so we need to unlock it in the
375          * kernel. We don't bother with decrementing the in-memory array
376          * element, we're about to overwrite it with the last array element
377          * anyway.
378          */
379
380         if (mark_lock) {
381                 ret = 0;
382         } else {
383                 ret = tdb->methods->brunlock(tdb, ltype, offset, 1);
384         }
385
386         /*
387          * Shrink the array by overwriting the element just unlocked with the
388          * last array element.
389          */
390         *lck = tdb->lockrecs[--tdb->num_lockrecs];
391
392         /*
393          * We don't bother with realloc when the array shrinks, but if we have
394          * a completely idle tdb we should get rid of the locked array.
395          */
396
397         if (tdb->num_lockrecs == 0) {
398                 SAFE_FREE(tdb->lockrecs);
399         }
400
401         if (ret)
402                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n")); 
403         return ret;
404 }
405
406 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
407 {
408         /* a global lock allows us to avoid per chain locks */
409         if (tdb->allrecord_lock.count &&
410             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
411                 return 0;
412         }
413
414         if (tdb->allrecord_lock.count) {
415                 tdb->ecode = TDB_ERR_LOCK;
416                 return -1;
417         }
418
419         return tdb_nest_unlock(tdb, lock_offset(list), ltype, false);
420 }
421
422 /*
423   get the transaction lock
424  */
425 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
426 {
427         return tdb_nest_lock(tdb, TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
428 }
429
430 /*
431   release the transaction lock
432  */
433 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
434 {
435         return tdb_nest_unlock(tdb, TRANSACTION_LOCK, ltype, false);
436 }
437
438
439 /* lock/unlock entire database.  It can only be upgradable if you have some
440  * other way of guaranteeing exclusivity (ie. transaction write lock). */
441 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
442                        enum tdb_lock_flags flags, bool upgradable)
443 {
444         /* There are no locks on read-only dbs */
445         if (tdb->read_only || tdb->traverse_read) {
446                 tdb->ecode = TDB_ERR_LOCK;
447                 return -1;
448         }
449
450         if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
451                 tdb->allrecord_lock.count++;
452                 return 0;
453         }
454
455         if (tdb->allrecord_lock.count) {
456                 /* a global lock of a different type exists */
457                 tdb->ecode = TDB_ERR_LOCK;
458                 return -1;
459         }
460
461         if (tdb_have_extra_locks(tdb)) {
462                 /* can't combine global and chain locks */
463                 tdb->ecode = TDB_ERR_LOCK;
464                 return -1;
465         }
466
467         if (upgradable && ltype != F_RDLCK) {
468                 /* tdb error: you can't upgrade a write lock! */
469                 tdb->ecode = TDB_ERR_LOCK;
470                 return -1;
471         }
472
473         if (tdb->methods->brlock(tdb, ltype, FREELIST_TOP, 0, flags)) {
474                 if (flags & TDB_LOCK_WAIT) {
475                         TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
476                 }
477                 return -1;
478         }
479
480         tdb->allrecord_lock.count = 1;
481         /* If it's upgradable, it's actually exclusive so we can treat
482          * it as a write lock. */
483         tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
484         tdb->allrecord_lock.off = upgradable;
485
486         return 0;
487 }
488
489
490
491 /* unlock entire db */
492 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype, bool mark_lock)
493 {
494         /* There are no locks on read-only dbs */
495         if (tdb->read_only || tdb->traverse_read) {
496                 tdb->ecode = TDB_ERR_LOCK;
497                 return -1;
498         }
499
500         if (tdb->allrecord_lock.count == 0) {
501                 tdb->ecode = TDB_ERR_LOCK;
502                 return -1;
503         }
504
505         /* Upgradable locks are marked as write locks. */
506         if (tdb->allrecord_lock.ltype != ltype
507             && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
508                 tdb->ecode = TDB_ERR_LOCK;
509                 return -1;
510         }
511
512         if (tdb->allrecord_lock.count > 1) {
513                 tdb->allrecord_lock.count--;
514                 return 0;
515         }
516
517         if (!mark_lock &&
518             tdb->methods->brunlock(tdb, ltype, FREELIST_TOP, 0)) {
519                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
520                 return -1;
521         }
522
523         tdb->allrecord_lock.count = 0;
524         tdb->allrecord_lock.ltype = 0;
525
526         return 0;
527 }
528
529 /* lock entire database with write lock */
530 int tdb_lockall(struct tdb_context *tdb)
531 {
532         tdb_trace(tdb, "tdb_lockall");
533         return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
534 }
535
536 /* lock entire database with write lock - mark only */
537 int tdb_lockall_mark(struct tdb_context *tdb)
538 {
539         tdb_trace(tdb, "tdb_lockall_mark");
540         return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY, false);
541 }
542
543 /* unlock entire database with write lock - unmark only */
544 int tdb_lockall_unmark(struct tdb_context *tdb)
545 {
546         tdb_trace(tdb, "tdb_lockall_unmark");
547         return tdb_allrecord_unlock(tdb, F_WRLCK, true);
548 }
549
550 /* lock entire database with write lock - nonblocking varient */
551 int tdb_lockall_nonblock(struct tdb_context *tdb)
552 {
553         int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
554         tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
555         return ret;
556 }
557
558 /* unlock entire database with write lock */
559 int tdb_unlockall(struct tdb_context *tdb)
560 {
561         tdb_trace(tdb, "tdb_unlockall");
562         return tdb_allrecord_unlock(tdb, F_WRLCK, false);
563 }
564
565 /* lock entire database with read lock */
566 int tdb_lockall_read(struct tdb_context *tdb)
567 {
568         tdb_trace(tdb, "tdb_lockall_read");
569         return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
570 }
571
572 /* lock entire database with read lock - nonblock varient */
573 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
574 {
575         int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
576         tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
577         return ret;
578 }
579
580 /* unlock entire database with read lock */
581 int tdb_unlockall_read(struct tdb_context *tdb)
582 {
583         tdb_trace(tdb, "tdb_unlockall_read");
584         return tdb_allrecord_unlock(tdb, F_RDLCK, false);
585 }
586
587 /* lock/unlock one hash chain. This is meant to be used to reduce
588    contention - it cannot guarantee how many records will be locked */
589 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
590 {
591         int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
592         tdb_trace_1rec(tdb, "tdb_chainlock", key);
593         return ret;
594 }
595
596 /* lock/unlock one hash chain, non-blocking. This is meant to be used
597    to reduce contention - it cannot guarantee how many records will be
598    locked */
599 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
600 {
601         int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
602         tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
603         return ret;
604 }
605
606 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
607 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
608 {
609         int ret = tdb_nest_lock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
610                                 F_WRLCK, TDB_LOCK_MARK_ONLY);
611         tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
612         return ret;
613 }
614
615 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
616 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
617 {
618         tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
619         return tdb_nest_unlock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
620                                F_WRLCK, true);
621 }
622
623 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
624 {
625         tdb_trace_1rec(tdb, "tdb_chainunlock", key);
626         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
627 }
628
629 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
630 {
631         int ret;
632         ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
633         tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
634         return ret;
635 }
636
637 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
638 {
639         tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
640         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
641 }
642
643
644
645 /* record lock stops delete underneath */
646 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
647 {
648         if (tdb->allrecord_lock.count) {
649                 return 0;
650         }
651         return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
652 }
653
654 /*
655   Write locks override our own fcntl readlocks, so check it here.
656   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
657   an error to fail to get the lock here.
658 */
659 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
660 {
661         struct tdb_traverse_lock *i;
662         for (i = &tdb->travlocks; i; i = i->next)
663                 if (i->off == off)
664                         return -1;
665         return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
666 }
667
668 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
669 {
670         return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
671 }
672
673 /* fcntl locks don't stack: avoid unlocking someone else's */
674 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
675 {
676         struct tdb_traverse_lock *i;
677         uint32_t count = 0;
678
679         if (tdb->allrecord_lock.count) {
680                 return 0;
681         }
682
683         if (off == 0)
684                 return 0;
685         for (i = &tdb->travlocks; i; i = i->next)
686                 if (i->off == off)
687                         count++;
688         return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);
689 }
690
691 bool tdb_have_extra_locks(struct tdb_context *tdb)
692 {
693         unsigned int extra = tdb->num_lockrecs;
694
695         /* A transaction holds the lock for all records. */
696         if (!tdb->transaction && tdb->allrecord_lock.count) {
697                 return true;
698         }
699
700         /* We always hold the active lock if CLEAR_IF_FIRST. */
701         if (find_nestlock(tdb, ACTIVE_LOCK)) {
702                 extra--;
703         }
704
705         /* In a transaction, we expect to hold the transaction lock */
706         if (tdb->transaction && find_nestlock(tdb, TRANSACTION_LOCK)) {
707                 extra--;
708         }
709
710         return extra;
711 }
712
713 /* The transaction code uses this to remove all locks.  Note that this
714    may include OPEN_LOCK. */
715 void tdb_release_extra_locks(struct tdb_context *tdb)
716 {
717         unsigned int i, extra = 0;
718
719         if (tdb->allrecord_lock.count != 0) {
720                 tdb_brunlock(tdb, tdb->allrecord_lock.ltype, FREELIST_TOP, 0);
721                 tdb->allrecord_lock.count = 0;
722         }
723
724         for (i=0;i<tdb->num_lockrecs;i++) {
725                 struct tdb_lock_type *lck = &tdb->lockrecs[i];
726
727                 /* Don't release transaction or active locks! */
728                 if (tdb->transaction && lck->off == TRANSACTION_LOCK) {
729                         tdb->lockrecs[extra++] = *lck;
730                 } else if (lck->off == ACTIVE_LOCK) {
731                         tdb->lockrecs[extra++] = *lck;
732                 } else {
733                         tdb_brunlock(tdb, lck->ltype, lck->off, 1);
734                 }
735         }
736         tdb->num_lockrecs = extra;
737         if (tdb->num_lockrecs == 0) {
738                 SAFE_FREE(tdb->lockrecs);
739         }
740 }