source: branches/samba-3.0/source/tdb/common/lock.c

Last change on this file was 298, checked in by Herwig Bauernfeind, 16 years ago

3.0.35 fix logging changes that slipped in with changeset #286

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