]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/lock.c
c503c9a28bb444efa805217d7cd483ae4eed5bed
[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         add_stat(tdb, lock_lowlevel, 1);
44         if (waitflag)
45                 return fcntl(tdb->fd, F_SETLKW, &fl);
46         else {
47                 add_stat(tdb, lock_nonblock, 1);
48                 return fcntl(tdb->fd, F_SETLK, &fl);
49         }
50 }
51
52 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
53 {
54         struct flock fl;
55 #if 0 /* Check they matched up locks and unlocks correctly. */
56         char line[80];
57         FILE *locks;
58         bool found = false;
59
60         locks = fopen("/proc/locks", "r");
61
62         while (fgets(line, 80, locks)) {
63                 char *p;
64                 int type, start, l;
65
66                 /* eg. 1: FLOCK  ADVISORY  WRITE 2440 08:01:2180826 0 EOF */
67                 p = strchr(line, ':') + 1;
68                 if (strncmp(p, " POSIX  ADVISORY  ", strlen(" POSIX  ADVISORY  ")))
69                         continue;
70                 p += strlen(" FLOCK  ADVISORY  ");
71                 if (strncmp(p, "READ  ", strlen("READ  ")) == 0)
72                         type = F_RDLCK;
73                 else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
74                         type = F_WRLCK;
75                 else
76                         abort();
77                 p += 6;
78                 if (atoi(p) != getpid())
79                         continue;
80                 p = strchr(strchr(p, ' ') + 1, ' ') + 1;
81                 start = atoi(p);
82                 p = strchr(p, ' ') + 1;
83                 if (strncmp(p, "EOF", 3) == 0)
84                         l = 0;
85                 else
86                         l = atoi(p) - start + 1;
87
88                 if (off == start) {
89                         if (len != l) {
90                                 fprintf(stderr, "Len %u should be %u: %s",
91                                         (int)len, l, line);
92                                 abort();
93                         }
94                         if (type != rw) {
95                                 fprintf(stderr, "Type %s wrong: %s",
96                                         rw == F_RDLCK ? "READ" : "WRITE", line);
97                                 abort();
98                         }
99                         found = true;
100                         break;
101                 }
102         }
103
104         if (!found) {
105                 fprintf(stderr, "Unlock on %u@%u not found!",
106                         (int)off, (int)len);
107                 abort();
108         }
109
110         fclose(locks);
111 #endif
112
113         fl.l_type = F_UNLCK;
114         fl.l_whence = SEEK_SET;
115         fl.l_start = off;
116         fl.l_len = len;
117         fl.l_pid = 0;
118
119         return fcntl(tdb->fd, F_SETLKW, &fl);
120 }
121
122 /* a byte range locking function - return 0 on success
123    this functions locks/unlocks 1 byte at the specified offset.
124
125    note that a len of zero means lock to end of file
126 */
127 static int tdb_brlock(struct tdb_context *tdb,
128                       int rw_type, tdb_off_t offset, tdb_off_t len,
129                       enum tdb_lock_flags flags)
130 {
131         int ret;
132
133         if (tdb->flags & TDB_NOLOCK) {
134                 return 0;
135         }
136
137         if (rw_type == F_WRLCK && tdb->read_only) {
138                 tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
139                            "Write lock attempted on read-only database");
140                 return -1;
141         }
142
143         /* A 32 bit system cannot open a 64-bit file, but it could have
144          * expanded since then: check here. */
145         if ((size_t)(offset + len) != offset + len) {
146                 tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
147                          "tdb_brlock: lock on giant offset %llu",
148                          (long long)(offset + len));
149                 return -1;
150         }
151
152         do {
153                 ret = fcntl_lock(tdb, rw_type, offset, len,
154                                  flags & TDB_LOCK_WAIT);
155         } while (ret == -1 && errno == EINTR);
156
157         if (ret == -1) {
158                 tdb->ecode = TDB_ERR_LOCK;
159                 /* Generic lock error. errno set by fcntl.
160                  * EAGAIN is an expected return from non-blocking
161                  * locks. */
162                 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
163                         tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
164                                    "tdb_brlock failed (fd=%d) at"
165                                    " offset %zu rw_type=%d flags=%d len=%zu:"
166                                    " %s",
167                                    tdb->fd, (size_t)offset, rw_type,
168                                    flags, (size_t)len, strerror(errno));
169                 }
170                 return -1;
171         }
172         return 0;
173 }
174
175 static int tdb_brunlock(struct tdb_context *tdb,
176                         int rw_type, tdb_off_t offset, size_t len)
177 {
178         int ret;
179
180         if (tdb->flags & TDB_NOLOCK) {
181                 return 0;
182         }
183
184         do {
185                 ret = fcntl_unlock(tdb, rw_type, offset, len);
186         } while (ret == -1 && errno == EINTR);
187
188         if (ret == -1) {
189                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
190                            "tdb_brunlock failed (fd=%d) at offset %zu"
191                            " rw_type=%d len=%zu",
192                            tdb->fd, (size_t)offset, rw_type, (size_t)len);
193         }
194         return ret;
195 }
196
197 /*
198   upgrade a read lock to a write lock. This needs to be handled in a
199   special way as some OSes (such as solaris) have too conservative
200   deadlock detection and claim a deadlock when progress can be
201   made. For those OSes we may loop for a while.  
202 */
203 int tdb_allrecord_upgrade(struct tdb_context *tdb)
204 {
205         int count = 1000;
206
207         if (tdb->allrecord_lock.count != 1) {
208                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
209                            "tdb_allrecord_upgrade failed: count %u too high",
210                            tdb->allrecord_lock.count);
211                 return -1;
212         }
213
214         if (tdb->allrecord_lock.off != 1) {
215                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
216                            "tdb_allrecord_upgrade failed: already upgraded?");
217                 return -1;
218         }
219
220         while (count--) {
221                 struct timeval tv;
222                 if (tdb_brlock(tdb, F_WRLCK,
223                                TDB_HASH_LOCK_START, 0,
224                                TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
225                         tdb->allrecord_lock.ltype = F_WRLCK;
226                         tdb->allrecord_lock.off = 0;
227                         return 0;
228                 }
229                 if (errno != EDEADLK) {
230                         break;
231                 }
232                 /* sleep for as short a time as we can - more portable than usleep() */
233                 tv.tv_sec = 0;
234                 tv.tv_usec = 1;
235                 select(0, NULL, NULL, NULL, &tv);
236         }
237         tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
238                    "tdb_allrecord_upgrade failed");
239         return -1;
240 }
241
242 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
243                                            tdb_off_t offset)
244 {
245         unsigned int i;
246
247         for (i=0; i<tdb->num_lockrecs; i++) {
248                 if (tdb->lockrecs[i].off == offset) {
249                         return &tdb->lockrecs[i];
250                 }
251         }
252         return NULL;
253 }
254
255 int tdb_lock_and_recover(struct tdb_context *tdb)
256 {
257         int ret;
258
259         if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
260                                false) == -1) {
261                 return -1;
262         }
263
264         if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
265                 tdb_allrecord_unlock(tdb, F_WRLCK);
266                 return -1;
267         }
268         ret = tdb_transaction_recover(tdb);
269
270         tdb_unlock_open(tdb);
271         tdb_allrecord_unlock(tdb, F_WRLCK);
272
273         return ret;
274 }
275
276 /* lock an offset in the database. */
277 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
278                          enum tdb_lock_flags flags)
279 {
280         struct tdb_lock_type *new_lck;
281
282         if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
283                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
284                            "tdb_nest_lock: invalid offset %zu ltype=%d",
285                            (size_t)offset, ltype);
286                 return -1;
287         }
288
289         if (tdb->flags & TDB_NOLOCK)
290                 return 0;
291
292         add_stat(tdb, locks, 1);
293
294         new_lck = find_nestlock(tdb, offset);
295         if (new_lck) {
296                 if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
297                         tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
298                                    "tdb_nest_lock: offset %zu has read lock",
299                                    (size_t)offset);
300                         return -1;
301                 }
302                 /* Just increment the struct, posix locks don't stack. */
303                 new_lck->count++;
304                 return 0;
305         }
306
307         if (tdb->num_lockrecs
308             && offset >= TDB_HASH_LOCK_START
309             && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
310                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
311                            "tdb_nest_lock: already have a hash lock?");
312                 return -1;
313         }
314
315         new_lck = (struct tdb_lock_type *)realloc(
316                 tdb->lockrecs,
317                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
318         if (new_lck == NULL) {
319                 tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
320                          "tdb_nest_lock: unable to allocate %zu lock struct",
321                          tdb->num_lockrecs + 1);
322                 errno = ENOMEM;
323                 return -1;
324         }
325         tdb->lockrecs = new_lck;
326
327         /* Since fcntl locks don't nest, we do a lock for the first one,
328            and simply bump the count for future ones */
329         if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
330                 return -1;
331         }
332
333         /* First time we grab a lock, perhaps someone died in commit? */
334         if (!(flags & TDB_LOCK_NOCHECK)
335             && tdb->num_lockrecs == 0
336             && unlikely(tdb_needs_recovery(tdb))) {
337                 tdb_brunlock(tdb, ltype, offset, 1);
338
339                 if (tdb_lock_and_recover(tdb) == -1) {
340                         return -1;
341                 }
342
343                 if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
344                         return -1;
345                 }
346         }
347
348         tdb->lockrecs[tdb->num_lockrecs].off = offset;
349         tdb->lockrecs[tdb->num_lockrecs].count = 1;
350         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
351         tdb->num_lockrecs++;
352
353         return 0;
354 }
355
356 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
357 {
358         int ret = -1;
359         struct tdb_lock_type *lck;
360
361         if (tdb->flags & TDB_NOLOCK)
362                 return 0;
363
364         lck = find_nestlock(tdb, off);
365         if ((lck == NULL) || (lck->count == 0)) {
366                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
367                            "tdb_nest_unlock: no lock for %zu", (size_t)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 /*
394   get the transaction lock
395  */
396 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
397 {
398         return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
399 }
400
401 /*
402   release the transaction lock
403  */
404 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
405 {
406         return tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
407 }
408
409 /* We only need to lock individual bytes, but Linux merges consecutive locks
410  * so we lock in contiguous ranges. */
411 static int tdb_lock_gradual(struct tdb_context *tdb,
412                             int ltype, enum tdb_lock_flags flags,
413                             tdb_off_t off, tdb_off_t len)
414 {
415         int ret;
416         enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
417
418         if (len <= 1) {
419                 /* 0 would mean to end-of-file... */
420                 assert(len != 0);
421                 /* Single hash.  Just do blocking lock. */
422                 return tdb_brlock(tdb, ltype, off, len, flags);
423         }
424
425         /* First we try non-blocking. */
426         ret = tdb_brlock(tdb, ltype, off, len, nb_flags);
427         if (ret == 0) {
428                 return 0;
429         }
430
431         /* Try locking first half, then second. */
432         ret = tdb_lock_gradual(tdb, ltype, flags, off, len / 2);
433         if (ret == -1)
434                 return -1;
435
436         ret = tdb_lock_gradual(tdb, ltype, flags,
437                                     off + len / 2, len - len / 2);
438         if (ret == -1) {
439                 tdb_brunlock(tdb, ltype, off, len / 2);
440                 return -1;
441         }
442         return 0;
443 }
444
445 /* lock/unlock entire database.  It can only be upgradable if you have some
446  * other way of guaranteeing exclusivity (ie. transaction write lock). */
447 int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
448                        enum tdb_lock_flags flags, bool upgradable)
449 {
450         /* FIXME: There are no locks on read-only dbs */
451         if (tdb->read_only) {
452                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
453                            "tdb_allrecord_lock: read-only");
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_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
466                            "tdb_allrecord_lock: already have %s lock",
467                            tdb->allrecord_lock.ltype == F_RDLCK
468                            ? "read" : "write");
469                 return -1;
470         }
471
472         if (tdb_has_hash_locks(tdb)) {
473                 /* can't combine global and chain locks */
474                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
475                          "tdb_allrecord_lock: already have chain lock");
476                 return -1;
477         }
478
479         if (upgradable && ltype != F_RDLCK) {
480                 /* tdb error: you can't upgrade a write lock! */
481                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
482                            "tdb_allrecord_lock: can't upgrade a write lock");
483                 return -1;
484         }
485
486         add_stat(tdb, locks, 1);
487 again:
488         /* Lock hashes, gradually. */
489         if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
490                              TDB_HASH_LOCK_RANGE)) {
491                 if (!(flags & TDB_LOCK_PROBE)) {
492                         tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
493                                    "tdb_allrecord_lock hashes failed");
494                 }
495                 return -1;
496         }
497
498         /* Lock free tables: there to end of file. */
499         if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
500                        0, flags)) {
501                 if (!(flags & TDB_LOCK_PROBE)) {
502                         tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
503                                  "tdb_allrecord_lock freetables failed");
504                 }
505                 tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 
506                              TDB_HASH_LOCK_RANGE);
507                 return -1;
508         }
509
510         tdb->allrecord_lock.count = 1;
511         /* If it's upgradable, it's actually exclusive so we can treat
512          * it as a write lock. */
513         tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
514         tdb->allrecord_lock.off = upgradable;
515
516         /* Now check for needing recovery. */
517         if (!(flags & TDB_LOCK_NOCHECK) && unlikely(tdb_needs_recovery(tdb))) {
518                 tdb_allrecord_unlock(tdb, ltype);
519                 if (tdb_lock_and_recover(tdb) == -1) {
520                         return -1;
521                 }               
522                 goto again;
523         }
524
525         return 0;
526 }
527
528 int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
529 {
530         return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
531 }
532
533 void tdb_unlock_open(struct tdb_context *tdb)
534 {
535         tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
536 }
537
538 bool tdb_has_open_lock(struct tdb_context *tdb)
539 {
540         return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
541 }
542
543 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
544 {
545         /* Lock doesn't protect data, so don't check (we recurse if we do!) */
546         return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
547                              TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
548 }
549
550 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
551 {
552         tdb_nest_unlock(tdb, TDB_EXPANSION_LOCK, ltype);
553 }
554
555 /* unlock entire db */
556 int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
557 {
558         if (tdb->allrecord_lock.count == 0) {
559                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
560                            "tdb_allrecord_unlock: not locked!");
561                 return -1;
562         }
563
564         /* Upgradable locks are marked as write locks. */
565         if (tdb->allrecord_lock.ltype != ltype
566             && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
567                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
568                          "tdb_allrecord_unlock: have %s lock",
569                            tdb->allrecord_lock.ltype == F_RDLCK
570                            ? "read" : "write");
571                 return -1;
572         }
573
574         if (tdb->allrecord_lock.count > 1) {
575                 tdb->allrecord_lock.count--;
576                 return 0;
577         }
578
579         tdb->allrecord_lock.count = 0;
580         tdb->allrecord_lock.ltype = 0;
581
582         return tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
583 }
584
585 bool tdb_has_expansion_lock(struct tdb_context *tdb)
586 {
587         return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
588 }
589
590 bool tdb_has_hash_locks(struct tdb_context *tdb)
591 {
592         unsigned int i;
593
594         for (i=0; i<tdb->num_lockrecs; i++) {
595                 if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
596                     && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
597                                                + TDB_HASH_LOCK_RANGE))
598                         return true;
599         }
600         return false;
601 }
602
603 static bool tdb_has_free_lock(struct tdb_context *tdb)
604 {
605         unsigned int i;
606
607         for (i=0; i<tdb->num_lockrecs; i++) {
608                 if (tdb->lockrecs[i].off
609                     > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
610                         return true;
611         }
612         return false;
613 }
614
615 int tdb_lock_hashes(struct tdb_context *tdb,
616                     tdb_off_t hash_lock,
617                     tdb_len_t hash_range,
618                     int ltype, enum tdb_lock_flags waitflag)
619 {
620         /* FIXME: Do this properly, using hlock_range */
621         unsigned lock = TDB_HASH_LOCK_START
622                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
623
624         /* a allrecord lock allows us to avoid per chain locks */
625         if (tdb->allrecord_lock.count &&
626             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
627                 return 0;
628         }
629
630         if (tdb->allrecord_lock.count) {
631                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
632                            "tdb_lock_hashes: already have %s allrecordlock",
633                            tdb->allrecord_lock.ltype == F_RDLCK
634                            ? "read" : "write");
635                 return -1;
636         }
637
638         if (tdb_has_free_lock(tdb)) {
639                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
640                            "tdb_lock_hashes: already have free lock");
641                 return -1;
642         }
643
644         if (tdb_has_expansion_lock(tdb)) {
645                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
646                            "tdb_lock_hashes: already have expansion lock");
647                 return -1;
648         }
649
650         return tdb_nest_lock(tdb, lock, ltype, waitflag);
651 }
652
653 int tdb_unlock_hashes(struct tdb_context *tdb,
654                       tdb_off_t hash_lock,
655                       tdb_len_t hash_range, int ltype)
656 {
657         unsigned lock = TDB_HASH_LOCK_START
658                 + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
659
660         /* a allrecord lock allows us to avoid per chain locks */
661         if (tdb->allrecord_lock.count) {
662                 if (tdb->allrecord_lock.ltype == F_RDLCK
663                     && ltype == F_WRLCK) {
664                         tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
665                                    "tdb_unlock_hashes RO allrecord!");
666                         return -1;
667                 }
668                 return 0;
669         }
670
671         return tdb_nest_unlock(tdb, lock, ltype);
672 }
673
674 /* Hash locks use TDB_HASH_LOCK_START + the next 30 bits.
675  * Then we begin; bucket offsets are sizeof(tdb_len_t) apart, so we divide.
676  * The result is that on 32 bit systems we don't use lock values > 2^31 on
677  * files that are less than 4GB.
678  */
679 static tdb_off_t free_lock_off(tdb_off_t b_off)
680 {
681         return TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
682                 + b_off / sizeof(tdb_off_t);
683 }
684
685 int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
686                          enum tdb_lock_flags waitflag)
687 {
688         assert(b_off >= sizeof(struct tdb_header));
689
690         /* a allrecord lock allows us to avoid per chain locks */
691         if (tdb->allrecord_lock.count) {
692                 if (tdb->allrecord_lock.ltype == F_WRLCK)
693                         return 0;
694                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
695                          "tdb_lock_free_bucket with RO allrecordlock!");
696                 return -1;
697         }
698
699 #if 0 /* FIXME */
700         if (tdb_has_expansion_lock(tdb)) {
701                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
702                            "tdb_lock_free_bucket: already have expansion lock");
703                 return -1;
704         }
705 #endif
706
707         return tdb_nest_lock(tdb, free_lock_off(b_off), F_WRLCK, waitflag);
708 }
709
710 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
711 {
712         if (tdb->allrecord_lock.count)
713                 return;
714
715         tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
716 }
717
718 void tdb_lock_init(struct tdb_context *tdb)
719 {
720         tdb->num_lockrecs = 0;
721         tdb->lockrecs = NULL;
722         tdb->allrecord_lock.count = 0;
723 }