]> git.ozlabs.org Git - ccan/blob - ccan/tdb/lock.c
4f8970fd34bc4e7766e2748e2bbf077ad76a5f97
[ccan] / ccan / tdb / 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 "tdb_private.h"
29
30 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
31 {
32         tdb->interrupt_sig_ptr = ptr;
33 }
34
35 static int fcntl_lock(struct tdb_context *tdb,
36                       int rw, off_t off, off_t len, bool waitflag)
37 {
38         struct flock fl;
39
40         fl.l_type = rw;
41         fl.l_whence = SEEK_SET;
42         fl.l_start = off;
43         fl.l_len = len;
44         fl.l_pid = 0;
45
46         if (waitflag)
47                 return fcntl(tdb->fd, F_SETLKW, &fl);
48         else
49                 return fcntl(tdb->fd, F_SETLK, &fl);
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!\n",
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    On error, errno is also set so that errors are passed back properly
126    through tdb_open(). 
127
128    note that a len of zero means lock to end of file
129 */
130 int tdb_brlock(struct tdb_context *tdb,
131                int rw_type, tdb_off_t offset, size_t len,
132                enum tdb_lock_flags flags)
133 {
134         int ret;
135
136         if (tdb->flags & TDB_NOLOCK) {
137                 return 0;
138         }
139
140         if (flags & TDB_LOCK_MARK_ONLY) {
141                 return 0;
142         }
143
144         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
145                 tdb->ecode = TDB_ERR_RDONLY;
146                 return -1;
147         }
148
149         do {
150                 ret = fcntl_lock(tdb, rw_type, offset, len,
151                                  flags & TDB_LOCK_WAIT);
152                 /* Check for a sigalarm break. */
153                 if (ret == -1 && errno == EINTR &&
154                                 tdb->interrupt_sig_ptr &&
155                                 *tdb->interrupt_sig_ptr) {
156                         break;
157                 }
158         } while (ret == -1 && errno == EINTR);
159
160         if (ret == -1) {
161                 tdb->ecode = TDB_ERR_LOCK;
162                 /* Generic lock error. errno set by fcntl.
163                  * EAGAIN is an expected return from non-blocking
164                  * locks. */
165                 if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
166                         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
167                                  tdb->fd, offset, rw_type, flags, (int)len));
168                 }
169                 return -1;
170         }
171         return 0;
172 }
173
174 int tdb_brunlock(struct tdb_context *tdb,
175                  int rw_type, tdb_off_t offset, size_t len)
176 {
177         int ret;
178
179         if (tdb->flags & TDB_NOLOCK) {
180                 return 0;
181         }
182
183         do {
184                 ret = fcntl_unlock(tdb, rw_type, offset, len);
185         } while (ret == -1 && errno == EINTR);
186
187         if (ret == -1) {
188                 TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
189                          tdb->fd, offset, rw_type, (int)len));
190         }
191         return ret;
192 }
193
194 /*
195   upgrade a read lock to a write lock. This needs to be handled in a
196   special way as some OSes (such as solaris) have too conservative
197   deadlock detection and claim a deadlock when progress can be
198   made. For those OSes we may loop for a while.  
199 */
200 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
201 {
202         int count = 1000;
203         while (count--) {
204                 struct timeval tv;
205                 if (tdb_brlock(tdb, F_WRLCK, offset, len,
206                                TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
207                         return 0;
208                 }
209                 if (errno != EDEADLK) {
210                         break;
211                 }
212                 /* sleep for as short a time as we can - more portable than usleep() */
213                 tv.tv_sec = 0;
214                 tv.tv_usec = 1;
215                 select(0, NULL, NULL, NULL, &tv);
216         }
217         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
218         return -1;
219 }
220
221 /* list -1 is the alloc list, otherwise a hash chain. */
222 static tdb_off_t lock_offset(int list)
223 {
224         return FREELIST_TOP + 4*list;
225 }
226
227 /* lock an offset in the database. */
228 int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
229                   enum tdb_lock_flags flags)
230 {
231         struct tdb_lock_type *new_lck;
232         int i;
233
234         if (offset >= lock_offset(tdb->header.hash_size)) {
235                 tdb->ecode = TDB_ERR_LOCK;
236                 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid offset %u for ltype=%d\n",
237                          offset, ltype));
238                 return -1;
239         }
240         if (tdb->flags & TDB_NOLOCK)
241                 return 0;
242
243         for (i=0; i<tdb->num_lockrecs; i++) {
244                 if (tdb->lockrecs[i].off == offset) {
245                         if (tdb->lockrecs[i].count == 0) {
246                                 /*
247                                  * Can't happen, see tdb_unlock(). It should
248                                  * be an assert.
249                                  */
250                                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
251                                          "lck->count == 0 for offset %u",
252                                          offset));
253                         }
254                         /*
255                          * Just increment the in-memory struct, posix locks
256                          * don't stack.
257                          */
258                         tdb->lockrecs[i].count++;
259                         return 0;
260                 }
261         }
262
263         new_lck = (struct tdb_lock_type *)realloc(
264                 tdb->lockrecs,
265                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
266         if (new_lck == NULL) {
267                 errno = ENOMEM;
268                 return -1;
269         }
270         tdb->lockrecs = new_lck;
271
272         /* Since fcntl locks don't nest, we do a lock for the first one,
273            and simply bump the count for future ones */
274         if (tdb->methods->brlock(tdb, ltype, offset, 1, flags)) {
275                 return -1;
276         }
277
278         tdb->num_locks++;
279
280         tdb->lockrecs[tdb->num_lockrecs].off = offset;
281         tdb->lockrecs[tdb->num_lockrecs].count = 1;
282         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
283         tdb->num_lockrecs += 1;
284
285         return 0;
286 }
287
288 /* lock a list in the database. list -1 is the alloc list */
289 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
290 {
291         int ret;
292
293         /* a allrecord lock allows us to avoid per chain locks */
294         if (tdb->allrecord_lock.count &&
295             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
296                 return 0;
297         }
298
299         if (tdb->allrecord_lock.count) {
300                 tdb->ecode = TDB_ERR_LOCK;
301                 ret = -1;
302         } else {
303                 ret = tdb_nest_lock(tdb, lock_offset(list), ltype,
304                                     TDB_LOCK_WAIT);
305         }
306         if (ret) {
307                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
308                          "ltype=%d (%s)\n",  list, ltype, strerror(errno)));
309         }
310         return ret;
311 }
312
313 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
314 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
315 {
316         /* a allrecord lock allows us to avoid per chain locks */
317         if (tdb->allrecord_lock.count &&
318             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
319                 return 0;
320         }
321
322         if (tdb->allrecord_lock.count) {
323                 tdb->ecode = TDB_ERR_LOCK;
324                 return -1;
325         }
326
327         return tdb_nest_lock(tdb, lock_offset(list), ltype, TDB_LOCK_NOWAIT);
328 }
329
330
331 int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
332                     bool mark_lock)
333 {
334         int ret = -1;
335         int i;
336         struct tdb_lock_type *lck = NULL;
337
338         if (tdb->flags & TDB_NOLOCK)
339                 return 0;
340
341         /* Sanity checks */
342         if (offset >= lock_offset(tdb->header.hash_size)) {
343                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: offset %u invalid (%d)\n", offset, tdb->header.hash_size));
344                 return ret;
345         }
346
347         for (i=0; i<tdb->num_lockrecs; i++) {
348                 if (tdb->lockrecs[i].off == offset) {
349                         lck = &tdb->lockrecs[i];
350                         break;
351                 }
352         }
353
354         if ((lck == NULL) || (lck->count == 0)) {
355                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
356                 return -1;
357         }
358
359         if (lck->count > 1) {
360                 lck->count--;
361                 return 0;
362         }
363
364         /*
365          * This lock has count==1 left, so we need to unlock it in the
366          * kernel. We don't bother with decrementing the in-memory array
367          * element, we're about to overwrite it with the last array element
368          * anyway.
369          */
370
371         if (mark_lock) {
372                 ret = 0;
373         } else {
374                 ret = tdb->methods->brunlock(tdb, ltype, offset, 1);
375         }
376         tdb->num_locks--;
377
378         /*
379          * Shrink the array by overwriting the element just unlocked with the
380          * last array element.
381          */
382
383         if (tdb->num_lockrecs > 1) {
384                 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
385         }
386         tdb->num_lockrecs -= 1;
387
388         /*
389          * We don't bother with realloc when the array shrinks, but if we have
390          * a completely idle tdb we should get rid of the locked array.
391          */
392
393         if (tdb->num_lockrecs == 0) {
394                 SAFE_FREE(tdb->lockrecs);
395         }
396
397         if (ret)
398                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n")); 
399         return ret;
400 }
401
402 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
403 {
404         /* a global lock allows us to avoid per chain locks */
405         if (tdb->allrecord_lock.count &&
406             (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
407                 return 0;
408         }
409
410         if (tdb->allrecord_lock.count) {
411                 tdb->ecode = TDB_ERR_LOCK;
412                 return -1;
413         }
414
415         return tdb_nest_unlock(tdb, lock_offset(list), ltype, false);
416 }
417
418 /*
419   get the transaction lock
420  */
421 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
422 {
423         if (tdb->transaction_lock_count > 0) {
424                 tdb->transaction_lock_count++;
425                 return 0;
426         }
427
428         if (tdb->methods->brlock(tdb, ltype, TRANSACTION_LOCK, 1, TDB_LOCK_WAIT) == -1) {
429                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
430                 tdb->ecode = TDB_ERR_LOCK;
431                 return -1;
432         }
433         tdb->transaction_lock_count++;
434         return 0;
435 }
436
437 /*
438   release the transaction lock
439  */
440 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
441 {
442         int ret;
443         if (tdb->transaction_lock_count > 1) {
444                 tdb->transaction_lock_count--;
445                 return 0;
446         }
447         ret = tdb->methods->brunlock(tdb, ltype, TRANSACTION_LOCK, 1);
448         if (ret == 0) {
449                 tdb->transaction_lock_count = 0;
450         }
451         return ret;
452 }
453
454
455
456
457 /* lock/unlock entire database */
458 static int _tdb_lockall(struct tdb_context *tdb, int ltype,
459                         enum tdb_lock_flags flags)
460 {
461         /* There are no locks on read-only dbs */
462         if (tdb->read_only || tdb->traverse_read) {
463                 tdb->ecode = TDB_ERR_LOCK;
464                 return -1;
465         }
466
467         if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
468                 tdb->allrecord_lock.count++;
469                 return 0;
470         }
471
472         if (tdb->allrecord_lock.count) {
473                 /* a global lock of a different type exists */
474                 tdb->ecode = TDB_ERR_LOCK;
475                 return -1;
476         }
477
478         if (tdb_have_extra_locks(tdb)) {
479                 /* can't combine global and chain locks */
480                 tdb->ecode = TDB_ERR_LOCK;
481                 return -1;
482         }
483
484         if (tdb->methods->brlock(tdb, ltype,
485                                  FREELIST_TOP, 4*tdb->header.hash_size,
486                                  flags)) {
487                 if (flags & TDB_LOCK_WAIT) {
488                         TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
489                 }
490                 return -1;
491         }
492
493         tdb->allrecord_lock.count = 1;
494         tdb->allrecord_lock.ltype = ltype;
495
496         return 0;
497 }
498
499
500
501 /* unlock entire db */
502 static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
503 {
504         /* There are no locks on read-only dbs */
505         if (tdb->read_only || tdb->traverse_read) {
506                 tdb->ecode = TDB_ERR_LOCK;
507                 return -1;
508         }
509
510         if (tdb->allrecord_lock.ltype != ltype || tdb->allrecord_lock.count == 0) {
511                 tdb->ecode = TDB_ERR_LOCK;
512                 return -1;
513         }
514
515         if (tdb->allrecord_lock.count > 1) {
516                 tdb->allrecord_lock.count--;
517                 return 0;
518         }
519
520         if (!mark_lock &&
521             tdb->methods->brunlock(tdb, ltype,
522                                    FREELIST_TOP, 4*tdb->header.hash_size)) {
523                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
524                 return -1;
525         }
526
527         tdb->allrecord_lock.count = 0;
528         tdb->allrecord_lock.ltype = 0;
529
530         return 0;
531 }
532
533 /* lock entire database with write lock */
534 int tdb_lockall(struct tdb_context *tdb)
535 {
536         tdb_trace(tdb, "tdb_lockall");
537         return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_WAIT);
538 }
539
540 /* lock entire database with write lock - mark only */
541 int tdb_lockall_mark(struct tdb_context *tdb)
542 {
543         tdb_trace(tdb, "tdb_lockall_mark");
544         return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY);
545 }
546
547 /* unlock entire database with write lock - unmark only */
548 int tdb_lockall_unmark(struct tdb_context *tdb)
549 {
550         tdb_trace(tdb, "tdb_lockall_unmark");
551         return _tdb_unlockall(tdb, F_WRLCK, true);
552 }
553
554 /* lock entire database with write lock - nonblocking varient */
555 int tdb_lockall_nonblock(struct tdb_context *tdb)
556 {
557         int ret = _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_NOWAIT);
558         tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
559         return ret;
560 }
561
562 /* unlock entire database with write lock */
563 int tdb_unlockall(struct tdb_context *tdb)
564 {
565         tdb_trace(tdb, "tdb_unlockall");
566         return _tdb_unlockall(tdb, F_WRLCK, false);
567 }
568
569 /* lock entire database with read lock */
570 int tdb_lockall_read(struct tdb_context *tdb)
571 {
572         tdb_trace(tdb, "tdb_lockall_read");
573         return _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_WAIT);
574 }
575
576 /* lock entire database with read lock - nonblock varient */
577 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
578 {
579         int ret = _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_NOWAIT);
580         tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
581         return ret;
582 }
583
584 /* unlock entire database with read lock */
585 int tdb_unlockall_read(struct tdb_context *tdb)
586 {
587         tdb_trace(tdb, "tdb_unlockall_read");
588         return _tdb_unlockall(tdb, F_RDLCK, false);
589 }
590
591 /* lock/unlock one hash chain. This is meant to be used to reduce
592    contention - it cannot guarantee how many records will be locked */
593 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
594 {
595         int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
596         tdb_trace_1rec(tdb, "tdb_chainlock", key);
597         return ret;
598 }
599
600 /* lock/unlock one hash chain, non-blocking. This is meant to be used
601    to reduce contention - it cannot guarantee how many records will be
602    locked */
603 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
604 {
605         int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
606         tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
607         return ret;
608 }
609
610 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
611 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
612 {
613         int ret = tdb_nest_lock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
614                                 F_WRLCK, TDB_LOCK_MARK_ONLY);
615         tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
616         return ret;
617 }
618
619 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
620 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
621 {
622         tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
623         return tdb_nest_unlock(tdb, lock_offset(BUCKET(tdb->hash_fn(&key))),
624                                F_WRLCK, true);
625 }
626
627 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
628 {
629         tdb_trace_1rec(tdb, "tdb_chainunlock", key);
630         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
631 }
632
633 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
634 {
635         int ret;
636         ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
637         tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
638         return ret;
639 }
640
641 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
642 {
643         tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
644         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
645 }
646
647
648
649 /* record lock stops delete underneath */
650 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
651 {
652         if (tdb->allrecord_lock.count) {
653                 return 0;
654         }
655         return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
656 }
657
658 /*
659   Write locks override our own fcntl readlocks, so check it here.
660   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
661   an error to fail to get the lock here.
662 */
663 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
664 {
665         struct tdb_traverse_lock *i;
666         for (i = &tdb->travlocks; i; i = i->next)
667                 if (i->off == off)
668                         return -1;
669         return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
670 }
671
672 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
673 {
674         return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
675 }
676
677 /* fcntl locks don't stack: avoid unlocking someone else's */
678 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
679 {
680         struct tdb_traverse_lock *i;
681         uint32_t count = 0;
682
683         if (tdb->allrecord_lock.count) {
684                 return 0;
685         }
686
687         if (off == 0)
688                 return 0;
689         for (i = &tdb->travlocks; i; i = i->next)
690                 if (i->off == off)
691                         count++;
692         return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);
693 }
694
695 bool tdb_have_extra_locks(struct tdb_context *tdb)
696 {
697         if (tdb->allrecord_lock.count) {
698                 return true;
699         }
700         if (tdb->num_lockrecs) {
701                 return true;
702         }
703         return false;
704 }