source: branches/samba-3.2.x/source/lib/tdb/common/lock.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

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