d649117c35d3ad4112356d493c17e15e6025023c
[gd/samba/.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, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 */
28
29 #include "tdb_private.h"
30
31 /* a byte range locking function - return 0 on success
32    this functions locks/unlocks 1 byte at the specified offset.
33
34    On error, errno is also set so that errors are passed back properly
35    through tdb_open(). 
36
37    note that a len of zero means lock to end of file
38 */
39 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, 
40                int rw_type, int lck_type, int probe, size_t len)
41 {
42         struct flock fl;
43         int ret;
44
45         if (tdb->flags & TDB_NOLOCK) {
46                 return 0;
47         }
48
49         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
50                 tdb->ecode = TDB_ERR_RDONLY;
51                 return -1;
52         }
53
54         fl.l_type = rw_type;
55         fl.l_whence = SEEK_SET;
56         fl.l_start = offset;
57         fl.l_len = len;
58         fl.l_pid = 0;
59
60         do {
61                 ret = fcntl(tdb->fd,lck_type,&fl);
62         } while (ret == -1 && errno == EINTR);
63
64         if (ret == -1) {
65                 /* Generic lock error. errno set by fcntl.
66                  * EAGAIN is an expected return from non-blocking
67                  * locks. */
68                 if (!probe && lck_type != F_SETLK) {
69                         /* Ensure error code is set for log fun to examine. */
70                         tdb->ecode = TDB_ERR_LOCK;
71                         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n", 
72                                  tdb->fd, offset, rw_type, lck_type, (int)len));
73                 }
74                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
75         }
76         return 0;
77 }
78
79
80 /*
81   upgrade a read lock to a write lock. This needs to be handled in a
82   special way as some OSes (such as solaris) have too conservative
83   deadlock detection and claim a deadlock when progress can be
84   made. For those OSes we may loop for a while.  
85 */
86 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
87 {
88         int count = 1000;
89         while (count--) {
90                 struct timeval tv;
91                 if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
92                         return 0;
93                 }
94                 if (errno != EDEADLK) {
95                         break;
96                 }
97                 /* sleep for as short a time as we can - more portable than usleep() */
98                 tv.tv_sec = 0;
99                 tv.tv_usec = 1;
100                 select(0, NULL, NULL, NULL, &tv);
101         }
102         TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
103         return -1;
104 }
105
106
107 /* lock a list in the database. list -1 is the alloc list */
108 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
109 {
110         struct tdb_lock_type *new_lck;
111         int i;
112
113         /* a global lock allows us to avoid per chain locks */
114         if (tdb->global_lock.count && 
115             (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
116                 return 0;
117         }
118
119         if (tdb->global_lock.count) {
120                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
121         }
122
123         if (list < -1 || list >= (int)tdb->header.hash_size) {
124                 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n", 
125                            list, ltype));
126                 return -1;
127         }
128         if (tdb->flags & TDB_NOLOCK)
129                 return 0;
130
131         for (i=0; i<tdb->num_lockrecs; i++) {
132                 if (tdb->lockrecs[i].list == list) {
133                         if (tdb->lockrecs[i].count == 0) {
134                                 /*
135                                  * Can't happen, see tdb_unlock(). It should
136                                  * be an assert.
137                                  */
138                                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
139                                          "lck->count == 0 for list %d", list));
140                         }
141                         /*
142                          * Just increment the in-memory struct, posix locks
143                          * don't stack.
144                          */
145                         tdb->lockrecs[i].count++;
146                         return 0;
147                 }
148         }
149
150         new_lck = (struct tdb_lock_type *)realloc(
151                 tdb->lockrecs,
152                 sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
153         if (new_lck == NULL) {
154                 errno = ENOMEM;
155                 return -1;
156         }
157         tdb->lockrecs = new_lck;
158
159         /* Since fcntl locks don't nest, we do a lock for the first one,
160            and simply bump the count for future ones */
161         if (tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW,
162                                      0, 1)) {
163                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
164                          "ltype=%d (%s)\n",  list, ltype, strerror(errno)));
165                 return -1;
166         }
167
168         tdb->num_locks++;
169
170         tdb->lockrecs[tdb->num_lockrecs].list = list;
171         tdb->lockrecs[tdb->num_lockrecs].count = 1;
172         tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
173         tdb->num_lockrecs += 1;
174
175         return 0;
176 }
177
178 /* unlock the database: returns void because it's too late for errors. */
179         /* changed to return int it may be interesting to know there
180            has been an error  --simo */
181 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
182 {
183         int ret = -1;
184         int i;
185         struct tdb_lock_type *lck = NULL;
186
187         /* a global lock allows us to avoid per chain locks */
188         if (tdb->global_lock.count && 
189             (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
190                 return 0;
191         }
192
193         if (tdb->global_lock.count) {
194                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
195         }
196
197         if (tdb->flags & TDB_NOLOCK)
198                 return 0;
199
200         /* Sanity checks */
201         if (list < -1 || list >= (int)tdb->header.hash_size) {
202                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
203                 return ret;
204         }
205
206         for (i=0; i<tdb->num_lockrecs; i++) {
207                 if (tdb->lockrecs[i].list == list) {
208                         lck = &tdb->lockrecs[i];
209                         break;
210                 }
211         }
212
213         if ((lck == NULL) || (lck->count == 0)) {
214                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
215                 return -1;
216         }
217
218         if (lck->count > 1) {
219                 lck->count--;
220                 return 0;
221         }
222
223         /*
224          * This lock has count==1 left, so we need to unlock it in the
225          * kernel. We don't bother with decrementing the in-memory array
226          * element, we're about to overwrite it with the last array element
227          * anyway.
228          */
229
230         ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
231                                        F_SETLKW, 0, 1);
232         tdb->num_locks--;
233
234         /*
235          * Shrink the array by overwriting the element just unlocked with the
236          * last array element.
237          */
238
239         if (tdb->num_lockrecs > 1) {
240                 *lck = tdb->lockrecs[tdb->num_lockrecs-1];
241         }
242         tdb->num_lockrecs -= 1;
243
244         /*
245          * We don't bother with realloc when the array shrinks, but if we have
246          * a completely idle tdb we should get rid of the locked array.
247          */
248
249         if (tdb->num_lockrecs == 0) {
250                 SAFE_FREE(tdb->lockrecs);
251         }
252
253         if (ret)
254                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n")); 
255         return ret;
256 }
257
258
259
260 /* lock/unlock entire database */
261 static int _tdb_lockall(struct tdb_context *tdb, int ltype)
262 {
263         /* There are no locks on read-only dbs */
264         if (tdb->read_only || tdb->traverse_read)
265                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
266
267         if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
268                 tdb->global_lock.count++;
269                 return 0;
270         }
271
272         if (tdb->global_lock.count) {
273                 /* a global lock of a different type exists */
274                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
275         }
276         
277         if (tdb->num_locks != 0) {
278                 /* can't combine global and chain locks */
279                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
280         }
281
282         if (tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, F_SETLKW, 
283                                      0, 4*tdb->header.hash_size)) {
284                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
285                 return -1;
286         }
287
288         tdb->global_lock.count = 1;
289         tdb->global_lock.ltype = ltype;
290
291         return 0;
292 }
293
294 /* unlock entire db */
295 static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
296 {
297         /* There are no locks on read-only dbs */
298         if (tdb->read_only || tdb->traverse_read) {
299                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
300         }
301
302         if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
303                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
304         }
305
306         if (tdb->global_lock.count > 1) {
307                 tdb->global_lock.count--;
308                 return 0;
309         }
310
311         if (tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 
312                                      0, 4*tdb->header.hash_size)) {
313                 TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
314                 return -1;
315         }
316
317         tdb->global_lock.count = 0;
318         tdb->global_lock.ltype = 0;
319
320         return 0;
321 }
322
323 /* lock entire database with write lock */
324 int tdb_lockall(struct tdb_context *tdb)
325 {
326         return _tdb_lockall(tdb, F_WRLCK);
327 }
328
329 /* unlock entire database with write lock */
330 int tdb_unlockall(struct tdb_context *tdb)
331 {
332         return _tdb_unlockall(tdb, F_WRLCK);
333 }
334
335 /* lock entire database with read lock */
336 int tdb_lockall_read(struct tdb_context *tdb)
337 {
338         return _tdb_lockall(tdb, F_RDLCK);
339 }
340
341 /* unlock entire database with read lock */
342 int tdb_unlockall_read(struct tdb_context *tdb)
343 {
344         return _tdb_unlockall(tdb, F_RDLCK);
345 }
346
347 /* lock/unlock one hash chain. This is meant to be used to reduce
348    contention - it cannot guarantee how many records will be locked */
349 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
350 {
351         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
352 }
353
354 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
355 {
356         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
357 }
358
359 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
360 {
361         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
362 }
363
364 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
365 {
366         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
367 }
368
369
370
371 /* record lock stops delete underneath */
372 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
373 {
374         return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
375 }
376
377 /*
378   Write locks override our own fcntl readlocks, so check it here.
379   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
380   an error to fail to get the lock here.
381 */
382 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
383 {
384         struct tdb_traverse_lock *i;
385         for (i = &tdb->travlocks; i; i = i->next)
386                 if (i->off == off)
387                         return -1;
388         return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
389 }
390
391 /*
392   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
393   an error to fail to get the lock here.
394 */
395 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
396 {
397         return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
398 }
399
400 /* fcntl locks don't stack: avoid unlocking someone else's */
401 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
402 {
403         struct tdb_traverse_lock *i;
404         u32 count = 0;
405
406         if (off == 0)
407                 return 0;
408         for (i = &tdb->travlocks; i; i = i->next)
409                 if (i->off == off)
410                         count++;
411         return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);
412 }