tdb: use tdb_nest_lock() for seqnum lock.
[ira/wip.git] / lib / tdb / common / tdb.c
index 0389d3cb961f83ceb6ab1eaa3246b5d99a2ca88a..7317a3aa521554c898d12a5e5b8f57c7dafea36f 100644 (file)
@@ -59,13 +59,14 @@ static void tdb_increment_seqnum(struct tdb_context *tdb)
                return;
        }
 
-       if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
+       if (tdb_nest_lock(tdb, TDB_SEQNUM_OFS, F_WRLCK,
+                         TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
                return;
        }
 
        tdb_increment_seqnum_nonblock(tdb);
 
-       tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
+       tdb_nest_unlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, false);
 }
 
 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
@@ -121,6 +122,7 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
        return rec_ptr;
 }
 
+static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
 
 /* update an entry in place - this only works if the new data size
    is <= the old data size and the key exists.
@@ -135,6 +137,25 @@ static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
        if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
                return -1;
 
+       /* it could be an exact duplicate of what is there - this is
+        * surprisingly common (eg. with a ldb re-index). */
+       if (rec.key_len == key.dsize && 
+           rec.data_len == dbuf.dsize &&
+           rec.full_hash == hash) {
+               TDB_DATA data = _tdb_fetch(tdb, key);
+               if (data.dsize == dbuf.dsize &&
+                   memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
+                       if (data.dptr) {
+                               free(data.dptr);
+                       }
+                       return 0;
+               }
+               if (data.dptr) {
+                       free(data.dptr);
+               }
+       }
+        
+
        /* must be long enough key, data and tailer */
        if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
                tdb->ecode = TDB_SUCCESS; /* Not really an error */
@@ -192,7 +213,7 @@ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
  * function. The parsing function is executed under the chain read lock, so it
  * should be fast and should not block on other syscalls.
  *
- * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
+ * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
  *
  * For mmapped tdb's that do not have a transaction open it points the parsing
  * function directly at the mmap area, it avoids the malloc/memcpy in this
@@ -710,11 +731,41 @@ int tdb_get_flags(struct tdb_context *tdb)
 
 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
 {
+       if ((flags & TDB_ALLOW_NESTING) &&
+           (flags & TDB_DISALLOW_NESTING)) {
+               tdb->ecode = TDB_ERR_NESTING;
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
+                       "allow_nesting and disallow_nesting are not allowed together!"));
+               return;
+       }
+
+       if (flags & TDB_ALLOW_NESTING) {
+               tdb->flags &= ~TDB_DISALLOW_NESTING;
+       }
+       if (flags & TDB_DISALLOW_NESTING) {
+               tdb->flags &= ~TDB_ALLOW_NESTING;
+       }
+
        tdb->flags |= flags;
 }
 
 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
 {
+       if ((flags & TDB_ALLOW_NESTING) &&
+           (flags & TDB_DISALLOW_NESTING)) {
+               tdb->ecode = TDB_ERR_NESTING;
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
+                       "allow_nesting and disallow_nesting are not allowed together!"));
+               return;
+       }
+
+       if (flags & TDB_ALLOW_NESTING) {
+               tdb->flags |= TDB_DISALLOW_NESTING;
+       }
+       if (flags & TDB_DISALLOW_NESTING) {
+               tdb->flags |= TDB_ALLOW_NESTING;
+       }
+
        tdb->flags &= ~flags;
 }
 
@@ -754,7 +805,7 @@ static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t le
 
 /*
   wipe the entire database, deleting all records. This can be done
-  very fast by using a global lock. The entire data portion of the
+  very fast by using a allrecord lock. The entire data portion of the
   file becomes a single entry in the freelist.
 
   This code carefully steps around the recovery area, leaving it alone