tdb2: fix tdb_chainlock
[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 #if 0
194 /*
195   upgrade a read lock to a write lock. This needs to be handled in a
196   special way as some OSes (such as solaris) have too conservative
197   deadlock detection and claim a deadlock when progress can be
198   made. For those OSes we may loop for a while.  
199 */
200 int tdb_allrecord_upgrade(struct tdb_context *tdb)
201 {
202         int count = 1000;
203
204         if (tdb->allrecord_lock.count != 1) {
205                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
206                          "tdb_allrecord_upgrade failed: count %u too high\n",
207                          tdb->allrecord_lock.count);
208                 return -1;
209         }
210
211         if (tdb->allrecord_lock.off != 1) {
212                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
213                          "tdb_allrecord_upgrade failed: already upgraded?\n");
214                 return -1;
215         }
216
217         while (count--) {
218                 struct timeval tv;
219                 if (tdb_brlock(tdb, F_WRLCK,
220                                TDB_HASH_LOCK_START
221                                + (1ULL << tdb->header.v.hash_bits), 0,
222                                TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
223                         tdb->allrecord_lock.ltype = F_WRLCK;
224                         tdb->allrecord_lock.off = 0;
225                         return 0;
226                 }
227                 if (errno != EDEADLK) {
228                         break;
229                 }
230                 /* sleep for as short a time as we can - more portable than usleep() */
231                 tv.tv_sec = 0;
232                 tv.tv_usec = 1;
233                 select(0, NULL, NULL, NULL, &tv);
234         }
235         tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
236                  "tdb_allrecord_upgrade failed\n");
237         return -1;
238 }
239 #endif
240
241 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
242                                            tdb_off_t offset)
243 {
244         unsigned int i;
245
246         for (i=0; i<tdb->num_lockrecs; i++) {
247                 if (tdb->lockrecs[i].off == offset) {
248                         return &tdb->lockrecs[i];
249                 }
250         }
251         return NULL;
252 }
253
254 /* lock an offset in the database. */
255 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
256                          enum tdb_lock_flags flags)
257 {
258         struct tdb_lock_type *new_lck;
259
260         if (offset >= TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
261                 tdb->ecode = TDB_ERR_LOCK;
262                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
263                          "tdb_nest_lock: invalid offset %llu ltype=%d\n",
264                          (long long)offset, ltype);
265                 return -1;
266         }
267
268         if (tdb->flags & TDB_NOLOCK)
269                 return 0;
270
271         new_lck = find_nestlock(tdb, offset);
272         if (new_lck) {
273                 /*
274                  * Just increment the in-memory struct, posix locks
275                  * don't stack.
276                  */
277                 new_lck->count++;
278                 return 0;
279         }
280
281         if (tdb->num_lockrecs
282             && offset >= TDB_HASH_LOCK_START
283             && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
284                 tdb->ecode = TDB_ERR_LOCK;
285                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
286                          "tdb_nest_lock: already have a hash lock?\n");
287                 return -1;
288         }
289
290         new_lck = (struct tdb_lock_type *)realloc(
291                 tdb->lockrecs,
292                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
293         if (new_lck == NULL) {
294                 tdb->ecode = TDB_ERR_OOM;
295                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
296                          "tdb_nest_lock: unable to allocate %llu lock struct",
297                          (long long)(tdb->num_lockrecs + 1));
298                 errno = ENOMEM;
299                 return -1;
300         }
301         tdb->lockrecs = new_lck;
302
303         /* Since fcntl locks don't nest, we do a lock for the first one,
304            and simply bump the count for future ones */
305         if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
306                 return -1;
307         }
308
309         tdb->lockrecs[tdb->num_lockrecs].off = offset;
310         tdb->lockrecs[tdb->num_lockrecs].count = 1;
311         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
312         tdb->num_lockrecs++;
313
314         return 0;
315 }
316
317 static int tdb_lock_and_recover(struct tdb_context *tdb)
318 {
319 #if 0 /* FIXME */
320
321         int ret;
322
323         /* We need to match locking order in transaction commit. */
324         if (tdb_brlock(tdb, F_WRLCK, FREELIST_TOP, 0, TDB_LOCK_WAIT)) {
325                 return -1;
326         }
327
328         if (tdb_brlock(tdb, F_WRLCK, OPEN_LOCK, 1, TDB_LOCK_WAIT)) {
329                 tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
330                 return -1;
331         }
332
333         ret = tdb_transaction_recover(tdb);
334
335         tdb_brunlock(tdb, F_WRLCK, OPEN_LOCK, 1);
336         tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
337
338         return ret;
339 #else
340         abort();
341         return -1;
342 #endif
343 }
344
345 static bool tdb_needs_recovery(struct tdb_context *tdb)
346 {
347         /* FIXME */
348         return false;
349 }
350
351 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
352 {
353         int ret = -1;
354         struct tdb_lock_type *lck;
355
356         if (tdb->flags & TDB_NOLOCK)
357                 return 0;
358
359         lck = find_nestlock(tdb, off);
360         if ((lck == NULL) || (lck->count == 0)) {
361                 tdb->ecode = TDB_ERR_LOCK;
362                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
363                          "tdb_nest_unlock: no lock for %llu\n", (long long)off);
364                 return -1;
365         }
366
367         if (lck->count > 1) {
368                 lck->count--;
369                 return 0;
370         }
371
372         /*
373          * This lock has count==1 left, so we need to unlock it in the
374          * kernel. We don't bother with decrementing the in-memory array
375          * element, we're about to overwrite it with the last array element
376          * anyway.
377          */
378         ret = tdb_brunlock(tdb, ltype, off, 1);
379
380         /*
381          * Shrink the array by overwriting the element just unlocked with the
382          * last array element.
383          */
384         *lck = tdb->lockrecs[--tdb->num_lockrecs];
385
386         return ret;
387 }
388
389 #if 0
390 /*
391   get the transaction lock
392  */
393 int tdb_transaction_lock(struct tdb_context *tdb, int ltype,
394                          enum tdb_lock_flags lockflags)
395 {
396         return tdb_nest_lock(tdb, TRANSACTION_LOCK, ltype, lockflags);
397 }
398
399 /*
400   release the transaction lock
401  */
402 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
403 {
404         return tdb_nest_unlock(tdb, TRANSACTION_LOCK, ltype, false);
405 }
406 #endif
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 && tdb->allrecord_lock.ltype == ltype) {
458                 tdb->allrecord_lock.count++;
459                 return 0;
460         }
461
462         if (tdb->allrecord_lock.count) {
463                 /* a global lock of a different type exists */
464                 tdb->ecode = TDB_ERR_LOCK;
465                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
466                          "tdb_allrecord_lock: already have %s lock\n",
467                          tdb->allrecord_lock.ltype == F_RDLCK
468                          ? "read" : "write");
469                 return -1;
470         }
471
472         if (tdb_has_locks(tdb)) {
473                 /* can't combine global and chain locks */
474                 tdb->ecode = TDB_ERR_LOCK;
475                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
476                          "tdb_allrecord_lock: already have chain lock\n");
477                 return -1;
478         }
479
480         if (upgradable && ltype != F_RDLCK) {
481                 /* tdb error: you can't upgrade a write lock! */
482                 tdb->ecode = TDB_ERR_LOCK;
483                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
484                          "tdb_allrecord_lock: can't upgrade a write lock\n");
485                 return -1;
486         }
487
488 again:
489         /* Lock hashes, gradually. */
490         if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
491                              TDB_HASH_LOCK_RANGE)) {
492                 if (!(flags & TDB_LOCK_PROBE)) {
493                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
494                                  "tdb_allrecord_lock hashes failed (%s)\n",
495                                  strerror(errno));
496                 }
497                 return -1;
498         }
499
500         /* Lock free lists: there to end of file. */
501         if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
502                        0, flags)) {
503                 if (!(flags & TDB_LOCK_PROBE)) {
504                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
505                                  "tdb_allrecord_lock freelist failed (%s)\n",
506                                  strerror(errno));
507                 }
508                 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 
509                              TDB_HASH_LOCK_RANGE);
510                 return -1;
511         }
512
513         tdb->allrecord_lock.count = 1;
514         /* If it's upgradable, it's actually exclusive so we can treat
515          * it as a write lock. */
516         tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
517         tdb->allrecord_lock.off = upgradable;
518
519         /* Now check for needing recovery. */
520         if (unlikely(tdb_needs_recovery(tdb))) {
521                 tdb_allrecord_unlock(tdb, ltype);
522                 if (tdb_lock_and_recover(tdb) == -1) {
523                         return -1;
524                 }               
525                 goto again;
526         }
527
528         return 0;
529 }
530
531 int tdb_lock_open(struct tdb_context *tdb)
532 {
533         return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT);
534 }
535
536 void tdb_unlock_open(struct tdb_context *tdb)
537 {
538         tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
539 }
540
541 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
542 {
543         return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype, TDB_LOCK_WAIT);
544 }
545
546 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
547 {
548         tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
549 }
550
551 /* unlock entire db */
552 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
553 {
554         /* FIXME: There are no locks on read-only dbs */
555         if (tdb->read_only) {
556                 tdb->ecode = TDB_ERR_LOCK;
557                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
558                          "tdb_allrecord_unlock: read-only\n");
559                 return -1;
560         }
561
562         if (tdb->allrecord_lock.count == 0) {
563                 tdb->ecode = TDB_ERR_LOCK;
564                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
565                          "tdb_allrecord_unlock: not locked!\n");
566                 return -1;
567         }
568
569         /* Upgradable locks are marked as write locks. */
570         if (tdb->allrecord_lock.ltype != ltype
571             && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
572                 tdb->ecode = TDB_ERR_LOCK;
573                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
574                          "tdb_allrecord_unlock: have %s lock\n",
575                          tdb->allrecord_lock.ltype == F_RDLCK
576                          ? "read" : "write");
577                 return -1;
578         }
579
580         if (tdb->allrecord_lock.count > 1) {
581                 tdb->allrecord_lock.count--;
582                 return 0;
583         }
584
585         tdb->allrecord_lock.count = 0;
586         tdb->allrecord_lock.ltype = 0;
587
588         return tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
589 }
590
591 bool tdb_has_expansion_lock(struct tdb_context *tdb)
592 {
593         return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
594 }
595
596 bool tdb_has_locks(struct tdb_context *tdb)
597 {
598         return tdb->allrecord_lock.count || tdb->num_lockrecs;
599 }
600
601 #if 0
602 /* lock entire database with write lock */
603 int tdb_lockall(struct tdb_context *tdb)
604 {
605         tdb_trace(tdb, "tdb_lockall");
606         return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
607 }
608
609 /* lock entire database with write lock - nonblocking varient */
610 int tdb_lockall_nonblock(struct tdb_context *tdb)
611 {
612         int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
613         tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
614         return ret;
615 }
616
617 /* unlock entire database with write lock */
618 int tdb_unlockall(struct tdb_context *tdb)
619 {
620         tdb_trace(tdb, "tdb_unlockall");
621         return tdb_allrecord_unlock(tdb, F_WRLCK);
622 }
623
624 /* lock entire database with read lock */
625 int tdb_lockall_read(struct tdb_context *tdb)
626 {
627         tdb_trace(tdb, "tdb_lockall_read");
628         return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
629 }
630
631 /* lock entire database with read lock - nonblock varient */
632 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
633 {
634         int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
635         tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
636         return ret;
637 }
638
639 /* unlock entire database with read lock */
640 int tdb_unlockall_read(struct tdb_context *tdb)
641 {
642         tdb_trace(tdb, "tdb_unlockall_read");
643         return tdb_allrecord_unlock(tdb, F_RDLCK);
644 }
645 #endif
646
647 static bool tdb_has_free_lock(struct tdb_context *tdb)
648 {
649         unsigned int i;
650
651         for (i=0; i<tdb->num_lockrecs; i++) {
652                 if (tdb->lockrecs[i].off
653                     > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
654                         return true;
655         }
656         return false;
657 }
658
659 int tdb_lock_hashes(struct tdb_context *tdb,
660                     tdb_off_t hash_lock,
661                     tdb_len_t hash_range,
662                     int ltype, enum tdb_lock_flags waitflag)
663 {
664         /* FIXME: Do this properly, using hlock_range */
665         unsigned lock = TDB_HASH_LOCK_START
666                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
667
668         /* a allrecord lock allows us to avoid per chain locks */
669         if (tdb->allrecord_lock.count &&
670             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
671                 return 0;
672         }
673
674         if (tdb->allrecord_lock.count) {
675                 tdb->ecode = TDB_ERR_LOCK;
676                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
677                          "tdb_lock_hashes: have %s allrecordlock\n",
678                          tdb->allrecord_lock.ltype == F_RDLCK
679                          ? "read" : "write");
680                 return -1;
681         }
682
683         if (tdb_has_free_lock(tdb)) {
684                 tdb->ecode = TDB_ERR_LOCK;
685                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
686                          "tdb_lock_hashes: have free lock already\n");
687                 return -1;
688         }
689
690         if (tdb_has_expansion_lock(tdb)) {
691                 tdb->ecode = TDB_ERR_LOCK;
692                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
693                          "tdb_lock_hashes: have expansion lock already\n");
694                 return -1;
695         }
696
697         return tdb_nest_lock(tdb, lock, ltype, waitflag);
698 }
699
700 int tdb_unlock_hashes(struct tdb_context *tdb,
701                       tdb_off_t hash_lock,
702                       tdb_len_t hash_range, int ltype)
703 {
704         unsigned lock = TDB_HASH_LOCK_START
705                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
706
707         /* a allrecord lock allows us to avoid per chain locks */
708         if (tdb->allrecord_lock.count) {
709                 if (tdb->allrecord_lock.ltype == F_RDLCK
710                     && ltype == F_WRLCK) {
711                         tdb->ecode = TDB_ERR_LOCK;
712                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
713                                  "tdb_unlock_hashes RO allrecord!\n");
714                         return -1;
715                 }
716                 return 0;
717         }
718
719         return tdb_nest_unlock(tdb, lock, ltype);
720 }
721
722 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
723  * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
724  * The result is that on 32 bit systems we don't use lock values > 2^31 on
725  * files that are less than 4GB.
726  */
727 static tdb_off_t free_lock_off(tdb_off_t b_off)
728 {
729         return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
730                 + b_off / sizeof(tdb_off_t);
731 }
732
733 int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
734                          enum tdb_lock_flags waitflag)
735 {
736         assert(b_off >= sizeof(struct tdb_header));
737
738         /* a allrecord lock allows us to avoid per chain locks */
739         if (tdb->allrecord_lock.count) {
740                 if (tdb->allrecord_lock.ltype == F_WRLCK)
741                         return 0;
742                 tdb->ecode = TDB_ERR_LOCK;
743                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
744                          "tdb_lock_free_bucket with RO allrecordlock!\n");
745                 return -1;
746         }
747
748 #if 0 /* FIXME */
749         if (tdb_has_expansion_lock(tdb)) {
750                 tdb->ecode = TDB_ERR_LOCK;
751                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
752                          "tdb_lock_free_bucket: have expansion lock already\n");
753                 return -1;
754         }
755 #endif
756
757         return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
758 }
759
760 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
761 {
762         if (tdb->allrecord_lock.count)
763                 return;
764
765         tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
766 }
767
768 #if 0
769 /* lock/unlock one hash chain, non-blocking. This is meant to be used
770    to reduce contention - it cannot guarantee how many records will be
771    locked */
772 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
773 {
774         return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT,
775                          "tdb_chainlock_nonblock");
776 }
777
778 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
779 {
780         return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
781                          "tdb_chainlock_read");
782 }
783
784 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
785 {
786         uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
787         tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
788         return tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1),
789                                F_RDLCK);
790 }
791
792 /* record lock stops delete underneath */
793 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
794 {
795         if (tdb->allrecord_lock.count) {
796                 return 0;
797         }
798         return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
799 }
800
801 /*
802   Write locks override our own fcntl readlocks, so check it here.
803   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
804   an error to fail to get the lock here.
805 */
806 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
807 {
808         struct tdb_traverse_lock *i;
809         for (i = &tdb->travlocks; i; i = i->next)
810                 if (i->off == off)
811                         return -1;
812         if (tdb->allrecord_lock.count) {
813                 if (tdb->allrecord_lock.ltype == F_WRLCK) {
814                         return 0;
815                 }
816                 return -1;
817         }
818         return tdb_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
819 }
820
821 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
822 {
823         if (tdb->allrecord_lock.count) {
824                 return 0;
825         }
826         return tdb_brunlock(tdb, F_WRLCK, off, 1);
827 }
828
829 /* fcntl locks don't stack: avoid unlocking someone else's */
830 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
831 {
832         struct tdb_traverse_lock *i;
833         uint32_t count = 0;
834
835         if (tdb->allrecord_lock.count) {
836                 return 0;
837         }
838
839         if (off == 0)
840                 return 0;
841         for (i = &tdb->travlocks; i; i = i->next)
842                 if (i->off == off)
843                         count++;
844         return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
845 }
846
847 /* The transaction code uses this to remove all locks. */
848 void tdb_release_transaction_locks(struct tdb_context *tdb)
849 {
850         unsigned int i;
851
852         if (tdb->allrecord_lock.count != 0) {
853                 tdb_off_t hash_size, free_size;
854
855                 hash_size = (1ULL << tdb->header.v.hash_bits)
856                         * sizeof(tdb_off_t);
857                 free_size = tdb->header.v.free_zones 
858                         * (tdb->header.v.free_buckets + 1) * sizeof(tdb_off_t);
859
860                 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
861                              tdb->header.v.hash_off, hash_size);
862                 tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
863                              tdb->header.v.free_off, free_size);
864                 tdb->allrecord_lock.count = 0;
865                 tdb->allrecord_lock.ltype = 0;
866         }
867
868         for (i = 0; i<tdb->num_lockrecs; i++) {
869                 struct tdb_lock_type *lck = &tdb->lockrecs[i];
870
871                 tdb_brunlock(tdb, lck->ltype, lck->off, 1);
872         }
873         tdb->num_lockrecs = 0;
874         SAFE_FREE(tdb->lockrecs);
875         tdb->header_uptodate = false;
876 }
877 #endif
878
879 void tdb_lock_init(struct tdb_context *tdb)
880 {
881         tdb->num_lockrecs = 0;
882         tdb->lockrecs = NULL;
883         tdb->allrecord_lock.count = 0;
884 }