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