tdb: suppress record write locks when allrecord lock is taken.
[samba.git] / lib / tdb / common / lock.c
index 870601126bdb82d72163f3aba5b0f76f66c5c86e..8401cb7f8fd32bb1fa486bc1f33757857aa38d7a 100644 (file)
@@ -277,12 +277,10 @@ int tdb_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
                return -1;
        }
 
-       tdb->num_locks++;
-
        tdb->lockrecs[tdb->num_lockrecs].off = offset;
        tdb->lockrecs[tdb->num_lockrecs].count = 1;
        tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
-       tdb->num_lockrecs += 1;
+       tdb->num_lockrecs++;
 
        return 0;
 }
@@ -368,17 +366,12 @@ int tdb_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype,
        } else {
                ret = tdb->methods->brunlock(tdb, ltype, offset, 1);
        }
-       tdb->num_locks--;
 
        /*
         * Shrink the array by overwriting the element just unlocked with the
         * last array element.
         */
-
-       if (tdb->num_lockrecs > 1) {
-               *lck = tdb->lockrecs[tdb->num_lockrecs-1];
-       }
-       tdb->num_lockrecs -= 1;
+       *lck = tdb->lockrecs[--tdb->num_lockrecs];
 
        /*
         * We don't bother with realloc when the array shrinks, but if we have
@@ -456,9 +449,7 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
-       if (tdb->methods->brlock(tdb, ltype,
-                                FREELIST_TOP, 4*tdb->header.hash_size,
-                                flags)) {
+       if (tdb->methods->brlock(tdb, ltype, FREELIST_TOP, 0, flags)) {
                if (flags & TDB_LOCK_WAIT) {
                        TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
                }
@@ -493,8 +484,7 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
        }
 
        if (!mark_lock &&
-           tdb->methods->brunlock(tdb, ltype,
-                                  FREELIST_TOP, 4*tdb->header.hash_size)) {
+           tdb->methods->brunlock(tdb, ltype, FREELIST_TOP, 0)) {
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
                return -1;
        }
@@ -641,11 +631,20 @@ int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
        for (i = &tdb->travlocks; i; i = i->next)
                if (i->off == off)
                        return -1;
+       if (tdb->allrecord_lock.count) {
+               if (tdb->allrecord_lock.ltype == F_WRLCK) {
+                       return 0;
+               }
+               return -1;
+       }
        return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
 }
 
 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
 {
+       if (tdb->allrecord_lock.count) {
+               return 0;
+       }
        return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
 }
 
@@ -675,6 +674,11 @@ bool tdb_have_extra_locks(struct tdb_context *tdb)
                return true;
        }
 
+       /* We always hold the active lock if CLEAR_IF_FIRST. */
+       if (find_nestlock(tdb, ACTIVE_LOCK)) {
+               extra--;
+       }
+
        /* In a transaction, we expect to hold the transaction lock */
        if (tdb->transaction && find_nestlock(tdb, TRANSACTION_LOCK)) {
                extra--;
@@ -683,27 +687,29 @@ bool tdb_have_extra_locks(struct tdb_context *tdb)
        return extra;
 }
 
-/* The transaction code uses this to remove all locks. */
+/* The transaction code uses this to remove all locks.  Note that this
+   may include OPEN_LOCK. */
 void tdb_release_extra_locks(struct tdb_context *tdb)
 {
        unsigned int i, extra = 0;
 
        if (tdb->allrecord_lock.count != 0) {
-               tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
-                            FREELIST_TOP, 4*tdb->header.hash_size);
+               tdb_brunlock(tdb, tdb->allrecord_lock.ltype, FREELIST_TOP, 0);
                tdb->allrecord_lock.count = 0;
        }
 
        for (i=0;i<tdb->num_lockrecs;i++) {
                struct tdb_lock_type *lck = &tdb->lockrecs[i];
 
+               /* Don't release transaction or active locks! */
                if (tdb->transaction && lck->off == TRANSACTION_LOCK) {
                        tdb->lockrecs[extra++] = *lck;
+               } else if (lck->off == ACTIVE_LOCK) {
+                       tdb->lockrecs[extra++] = *lck;
                } else {
                        tdb_brunlock(tdb, lck->ltype, lck->off, 1);
                }
        }
-       tdb->num_locks = extra;
        tdb->num_lockrecs = extra;
        if (tdb->num_lockrecs == 0) {
                SAFE_FREE(tdb->lockrecs);