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