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