source: trunk-3.0/source/tdb/common/lock.c@ 101

Last change on this file since 101 was 71, checked in by Paul Smedley, 18 years ago

Update source to 3.0.26a

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