]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_lock.c
a33c851c1721e0c92b10b834b33756bed2c602bc
[ccan] / ccan / tdb2 / tdb1_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 "tdb1_private.h"
29
30 static int fcntl_lock(struct tdb1_context *tdb,
31                       int rw, off_t off, off_t len, bool waitflag)
32 {
33         struct flock fl;
34
35         fl.l_type = rw;
36         fl.l_whence = SEEK_SET;
37         fl.l_start = off;
38         fl.l_len = len;
39         fl.l_pid = 0;
40
41         if (waitflag)
42                 return fcntl(tdb->fd, F_SETLKW, &fl);
43         else
44                 return fcntl(tdb->fd, F_SETLK, &fl);
45 }
46
47 static int fcntl_unlock(struct tdb1_context *tdb, int rw, off_t off, off_t len)
48 {
49         struct flock fl;
50 #if 0 /* Check they matched up locks and unlocks correctly. */
51         char line[80];
52         FILE *locks;
53         bool found = false;
54
55         locks = fopen("/proc/locks", "r");
56
57         while (fgets(line, 80, locks)) {
58                 char *p;
59                 int type, start, l;
60
61                 /* eg. 1: FLOCK  ADVISORY  WRITE 2440 08:01:2180826 0 EOF */
62                 p = strchr(line, ':') + 1;
63                 if (strncmp(p, " POSIX  ADVISORY  ", strlen(" POSIX  ADVISORY  ")))
64                         continue;
65                 p += strlen(" FLOCK  ADVISORY  ");
66                 if (strncmp(p, "READ  ", strlen("READ  ")) == 0)
67                         type = F_RDLCK;
68                 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
69                         type = F_WRLCK;
70                 else
71                         abort();
72                 p += 6;
73                 if (atoi(p) != getpid())
74                         continue;
75                 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
76                 start = atoi(p);
77                 p = strchr(p, ' ') + 1;
78                 if (strncmp(p, "EOF", 3) == 0)
79                         l = 0;
80                 else
81                         l = atoi(p) - start + 1;
82
83                 if (off == start) {
84                         if (len != l) {
85                                 fprintf(stderr, "Len %u should be %u: %s",
86                                         (int)len, l, line);
87                                 abort();
88                         }
89                         if (type != rw) {
90                                 fprintf(stderr, "Type %s wrong: %s",
91                                         rw == F_RDLCK ? "READ" : "WRITE", line);
92                                 abort();
93                         }
94                         found = true;
95                         break;
96                 }
97         }
98
99         if (!found) {
100                 fprintf(stderr, "Unlock on %u@%u not found!\n",
101                         (int)off, (int)len);
102                 abort();
103         }
104
105         fclose(locks);
106 #endif
107
108         fl.l_type = F_UNLCK;
109         fl.l_whence = SEEK_SET;
110         fl.l_start = off;
111         fl.l_len = len;
112         fl.l_pid = 0;
113
114         return fcntl(tdb->fd, F_SETLKW, &fl);
115 }
116
117 /* list -1 is the alloc list, otherwise a hash chain. */
118 static tdb1_off_t lock_offset(int list)
119 {
120         return TDB1_FREELIST_TOP + 4*list;
121 }
122
123 /* a byte range locking function - return 0 on success
124    this functions locks/unlocks 1 byte at the specified offset.
125
126    On error, errno is also set so that errors are passed back properly
127    through tdb1_open().
128
129    note that a len of zero means lock to end of file
130 */
131 int tdb1_brlock(struct tdb1_context *tdb,
132                int rw_type, tdb1_off_t offset, size_t len,
133                enum tdb1_lock_flags flags)
134 {
135         int ret;
136
137         if (tdb->flags & TDB1_NOLOCK) {
138                 return 0;
139         }
140
141         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
142                 tdb->ecode = TDB1_ERR_RDONLY;
143                 return -1;
144         }
145
146         do {
147                 ret = fcntl_lock(tdb, rw_type, offset, len,
148                                  flags & TDB1_LOCK_WAIT);
149         } while (ret == -1 && errno == EINTR);
150
151         if (ret == -1) {
152                 tdb->ecode = TDB1_ERR_LOCK;
153                 /* Generic lock error. errno set by fcntl.
154                  * EAGAIN is an expected return from non-blocking
155                  * locks. */
156                 if (!(flags & TDB1_LOCK_PROBE) && errno != EAGAIN) {
157                         TDB1_LOG((tdb, TDB1_DEBUG_TRACE,"tdb1_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
158                                  tdb->fd, offset, rw_type, flags, (int)len));
159                 }
160                 return -1;
161         }
162         return 0;
163 }
164
165 int tdb1_brunlock(struct tdb1_context *tdb,
166                  int rw_type, tdb1_off_t offset, size_t len)
167 {
168         int ret;
169
170         if (tdb->flags & TDB1_NOLOCK) {
171                 return 0;
172         }
173
174         do {
175                 ret = fcntl_unlock(tdb, rw_type, offset, len);
176         } while (ret == -1 && errno == EINTR);
177
178         if (ret == -1) {
179                 TDB1_LOG((tdb, TDB1_DEBUG_TRACE,"tdb1_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
180                          tdb->fd, offset, rw_type, (int)len));
181         }
182         return ret;
183 }
184
185 /*
186   upgrade a read lock to a write lock. This needs to be handled in a
187   special way as some OSes (such as solaris) have too conservative
188   deadlock detection and claim a deadlock when progress can be
189   made. For those OSes we may loop for a while.
190 */
191 int tdb1_allrecord_upgrade(struct tdb1_context *tdb)
192 {
193         int count = 1000;
194
195         if (tdb->allrecord_lock.count != 1) {
196                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR,
197                          "tdb1_allrecord_upgrade failed: count %u too high\n",
198                          tdb->allrecord_lock.count));
199                 return -1;
200         }
201
202         if (tdb->allrecord_lock.off != 1) {
203                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR,
204                          "tdb1_allrecord_upgrade failed: already upgraded?\n"));
205                 return -1;
206         }
207
208         while (count--) {
209                 struct timeval tv;
210                 if (tdb1_brlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0,
211                                TDB1_LOCK_WAIT|TDB1_LOCK_PROBE) == 0) {
212                         tdb->allrecord_lock.ltype = F_WRLCK;
213                         tdb->allrecord_lock.off = 0;
214                         return 0;
215                 }
216                 if (errno != EDEADLK) {
217                         break;
218                 }
219                 /* sleep for as short a time as we can - more portable than usleep() */
220                 tv.tv_sec = 0;
221                 tv.tv_usec = 1;
222                 select(0, NULL, NULL, NULL, &tv);
223         }
224         TDB1_LOG((tdb, TDB1_DEBUG_TRACE,"tdb1_allrecord_upgrade failed\n"));
225         return -1;
226 }
227
228 static struct tdb1_lock_type *tdb1_find_nestlock(struct tdb1_context *tdb,
229                                                  tdb1_off_t offset)
230 {
231         unsigned int i;
232
233         for (i=0; i<tdb->num_lockrecs; i++) {
234                 if (tdb->lockrecs[i].off == offset) {
235                         return &tdb->lockrecs[i];
236                 }
237         }
238         return NULL;
239 }
240
241 /* lock an offset in the database. */
242 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
243                   enum tdb1_lock_flags flags)
244 {
245         struct tdb1_lock_type *new_lck;
246
247         if (offset >= lock_offset(tdb->header.hash_size)) {
248                 tdb->ecode = TDB1_ERR_LOCK;
249                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR,"tdb1_lock: invalid offset %u for ltype=%d\n",
250                          offset, ltype));
251                 return -1;
252         }
253         if (tdb->flags & TDB1_NOLOCK)
254                 return 0;
255
256         new_lck = tdb1_find_nestlock(tdb, offset);
257         if (new_lck) {
258                 /*
259                  * Just increment the in-memory struct, posix locks
260                  * don't stack.
261                  */
262                 new_lck->count++;
263                 return 0;
264         }
265
266         new_lck = (struct tdb1_lock_type *)realloc(
267                 tdb->lockrecs,
268                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
269         if (new_lck == NULL) {
270                 errno = ENOMEM;
271                 return -1;
272         }
273         tdb->lockrecs = new_lck;
274
275         /* Since fcntl locks don't nest, we do a lock for the first one,
276            and simply bump the count for future ones */
277         if (tdb1_brlock(tdb, ltype, offset, 1, flags)) {
278                 return -1;
279         }
280
281         tdb->lockrecs[tdb->num_lockrecs].off = offset;
282         tdb->lockrecs[tdb->num_lockrecs].count = 1;
283         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
284         tdb->num_lockrecs++;
285
286         return 0;
287 }
288
289 static int tdb1_lock_and_recover(struct tdb1_context *tdb)
290 {
291         int ret;
292
293         /* We need to match locking order in transaction commit. */
294         if (tdb1_brlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0, TDB1_LOCK_WAIT)) {
295                 return -1;
296         }
297
298         if (tdb1_brlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1, TDB1_LOCK_WAIT)) {
299                 tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
300                 return -1;
301         }
302
303         ret = tdb1_transaction_recover(tdb);
304
305         tdb1_brunlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1);
306         tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
307
308         return ret;
309 }
310
311 static bool have_data_locks(const struct tdb1_context *tdb)
312 {
313         unsigned int i;
314
315         for (i = 0; i < tdb->num_lockrecs; i++) {
316                 if (tdb->lockrecs[i].off >= lock_offset(-1))
317                         return true;
318         }
319         return false;
320 }
321
322 static int tdb1_lock_list(struct tdb1_context *tdb, int list, int ltype,
323                          enum tdb1_lock_flags waitflag)
324 {
325         int ret;
326         bool check = false;
327
328         /* a allrecord lock allows us to avoid per chain locks */
329         if (tdb->allrecord_lock.count &&
330             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
331                 return 0;
332         }
333
334         if (tdb->allrecord_lock.count) {
335                 tdb->ecode = TDB1_ERR_LOCK;
336                 ret = -1;
337         } else {
338                 /* Only check when we grab first data lock. */
339                 check = !have_data_locks(tdb);
340                 ret = tdb1_nest_lock(tdb, lock_offset(list), ltype, waitflag);
341
342                 if (ret == 0 && check && tdb1_needs_recovery(tdb)) {
343                         tdb1_nest_unlock(tdb, lock_offset(list), ltype);
344
345                         if (tdb1_lock_and_recover(tdb) == -1) {
346                                 return -1;
347                         }
348                         return tdb1_lock_list(tdb, list, ltype, waitflag);
349                 }
350         }
351         return ret;
352 }
353
354 /* lock a list in the database. list -1 is the alloc list */
355 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype)
356 {
357         int ret;
358
359         ret = tdb1_lock_list(tdb, list, ltype, TDB1_LOCK_WAIT);
360         if (ret) {
361                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_lock failed on list %d "
362                          "ltype=%d (%s)\n",  list, ltype, strerror(errno)));
363         }
364         return ret;
365 }
366
367 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype)
368 {
369         int ret = -1;
370         struct tdb1_lock_type *lck;
371
372         if (tdb->flags & TDB1_NOLOCK)
373                 return 0;
374
375         /* Sanity checks */
376         if (offset >= lock_offset(tdb->header.hash_size)) {
377                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_unlock: offset %u invalid (%d)\n", offset, tdb->header.hash_size));
378                 return ret;
379         }
380
381         lck = tdb1_find_nestlock(tdb, offset);
382         if ((lck == NULL) || (lck->count == 0)) {
383                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_unlock: count is 0\n"));
384                 return -1;
385         }
386
387         if (lck->count > 1) {
388                 lck->count--;
389                 return 0;
390         }
391
392         /*
393          * This lock has count==1 left, so we need to unlock it in the
394          * kernel. We don't bother with decrementing the in-memory array
395          * element, we're about to overwrite it with the last array element
396          * anyway.
397          */
398
399         ret = tdb1_brunlock(tdb, ltype, offset, 1);
400
401         /*
402          * Shrink the array by overwriting the element just unlocked with the
403          * last array element.
404          */
405         *lck = tdb->lockrecs[--tdb->num_lockrecs];
406
407         /*
408          * We don't bother with realloc when the array shrinks, but if we have
409          * a completely idle tdb we should get rid of the locked array.
410          */
411
412         if (tdb->num_lockrecs == 0) {
413                 SAFE_FREE(tdb->lockrecs);
414         }
415
416         if (ret)
417                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_unlock: An error occurred unlocking!\n"));
418         return ret;
419 }
420
421 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype)
422 {
423         /* a global lock allows us to avoid per chain locks */
424         if (tdb->allrecord_lock.count &&
425             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
426                 return 0;
427         }
428
429         if (tdb->allrecord_lock.count) {
430                 tdb->ecode = TDB1_ERR_LOCK;
431                 return -1;
432         }
433
434         return tdb1_nest_unlock(tdb, lock_offset(list), ltype);
435 }
436
437 /*
438   get the transaction lock
439  */
440 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
441                          enum tdb1_lock_flags lockflags)
442 {
443         return tdb1_nest_lock(tdb, TDB1_TRANSACTION_LOCK, ltype, lockflags);
444 }
445
446 /*
447   release the transaction lock
448  */
449 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype)
450 {
451         return tdb1_nest_unlock(tdb, TDB1_TRANSACTION_LOCK, ltype);
452 }
453
454 /* Returns 0 if all done, -1 if error, 1 if ok. */
455 static int tdb1_allrecord_check(struct tdb1_context *tdb, int ltype,
456                                enum tdb1_lock_flags flags, bool upgradable)
457 {
458         /* There are no locks on read-only dbs */
459         if (tdb->read_only || tdb->traverse_read) {
460                 tdb->ecode = TDB1_ERR_LOCK;
461                 return -1;
462         }
463
464         if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
465                 tdb->allrecord_lock.count++;
466                 return 0;
467         }
468
469         if (tdb->allrecord_lock.count) {
470                 /* a global lock of a different type exists */
471                 tdb->ecode = TDB1_ERR_LOCK;
472                 return -1;
473         }
474
475         if (tdb1_have_extra_locks(tdb)) {
476                 /* can't combine global and chain locks */
477                 tdb->ecode = TDB1_ERR_LOCK;
478                 return -1;
479         }
480
481         if (upgradable && ltype != F_RDLCK) {
482                 /* tdb error: you can't upgrade a write lock! */
483                 tdb->ecode = TDB1_ERR_LOCK;
484                 return -1;
485         }
486         return 1;
487 }
488
489 /* We only need to lock individual bytes, but Linux merges consecutive locks
490  * so we lock in contiguous ranges. */
491 static int tdb1_chainlock_gradual(struct tdb1_context *tdb,
492                                  int ltype, enum tdb1_lock_flags flags,
493                                  size_t off, size_t len)
494 {
495         int ret;
496         enum tdb1_lock_flags nb_flags = (flags & ~TDB1_LOCK_WAIT);
497
498         if (len <= 4) {
499                 /* Single record.  Just do blocking lock. */
500                 return tdb1_brlock(tdb, ltype, off, len, flags);
501         }
502
503         /* First we try non-blocking. */
504         ret = tdb1_brlock(tdb, ltype, off, len, nb_flags);
505         if (ret == 0) {
506                 return 0;
507         }
508
509         /* Try locking first half, then second. */
510         ret = tdb1_chainlock_gradual(tdb, ltype, flags, off, len / 2);
511         if (ret == -1)
512                 return -1;
513
514         ret = tdb1_chainlock_gradual(tdb, ltype, flags,
515                                     off + len / 2, len - len / 2);
516         if (ret == -1) {
517                 tdb1_brunlock(tdb, ltype, off, len / 2);
518                 return -1;
519         }
520         return 0;
521 }
522
523 /* lock/unlock entire database.  It can only be upgradable if you have some
524  * other way of guaranteeing exclusivity (ie. transaction write lock).
525  * We do the locking gradually to avoid being starved by smaller locks. */
526 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
527                        enum tdb1_lock_flags flags, bool upgradable)
528 {
529         switch (tdb1_allrecord_check(tdb, ltype, flags, upgradable)) {
530         case -1:
531                 return -1;
532         case 0:
533                 return 0;
534         }
535
536         /* We cover two kinds of locks:
537          * 1) Normal chain locks.  Taken for almost all operations.
538          * 3) Individual records locks.  Taken after normal or free
539          *    chain locks.
540          *
541          * It is (1) which cause the starvation problem, so we're only
542          * gradual for that. */
543         if (tdb1_chainlock_gradual(tdb, ltype, flags, TDB1_FREELIST_TOP,
544                                   tdb->header.hash_size * 4) == -1) {
545                 return -1;
546         }
547
548         /* Grab individual record locks. */
549         if (tdb1_brlock(tdb, ltype, lock_offset(tdb->header.hash_size), 0,
550                        flags) == -1) {
551                 tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP,
552                              tdb->header.hash_size * 4);
553                 return -1;
554         }
555
556         tdb->allrecord_lock.count = 1;
557         /* If it's upgradable, it's actually exclusive so we can treat
558          * it as a write lock. */
559         tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
560         tdb->allrecord_lock.off = upgradable;
561
562         if (tdb1_needs_recovery(tdb)) {
563                 tdb1_allrecord_unlock(tdb, ltype);
564                 if (tdb1_lock_and_recover(tdb) == -1) {
565                         return -1;
566                 }
567                 return tdb1_allrecord_lock(tdb, ltype, flags, upgradable);
568         }
569
570         return 0;
571 }
572
573
574
575 /* unlock entire db */
576 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype)
577 {
578         /* There are no locks on read-only dbs */
579         if (tdb->read_only || tdb->traverse_read) {
580                 tdb->ecode = TDB1_ERR_LOCK;
581                 return -1;
582         }
583
584         if (tdb->allrecord_lock.count == 0) {
585                 tdb->ecode = TDB1_ERR_LOCK;
586                 return -1;
587         }
588
589         /* Upgradable locks are marked as write locks. */
590         if (tdb->allrecord_lock.ltype != ltype
591             && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
592                 tdb->ecode = TDB1_ERR_LOCK;
593                 return -1;
594         }
595
596         if (tdb->allrecord_lock.count > 1) {
597                 tdb->allrecord_lock.count--;
598                 return 0;
599         }
600
601         if (tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP, 0)) {
602                 TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_unlockall failed (%s)\n", strerror(errno)));
603                 return -1;
604         }
605
606         tdb->allrecord_lock.count = 0;
607         tdb->allrecord_lock.ltype = 0;
608
609         return 0;
610 }
611
612 /* lock entire database with write lock */
613 int tdb1_lockall(struct tdb1_context *tdb)
614 {
615         return tdb1_allrecord_lock(tdb, F_WRLCK, TDB1_LOCK_WAIT, false);
616 }
617
618 /* unlock entire database with write lock */
619 int tdb1_unlockall(struct tdb1_context *tdb)
620 {
621         return tdb1_allrecord_unlock(tdb, F_WRLCK);
622 }
623
624 /* lock entire database with read lock */
625 int tdb1_lockall_read(struct tdb1_context *tdb)
626 {
627         return tdb1_allrecord_lock(tdb, F_RDLCK, TDB1_LOCK_WAIT, false);
628 }
629
630 /* unlock entire database with read lock */
631 int tdb1_unlockall_read(struct tdb1_context *tdb)
632 {
633         return tdb1_allrecord_unlock(tdb, F_RDLCK);
634 }
635
636 /* lock/unlock one hash chain. This is meant to be used to reduce
637    contention - it cannot guarantee how many records will be locked */
638 int tdb1_chainlock(struct tdb1_context *tdb, TDB1_DATA key)
639 {
640         int ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
641         return ret;
642 }
643
644 int tdb1_chainunlock(struct tdb1_context *tdb, TDB1_DATA key)
645 {
646         return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
647 }
648
649 int tdb1_chainlock_read(struct tdb1_context *tdb, TDB1_DATA key)
650 {
651         int ret;
652         ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
653         return ret;
654 }
655
656 int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB1_DATA key)
657 {
658         return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
659 }
660
661 /* record lock stops delete underneath */
662 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
663 {
664         if (tdb->allrecord_lock.count) {
665                 return 0;
666         }
667         return off ? tdb1_brlock(tdb, F_RDLCK, off, 1, TDB1_LOCK_WAIT) : 0;
668 }
669
670 /*
671   Write locks override our own fcntl readlocks, so check it here.
672   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
673   an error to fail to get the lock here.
674 */
675 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
676 {
677         struct tdb1_traverse_lock *i;
678         for (i = &tdb->travlocks; i; i = i->next)
679                 if (i->off == off)
680                         return -1;
681         if (tdb->allrecord_lock.count) {
682                 if (tdb->allrecord_lock.ltype == F_WRLCK) {
683                         return 0;
684                 }
685                 return -1;
686         }
687         return tdb1_brlock(tdb, F_WRLCK, off, 1, TDB1_LOCK_NOWAIT|TDB1_LOCK_PROBE);
688 }
689
690 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
691 {
692         if (tdb->allrecord_lock.count) {
693                 return 0;
694         }
695         return tdb1_brunlock(tdb, F_WRLCK, off, 1);
696 }
697
698 /* fcntl locks don't stack: avoid unlocking someone else's */
699 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
700 {
701         struct tdb1_traverse_lock *i;
702         uint32_t count = 0;
703
704         if (tdb->allrecord_lock.count) {
705                 return 0;
706         }
707
708         if (off == 0)
709                 return 0;
710         for (i = &tdb->travlocks; i; i = i->next)
711                 if (i->off == off)
712                         count++;
713         return (count == 1 ? tdb1_brunlock(tdb, F_RDLCK, off, 1) : 0);
714 }
715
716 bool tdb1_have_extra_locks(struct tdb1_context *tdb)
717 {
718         unsigned int extra = tdb->num_lockrecs;
719
720         /* A transaction holds the lock for all records. */
721         if (!tdb->transaction && tdb->allrecord_lock.count) {
722                 return true;
723         }
724
725         /* We always hold the active lock if CLEAR_IF_FIRST. */
726         if (tdb1_find_nestlock(tdb, TDB1_ACTIVE_LOCK)) {
727                 extra--;
728         }
729
730         /* In a transaction, we expect to hold the transaction lock */
731         if (tdb->transaction
732             && tdb1_find_nestlock(tdb, TDB1_TRANSACTION_LOCK)) {
733                 extra--;
734         }
735
736         return extra;
737 }
738
739 /* The transaction code uses this to remove all locks. */
740 void tdb1_release_transaction_locks(struct tdb1_context *tdb)
741 {
742         unsigned int i, active = 0;
743
744         if (tdb->allrecord_lock.count != 0) {
745                 tdb1_brunlock(tdb, tdb->allrecord_lock.ltype, TDB1_FREELIST_TOP, 0);
746                 tdb->allrecord_lock.count = 0;
747         }
748
749         for (i=0;i<tdb->num_lockrecs;i++) {
750                 struct tdb1_lock_type *lck = &tdb->lockrecs[i];
751
752                 /* Don't release the active lock!  Copy it to first entry. */
753                 if (lck->off == TDB1_ACTIVE_LOCK) {
754                         tdb->lockrecs[active++] = *lck;
755                 } else {
756                         tdb1_brunlock(tdb, lck->ltype, lck->off, 1);
757                 }
758         }
759         tdb->num_lockrecs = active;
760         if (tdb->num_lockrecs == 0) {
761                 SAFE_FREE(tdb->lockrecs);
762         }
763 }