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