tdb2: transaction support
[ccan] / ccan / tdb2 / 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 "private.h"
29 #include <assert.h>
30 #include <ccan/build_assert/build_assert.h>
31
32 static int fcntl_lock(struct tdb_context *tdb,
33                       int rw, off_t off, off_t len, bool waitflag)
34 {
35         struct flock fl;
36
37         fl.l_type = rw;
38         fl.l_whence = SEEK_SET;
39         fl.l_start = off;
40         fl.l_len = len;
41         fl.l_pid = 0;
42
43         if (waitflag)
44                 return fcntl(tdb->fd, F_SETLKW, &fl);
45         else
46                 return fcntl(tdb->fd, F_SETLK, &fl);
47 }
48
49 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
50 {
51         struct flock fl;
52 #if 0 /* Check they matched up locks and unlocks correctly. */
53         char line[80];
54         FILE *locks;
55         bool found = false;
56
57         locks = fopen("/proc/locks", "r");
58
59         while (fgets(line, 80, locks)) {
60                 char *p;
61                 int type, start, l;
62
63                 /* eg. 1: FLOCK  ADVISORY  WRITE 2440 08:01:2180826 0 EOF */
64                 p = strchr(line, ':') + 1;
65                 if (strncmp(p, " POSIX  ADVISORY  ", strlen(" POSIX  ADVISORY  ")))
66                         continue;
67                 p += strlen(" FLOCK  ADVISORY  ");
68                 if (strncmp(p, "READ  ", strlen("READ  ")) == 0)
69                         type = F_RDLCK;
70                 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
71                         type = F_WRLCK;
72                 else
73                         abort();
74                 p += 6;
75                 if (atoi(p) != getpid())
76                         continue;
77                 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
78                 start = atoi(p);
79                 p = strchr(p, ' ') + 1;
80                 if (strncmp(p, "EOF", 3) == 0)
81                         l = 0;
82                 else
83                         l = atoi(p) - start + 1;
84
85                 if (off == start) {
86                         if (len != l) {
87                                 fprintf(stderr, "Len %u should be %u: %s",
88                                         (int)len, l, line);
89                                 abort();
90                         }
91                         if (type != rw) {
92                                 fprintf(stderr, "Type %s wrong: %s",
93                                         rw == F_RDLCK ? "READ" : "WRITE", line);
94                                 abort();
95                         }
96                         found = true;
97                         break;
98                 }
99         }
100
101         if (!found) {
102                 fprintf(stderr, "Unlock on %u@%u not found!\n",
103                         (int)off, (int)len);
104                 abort();
105         }
106
107         fclose(locks);
108 #endif
109
110         fl.l_type = F_UNLCK;
111         fl.l_whence = SEEK_SET;
112         fl.l_start = off;
113         fl.l_len = len;
114         fl.l_pid = 0;
115
116         return fcntl(tdb->fd, F_SETLKW, &fl);
117 }
118
119 /* a byte range locking function - return 0 on success
120    this functions locks/unlocks 1 byte at the specified offset.
121
122    note that a len of zero means lock to end of file
123 */
124 static int tdb_brlock(struct tdb_context *tdb,
125                       int rw_type, tdb_off_t offset, tdb_off_t len,
126                       enum tdb_lock_flags flags)
127 {
128         int ret;
129
130         if (tdb->flags & TDB_NOLOCK) {
131                 return 0;
132         }
133
134         if (rw_type == F_WRLCK && tdb->read_only) {
135                 tdb->ecode = TDB_ERR_RDONLY;
136                 return -1;
137         }
138
139         /* A 32 bit system cannot open a 64-bit file, but it could have
140          * expanded since then: check here. */
141         if ((size_t)(offset + len) != offset + len) {
142                 tdb->ecode = TDB_ERR_IO;
143                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
144                          "tdb_brlock: lock on giant offset %llu\n",
145                          (long long)(offset + len));
146                 return -1;
147         }
148
149         do {
150                 ret = fcntl_lock(tdb, rw_type, offset, len,
151                                  flags & TDB_LOCK_WAIT);
152         } while (ret == -1 && errno == EINTR);
153
154         if (ret == -1) {
155                 tdb->ecode = TDB_ERR_LOCK;
156                 /* Generic lock error. errno set by fcntl.
157                  * EAGAIN is an expected return from non-blocking
158                  * locks. */
159                 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
160                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
161                                  "tdb_brlock failed (fd=%d) at"
162                                  " offset %llu rw_type=%d flags=%d len=%llu\n",
163                                  tdb->fd, (long long)offset, rw_type,
164                                  flags, (long long)len);
165                 }
166                 return -1;
167         }
168         return 0;
169 }
170
171 static int tdb_brunlock(struct tdb_context *tdb,
172                         int rw_type, tdb_off_t offset, size_t len)
173 {
174         int ret;
175
176         if (tdb->flags & TDB_NOLOCK) {
177                 return 0;
178         }
179
180         do {
181                 ret = fcntl_unlock(tdb, rw_type, offset, len);
182         } while (ret == -1 && errno == EINTR);
183
184         if (ret == -1) {
185                 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
186                          "tdb_brunlock failed (fd=%d) at offset %llu"
187                          " rw_type=%d len=%llu\n",
188                          tdb->fd, (long long)offset, rw_type, (long long)len);
189         }
190         return ret;
191 }
192
193 /*
194   upgrade a read lock to a write lock. This needs to be handled in a
195   special way as some OSes (such as solaris) have too conservative
196   deadlock detection and claim a deadlock when progress can be
197   made. For those OSes we may loop for a while.  
198 */
199 int tdb_allrecord_upgrade(struct tdb_context *tdb)
200 {
201         int count = 1000;
202
203         if (tdb->allrecord_lock.count != 1) {
204                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
205                          "tdb_allrecord_upgrade failed: count %u too high\n",
206                          tdb->allrecord_lock.count);
207                 return -1;
208         }
209
210         if (tdb->allrecord_lock.off != 1) {
211                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
212                          "tdb_allrecord_upgrade failed: already upgraded?\n");
213                 return -1;
214         }
215
216         while (count--) {
217                 struct timeval tv;
218                 if (tdb_brlock(tdb, F_WRLCK,
219                                TDB_HASH_LOCK_START, 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_WARNING, tdb->log_priv,
234                  "tdb_allrecord_upgrade failed\n");
235         return -1;
236 }
237
238 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
239                                            tdb_off_t offset)
240 {
241         unsigned int i;
242
243         for (i=0; i<tdb->num_lockrecs; i++) {
244                 if (tdb->lockrecs[i].off == offset) {
245                         return &tdb->lockrecs[i];
246                 }
247         }
248         return NULL;
249 }
250
251 int tdb_lock_and_recover(struct tdb_context *tdb)
252 {
253         int ret;
254
255         if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
256                                false) == -1) {
257                 return -1;
258         }
259
260         if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
261                 tdb_allrecord_unlock(tdb, F_WRLCK);
262                 return -1;
263         }
264         ret = tdb_transaction_recover(tdb);
265
266         tdb_unlock_open(tdb);
267         tdb_allrecord_unlock(tdb, F_WRLCK);
268
269         return ret;
270 }
271
272 /* lock an offset in the database. */
273 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
274                          enum tdb_lock_flags flags)
275 {
276         struct tdb_lock_type *new_lck;
277
278         if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
279                 tdb->ecode = TDB_ERR_LOCK;
280                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
281                          "tdb_nest_lock: invalid offset %llu ltype=%d\n",
282                          (long long)offset, ltype);
283                 return -1;
284         }
285
286         if (tdb->flags & TDB_NOLOCK)
287                 return 0;
288
289         new_lck = find_nestlock(tdb, offset);
290         if (new_lck) {
291                 if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
292                         tdb->ecode = TDB_ERR_LOCK;
293                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
294                                  "tdb_nest_lock: offset %llu has read lock\n",
295                                  (long long)offset);
296                         return -1;
297                 }
298                 /* Just increment the struct, posix locks don't stack. */
299                 new_lck->count++;
300                 return 0;
301         }
302
303         if (tdb->num_lockrecs
304             && offset >= TDB_HASH_LOCK_START
305             && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
306                 tdb->ecode = TDB_ERR_LOCK;
307                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
308                          "tdb_nest_lock: already have a hash lock?\n");
309                 return -1;
310         }
311
312         new_lck = (struct tdb_lock_type *)realloc(
313                 tdb->lockrecs,
314                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
315         if (new_lck == NULL) {
316                 tdb->ecode = TDB_ERR_OOM;
317                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
318                          "tdb_nest_lock: unable to allocate %llu lock struct",
319                          (long long)(tdb->num_lockrecs + 1));
320                 errno = ENOMEM;
321                 return -1;
322         }
323         tdb->lockrecs = new_lck;
324
325         /* Since fcntl locks don't nest, we do a lock for the first one,
326            and simply bump the count for future ones */
327         if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
328                 return -1;
329         }
330
331         /* First time we grab a lock, perhaps someone died in commit? */
332         if (!(flags & TDB_LOCK_NOCHECK)
333             && tdb->num_lockrecs == 0
334             && unlikely(tdb_needs_recovery(tdb))) {
335                 tdb_brunlock(tdb, ltype, offset, 1);
336
337                 if (tdb_lock_and_recover(tdb) == -1) {
338                         return -1;
339                 }
340
341                 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
342                         return -1;
343                 }
344         }
345
346         tdb->lockrecs[tdb->num_lockrecs].off = offset;
347         tdb->lockrecs[tdb->num_lockrecs].count = 1;
348         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
349         tdb->num_lockrecs++;
350
351         return 0;
352 }
353
354 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
355 {
356         int ret = -1;
357         struct tdb_lock_type *lck;
358
359         if (tdb->flags & TDB_NOLOCK)
360                 return 0;
361
362         lck = find_nestlock(tdb, off);
363         if ((lck == NULL) || (lck->count == 0)) {
364                 tdb->ecode = TDB_ERR_LOCK;
365                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
366                          "tdb_nest_unlock: no lock for %llu\n", (long long)off);
367                 return -1;
368         }
369
370         if (lck->count > 1) {
371                 lck->count--;
372                 return 0;
373         }
374
375         /*
376          * This lock has count==1 left, so we need to unlock it in the
377          * kernel. We don't bother with decrementing the in-memory array
378          * element, we're about to overwrite it with the last array element
379          * anyway.
380          */
381         ret = tdb_brunlock(tdb, ltype, off, 1);
382
383         /*
384          * Shrink the array by overwriting the element just unlocked with the
385          * last array element.
386          */
387         *lck = tdb->lockrecs[--tdb->num_lockrecs];
388
389         return ret;
390 }
391
392 /*
393   get the transaction lock
394  */
395 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
396 {
397         return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
398 }
399
400 /*
401   release the transaction lock
402  */
403 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
404 {
405         return tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
406 }
407
408 /* We only need to lock individual bytes, but Linux merges consecutive locks
409  * so we lock in contiguous ranges. */
410 static int tdb_lock_gradual(struct tdb_context *tdb,
411                             int ltype, enum tdb_lock_flags flags,
412                             tdb_off_t off, tdb_off_t len)
413 {
414         int ret;
415         enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
416
417         if (len <= 1) {
418                 /* 0 would mean to end-of-file... */
419                 assert(len != 0);
420                 /* Single hash.  Just do blocking lock. */
421                 return tdb_brlock(tdb, ltype, off, len, flags);
422         }
423
424         /* First we try non-blocking. */
425         ret = tdb_brlock(tdb, ltype, off, len, nb_flags);
426         if (ret == 0) {
427                 return 0;
428         }
429
430         /* Try locking first half, then second. */
431         ret = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
432         if (ret == -1)
433                 return -1;
434
435         ret = tdb_lock_gradual(tdb, ltype, flags,
436                                     off + len / 2, len - len / 2);
437         if (ret == -1) {
438                 tdb_brunlock(tdb, ltype, off, len / 2);
439                 return -1;
440         }
441         return 0;
442 }
443
444 /* lock/unlock entire database.  It can only be upgradable if you have some
445  * other way of guaranteeing exclusivity (ie. transaction write lock). */
446 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
447                        enum tdb_lock_flags flags, bool upgradable)
448 {
449         /* FIXME: There are no locks on read-only dbs */
450         if (tdb->read_only) {
451                 tdb->ecode = TDB_ERR_LOCK;
452                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
453                          "tdb_allrecord_lock: read-only\n");
454                 return -1;
455         }
456
457         if (tdb->allrecord_lock.count
458             && (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) {
459                 tdb->allrecord_lock.count++;
460                 return 0;
461         }
462
463         if (tdb->allrecord_lock.count) {
464                 /* a global lock of a different type exists */
465                 tdb->ecode = TDB_ERR_LOCK;
466                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
467                          "tdb_allrecord_lock: already have %s lock\n",
468                          tdb->allrecord_lock.ltype == F_RDLCK
469                          ? "read" : "write");
470                 return -1;
471         }
472
473         if (tdb_has_hash_locks(tdb)) {
474                 /* can't combine global and chain locks */
475                 tdb->ecode = TDB_ERR_LOCK;
476                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
477                          "tdb_allrecord_lock: already have chain lock\n");
478                 return -1;
479         }
480
481         if (upgradable && ltype != F_RDLCK) {
482                 /* tdb error: you can't upgrade a write lock! */
483                 tdb->ecode = TDB_ERR_LOCK;
484                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
485                          "tdb_allrecord_lock: can't upgrade a write lock\n");
486                 return -1;
487         }
488
489 again:
490         /* Lock hashes, gradually. */
491         if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
492                              TDB_HASH_LOCK_RANGE)) {
493                 if (!(flags & TDB_LOCK_PROBE)) {
494                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
495                                  "tdb_allrecord_lock hashes failed (%s)\n",
496                                  strerror(errno));
497                 }
498                 return -1;
499         }
500
501         /* Lock free lists: there to end of file. */
502         if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
503                        0, flags)) {
504                 if (!(flags & TDB_LOCK_PROBE)) {
505                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
506                                  "tdb_allrecord_lock freelist failed (%s)\n",
507                                  strerror(errno));
508                 }
509                 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 
510                              TDB_HASH_LOCK_RANGE);
511                 return -1;
512         }
513
514         tdb->allrecord_lock.count = 1;
515         /* If it's upgradable, it's actually exclusive so we can treat
516          * it as a write lock. */
517         tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
518         tdb->allrecord_lock.off = upgradable;
519
520         /* Now check for needing recovery. */
521         if (!(flags & TDB_LOCK_NOCHECK) && unlikely(tdb_needs_recovery(tdb))) {
522                 tdb_allrecord_unlock(tdb, ltype);
523                 if (tdb_lock_and_recover(tdb) == -1) {
524                         return -1;
525                 }               
526                 goto again;
527         }
528
529         return 0;
530 }
531
532 int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
533 {
534         return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
535 }
536
537 void tdb_unlock_open(struct tdb_context *tdb)
538 {
539         tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
540 }
541
542 bool tdb_has_open_lock(struct tdb_context *tdb)
543 {
544         return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
545 }
546
547 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
548 {
549         /* Lock doesn't protect data, so don't check (we recurse if we do!) */
550         return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
551                              TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
552 }
553
554 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
555 {
556         tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
557 }
558
559 /* unlock entire db */
560 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
561 {
562         /* FIXME: There are no locks on read-only dbs */
563         if (tdb->read_only) {
564                 tdb->ecode = TDB_ERR_LOCK;
565                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
566                          "tdb_allrecord_unlock: read-only\n");
567                 return -1;
568         }
569
570         if (tdb->allrecord_lock.count == 0) {
571                 tdb->ecode = TDB_ERR_LOCK;
572                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
573                          "tdb_allrecord_unlock: not locked!\n");
574                 return -1;
575         }
576
577         /* Upgradable locks are marked as write locks. */
578         if (tdb->allrecord_lock.ltype != ltype
579             && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
580                 tdb->ecode = TDB_ERR_LOCK;
581                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
582                          "tdb_allrecord_unlock: have %s lock\n",
583                          tdb->allrecord_lock.ltype == F_RDLCK
584                          ? "read" : "write");
585                 return -1;
586         }
587
588         if (tdb->allrecord_lock.count > 1) {
589                 tdb->allrecord_lock.count--;
590                 return 0;
591         }
592
593         tdb->allrecord_lock.count = 0;
594         tdb->allrecord_lock.ltype = 0;
595
596         return tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
597 }
598
599 bool tdb_has_expansion_lock(struct tdb_context *tdb)
600 {
601         return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
602 }
603
604 bool tdb_has_hash_locks(struct tdb_context *tdb)
605 {
606         unsigned int i;
607
608         for (i=0; i<tdb->num_lockrecs; i++) {
609                 if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
610                     && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
611                                                + TDB_HASH_LOCK_RANGE))
612                         return true;
613         }
614         return false;
615 }
616
617 #if 0
618 /* lock entire database with write lock */
619 int tdb_lockall(struct tdb_context *tdb)
620 {
621         tdb_trace(tdb, "tdb_lockall");
622         return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
623 }
624
625 /* lock entire database with write lock - nonblocking varient */
626 int tdb_lockall_nonblock(struct tdb_context *tdb)
627 {
628         int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
629         tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
630         return ret;
631 }
632
633 /* unlock entire database with write lock */
634 int tdb_unlockall(struct tdb_context *tdb)
635 {
636         tdb_trace(tdb, "tdb_unlockall");
637         return tdb_allrecord_unlock(tdb, F_WRLCK);
638 }
639
640 /* lock entire database with read lock */
641 int tdb_lockall_read(struct tdb_context *tdb)
642 {
643         tdb_trace(tdb, "tdb_lockall_read");
644         return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
645 }
646
647 /* lock entire database with read lock - nonblock varient */
648 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
649 {
650         int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
651         tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
652         return ret;
653 }
654
655 /* unlock entire database with read lock */
656 int tdb_unlockall_read(struct tdb_context *tdb)
657 {
658         tdb_trace(tdb, "tdb_unlockall_read");
659         return tdb_allrecord_unlock(tdb, F_RDLCK);
660 }
661 #endif
662
663 static bool tdb_has_free_lock(struct tdb_context *tdb)
664 {
665         unsigned int i;
666
667         for (i=0; i<tdb->num_lockrecs; i++) {
668                 if (tdb->lockrecs[i].off
669                     > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
670                         return true;
671         }
672         return false;
673 }
674
675 int tdb_lock_hashes(struct tdb_context *tdb,
676                     tdb_off_t hash_lock,
677                     tdb_len_t hash_range,
678                     int ltype, enum tdb_lock_flags waitflag)
679 {
680         /* FIXME: Do this properly, using hlock_range */
681         unsigned lock = TDB_HASH_LOCK_START
682                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
683
684         /* a allrecord lock allows us to avoid per chain locks */
685         if (tdb->allrecord_lock.count &&
686             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
687                 return 0;
688         }
689
690         if (tdb->allrecord_lock.count) {
691                 tdb->ecode = TDB_ERR_LOCK;
692                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
693                          "tdb_lock_hashes: have %s allrecordlock\n",
694                          tdb->allrecord_lock.ltype == F_RDLCK
695                          ? "read" : "write");
696                 return -1;
697         }
698
699         if (tdb_has_free_lock(tdb)) {
700                 tdb->ecode = TDB_ERR_LOCK;
701                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
702                          "tdb_lock_hashes: have free lock already\n");
703                 return -1;
704         }
705
706         if (tdb_has_expansion_lock(tdb)) {
707                 tdb->ecode = TDB_ERR_LOCK;
708                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
709                          "tdb_lock_hashes: have expansion lock already\n");
710                 return -1;
711         }
712
713         return tdb_nest_lock(tdb, lock, ltype, waitflag);
714 }
715
716 int tdb_unlock_hashes(struct tdb_context *tdb,
717                       tdb_off_t hash_lock,
718                       tdb_len_t hash_range, int ltype)
719 {
720         unsigned lock = TDB_HASH_LOCK_START
721                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
722
723         /* a allrecord lock allows us to avoid per chain locks */
724         if (tdb->allrecord_lock.count) {
725                 if (tdb->allrecord_lock.ltype == F_RDLCK
726                     && ltype == F_WRLCK) {
727                         tdb->ecode = TDB_ERR_LOCK;
728                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
729                                  "tdb_unlock_hashes RO allrecord!\n");
730                         return -1;
731                 }
732                 return 0;
733         }
734
735         return tdb_nest_unlock(tdb, lock, ltype);
736 }
737
738 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
739  * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
740  * The result is that on 32 bit systems we don't use lock values > 2^31 on
741  * files that are less than 4GB.
742  */
743 static tdb_off_t free_lock_off(tdb_off_t b_off)
744 {
745         return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
746                 + b_off / sizeof(tdb_off_t);
747 }
748
749 int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
750                          enum tdb_lock_flags waitflag)
751 {
752         assert(b_off >= sizeof(struct tdb_header));
753
754         /* a allrecord lock allows us to avoid per chain locks */
755         if (tdb->allrecord_lock.count) {
756                 if (tdb->allrecord_lock.ltype == F_WRLCK)
757                         return 0;
758                 tdb->ecode = TDB_ERR_LOCK;
759                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
760                          "tdb_lock_free_bucket with RO allrecordlock!\n");
761                 return -1;
762         }
763
764 #if 0 /* FIXME */
765         if (tdb_has_expansion_lock(tdb)) {
766                 tdb->ecode = TDB_ERR_LOCK;
767                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
768                          "tdb_lock_free_bucket: have expansion lock already\n");
769                 return -1;
770         }
771 #endif
772
773         return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
774 }
775
776 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
777 {
778         if (tdb->allrecord_lock.count)
779                 return;
780
781         tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
782 }
783
784 #if 0
785 /* lock/unlock one hash chain, non-blocking. This is meant to be used
786    to reduce contention - it cannot guarantee how many records will be
787    locked */
788 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
789 {
790         return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT,
791                          "tdb_chainlock_nonblock");
792 }
793
794 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
795 {
796         return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
797                          "tdb_chainlock_read");
798 }
799
800 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
801 {
802         uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
803         tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
804         return tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1),
805                                F_RDLCK);
806 }
807
808 /* record lock stops delete underneath */
809 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
810 {
811         if (tdb->allrecord_lock.count) {
812                 return 0;
813         }
814         return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
815 }
816
817 /*
818   Write locks override our own fcntl readlocks, so check it here.
819   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
820   an error to fail to get the lock here.
821 */
822 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
823 {
824         struct tdb_traverse_lock *i;
825         for (i = &tdb->travlocks; i; i = i->next)
826                 if (i->off == off)
827                         return -1;
828         if (tdb->allrecord_lock.count) {
829                 if (tdb->allrecord_lock.ltype == F_WRLCK) {
830                         return 0;
831                 }
832                 return -1;
833         }
834         return tdb_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
835 }
836
837 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
838 {
839         if (tdb->allrecord_lock.count) {
840                 return 0;
841         }
842         return tdb_brunlock(tdb, F_WRLCK, off, 1);
843 }
844
845 /* fcntl locks don't stack: avoid unlocking someone else's */
846 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
847 {
848         struct tdb_traverse_lock *i;
849         uint32_t count = 0;
850
851         if (tdb->allrecord_lock.count) {
852                 return 0;
853         }
854
855         if (off == 0)
856                 return 0;
857         for (i = &tdb->travlocks; i; i = i->next)
858                 if (i->off == off)
859                         count++;
860         return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
861 }
862
863 /* The transaction code uses this to remove all locks. */
864 void tdb_release_transaction_locks(struct tdb_context *tdb)
865 {
866         unsigned int i;
867
868         if (tdb->allrecord_lock.count != 0) {
869                 tdb_off_t hash_size, free_size;
870
871                 hash_size = (1ULL << tdb->header.v.hash_bits)
872                         * sizeof(tdb_off_t);
873                 free_size = tdb->header.v.free_zones 
874                         * (tdb->header.v.free_buckets + 1) * sizeof(tdb_off_t);
875
876                 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
877                              tdb->header.v.hash_off, hash_size);
878                 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
879                              tdb->header.v.free_off, free_size);
880                 tdb->allrecord_lock.count = 0;
881                 tdb->allrecord_lock.ltype = 0;
882         }
883
884         for (i = 0; i<tdb->num_lockrecs; i++) {
885                 struct tdb_lock_type *lck = &tdb->lockrecs[i];
886
887                 tdb_brunlock(tdb, lck->ltype, lck->off, 1);
888         }
889         tdb->num_lockrecs = 0;
890         SAFE_FREE(tdb->lockrecs);
891         tdb->header_uptodate = false;
892 }
893 #endif
894
895 void tdb_lock_init(struct tdb_context *tdb)
896 {
897         tdb->num_lockrecs = 0;
898         tdb->lockrecs = NULL;
899         tdb->allrecord_lock.count = 0;
900 }