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