]> git.ozlabs.org Git - ccan/blob - ccan/tdb/lock.c
07545b345ba0c6d80af5ed541376eca195afb87b
[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 #define TDB_MARK_LOCK 0x80000000
31
32 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
33 {
34         tdb->interrupt_sig_ptr = ptr;
35 }
36
37 /* a byte range locking function - return 0 on success
38    this functions locks/unlocks 1 byte at the specified offset.
39
40    On error, errno is also set so that errors are passed back properly
41    through tdb_open(). 
42
43    note that a len of zero means lock to end of file
44 */
45 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, 
46                int rw_type, int lck_type, int probe, size_t len)
47 {
48         struct flock fl;
49         int ret;
50
51         if (tdb->flags & TDB_NOLOCK) {
52                 return 0;
53         }
54
55         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
56                 tdb->ecode = TDB_ERR_RDONLY;
57                 return -1;
58         }
59
60         fl.l_type = rw_type;
61         fl.l_whence = SEEK_SET;
62         fl.l_start = offset;
63         fl.l_len = len;
64         fl.l_pid = 0;
65
66         do {
67                 ret = fcntl(tdb->fd,lck_type,&fl);
68
69                 /* Check for a sigalarm break. */
70                 if (ret == -1 && errno == EINTR &&
71                                 tdb->interrupt_sig_ptr &&
72                                 *tdb->interrupt_sig_ptr) {
73                         break;
74                 }
75         } while (ret == -1 && errno == EINTR);
76
77         if (ret == -1) {
78                 /* Generic lock error. errno set by fcntl.
79                  * EAGAIN is an expected return from non-blocking
80                  * locks. */
81                 if (!probe && lck_type != F_SETLK) {
82                         /* Ensure error code is set for log fun to examine. */
83                         tdb->ecode = TDB_ERR_LOCK;
84                         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n", 
85                                  tdb->fd, offset, rw_type, lck_type, (int)len));
86                 }
87                 return -1;
88         }
89         return 0;
90 }
91
92
93 /*
94   upgrade a read lock to a write lock. This needs to be handled in a
95   special way as some OSes (such as solaris) have too conservative
96   deadlock detection and claim a deadlock when progress can be
97   made. For those OSes we may loop for a while.  
98 */
99 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
100 {
101         int count = 1000;
102         while (count--) {
103                 struct timeval tv;
104                 if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
105                         return 0;
106                 }
107                 if (errno != EDEADLK) {
108                         break;
109                 }
110                 /* sleep for as short a time as we can - more portable than usleep() */
111                 tv.tv_sec = 0;
112                 tv.tv_usec = 1;
113                 select(0, NULL, NULL, NULL, &tv);
114         }
115         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
116         return -1;
117 }
118
119
120 /* lock a list in the database. list -1 is the alloc list */
121 static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
122 {
123         struct tdb_lock_type *new_lck;
124         int i;
125         bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
126
127         ltype &= ~TDB_MARK_LOCK;
128
129         /* a global lock allows us to avoid per chain locks */
130         if (tdb->global_lock.count && 
131             (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
132                 return 0;
133         }
134
135         if (tdb->global_lock.count) {
136                 tdb->ecode = TDB_ERR_LOCK;
137                 return -1;
138         }
139
140         if (list < -1 || list >= (int)tdb->header.hash_size) {
141                 tdb->ecode = TDB_ERR_LOCK;
142                 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n", 
143                            list, ltype));
144                 return -1;
145         }
146         if (tdb->flags & TDB_NOLOCK)
147                 return 0;
148
149         for (i=0; i<tdb->num_lockrecs; i++) {
150                 if (tdb->lockrecs[i].list == list) {
151                         if (tdb->lockrecs[i].count == 0) {
152                                 /*
153                                  * Can't happen, see tdb_unlock(). It should
154                                  * be an assert.
155                                  */
156                                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
157                                          "lck->count == 0 for list %d", list));
158                         }
159                         /*
160                          * Just increment the in-memory struct, posix locks
161                          * don't stack.
162                          */
163                         tdb->lockrecs[i].count++;
164                         return 0;
165                 }
166         }
167
168         new_lck = (struct tdb_lock_type *)realloc(
169                 tdb->lockrecs,
170                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
171         if (new_lck == NULL) {
172                 errno = ENOMEM;
173                 return -1;
174         }
175         tdb->lockrecs = new_lck;
176
177         /* Since fcntl locks don't nest, we do a lock for the first one,
178            and simply bump the count for future ones */
179         if (!mark_lock &&
180             tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list, ltype, op,
181                                      0, 1)) {
182                 return -1;
183         }
184
185         tdb->num_locks++;
186
187         tdb->lockrecs[tdb->num_lockrecs].list = list;
188         tdb->lockrecs[tdb->num_lockrecs].count = 1;
189         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
190         tdb->num_lockrecs += 1;
191
192         return 0;
193 }
194
195 /* lock a list in the database. list -1 is the alloc list */
196 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
197 {
198         int ret;
199         ret = _tdb_lock(tdb, list, ltype, F_SETLKW);
200         if (ret) {
201                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
202                          "ltype=%d (%s)\n",  list, ltype, strerror(errno)));
203         }
204         return ret;
205 }
206
207 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
208 int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
209 {
210         return _tdb_lock(tdb, list, ltype, F_SETLK);
211 }
212
213
214 /* unlock the database: returns void because it's too late for errors. */
215         /* changed to return int it may be interesting to know there
216            has been an error  --simo */
217 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
218 {
219         int ret = -1;
220         int i;
221         struct tdb_lock_type *lck = NULL;
222         bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
223
224         ltype &= ~TDB_MARK_LOCK;
225
226         /* a global lock allows us to avoid per chain locks */
227         if (tdb->global_lock.count && 
228             (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
229                 return 0;
230         }
231
232         if (tdb->global_lock.count) {
233                 tdb->ecode = TDB_ERR_LOCK;
234                 return -1;
235         }
236
237         if (tdb->flags & TDB_NOLOCK)
238                 return 0;
239
240         /* Sanity checks */
241         if (list < -1 || list >= (int)tdb->header.hash_size) {
242                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
243                 return ret;
244         }
245
246         for (i=0; i<tdb->num_lockrecs; i++) {
247                 if (tdb->lockrecs[i].list == list) {
248                         lck = &tdb->lockrecs[i];
249                         break;
250                 }
251         }
252
253         if ((lck == NULL) || (lck->count == 0)) {
254                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
255                 return -1;
256         }
257
258         if (lck->count > 1) {
259                 lck->count--;
260                 return 0;
261         }
262
263         /*
264          * This lock has count==1 left, so we need to unlock it in the
265          * kernel. We don't bother with decrementing the in-memory array
266          * element, we're about to overwrite it with the last array element
267          * anyway.
268          */
269
270         if (mark_lock) {
271                 ret = 0;
272         } else {
273                 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
274                                                F_SETLKW, 0, 1);
275         }
276         tdb->num_locks--;
277
278         /*
279          * Shrink the array by overwriting the element just unlocked with the
280          * last array element.
281          */
282
283         if (tdb->num_lockrecs > 1) {
284                 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
285         }
286         tdb->num_lockrecs -= 1;
287
288         /*
289          * We don't bother with realloc when the array shrinks, but if we have
290          * a completely idle tdb we should get rid of the locked array.
291          */
292
293         if (tdb->num_lockrecs == 0) {
294                 SAFE_FREE(tdb->lockrecs);
295         }
296
297         if (ret)
298                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n")); 
299         return ret;
300 }
301
302 /*
303   get the transaction lock
304  */
305 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
306 {
307         if (tdb->global_lock.count) {
308                 return 0;
309         }
310         if (tdb->transaction_lock_count > 0) {
311                 tdb->transaction_lock_count++;
312                 return 0;
313         }
314
315         if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype, 
316                                      F_SETLKW, 0, 1) == -1) {
317                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
318                 tdb->ecode = TDB_ERR_LOCK;
319                 return -1;
320         }
321         tdb->transaction_lock_count++;
322         return 0;
323 }
324
325 /*
326   release the transaction lock
327  */
328 int tdb_transaction_unlock(struct tdb_context *tdb)
329 {
330         int ret;
331         if (tdb->global_lock.count) {
332                 return 0;
333         }
334         if (tdb->transaction_lock_count > 1) {
335                 tdb->transaction_lock_count--;
336                 return 0;
337         }
338         ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
339         if (ret == 0) {
340                 tdb->transaction_lock_count = 0;
341         }
342         return ret;
343 }
344
345
346
347
348 /* lock/unlock entire database */
349 static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
350 {
351         bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
352
353         ltype &= ~TDB_MARK_LOCK;
354
355         /* There are no locks on read-only dbs */
356         if (tdb->read_only || tdb->traverse_read) {
357                 tdb->ecode = TDB_ERR_LOCK;
358                 return -1;
359         }
360
361         if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
362                 tdb->global_lock.count++;
363                 return 0;
364         }
365
366         if (tdb->global_lock.count) {
367                 /* a global lock of a different type exists */
368                 tdb->ecode = TDB_ERR_LOCK;
369                 return -1;
370         }
371         
372         if (tdb->num_locks != 0) {
373                 /* can't combine global and chain locks */
374                 tdb->ecode = TDB_ERR_LOCK;
375                 return -1;
376         }
377
378         if (!mark_lock &&
379             tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, op,
380                                      0, 4*tdb->header.hash_size)) {
381                 if (op == F_SETLKW) {
382                         TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
383                 }
384                 return -1;
385         }
386
387         tdb->global_lock.count = 1;
388         tdb->global_lock.ltype = ltype;
389
390         return 0;
391 }
392
393
394
395 /* unlock entire db */
396 static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
397 {
398         bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
399
400         ltype &= ~TDB_MARK_LOCK;
401
402         /* There are no locks on read-only dbs */
403         if (tdb->read_only || tdb->traverse_read) {
404                 tdb->ecode = TDB_ERR_LOCK;
405                 return -1;
406         }
407
408         if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
409                 tdb->ecode = TDB_ERR_LOCK;
410                 return -1;
411         }
412
413         if (tdb->global_lock.count > 1) {
414                 tdb->global_lock.count--;
415                 return 0;
416         }
417
418         if (!mark_lock &&
419             tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 
420                                      0, 4*tdb->header.hash_size)) {
421                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
422                 return -1;
423         }
424
425         tdb->global_lock.count = 0;
426         tdb->global_lock.ltype = 0;
427
428         return 0;
429 }
430
431 /* lock entire database with write lock */
432 int tdb_lockall(struct tdb_context *tdb)
433 {
434         tdb_trace(tdb, "tdb_lockall");
435         return _tdb_lockall(tdb, F_WRLCK, F_SETLKW);
436 }
437
438 /* lock entire database with write lock - mark only */
439 int tdb_lockall_mark(struct tdb_context *tdb)
440 {
441         tdb_trace(tdb, "tdb_lockall_mark");
442         return _tdb_lockall(tdb, F_WRLCK | TDB_MARK_LOCK, F_SETLKW);
443 }
444
445 /* unlock entire database with write lock - unmark only */
446 int tdb_lockall_unmark(struct tdb_context *tdb)
447 {
448         tdb_trace(tdb, "tdb_lockall_unmark");
449         return _tdb_unlockall(tdb, F_WRLCK | TDB_MARK_LOCK);
450 }
451
452 /* lock entire database with write lock - nonblocking varient */
453 int tdb_lockall_nonblock(struct tdb_context *tdb)
454 {
455         int ret = _tdb_lockall(tdb, F_WRLCK, F_SETLK);
456         tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
457         return ret;
458 }
459
460 /* unlock entire database with write lock */
461 int tdb_unlockall(struct tdb_context *tdb)
462 {
463         tdb_trace(tdb, "tdb_unlockall");
464         return _tdb_unlockall(tdb, F_WRLCK);
465 }
466
467 /* lock entire database with read lock */
468 int tdb_lockall_read(struct tdb_context *tdb)
469 {
470         tdb_trace(tdb, "tdb_lockall_read");
471         return _tdb_lockall(tdb, F_RDLCK, F_SETLKW);
472 }
473
474 /* lock entire database with read lock - nonblock varient */
475 int tdb_lockall_read_nonblock(struct tdb_context *tdb)
476 {
477         int ret = _tdb_lockall(tdb, F_RDLCK, F_SETLK);
478         tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
479         return ret;
480 }
481
482 /* unlock entire database with read lock */
483 int tdb_unlockall_read(struct tdb_context *tdb)
484 {
485         tdb_trace(tdb, "tdb_unlockall_read");
486         return _tdb_unlockall(tdb, F_RDLCK);
487 }
488
489 /* lock/unlock one hash chain. This is meant to be used to reduce
490    contention - it cannot guarantee how many records will be locked */
491 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
492 {
493         int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
494         tdb_trace_1rec(tdb, "tdb_chainlock", key);
495         return ret;
496 }
497
498 /* lock/unlock one hash chain, non-blocking. This is meant to be used
499    to reduce contention - it cannot guarantee how many records will be
500    locked */
501 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
502 {
503         int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
504         tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
505         return ret;
506 }
507
508 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
509 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
510 {
511         int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
512         tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
513         return ret;
514 }
515
516 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
517 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
518 {
519         tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
520         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
521 }
522
523 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
524 {
525         tdb_trace_1rec(tdb, "tdb_chainunlock", key);
526         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
527 }
528
529 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
530 {
531         int ret;
532         ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
533         tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
534         return ret;
535 }
536
537 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
538 {
539         tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
540         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
541 }
542
543 /* record lock stops delete underneath */
544 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
545 {
546         if (tdb->global_lock.count) {
547                 return 0;
548         }
549         return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
550 }
551
552 /*
553   Write locks override our own fcntl readlocks, so check it here.
554   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
555   an error to fail to get the lock here.
556 */
557 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
558 {
559         struct tdb_traverse_lock *i;
560         for (i = &tdb->travlocks; i; i = i->next)
561                 if (i->off == off)
562                         return -1;
563         return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
564 }
565
566 /*
567   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
568   an error to fail to get the lock here.
569 */
570 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
571 {
572         return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
573 }
574
575 /* fcntl locks don't stack: avoid unlocking someone else's */
576 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
577 {
578         struct tdb_traverse_lock *i;
579         uint32_t count = 0;
580
581         if (tdb->global_lock.count) {
582                 return 0;
583         }
584
585         if (off == 0)
586                 return 0;
587         for (i = &tdb->travlocks; i; i = i->next)
588                 if (i->off == off)
589                         count++;
590         return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);
591 }