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